Search in sources :

Example 11 with ResourceData

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

the class YangToolUtil method toDataNode.

/**
 * Converts ModelObject into a DataNode.
 *
 * @param input ModelOject
 * @return DataNode
 */
public static DataNode toDataNode(ModelObject input) {
    // FIXME this converter will work with root-level nodes only.
    initStaticContext();
    ModelObjectData modelData = DefaultModelObjectData.builder().addModelObject(input).identifier(null).build();
    ResourceData rnode = converter.createDataNode(modelData);
    if (rnode.dataNodes().isEmpty()) {
        log.error("input did not result in any datanode. {}", input);
        return null;
    }
    return rnode.dataNodes().get(0);
}
Also used : ResourceData(org.onosproject.yang.model.ResourceData) DefaultResourceData(org.onosproject.yang.model.DefaultResourceData) DefaultModelObjectData(org.onosproject.yang.model.DefaultModelObjectData) ModelObjectData(org.onosproject.yang.model.ModelObjectData)

Example 12 with ResourceData

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

the class RestconfUtils method convertDataNodeToJson.

/**
 * Convert Resource Id and Data Node to Json ObjectNode.
 *
 * @param rid      resource identifier
 * @param dataNode represents type of node in data store
 * @return JSON representation of the data resource
 */
public static ObjectNode convertDataNodeToJson(ResourceId rid, DataNode dataNode) {
    RuntimeContext.Builder runtimeContextBuilder = DefaultRuntimeContext.builder();
    runtimeContextBuilder.setDataFormat(JSON_FORMAT);
    RuntimeContext context = runtimeContextBuilder.build();
    DefaultResourceData.Builder resourceDataBuilder = DefaultResourceData.builder();
    resourceDataBuilder.addDataNode(dataNode);
    resourceDataBuilder.resourceId(rid);
    ResourceData resourceData = resourceDataBuilder.build();
    DefaultCompositeData.Builder compositeDataBuilder = DefaultCompositeData.builder();
    compositeDataBuilder.resourceData(resourceData);
    CompositeData compositeData = compositeDataBuilder.build();
    ObjectNode rootNode = null;
    try {
        // CompositeData --- YangRuntimeService ---> CompositeStream.
        CompositeStream compositeStream = YANG_RUNTIME.encode(compositeData, context);
        InputStream inputStream = compositeStream.resourceData();
        rootNode = convertInputStreamToObjectNode(inputStream);
    } catch (Exception ex) {
        log.error("convertInputStreamToObjectNode failure: {}", ex.getMessage());
        log.debug("convertInputStreamToObjectNode failure", ex);
    }
    if (rootNode == null) {
        throw new RestconfException("ERROR: InputStream can not be convert to ObjectNode", null, RestconfError.ErrorTag.DATA_MISSING, CONFLICT, Optional.empty());
    }
    return rootNode;
}
Also used : ResourceData(org.onosproject.yang.model.ResourceData) DefaultResourceData(org.onosproject.yang.model.DefaultResourceData) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) InputStream(java.io.InputStream) CompositeData(org.onosproject.yang.runtime.CompositeData) DefaultCompositeData(org.onosproject.yang.runtime.DefaultCompositeData) DefaultCompositeStream(org.onosproject.yang.runtime.DefaultCompositeStream) CompositeStream(org.onosproject.yang.runtime.CompositeStream) IOException(java.io.IOException) RestconfException(org.onosproject.restconf.api.RestconfException) DefaultResourceData(org.onosproject.yang.model.DefaultResourceData) DefaultCompositeData(org.onosproject.yang.runtime.DefaultCompositeData) RestconfException(org.onosproject.restconf.api.RestconfException) RuntimeContext(org.onosproject.yang.runtime.RuntimeContext) DefaultRuntimeContext(org.onosproject.yang.runtime.DefaultRuntimeContext)

Example 13 with ResourceData

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

the class RestconfManager method runPutOperationOnDataResource.

