blob: dfb72949ba0256b277178d67d195eaf320530abb [file] [log] [blame]
Hesam Rahimi1856ed92016-10-27 18:46:42 -04001/*
2 * Copyright 2016 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.protocol.restconf.ctl;
17
Shankara-Huaweid5823ab2016-11-22 10:14:52 +053018import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ObjectNode;
Hesam Rahimi1856ed92016-10-27 18:46:42 -040020import org.apache.commons.io.IOUtils;
21import org.onosproject.protocol.restconf.server.utils.parser.json.ParserUtils;
22import org.onosproject.yms.ych.YangCompositeEncoding;
23import org.onosproject.yms.ych.YangDataTreeCodec;
24import org.onosproject.yms.ydt.YdtBuilder;
25import org.onosproject.yms.ydt.YmsOperationType;
26import org.onosproject.yms.ymsm.YmsService;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
Shankara-Huaweid5823ab2016-11-22 10:14:52 +053030import java.io.IOException;
31import java.io.InputStream;
32
33import static org.onosproject.yms.ydt.YdtContextOperationType.NONE;
Hesam Rahimi1856ed92016-10-27 18:46:42 -040034
35
36/**
37 * JSON/YDT Codec implementation.
38 */
39public class JsonYdtCodec implements YangDataTreeCodec {
40 private static final String RESTCONF_ROOT = "restconf/data";
41
42 protected final YmsService ymsService;
43
44 private final Logger log = LoggerFactory.getLogger(getClass());
45
46 public JsonYdtCodec(YmsService service) {
47 ymsService = service;
48 }
49
50 @Override
Shankara-Huaweid5823ab2016-11-22 10:14:52 +053051 public String encodeYdtToProtocolFormat(YdtBuilder ydtBuilder) {
52 String json = ParserUtils.convertYdtToJson(ydtBuilder.getRootNode().getName(),
53 ydtBuilder.getRootNode(),
Hesam Rahimi1856ed92016-10-27 18:46:42 -040054 ymsService.getYdtWalker())
Shankara-Huaweid5823ab2016-11-22 10:14:52 +053055 .textValue();
Hesam Rahimi1856ed92016-10-27 18:46:42 -040056 return json;
Shankara-Huaweid5823ab2016-11-22 10:14:52 +053057 }
Hesam Rahimi1856ed92016-10-27 18:46:42 -040058
59 @Override
Shankara-Huaweid5823ab2016-11-22 10:14:52 +053060 public YangCompositeEncoding encodeYdtToCompositeProtocolFormat(YdtBuilder ydtBuilder) {
Hesam Rahimi1856ed92016-10-27 18:46:42 -040061 // Mainly for POST/PUT operation.
62 // YdtBuilder/YdtContext has YdtContextType NONE for URI,
63 // YdtContextType CREATE/MERGE/REPLACE for Resource data.
64
65 // TODO: Implement this method in Release Ibis for TE Tunnel.
66
67 return null;
68 }
69
70 @Override
71 public YdtBuilder decodeProtocolDataToYdt(String protocolData,
72 Object schemaRegistryForYdt,
73 YmsOperationType opType) {
74 // Get a new builder
75 YdtBuilder builder = ymsService.getYdtBuilder(RESTCONF_ROOT,
76 null,
77 opType,
78 schemaRegistryForYdt);
79 ParserUtils.convertJsonToYdt(getObjectNode(protocolData), builder);
80 return builder;
81 }
82
83 @Override
84 public YdtBuilder decodeCompositeProtocolDataToYdt(YangCompositeEncoding protocolData,
85 Object schemaRegistryForYdt,
86 YmsOperationType opType) {
87 // opType should be QUERY_REPLY
88 // Get a new builder
89 YdtBuilder builder = ymsService.getYdtBuilder(RESTCONF_ROOT,
90 null,
91 opType,
92 schemaRegistryForYdt);
93 // Convert the URI to ydtBuilder
94
95 // YdtContextOperationType should be NONE for URI in QUERY_RESPONSE.
96 ParserUtils.convertUriToYdt(protocolData.getResourceIdentifier(), builder, NONE);
97 // Set default operation type for the payload node, is this for resource data?
98 // NULL/EMPTY for Resource data
99 builder.setDefaultEditOperationType(null);
100
101 // Convert the payload json body to ydt
102 ParserUtils.convertJsonToYdt(getObjectNode(protocolData.getResourceInformation()), builder);
103 return builder;
104 }
105
106 // Returns an ObjectNode from s JSON string.
107 private ObjectNode getObjectNode(String json) {
108 InputStream stream = IOUtils.toInputStream(json);
109
110 ObjectNode rootNode;
111 ObjectMapper mapper = new ObjectMapper();
112 try {
113 rootNode = (ObjectNode) mapper.readTree(stream);
114 } catch (IOException e) {
115 log.error("Can't read stream as a JSON ObjectNode: {}", e);
116 return null;
117 }
118 return rootNode;
119 }
120}