Search in sources :

Example 1 with Node

use of org.onosproject.cluster.Node in project onos by opennetworkinglab.

the class StorageNodesListCommand method doExecute.

@Override
protected void doExecute() {
    ClusterAdminService service = get(ClusterAdminService.class);
    List<Node> nodes = newArrayList(service.getConsensusNodes());
    Collections.sort(nodes, Comparator.comparing(Node::id));
    if (outputJson()) {
        print("%s", json(nodes));
    } else {
        for (Node node : nodes) {
            print(FMT, node.id(), node.host(), node.tcpPort());
        }
    }
}
Also used : ClusterAdminService(org.onosproject.cluster.ClusterAdminService) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Node(org.onosproject.cluster.Node) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 2 with Node

use of org.onosproject.cluster.Node in project onos by opennetworkinglab.

the class SimpleVirtualMastershipStore method getMastership.

@Override
public MastershipInfo getMastership(NetworkId networkId, DeviceId deviceId) {
    Map<DeviceId, NodeId> masterMap = getMasterMap(networkId);
    Map<DeviceId, AtomicInteger> termMap = getTermMap(networkId);
    Map<DeviceId, List<NodeId>> backups = getBackups(networkId);
    ImmutableMap.Builder<NodeId, MastershipRole> roleBuilder = ImmutableMap.builder();
    NodeId master = masterMap.get(deviceId);
    if (master != null) {
        roleBuilder.put(master, MastershipRole.MASTER);
    }
    backups.getOrDefault(deviceId, Collections.emptyList()).forEach(nodeId -> roleBuilder.put(nodeId, MastershipRole.STANDBY));
    clusterService.getNodes().stream().filter(node -> !masterMap.containsValue(node.id())).filter(node -> !backups.get(deviceId).contains(node.id())).forEach(node -> roleBuilder.put(node.id(), MastershipRole.NONE));
    return new MastershipInfo(termMap.getOrDefault(deviceId, new AtomicInteger(NOTHING)).get(), Optional.ofNullable(master), roleBuilder.build());
}
Also used : HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ControllerNode(org.onosproject.cluster.ControllerNode) DefaultControllerNode(org.onosproject.cluster.DefaultControllerNode) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MastershipEvent(org.onosproject.mastership.MastershipEvent) Component(org.osgi.service.component.annotations.Component) ImmutableList(com.google.common.collect.ImmutableList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Node(org.onosproject.cluster.Node) VersionService(org.onosproject.core.VersionService) BACKUPS_CHANGED(org.onosproject.mastership.MastershipEvent.Type.BACKUPS_CHANGED) Map(java.util.Map) Version(org.onosproject.core.Version) NetworkId(org.onosproject.incubator.net.virtual.NetworkId) Activate(org.osgi.service.component.annotations.Activate) IpAddress(org.onlab.packet.IpAddress) NodeId(org.onosproject.cluster.NodeId) ClusterEventListener(org.onosproject.cluster.ClusterEventListener) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) MASTER_CHANGED(org.onosproject.mastership.MastershipEvent.Type.MASTER_CHANGED) ImmutableMap(com.google.common.collect.ImmutableMap) MastershipRole(org.onosproject.net.MastershipRole) Deactivate(org.osgi.service.component.annotations.Deactivate) VirtualNetworkMastershipStore(org.onosproject.incubator.net.virtual.VirtualNetworkMastershipStore) MastershipInfo(org.onosproject.mastership.MastershipInfo) Set(java.util.Set) Instant(java.time.Instant) MastershipTerm(org.onosproject.mastership.MastershipTerm) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) Objects(java.util.Objects) List(java.util.List) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) Optional(java.util.Optional) ClusterService(org.onosproject.cluster.ClusterService) RoleInfo(org.onosproject.cluster.RoleInfo) DeviceId(org.onosproject.net.DeviceId) Reference(org.osgi.service.component.annotations.Reference) Collections(java.util.Collections) MastershipStoreDelegate(org.onosproject.mastership.MastershipStoreDelegate) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DeviceId(org.onosproject.net.DeviceId) NodeId(org.onosproject.cluster.NodeId) MastershipInfo(org.onosproject.mastership.MastershipInfo) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) MastershipRole(org.onosproject.net.MastershipRole) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 3 with Node

