Search in sources :

Example 1 with ClusterMetadata

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

the class DefaultClusterMetadataProvider method activate.

@Activate
public void activate() {
    String localIp = getSiteLocalAddress();
    ControllerNode localNode = new DefaultControllerNode(new NodeId(localIp), IpAddress.valueOf(localIp), DEFAULT_ONOS_PORT);
    ClusterMetadata metadata = new ClusterMetadata(PROVIDER_ID, "default", localNode, ImmutableSet.of(), ImmutableSet.of(), UUID.randomUUID().toString());
    long version = System.currentTimeMillis();
    cachedMetadata.set(new Versioned<>(metadata, version));
    providerRegistry.register(this);
    log.info("Started");
}
Also used : ClusterMetadata(org.onosproject.cluster.ClusterMetadata) NodeId(org.onosproject.cluster.NodeId) ControllerNode(org.onosproject.cluster.ControllerNode) DefaultControllerNode(org.onosproject.cluster.DefaultControllerNode) DefaultControllerNode(org.onosproject.cluster.DefaultControllerNode) Activate(org.osgi.service.component.annotations.Activate)

Example 2 with ClusterMetadata

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

the class ClusterMetadataManager method getLocalNode.

@Override
public ControllerNode getLocalNode() {
    checkPermission(CLUSTER_READ);
    if (localNode == null) {
        ClusterMetadata metadata = getProvider().getClusterMetadata().value();
        ControllerNode localNode = metadata.getLocalNode();
        try {
            if (localNode != null) {
                this.localNode = new DefaultControllerNode(localNode.id(), localNode.ip() != null ? localNode.ip() : findLocalIp(), localNode.tcpPort());
            } else {
                IpAddress ip = findLocalIp();
                localNode = metadata.getControllerNodes().stream().filter(node -> node.ip().equals(ip)).findFirst().orElse(null);
                if (localNode != null) {
                    this.localNode = localNode;
                } else {
                    this.localNode = new DefaultControllerNode(NodeId.nodeId(ip.toString()), ip);
                }
            }
        } catch (SocketException e) {
            throw new IllegalStateException(e);
        }
    }
    return localNode;
}
Also used : ClusterMetadata(org.onosproject.cluster.ClusterMetadata) CLUSTER_READ(org.onosproject.security.AppPermission.Type.CLUSTER_READ) Enumeration(java.util.Enumeration) URL(java.net.URL) AppGuard.checkPermission(org.onosproject.security.AppGuard.checkPermission) AbstractProviderService(org.onosproject.net.provider.AbstractProviderService) ClusterMetadataProvider(org.onosproject.cluster.ClusterMetadataProvider) ControllerNode(org.onosproject.cluster.ControllerNode) DefaultControllerNode(org.onosproject.cluster.DefaultControllerNode) InetAddress(java.net.InetAddress) Component(org.osgi.service.component.annotations.Component) SocketException(java.net.SocketException) ImmutableList(com.google.common.collect.ImmutableList) ClusterMetadataProviderRegistry(org.onosproject.cluster.ClusterMetadataProviderRegistry) ClusterMetadataProviderService(org.onosproject.cluster.ClusterMetadataProviderService) ClusterMetadata(org.onosproject.cluster.ClusterMetadata) Activate(org.osgi.service.component.annotations.Activate) IpAddress(org.onlab.packet.IpAddress) NodeId(org.onosproject.cluster.NodeId) Logger(org.slf4j.Logger) ClusterActivator(org.onosproject.store.atomix.ClusterActivator) MalformedURLException(java.net.MalformedURLException) Deactivate(org.osgi.service.component.annotations.Deactivate) ClusterMetadataEventListener(org.onosproject.cluster.ClusterMetadataEventListener) NetworkInterface(java.net.NetworkInterface) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Set(java.util.Set) ProviderId(org.onosproject.net.provider.ProviderId) Inet4Address(java.net.Inet4Address) UnknownHostException(java.net.UnknownHostException) Collectors(java.util.stream.Collectors) AbstractListenerProviderRegistry(org.onosproject.net.provider.AbstractListenerProviderRegistry) ClusterMetadataService(org.onosproject.cluster.ClusterMetadataService) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) Versioned(org.onosproject.store.service.Versioned) List(java.util.List) ClusterMetadataAdminService(org.onosproject.cluster.ClusterMetadataAdminService) ClusterMetadataEvent(org.onosproject.cluster.ClusterMetadataEvent) PartitionId(org.onosproject.cluster.PartitionId) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) Reference(org.osgi.service.component.annotations.Reference) SocketException(java.net.SocketException) ControllerNode(org.onosproject.cluster.ControllerNode) DefaultControllerNode(org.onosproject.cluster.DefaultControllerNode) IpAddress(org.onlab.packet.IpAddress) DefaultControllerNode(org.onosproject.cluster.DefaultControllerNode)

