use of org.onosproject.store.service.Versioned in project fabric-tna by stratum.
the class MockConsistentMap method compute.
@Override
public Versioned<V> compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
AtomicBoolean updated = new AtomicBoolean(false);
AtomicReference<Versioned<V>> previousValue = new AtomicReference<>();
Versioned<V> result = map.compute(key, (k, v) -> {
updated.set(true);
previousValue.set(serializer.decode(serializer.encode(v)));
return version(remappingFunction.apply(k, Versioned.valueOrNull(v)));
});
if (updated.get()) {
notifyListeners(mapName, key, result, previousValue.get());
}
return result;
}
use of org.onosproject.store.service.Versioned 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.store.service.Versioned in project onos by opennetworkinglab.
the class DistributedContextEventMapTreeStore method getEventMapByHint.
@Override
public Map<String, String> getEventMapByHint(String eventType, String eventHint) throws WorkflowException {
DocumentPath path = DocumentPath.from(Lists.newArrayList("root", eventType, eventHint));
Map<String, Versioned<String>> contexts = complete(eventMapTree.getChildren(path));
Map<String, String> eventMap = Maps.newHashMap();
if (Objects.isNull(contexts)) {
return eventMap;
}
for (Map.Entry<String, Versioned<String>> entry : contexts.entrySet()) {
eventMap.put(entry.getKey(), entry.getValue().value());
}
log.trace("getEventMapByHint returns eventMap {} ", eventMap);
return eventMap;
}
use of org.onosproject.store.service.Versioned in project onos by opennetworkinglab.
the class DistributedDynamicConfigStore method deleteNodeRecursive.
@Override
public CompletableFuture<Boolean> deleteNodeRecursive(ResourceId path) {
String spath = ResourceIdParser.parseResId(path);
if (spath == null) {
throw new FailedException("Invalid RsourceId, cannot delete Node");
}
if (spath.compareTo(ResourceIdParser.ROOT) == 0) {
throw new FailedException("Cannot delete Root");
}
DocumentPath dpath = DocumentPath.from(spath);
DataNode.Type type = null;
CompletableFuture<Versioned<DataNode.Type>> ret = keystore.get(dpath);
type = completeVersioned(ret);
if (type == null) {
throw new FailedException("Cannot delete, Requested node or some of the parents" + "are not present in the requested path");
}
DataNode retVal = null;
if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
removeLeaf(spath);
} else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
removeLeaf(spath);
} else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) {
deleteInner(spath);
} else if (type == DataNode.Type.MULTI_INSTANCE_NODE) {
deleteInner(spath);
} else {
throw new FailedException("Invalid node type");
}
return CompletableFuture.completedFuture(true);
}
use of org.onosproject.store.service.Versioned in project onos by opennetworkinglab.
the class DistributedDynamicConfigStore method readNode.
@Override
public CompletableFuture<DataNode> readNode(ResourceId path, Filter filter) {
CompletableFuture<DataNode> eventFuture = CompletableFuture.completedFuture(null);
String spath = ResourceIdParser.parseResId(path);
DocumentPath dpath = DocumentPath.from(spath);
DataNode.Type type = null;
CompletableFuture<Versioned<DataNode.Type>> ret = keystore.get(dpath);
type = completeVersioned(ret);
if (type == null) {
throw new FailedException("Requested node or some of the parents " + "are not present in the requested path: " + spath);
}
DataNode retVal = null;
if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
retVal = readLeaf(spath);
} else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
retVal = readLeaf(spath);
} else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) {
NodeKey key = ResourceIdParser.getInstanceKey(path);
if (key == null) {
throw new FailedException("Key type did not match node type");
}
DataNode.Builder superBldr = InnerNode.builder(key.schemaId().name(), key.schemaId().namespace()).type(type);
readInner(superBldr, spath);
retVal = superBldr.build();
} else if (type == DataNode.Type.MULTI_INSTANCE_NODE) {
NodeKey key = ResourceIdParser.getMultiInstanceKey(path);
if (key == null) {
throw new FailedException("Key type did not match node type");
}
DataNode.Builder superBldr = InnerNode.builder(key.schemaId().name(), key.schemaId().namespace()).type(type);
for (KeyLeaf keyLeaf : ((ListKey) key).keyLeafs()) {
// String tempPath = ResourceIdParser.appendKeyLeaf(spath, keyLeaf);
// LeafNode lfnd = readLeaf(tempPath);
superBldr.addKeyLeaf(keyLeaf.leafSchema().name(), keyLeaf.leafSchema().namespace(), String.valueOf(keyLeaf.leafValue()));
}
readInner(superBldr, spath);
retVal = superBldr.build();
} else {
throw new FailedException("Invalid node type");
}
if (retVal != null) {
eventFuture = CompletableFuture.completedFuture(retVal);
} else {
log.info("STORE: Failed to READ node");
}
return eventFuture;
}
Aggregations