use of org.onosproject.cluster.Node in project onos by opennetworkinglab.

the class SimpleVirtualMastershipStore method createFakeClusterService.

/**
 * Returns a fake cluster service for a test purpose only.
 *
 * @return a fake cluster service
 */
private ClusterService createFakeClusterService() {
    // just for ease of unit test
    final ControllerNode instance = new DefaultControllerNode(new NodeId("local"), IpAddress.valueOf("127.0.0.1"));
    ClusterService faceClusterService = new ClusterService() {

        private final Instant creationTime = Instant.now();

        @Override
        public ControllerNode getLocalNode() {
            return instance;
        }

        @Override
        public Set<ControllerNode> getNodes() {
            return ImmutableSet.of(instance);
        }

        @Override
        public Set<Node> getConsensusNodes() {
            return ImmutableSet.of();
        }

        @Override
        public ControllerNode getNode(NodeId nodeId) {
            if (instance.id().equals(nodeId)) {
                return instance;
            }
            return null;
        }

        @Override
        public ControllerNode.State getState(NodeId nodeId) {
            if (instance.id().equals(nodeId)) {
                return ControllerNode.State.ACTIVE;
            } else {
                return ControllerNode.State.INACTIVE;
            }
        }

        @Override
        public Version getVersion(NodeId nodeId) {
            if (instance.id().equals(nodeId)) {
                return versionService.version();
            }
            return null;
        }

        @Override
        public Instant getLastUpdatedInstant(NodeId nodeId) {
            return creationTime;
        }

        @Override
        public void addListener(ClusterEventListener listener) {
        }

        @Override
        public void removeListener(ClusterEventListener listener) {
        }
    };
    return faceClusterService;
}
Also used : ClusterService(org.onosproject.cluster.ClusterService) Instant(java.time.Instant) ControllerNode(org.onosproject.cluster.ControllerNode) DefaultControllerNode(org.onosproject.cluster.DefaultControllerNode) Node(org.onosproject.cluster.Node) NodeId(org.onosproject.cluster.NodeId) ControllerNode(org.onosproject.cluster.ControllerNode) DefaultControllerNode(org.onosproject.cluster.DefaultControllerNode) DefaultControllerNode(org.onosproject.cluster.DefaultControllerNode) ClusterEventListener(org.onosproject.cluster.ClusterEventListener)

Example 4 with Node

use of org.onosproject.cluster.Node in project onos by opennetworkinglab.

the class StorageNodesListCommand method json.

