org.graylog2.cluster.NodeImpl Maven / Gradle / Ivy
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* .
*/
package org.graylog2.cluster;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.bson.types.ObjectId;
import org.graylog2.database.CollectionName;
import org.graylog2.database.PersistedImpl;
import org.graylog2.plugin.database.validators.Validator;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import java.util.Collections;
import java.util.Locale;
import java.util.Map;
@CollectionName("nodes")
public class NodeImpl extends PersistedImpl implements Node {
protected NodeImpl(Map fields) {
super(fields);
}
protected NodeImpl(ObjectId id, Map fields) {
super(id, fields);
}
@Override
public String getNodeId() {
return (String) fields.get("node_id");
}
@Override
@JsonProperty("is_leader")
public boolean isLeader() {
return (boolean) fields.get("is_leader");
}
@Override
public String getTransportAddress() {
return (String) fields.get("transport_address");
}
@Override
public DateTime getLastSeen() {
return new DateTime(((Integer) fields.getOrDefault("last_seen", 0)) * 1000L, DateTimeZone.UTC);
}
@Override
public String getShortNodeId() {
return getNodeId().split("-")[0];
}
@Override
public Type getType() {
if (!fields.containsKey("type")) {
return Type.SERVER;
}
return Type.valueOf(fields.get("type").toString().toUpperCase(Locale.ENGLISH));
}
@Override
public String getHostname() {
return (String)fields.get("hostname");
}
@Override
@JsonIgnore
public Map getValidations() {
return Collections.emptyMap();
}
@Override
@JsonIgnore
public Map getEmbeddedValidations(String key) {
return Collections.emptyMap();
}
}