Search in sources :

Example 6 with Versioned

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;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Versioned(org.onosproject.store.service.Versioned) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 7 with Versioned

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);
    }
}
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 8 with Versioned

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;
}
Also used : Versioned(org.onosproject.store.service.Versioned) DocumentPath(org.onosproject.store.service.DocumentPath) Map(java.util.Map) EventuallyConsistentMap(org.onosproject.store.service.EventuallyConsistentMap)

Example 9 with Versioned

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);
}
Also used : Versioned(org.onosproject.store.service.Versioned) FailedException(org.onosproject.config.FailedException) DataNode(org.onosproject.yang.model.DataNode) DocumentPath(org.onosproject.store.service.DocumentPath)

Example 10 with Versioned

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;
}
Also used : Versioned(org.onosproject.store.service.Versioned) DataNode(org.onosproject.yang.model.DataNode) FailedException(org.onosproject.config.FailedException) KeyLeaf(org.onosproject.yang.model.KeyLeaf) DocumentPath(org.onosproject.store.service.DocumentPath) NodeKey(org.onosproject.yang.model.NodeKey)

Aggregations

Versioned (org.onosproject.store.service.Versioned)14 Map (java.util.Map)7 ConsistentMap (org.onosproject.store.service.ConsistentMap)5 DocumentPath (org.onosproject.store.service.DocumentPath)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Set (java.util.Set)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 Collectors (java.util.stream.Collectors)3 FailedException (org.onosproject.config.FailedException)3 DataNode (org.onosproject.yang.model.DataNode)3 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Executors.newSingleThreadScheduledExecutor (java.util.concurrent.Executors.newSingleThreadScheduledExecutor)2 TimeUnit (java.util.concurrent.TimeUnit)2 MapEvent (org.onosproject.store.service.MapEvent)2 MapEventListener (org.onosproject.store.service.MapEventListener)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1