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");
}
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;
}
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;
}
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);
}
}
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));
}
Aggregations