Search in sources :

Example 1 with CompositeStream

use of org.onosproject.yang.runtime.CompositeStream in project onos by opennetworkinglab.

the class YangToolUtil method toJsonCompositeStream.

/**
 * Converts CompositeData into JSON CompositeStream.
 *
 * @param input CompositeData to convert
 * @return JSON CompositeStream
 */
public static CompositeStream toJsonCompositeStream(CompositeData input) {
    initStaticContext();
    RuntimeContext yrtContext = new DefaultRuntimeContext.Builder().setDataFormat("JSON").build();
    CompositeStream xml = yrs.encode(input, yrtContext);
    return xml;
}
Also used : RuntimeContext(org.onosproject.yang.runtime.RuntimeContext) DefaultRuntimeContext(org.onosproject.yang.runtime.DefaultRuntimeContext) CompositeStream(org.onosproject.yang.runtime.CompositeStream)

Example 2 with CompositeStream

use of org.onosproject.yang.runtime.CompositeStream in project onos by opennetworkinglab.

the class RestconfUtils method convertJsonToDataNode.

/**
 * Convert URI and ObjectNode to ResourceData.
 *
 * @param uri      URI of the data resource
 * @param rootNode JSON representation of the data resource
 * @return represents type of node in data store
 */
public static ResourceData convertJsonToDataNode(URI uri, ObjectNode rootNode) {
    RuntimeContext.Builder runtimeContextBuilder = new DefaultRuntimeContext.Builder();
    runtimeContextBuilder.setDataFormat(JSON_FORMAT);
    RuntimeContext context = runtimeContextBuilder.build();
    ResourceData resourceData = null;
    InputStream jsonData = null;
    try {
        if (rootNode != null) {
            jsonData = convertObjectNodeToInputStream(rootNode);
        }
        String uriString = getRawUriPath(uri);
        CompositeStream compositeStream = new DefaultCompositeStream(uriString, jsonData);
        // CompositeStream --- YangRuntimeService ---> CompositeData.
        CompositeData compositeData = YANG_RUNTIME.decode(compositeStream, context);
        resourceData = compositeData.resourceData();
    } catch (RestconfException ex) {
        throw ex;
    } catch (Exception ex) {
        log.error("convertJsonToDataNode failure: {}", ex.getMessage(), ex);
        log.info("Failed JSON: \n{}", rootNode);
        log.debug("convertJsonToDataNode failure", ex);
        throw new RestconfException("ERROR: JSON cannot be converted to DataNode", ex, RestconfError.ErrorTag.OPERATION_FAILED, INTERNAL_SERVER_ERROR, Optional.of(uri.getPath()));
    }
    if (resourceData == null) {
        throw new RestconfException("ERROR: JSON cannot be converted to DataNode", RestconfError.ErrorTag.DATA_MISSING, CONFLICT, Optional.of(uri.getPath()), Optional.empty());
    }
    return resourceData;
}
Also used : ResourceData(org.onosproject.yang.model.ResourceData) DefaultResourceData(org.onosproject.yang.model.DefaultResourceData) InputStream(java.io.InputStream) RestconfException(org.onosproject.restconf.api.RestconfException) UriBuilder(javax.ws.rs.core.UriBuilder) DefaultCompositeStream(org.onosproject.yang.runtime.DefaultCompositeStream) CompositeData(org.onosproject.yang.runtime.CompositeData) DefaultCompositeData(org.onosproject.yang.runtime.DefaultCompositeData) RuntimeContext(org.onosproject.yang.runtime.RuntimeContext) DefaultRuntimeContext(org.onosproject.yang.runtime.DefaultRuntimeContext) DefaultCompositeStream(org.onosproject.yang.runtime.DefaultCompositeStream) CompositeStream(org.onosproject.yang.runtime.CompositeStream) IOException(java.io.IOException) RestconfException(org.onosproject.restconf.api.RestconfException)

Example 3 with CompositeStream

use of org.onosproject.yang.runtime.CompositeStream in project onos by opennetworkinglab.

the class YangToolUtil method toXmlCompositeStream.

/**
 * Converts CompositeData into XML CompositeStream.
 *
 * @param input CompositeData to convert
 * @return XML CompositeStream
 */
public static CompositeStream toXmlCompositeStream(CompositeData input) {
    initStaticContext();
    RuntimeContext yrtContext = new DefaultRuntimeContext.Builder().setDataFormat("xml").build();
    CompositeStream xml = yrs.encode(input, yrtContext);
    return xml;
}
Also used : RuntimeContext(org.onosproject.yang.runtime.RuntimeContext) DefaultRuntimeContext(org.onosproject.yang.runtime.DefaultRuntimeContext) CompositeStream(org.onosproject.yang.runtime.CompositeStream)

Example 4 with CompositeStream

use of org.onosproject.yang.runtime.CompositeStream 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)

Aggregations

CompositeStream (org.onosproject.yang.runtime.CompositeStream)4 DefaultRuntimeContext (org.onosproject.yang.runtime.DefaultRuntimeContext)4 RuntimeContext (org.onosproject.yang.runtime.RuntimeContext)4 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 RestconfException (org.onosproject.restconf.api.RestconfException)2 DefaultResourceData (org.onosproject.yang.model.DefaultResourceData)2 ResourceData (org.onosproject.yang.model.ResourceData)2 CompositeData (org.onosproject.yang.runtime.CompositeData)2 DefaultCompositeData (org.onosproject.yang.runtime.DefaultCompositeData)2 DefaultCompositeStream (org.onosproject.yang.runtime.DefaultCompositeStream)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 UriBuilder (javax.ws.rs.core.UriBuilder)1