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