Search in sources :

Example 1 with LeafListKey

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

use of org.onosproject.yang.model.LeafListKey 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 3 with LeafListKey

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

the class ResourceIds method resourceId.

/**
 * Builds the ResourceId of specified {@code node}.
 *
 * @param parent ResourceId of {@code node} parent
 * @param node   to create ResourceId.
 * @return ResourceId of {@code node}
 */
public static ResourceId resourceId(ResourceId parent, DataNode node) {
    final ResourceId.Builder builder;
    if (parent == null) {
        builder = ResourceId.builder();
    } else {
        try {
            builder = parent.copyBuilder();
        } catch (CloneNotSupportedException e) {
            throw new IllegalArgumentException(e);
        }
    }
    SchemaId sid = node.key().schemaId();
    switch(node.type()) {
        case MULTI_INSTANCE_LEAF_VALUE_NODE:
            builder.addLeafListBranchPoint(sid.name(), sid.namespace(), ((LeafListKey) node.key()).asString());
            break;
        case MULTI_INSTANCE_NODE:
            builder.addBranchPointSchema(sid.name(), sid.namespace());
            for (KeyLeaf keyLeaf : ((ListKey) node.key()).keyLeafs()) {
                builder.addKeyLeaf(keyLeaf.leafSchema().name(), keyLeaf.leafSchema().namespace(), keyLeaf.leafValAsString());
            }
            break;
        case SINGLE_INSTANCE_LEAF_VALUE_NODE:
        case SINGLE_INSTANCE_NODE:
            builder.addBranchPointSchema(sid.name(), sid.namespace());
            break;
        default:
            throw new IllegalArgumentException("Unknown type " + node);
    }
    return builder.build();
}
Also used : ListKey(org.onosproject.yang.model.ListKey) LeafListKey(org.onosproject.yang.model.LeafListKey) ResourceId(org.onosproject.yang.model.ResourceId) KeyLeaf(org.onosproject.yang.model.KeyLeaf) SchemaId(org.onosproject.yang.model.SchemaId) Builder(org.onosproject.yang.model.ResourceId.Builder)

Example 4 with LeafListKey

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

LeafListKey (org.onosproject.yang.model.LeafListKey)4 ListKey (org.onosproject.yang.model.ListKey)4 KeyLeaf (org.onosproject.yang.model.KeyLeaf)3 NodeKey (org.onosproject.yang.model.NodeKey)3 Builder (org.onosproject.yang.model.ResourceId.Builder)2 LinkedList (java.util.LinkedList)1 LeafListKeyBuilder (org.onosproject.yang.model.LeafListKey.LeafListKeyBuilder)1 ListKeyBuilder (org.onosproject.yang.model.ListKey.ListKeyBuilder)1 ResourceId (org.onosproject.yang.model.ResourceId)1 SchemaId (org.onosproject.yang.model.SchemaId)1