Search in sources :

Example 6 with NodeKey

use of org.onosproject.yang.model.NodeKey in project onos by opennetworkinglab.

the class RestconfManager method getDataForStore.

private ResourceData getDataForStore(ResourceData resourceData) {
    List<DataNode> nodes = resourceData.dataNodes();
    ResourceId rid = resourceData.resourceId();
    DataNode.Builder dbr = null;
    ResourceId parentId = null;
    try {
        NodeKey lastKey = rid.nodeKeys().get(rid.nodeKeys().size() - 1);
        SchemaId sid = lastKey.schemaId();
        if (lastKey instanceof ListKey) {
            dbr = InnerNode.builder(sid.name(), sid.namespace()).type(MULTI_INSTANCE_NODE);
            for (KeyLeaf keyLeaf : ((ListKey) lastKey).keyLeafs()) {
                Object val = keyLeaf.leafValue();
                dbr = dbr.addKeyLeaf(keyLeaf.leafSchema().name(), sid.namespace(), val);
                dbr = dbr.createChildBuilder(keyLeaf.leafSchema().name(), sid.namespace(), val).type(SINGLE_INSTANCE_LEAF_VALUE_NODE);
                // Exit for key leaf node
                dbr = dbr.exitNode();
            }
        } else {
            dbr = InnerNode.builder(sid.name(), sid.namespace()).type(SINGLE_INSTANCE_NODE);
        }
        if (nodes != null && !nodes.isEmpty()) {
            // adding the parent node for given list of nodes
            for (DataNode node : nodes) {
                dbr = ((InnerNode.Builder) dbr).addNode(node);
            }
        }
        parentId = rid.copyBuilder().removeLastKey().build();
    } catch (CloneNotSupportedException e) {
        log.error("getDataForStore()", e);
        return null;
    }
    ResourceData.Builder resData = DefaultResourceData.builder();
    resData.addDataNode(dbr.build());
    resData.resourceId(parentId);
    return resData.build();
}
Also used : ResourceData(org.onosproject.yang.model.ResourceData) DefaultResourceData(org.onosproject.yang.model.DefaultResourceData) KeyLeaf(org.onosproject.yang.model.KeyLeaf) InnerNode(org.onosproject.yang.model.InnerNode) ListKey(org.onosproject.yang.model.ListKey) ResourceId(org.onosproject.yang.model.ResourceId) DataNode(org.onosproject.yang.model.DataNode) RestconfUtils.convertJsonToDataNode(org.onosproject.restconf.utils.RestconfUtils.convertJsonToDataNode) SchemaId(org.onosproject.yang.model.SchemaId) NodeKey(org.onosproject.yang.model.NodeKey)

Example 7 with NodeKey

use of org.onosproject.yang.model.NodeKey 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)

Example 8 with NodeKey

use of org.onosproject.yang.model.NodeKey in project onos by opennetworkinglab.

the class DistributedDynamicConfigStore method traverseInner.

// FIXME this is more like addInnteNode
/**
 * @param path pointing to {@code node}
 * @param node node
 */
private void traverseInner(String path, InnerNode node) {
    log.trace("traverseInner({}, {})", path, node);
    addKey(path, node.type());
    Map<NodeKey, DataNode> entries = node.childNodes();
    if (entries.size() == 0) {
        return;
    }
    // FIXME ignoring results
    entries.forEach((k, v) -> {
        String tempPath;
        tempPath = ResourceIdParser.appendNodeKey(path, v.key());
        if (v.type() == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
            addLeaf(tempPath, (LeafNode) v);
        } else if (v.type() == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
            tempPath = ResourceIdParser.appendLeafList(tempPath, (LeafListKey) v.key());
            addLeaf(tempPath, (LeafNode) v);
        } else if (v.type() == DataNode.Type.SINGLE_INSTANCE_NODE) {
            traverseInner(tempPath, (InnerNode) v);
        } else if (v.type() == DataNode.Type.MULTI_INSTANCE_NODE) {
            tempPath = ResourceIdParser.appendKeyList(tempPath, (ListKey) v.key());
            traverseInner(tempPath, (InnerNode) v);
        } else {
            throw new FailedException("Invalid node type");
        }
    });
}
Also used : DataNode(org.onosproject.yang.model.DataNode) FailedException(org.onosproject.config.FailedException) LeafNode(org.onosproject.yang.model.LeafNode) NodeKey(org.onosproject.yang.model.NodeKey) InnerNode(org.onosproject.yang.model.InnerNode)