@Override
public void runPutOperationOnDataResource(URI uri, ObjectNode rootNode) throws RestconfException {
    DataResourceLocator rl = DataResourceLocator.newInstance(uri);
    ResourceData receivedData = convertJsonToDataNode(rmLastPathSegment(rl.uriForYangRuntime()), rootNode);
    List<DataNode> dataNodeList = receivedData.dataNodes();
    if (dataNodeList == null || dataNodeList.isEmpty()) {
        log.warn("There is no one Data Node can be proceed.");
        return;
    }
    if (dataNodeList.size() > 1) {
        log.warn("There are more than one Data Node can be proceed: {}", dataNodeList.size());
    }
    DataNode dataNode = dataNodeList.get(0);
    try {
        /*
             * If the data node already exists, then replace it.
             * Otherwise, create it.
             */
        if (dynamicConfigService.nodeExist(rl.ridForDynConfig())) {
            dynamicConfigService.replaceNode(parentOf(rl.ridForDynConfig()), dataNode);
        } else {
            dynamicConfigService.createNode(parentOf(rl.ridForDynConfig()), dataNode);
        }
    } catch (FailedException e) {
        log.error("ERROR: DynamicConfigService: ", e);
        throw new RestconfException("ERROR: DynamicConfigService", e, RestconfError.ErrorTag.OPERATION_FAILED, INTERNAL_SERVER_ERROR, Optional.of(uri.getPath()));
    }
}
Also used : ResourceData(org.onosproject.yang.model.ResourceData) DefaultResourceData(org.onosproject.yang.model.DefaultResourceData) DataNode(org.onosproject.yang.model.DataNode) RestconfUtils.convertJsonToDataNode(org.onosproject.restconf.utils.RestconfUtils.convertJsonToDataNode) FailedException(org.onosproject.config.FailedException) RestconfException(org.onosproject.restconf.api.RestconfException)

Example 14 with ResourceData

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

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

the class RestconfManager method runPatchOperationOnDataResource.

@Override
public void runPatchOperationOnDataResource(URI uri, ObjectNode rootNode) throws RestconfException {
    DataResourceLocator rl = DataResourceLocator.newInstance(uri);
    ResourceData receivedData = convertJsonToDataNode(rmLastPathSegment(rl.uriForYangRuntime()), rootNode);
    ResourceId rid = receivedData.resourceId();
    List<DataNode> dataNodeList = receivedData.dataNodes();
    if (dataNodeList == null || dataNodeList.isEmpty()) {
        log.warn("There is no one Data Node can be proceed.");
        return;
    }
    if (dataNodeList.size() > 1) {
        log.warn("There are more than one Data Node can be proceed: {}", dataNodeList.size());
    }
    DataNode dataNode = dataNodeList.get(0);
    if (rid == null) {
        rid = ResourceId.builder().addBranchPointSchema("/", null).build();
        dataNode = removeTopNode(dataNode);
    }
    try {
        dynamicConfigService.updateNode(parentOf(rl.ridForDynConfig()), dataNode);
    } catch (FailedException e) {
        log.error("ERROR: DynamicConfigService: ", e);
        throw new RestconfException("ERROR: DynamicConfigService", e, RestconfError.ErrorTag.OPERATION_FAILED, INTERNAL_SERVER_ERROR, Optional.of(uri.getPath()));
    }
}
Also used : ResourceData(org.onosproject.yang.model.ResourceData) DefaultResourceData(org.onosproject.yang.model.DefaultResourceData) ResourceId(org.onosproject.yang.model.ResourceId) DataNode(org.onosproject.yang.model.DataNode) RestconfUtils.convertJsonToDataNode(org.onosproject.restconf.utils.RestconfUtils.convertJsonToDataNode) FailedException(org.onosproject.config.FailedException) RestconfException(org.onosproject.restconf.api.RestconfException)

Aggregations

ResourceData (org.onosproject.yang.model.ResourceData)16 DefaultResourceData (org.onosproject.yang.model.DefaultResourceData)15 DataNode (org.onosproject.yang.model.DataNode)7 RestconfException (org.onosproject.restconf.api.RestconfException)6 RestconfUtils.convertJsonToDataNode (org.onosproject.restconf.utils.RestconfUtils.convertJsonToDataNode)5 DefaultModelObjectData (org.onosproject.yang.model.DefaultModelObjectData)5 ModelObjectData (org.onosproject.yang.model.ModelObjectData)5 FailedException (org.onosproject.config.FailedException)4 ResourceId (org.onosproject.yang.model.ResourceId)4 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 CompositeData (org.onosproject.yang.runtime.CompositeData)2 CompositeStream (org.onosproject.yang.runtime.CompositeStream)2 DefaultCompositeData (org.onosproject.yang.runtime.DefaultCompositeData)2 DefaultCompositeStream (org.onosproject.yang.runtime.DefaultCompositeStream)2 DefaultRuntimeContext (org.onosproject.yang.runtime.DefaultRuntimeContext)2 RuntimeContext (org.onosproject.yang.runtime.RuntimeContext)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 UriBuilder (javax.ws.rs.core.UriBuilder)1 YangToolUtil.toCharSequence (org.onosproject.odtn.utils.YangToolUtil.toCharSequence)1