Search in sources :

Example 1 with NodeKey

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

the class ResourceIdParser method parseResId.

/**
 * Gets String representation of ResourceId.
 *
 * <pre>
 *   ResourceId := 'root' ('|' element)*
 *   element := LeafListKey | ListKey | NodeKey
 *   SchemaId := [string SchemaId#name] '#' [string SchemaId#namespace]
 *   LeafListKey := SchemaId '#' [string representation of LeafListKey#value]
 *   ListKey := SchemaId (KeyLeaf)*
 *   KeyLeaf := '$' SchemaId '#' [string representation of KeyLeaf#leafValue]
 *   NodeKey := SchemaId
 * </pre>
 *
 * @param path to convert
 * @return String representation
 */
public static String parseResId(ResourceId path) {
    StringBuilder bldr = new StringBuilder();
    bldr.append(ROOT);
    if (path == null) {
        return bldr.toString();
    }
    List<NodeKey> nodeKeyList = new LinkedList<>();
    Iterator<NodeKey> itr = path.nodeKeys().iterator();
    while (itr.hasNext()) {
        nodeKeyList.add(itr.next());
    }
    // exception for dealing with root
    if (nodeKeyList.get(0).schemaId().name().equals("/")) {
        nodeKeyList.remove(0);
    }
    for (NodeKey key : nodeKeyList) {
        bldr.append(EL_SEP);
        if (key instanceof LeafListKey) {
            parseLeafList((LeafListKey) key, bldr);
        } else if (key instanceof ListKey) {
            parseKeyList((ListKey) key, bldr);
        } else {
            parseNodeKey(key, bldr);
        }
    }
    return bldr.toString();
}
Also used : ListKey(org.onosproject.yang.model.ListKey) LeafListKey(org.onosproject.yang.model.LeafListKey) LeafListKey(org.onosproject.yang.model.LeafListKey) NodeKey(org.onosproject.yang.model.NodeKey) LinkedList(java.util.LinkedList)

Example 2 with NodeKey

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

the class ResourceIdParser method getMultiInstanceKey.

public static NodeKey getMultiInstanceKey(ResourceId path) {
    int last = path.nodeKeys().size();
    NodeKey ret = path.nodeKeys().get(last - 1);
    if (ret instanceof ListKey) {
        return ret;
    } else {
        return null;
    }
}
Also used : ListKey(org.onosproject.yang.model.ListKey) LeafListKey(org.onosproject.yang.model.LeafListKey) NodeKey(org.onosproject.yang.model.NodeKey)

Example 3 with NodeKey

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

the class DeviceResourceIds method toDeviceId.

/**
 * Transforms root node of a device to corresponding DeviceId.
 *
 * @param deviceRoot NodeKey of a device root node
 * @return DeviceId
 * @throws IllegalArgumentException if not a device node
 */
public static DeviceId toDeviceId(NodeKey<?> deviceRoot) {
    if (!DEVICE_SCHEMA.equals(deviceRoot.schemaId())) {
        throw new IllegalArgumentException(deviceRoot + " is not a device node");
    }
    if (deviceRoot instanceof ListKey) {
        ListKey device = (ListKey) deviceRoot;
        Optional<DeviceId> did = device.keyLeafs().stream().filter(kl -> DEVICE_ID_KL_NAME.equals(kl.leafSchema().name())).map(KeyLeaf::leafValAsString).map(DeviceId::deviceId).findFirst();
        if (did.isPresent()) {
            return did.get();
        }
        throw new IllegalArgumentException("device-id not found in " + deviceRoot);
    }
    throw new IllegalArgumentException("Unexpected type " + deviceRoot.getClass());
}
Also used : KeyLeaf(org.onosproject.yang.model.KeyLeaf) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) SchemaId(org.onosproject.yang.model.SchemaId) ListKey(org.onosproject.yang.model.ListKey) Optional(java.util.Optional) DeviceId(org.onosproject.net.DeviceId) NodeKey(org.onosproject.yang.model.NodeKey) ResourceId(org.onosproject.yang.model.ResourceId) Beta(com.google.common.annotations.Beta) ListKey(org.onosproject.yang.model.ListKey) DeviceId(org.onosproject.net.DeviceId) KeyLeaf(org.onosproject.yang.model.KeyLeaf)

Example 4 with NodeKey

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

the class ResourceIds method fromInstanceIdentifier.

/**
 * Converts instance-identifier String into a ResourceId.
 *
 * @param input instance-identifier
 * @return Corresponding ResourceId relative to root or null if {@code iid} is '/'
 * Returned ResourceId will not have root NodeKey in it's path.
 * consider using {@link #prefixDcsRoot(ResourceId)},
 * {@link #prefixYrsRoot(ResourceId)} to add them.
 */
public static ResourceId fromInstanceIdentifier(String input) {
    String[] nodes = input.split("/");
    List<NodeKey> nodeKeys = Arrays.stream(nodes).filter(s -> !s.isEmpty()).map(ResourceIds::toNodeKey).collect(Collectors.toList());
    if (nodeKeys.isEmpty()) {
        return null;
    }
    Builder builder = ResourceId.builder();
    // fill-in null (=inherit from parent) nameSpace
    String lastNamespace = null;
    for (NodeKey nodeKey : nodeKeys) {
        if (nodeKey.schemaId().namespace() != null) {
            lastNamespace = nodeKey.schemaId().namespace();
        }
        if (nodeKey instanceof LeafListKey) {
            builder.addLeafListBranchPoint(nodeKey.schemaId().name(), firstNonNull(nodeKey.schemaId().namespace(), lastNamespace), ((LeafListKey) nodeKey).value());
        } else if (nodeKey instanceof ListKey) {
            builder.addBranchPointSchema(nodeKey.schemaId().name(), lastNamespace);
            for (KeyLeaf kl : ((ListKey) nodeKey).keyLeafs()) {
                builder.addKeyLeaf(kl.leafSchema().name(), firstNonNull(kl.leafSchema().namespace(), lastNamespace), kl.leafValue());
            }
        } else {
            builder.addBranchPointSchema(nodeKey.schemaId().name(), lastNamespace);
        }
    }
    return builder.build();
}
Also used : ListKey(org.onosproject.yang.model.ListKey) LeafListKey(org.onosproject.yang.model.LeafListKey) LeafListKey(org.onosproject.yang.model.LeafListKey) ListKeyBuilder(org.onosproject.yang.model.ListKey.ListKeyBuilder) Builder(org.onosproject.yang.model.ResourceId.Builder) LeafListKeyBuilder(org.onosproject.yang.model.LeafListKey.LeafListKeyBuilder) KeyLeaf(org.onosproject.yang.model.KeyLeaf) NodeKey(org.onosproject.yang.model.NodeKey)

Example 5 with NodeKey

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

the class YangRuntimeManager method getSchemaContext.

@Override
public SchemaContext getSchemaContext(ResourceId resourceId) {
    checkNotNull(resourceId, " resource id can't be null.");
    NodeKey key = resourceId.nodeKeys().get(0);
    if (resourceId.nodeKeys().size() == 1 && "/".equals(key.schemaId().name())) {
        return modelRegistry;
    }
    log.info("To be implemented.");
    return null;
}
Also used : 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