Example 9 with NodeKey

use of org.onosproject.yang.model.NodeKey in project onos by opennetworkinglab.

the class ResourceIds method toInstanceIdentifier.

/**
 * Converts ResourceId to instance-identifier.
 *
 * @param rid to convert
 * @return instance-identifier
 *
 * @see <a href="https://tools.ietf.org/html/rfc7951#section-6.11">RFC 7951</a>
 * @see <a href="https://tools.ietf.org/html/rfc7950#section-14">RFC 7950 for ABNF</a>
 */
public static String toInstanceIdentifier(ResourceId rid) {
    StringBuilder s = new StringBuilder();
    String lastNamespace = null;
    for (NodeKey nk : rid.nodeKeys()) {
        if (nk.schemaId().name().equals("/")) {
            // DCS root: org.onosproject.dcs:/
            continue;
        }
        s.append('/');
        if (!Objects.equals(lastNamespace, nk.schemaId().namespace())) {
            s.append(nk.schemaId().namespace());
            s.append(':');
            lastNamespace = nk.schemaId().namespace();
        }
        s.append(nk.schemaId().name());
        if (nk instanceof LeafListKey) {
            LeafListKey llk = (LeafListKey) nk;
            s.append('[');
            s.append('.');
            s.append('=');
            s.append('"').append(StringEscapeUtils.escapeJson(llk.asString())).append('"');
            s.append(']');
        } else if (nk instanceof ListKey) {
            ListKey lk = (ListKey) nk;
            for (KeyLeaf kl : lk.keyLeafs()) {
                s.append('[');
                if (!Objects.equals(kl.leafSchema().namespace(), lastNamespace)) {
                    s.append(kl.leafSchema().namespace());
                    s.append(':');
                }
                s.append(kl.leafSchema().name());
                s.append('=');
                s.append('"').append(StringEscapeUtils.escapeJson(kl.leafValAsString())).append('"');
                s.append(']');
            }
        } else {
        // normal NodeKey
        // nothing to do
        }
    }
    if (s.length() == 0) {
        return "/";
    }
    return s.toString();
}
Also used : ListKey(org.onosproject.yang.model.ListKey) LeafListKey(org.onosproject.yang.model.LeafListKey) LeafListKey(org.onosproject.yang.model.LeafListKey) KeyLeaf(org.onosproject.yang.model.KeyLeaf) NodeKey(org.onosproject.yang.model.NodeKey)

Aggregations

NodeKey (org.onosproject.yang.model.NodeKey)9 ListKey (org.onosproject.yang.model.ListKey)6 KeyLeaf (org.onosproject.yang.model.KeyLeaf)5 LeafListKey (org.onosproject.yang.model.LeafListKey)4 DataNode (org.onosproject.yang.model.DataNode)3 FailedException (org.onosproject.config.FailedException)2 InnerNode (org.onosproject.yang.model.InnerNode)2 ResourceId (org.onosproject.yang.model.ResourceId)2 SchemaId (org.onosproject.yang.model.SchemaId)2 Beta (com.google.common.annotations.Beta)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 LinkedList (java.util.LinkedList)1 Optional (java.util.Optional)1 DeviceId (org.onosproject.net.DeviceId)1 RestconfUtils.convertJsonToDataNode (org.onosproject.restconf.utils.RestconfUtils.convertJsonToDataNode)1 DocumentPath (org.onosproject.store.service.DocumentPath)1 Versioned (org.onosproject.store.service.Versioned)1 DefaultResourceData (org.onosproject.yang.model.DefaultResourceData)1 LeafListKeyBuilder (org.onosproject.yang.model.LeafListKey.LeafListKeyBuilder)1 LeafNode (org.onosproject.yang.model.LeafNode)1