Example 3 with ClusterMetadata

use of org.onosproject.cluster.ClusterMetadata 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)

Example 4 with ClusterMetadata

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

the class AtomixManager method createAtomix.

private Atomix createAtomix() {
    ClusterMetadata metadata = metadataService.getClusterMetadata();
    // If a storage DNS service was provided, use the DNS service for service discovery.
    // Otherwise, use a static list of storage nodes.
    NodeDiscoveryProvider discovery;
    if (metadata.getStorageDnsService() != null) {
        discovery = DnsDiscoveryProvider.builder().withService(metadata.getStorageDnsService()).build();
    } else {
        discovery = BootstrapDiscoveryProvider.builder().withNodes(metadata.getStorageNodes().stream().map(node -> Node.builder().withId(node.id().id()).withHost(node.host()).withPort(node.tcpPort()).build()).collect(Collectors.toList())).build();
    }
    if (!metadata.getStorageNodes().isEmpty()) {
        // If storage nodes are defined, construct an instance that connects to them for service discovery.
        return Atomix.builder(getClass().getClassLoader()).withClusterId(metadata.getName()).withMemberId(metadataService.getLocalNode().id().id()).withHost(metadata.getLocalNode().host()).withPort(metadata.getLocalNode().tcpPort()).withProperty("type", "onos").withMembershipProvider(discovery).build();
    } else {
        log.warn("No storage nodes found in cluster metadata!");
        log.warn("Bootstrapping ONOS cluster in test mode! For production use, configure external storage nodes.");
        // If storage nodes are not defined, construct a local instance with a Raft partition group.
        List<String> raftMembers = !metadata.getControllerNodes().isEmpty() ? metadata.getControllerNodes().stream().map(node -> node.id().id()).collect(Collectors.toList()) : Collections.singletonList(metadataService.getLocalNode().id().id());
        return Atomix.builder(getClass().getClassLoader()).withClusterId(metadata.getName()).withMemberId(metadataService.getLocalNode().id().id()).withHost(metadata.getLocalNode().host()).withPort(metadata.getLocalNode().tcpPort()).withProperty("type", "onos").withMembershipProvider(discovery).withManagementGroup(RaftPartitionGroup.builder("system").withNumPartitions(1).withDataDirectory(new File(LOCAL_DATA_DIR, "system")).withMembers(raftMembers).build()).addPartitionGroup(RaftPartitionGroup.builder("raft").withNumPartitions(raftMembers.size()).withDataDirectory(new File(LOCAL_DATA_DIR, "data")).withMembers(raftMembers).build()).build();
    }
}
Also used : ClusterMetadata(org.onosproject.cluster.ClusterMetadata) NodeDiscoveryProvider(io.atomix.cluster.discovery.NodeDiscoveryProvider) File(java.io.File)

Aggregations

ClusterMetadata (org.onosproject.cluster.ClusterMetadata)4 DefaultControllerNode (org.onosproject.cluster.DefaultControllerNode)3 NodeId (org.onosproject.cluster.NodeId)3 Activate (org.osgi.service.component.annotations.Activate)3 File (java.io.File)2 URL (java.net.URL)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 ClusterMetadataProvider (org.onosproject.cluster.ClusterMetadataProvider)2 ClusterMetadataProviderRegistry (org.onosproject.cluster.ClusterMetadataProviderRegistry)2 ClusterMetadataProviderService (org.onosproject.cluster.ClusterMetadataProviderService)2 PartitionId (org.onosproject.cluster.PartitionId)2 ProviderId (org.onosproject.net.provider.ProviderId)2 Versioned (org.onosproject.store.service.Versioned)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