// Produces JSON structure.
private JsonNode json(List<Node> nodes) {
    ObjectMapper mapper = new ObjectMapper();
    ArrayNode result = mapper.createArrayNode();
    for (Node node : nodes) {
        IpAddress nodeIp = node.ip();
        ObjectNode newNode = mapper.createObjectNode().put("id", node.id().toString()).put("ip", nodeIp != null ? nodeIp.toString() : node.host()).put("host", node.host()).put("tcpPort", node.tcpPort());
        result.add(newNode);
    }
    return result;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Node(org.onosproject.cluster.Node) JsonNode(com.fasterxml.jackson.databind.JsonNode) IpAddress(org.onlab.packet.IpAddress) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 5 with Node

use of org.onosproject.cluster.Node in project onos by opennetworkinglab.

the class ConfigFileBasedClusterMetadataProvider method fetchMetadata.

private Versioned<ClusterMetadata> fetchMetadata(String metadataUrl) {
    try {
        URL url = new URL(metadataUrl);
        ClusterMetadataPrototype metadata = null;
        long version = 0;
        if ("file".equals(url.getProtocol())) {
            File file = new File(metadataUrl.replaceFirst("file://", ""));
            version = file.lastModified();
            metadata = mapper.readValue(new FileInputStream(file), ClusterMetadataPrototype.class);
        } else if ("http".equals(url.getProtocol())) {
            try {
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                if (conn.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
                    log.warn("Could not reach metadata URL {}. Retrying...", url);
                    return null;
                }
                if (conn.getResponseCode() == HttpURLConnection.HTTP_NO_CONTENT) {
                    return null;
                }
                version = conn.getLastModified();
                metadata = mapper.readValue(conn.getInputStream(), ClusterMetadataPrototype.class);
            } catch (IOException e) {
                log.warn("Could not reach metadata URL {}. Retrying...", url);
                return null;
            }
        }
        if (null == metadata) {
            log.warn("Metadata is null in the function fetchMetadata");
            throw new NullPointerException();
        }
        return new Versioned<>(new ClusterMetadata(PROVIDER_ID, metadata.getName(), metadata.getNode() != null ? new DefaultControllerNode(getNodeId(metadata.getNode()), getNodeHost(metadata.getNode()), getNodePort(metadata.getNode())) : null, metadata.getController().stream().map(node -> new DefaultControllerNode(getNodeId(node), getNodeHost(node), getNodePort(node))).collect(Collectors.toSet()), metadata.getStorageDnsService(), metadata.getStorage().stream().map(node -> new DefaultControllerNode(getNodeId(node), getNodeHost(node), getNodePort(node))).collect(Collectors.toSet()), metadata.getClusterSecret()), version);
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    }
}
Also used : ClusterMetadata(org.onosproject.cluster.ClusterMetadata) HttpURLConnection(java.net.HttpURLConnection) URL(java.net.URL) Tools.groupedThreads(org.onlab.util.Tools.groupedThreads) ClusterMetadataProvider(org.onosproject.cluster.ClusterMetadataProvider) AtomicReference(java.util.concurrent.atomic.AtomicReference) DefaultControllerNode(org.onosproject.cluster.DefaultControllerNode) Component(org.osgi.service.component.annotations.Component) Executors.newSingleThreadScheduledExecutor(java.util.concurrent.Executors.newSingleThreadScheduledExecutor) ClusterMetadataProviderRegistry(org.onosproject.cluster.ClusterMetadataProviderRegistry) Files(com.google.common.io.Files) Node(org.onosproject.cluster.Node) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ClusterMetadataProviderService(org.onosproject.cluster.ClusterMetadataProviderService) Activate(org.osgi.service.component.annotations.Activate) ClusterMetadata(org.onosproject.cluster.ClusterMetadata) NodeId(org.onosproject.cluster.NodeId) Logger(org.slf4j.Logger) Deactivate(org.osgi.service.component.annotations.Deactivate) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) IOException(java.io.IOException) ProviderId(org.onosproject.net.provider.ProviderId) FileInputStream(java.io.FileInputStream) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) File(java.io.File) Sets(com.google.common.collect.Sets) Preconditions.checkState(com.google.common.base.Preconditions.checkState) TimeUnit(java.util.concurrent.TimeUnit) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) Versioned(org.onosproject.store.service.Versioned) PartitionId(org.onosproject.cluster.PartitionId) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) Reference(org.osgi.service.component.annotations.Reference) Versioned(org.onosproject.store.service.Versioned) IOException(java.io.IOException) DefaultControllerNode(org.onosproject.cluster.DefaultControllerNode) URL(java.net.URL) FileInputStream(java.io.FileInputStream) HttpURLConnection(java.net.HttpURLConnection) File(java.io.File)

Aggregations

Node (org.onosproject.cluster.Node)5 DefaultControllerNode (org.onosproject.cluster.DefaultControllerNode)3 NodeId (org.onosproject.cluster.NodeId)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 Instant (java.time.Instant)2 Set (java.util.Set)2 IpAddress (org.onlab.packet.IpAddress)2 ClusterEventListener (org.onosproject.cluster.ClusterEventListener)2 ClusterService (org.onosproject.cluster.ClusterService)2 ControllerNode (org.onosproject.cluster.ControllerNode)2 Activate (org.osgi.service.component.annotations.Activate)2 Component (org.osgi.service.component.annotations.Component)2 Deactivate (org.osgi.service.component.annotations.Deactivate)2 Reference (org.osgi.service.component.annotations.Reference)2 ReferenceCardinality (org.osgi.service.component.annotations.ReferenceCardinality)2 Logger (org.slf4j.Logger)2 LoggerFactory.getLogger (org.slf4j.LoggerFactory.getLogger)2