Search in sources :

Example 11 with DefaultControllerNode

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

the class SimpleMastershipStore method activate.

@Activate
public void activate() {
    if (clusterService == null) {
        // just for ease of unit test
        final ControllerNode instance = new DefaultControllerNode(new NodeId("local"), IpAddress.valueOf("127.0.0.1"));
        clusterService = 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 State getState(NodeId nodeId) {
                if (instance.id().equals(nodeId)) {
                    return State.ACTIVE;
                } else {
                    return 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) {
            }
        };
    }
    log.info("Started");
}
Also used : ClusterService(org.onosproject.cluster.ClusterService) HashSet(java.util.HashSet) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Version(org.onosproject.core.Version) State(org.onosproject.cluster.ControllerNode.State) Instant(java.time.Instant) 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) Activate(org.osgi.service.component.annotations.Activate)

Example 12 with DefaultControllerNode

use of org.onosproject.cluster.DefaultControllerNode 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 13 with DefaultControllerNode

use of org.onosproject.cluster.DefaultControllerNode 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 14 with DefaultControllerNode

use of org.onosproject.cluster.DefaultControllerNode 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 15 with DefaultControllerNode

use of org.onosproject.cluster.DefaultControllerNode in project trellis-control by opennetworkinglab.

the class DefaultRoutingHandlerTest method testShouldHandleRoutingCase5.

/*
     * Node 1 is the leader of 1A, 1B. Node 2 becomes the leader of 1A, 1B later
     * shouldP is not purged in time. This can easily happen if we dont get in
     * time cluster/mastership events. shouldProgram absorbs this negative scenario.
     */
@Test
public void testShouldHandleRoutingCase5() {
    expect(mockWps.leadershipService.getLeadership(DEV1A_PARTITION_ID)).andReturn(new Leadership(DEV1A_PARTITION_ID, new Leader(NODE1, 0, 0), List.of(NODE2, NODE3))).anyTimes();
    expect(mockWps.leadershipService.getLeadership(DEV1B_PARTITION_ID)).andReturn(new Leadership(DEV1B_PARTITION_ID, new Leader(NODE1, 0, 0), List.of(NODE2, NODE3))).anyTimes();
    replay(mockWps.leadershipService);
    expect(srManager.getPairDeviceId(DEV1A)).andReturn(Optional.of(DEV1B)).anyTimes();
    expect(srManager.getPairDeviceId(DEV1B)).andReturn(Optional.of(DEV1A)).anyTimes();
    replay(srManager);
    // Node 1 should program both 1A and 1B
    expect(srManager.clusterService.getLocalNode()).andReturn(new DefaultControllerNode(NODE1, IP1)).anyTimes();
    replay(srManager.clusterService);
    assertNull(dfh.shouldProgram.get(DEV1A));
    assertNull(dfh.shouldProgram.get(DEV1B));
    assertTrue(dfh.shouldProgram(DEV1A));
    assertTrue(dfh.shouldProgram(DEV1B));
    assertEquals(NODE1, dfh.shouldProgram.get(DEV1A));
    assertEquals(NODE1, dfh.shouldProgram.get(DEV1B));
    reset(srManager.clusterService);
    clearShouldProgram();
    // Node 2 should program no device
    expect(srManager.clusterService.getLocalNode()).andReturn(new DefaultControllerNode(NODE2, IP2)).anyTimes();
    replay(srManager.clusterService);
    assertNull(dfh.shouldProgram.get(DEV1A));
    assertNull(dfh.shouldProgram.get(DEV1B));
    assertFalse(dfh.shouldProgram(DEV1A));
    assertFalse(dfh.shouldProgram(DEV1B));
    assertNotEquals(NODE2, dfh.shouldProgram.get(DEV1A));
    assertNotEquals(NODE2, dfh.shouldProgram.get(DEV1B));
    // Leadership moves to Node 2
    reset(mockWps.leadershipService);
    expect(mockWps.leadershipService.getLeadership(DEV1A_PARTITION_ID)).andReturn(new Leadership(DEV1A_PARTITION_ID, new Leader(NODE2, 0, 0), List.of(NODE2, NODE3))).anyTimes();
    expect(mockWps.leadershipService.getLeadership(DEV1B_PARTITION_ID)).andReturn(new Leadership(DEV1B_PARTITION_ID, new Leader(NODE2, 0, 0), List.of(NODE2, NODE3))).anyTimes();
    replay(mockWps.leadershipService);
    reset(srManager.clusterService);
    // Node 1 should program 1A, 1B
    expect(srManager.clusterService.getLocalNode()).andReturn(new DefaultControllerNode(NODE1, IP1)).anyTimes();
    replay(srManager.clusterService);
    assertNotNull(dfh.shouldProgram.get(DEV1A));
    assertNotNull(dfh.shouldProgram.get(DEV1B));
    assertTrue(dfh.shouldProgram(DEV1A));
    assertTrue(dfh.shouldProgram(DEV1B));
    assertEquals(NODE1, dfh.shouldProgram.get(DEV1A));
    assertEquals(NODE1, dfh.shouldProgram.get(DEV1B));
    reset(srManager.clusterService);
    // Node 2 should program no device
    expect(srManager.clusterService.getLocalNode()).andReturn(new DefaultControllerNode(NODE2, IP2)).anyTimes();
    replay(srManager.clusterService);
    assertNotNull(dfh.shouldProgram.get(DEV1A));
    assertNotNull(dfh.shouldProgram.get(DEV1B));
    assertFalse(dfh.shouldProgram(DEV1A));
    assertFalse(dfh.shouldProgram(DEV1B));
    assertEquals(NODE1, dfh.shouldProgram.get(DEV1A));
    assertEquals(NODE1, dfh.shouldProgram.get(DEV1B));
}
Also used : Leadership(org.onosproject.cluster.Leadership) Leader(org.onosproject.cluster.Leader) DefaultControllerNode(org.onosproject.cluster.DefaultControllerNode) Test(org.junit.Test)

Aggregations

DefaultControllerNode (org.onosproject.cluster.DefaultControllerNode)16 NodeId (org.onosproject.cluster.NodeId)9 Test (org.junit.Test)7 ControllerNode (org.onosproject.cluster.ControllerNode)7 Leader (org.onosproject.cluster.Leader)5 Leadership (org.onosproject.cluster.Leadership)5 Activate (org.osgi.service.component.annotations.Activate)5 Set (java.util.Set)4 Collectors (java.util.stream.Collectors)3 ClusterMetadata (org.onosproject.cluster.ClusterMetadata)3 Sets (com.google.common.collect.Sets)2 InetAddress (java.net.InetAddress)2 URL (java.net.URL)2 ClusterMetadataProvider (org.onosproject.cluster.ClusterMetadataProvider)2 ClusterMetadataProviderRegistry (org.onosproject.cluster.ClusterMetadataProviderRegistry)2 ClusterMetadataProviderService (org.onosproject.cluster.ClusterMetadataProviderService)2 ClusterService (org.onosproject.cluster.ClusterService)2 ClusterServiceAdapter (org.onosproject.cluster.ClusterServiceAdapter)2 Node (org.onosproject.cluster.Node)2 PartitionId (org.onosproject.cluster.PartitionId)2