blob: 6fda6cd0ce79122c490d52b9f4502f016de72050 [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;
Henry Yu52a8a722016-12-19 14:50:50 -050022import org.onosproject.provider.te.utils.YangCompositeEncodingImpl;
Hesam Rahimi1856ed92016-10-27 18:46:42 -040023import org.onosproject.yms.ych.YangCompositeEncoding;
24import org.onosproject.yms.ych.YangDataTreeCodec;
Henry Yu52a8a722016-12-19 14:50:50 -050025import org.onosproject.yms.ych.YangResourceIdentifierType;
Hesam Rahimi1856ed92016-10-27 18:46:42 -040026import org.onosproject.yms.ydt.YdtBuilder;
Henry Yu52a8a722016-12-19 14:50:50 -050027import org.onosproject.yms.ydt.YdtContext;
Hesam Rahimi1856ed92016-10-27 18:46:42 -040028import org.onosproject.yms.ydt.YmsOperationType;
29import org.onosproject.yms.ymsm.YmsService;
30import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
Shankara-Huaweid5823ab2016-11-22 10:14:52 +053033import java.io.IOException;
34import java.io.InputStream;
35
Henry Yu52a8a722016-12-19 14:50:50 -050036import static org.onosproject.protocol.restconf.server.utils.parser.json.ParserUtils.convertYdtToJson;
37import static org.onosproject.protocol.restconf.server.utils.parser.json.ParserUtils.findTopNodeInCompositeYdt;
38import static org.onosproject.protocol.restconf.server.utils.parser.json.ParserUtils.getJsonNameFromYdtNode;
39import static org.onosproject.protocol.restconf.server.utils.parser.json.ParserUtils.getUriInCompositeYdt;
Shankara-Huaweid5823ab2016-11-22 10:14:52 +053040import static org.onosproject.yms.ydt.YdtContextOperationType.NONE;
Hesam Rahimi1856ed92016-10-27 18:46:42 -040041
42
43/**
44 * JSON/YDT Codec implementation.
45 */
46public class JsonYdtCodec implements YangDataTreeCodec {
47 private static final String RESTCONF_ROOT = "restconf/data";
Henry Yu52a8a722016-12-19 14:50:50 -050048 private static final String EMPTY_JSON_OBJECT = "{}";
Hesam Rahimi1856ed92016-10-27 18:46:42 -040049
50 protected final YmsService ymsService;
51
52 private final Logger log = LoggerFactory.getLogger(getClass());
53
54 public JsonYdtCodec(YmsService service) {
55 ymsService = service;
56 }
57
58 @Override
Henry Yu52a8a722016-12-19 14:50:50 -050059 public String encodeYdtToProtocolFormat(YdtBuilder builder) {
60 return convertYdtToJson(getJsonNameFromYdtNode(builder.getRootNode()),
61 builder.getRootNode(),
62 ymsService.getYdtWalker()).textValue();
Shankara-Huaweid5823ab2016-11-22 10:14:52 +053063 }
Hesam Rahimi1856ed92016-10-27 18:46:42 -040064
65 @Override
Henry Yu52a8a722016-12-19 14:50:50 -050066 public YangCompositeEncoding encodeYdtToCompositeProtocolFormat(YdtBuilder builder) {
67 String uriString = getUriInCompositeYdt(builder);
68 YdtContext topNode = findTopNodeInCompositeYdt(builder);
69 if (topNode != null) {
70 ObjectNode objectNode = convertYdtToJson(getJsonNameFromYdtNode(topNode),
71 topNode,
72 ymsService.getYdtWalker());
73 return new YangCompositeEncodingImpl(YangResourceIdentifierType.URI,
74 uriString,
75 objectNode.toString());
76 }
Hesam Rahimi1856ed92016-10-27 18:46:42 -040077
Henry Yu52a8a722016-12-19 14:50:50 -050078 return new YangCompositeEncodingImpl(YangResourceIdentifierType.URI,
79 uriString,
80 EMPTY_JSON_OBJECT);
Hesam Rahimi1856ed92016-10-27 18:46:42 -040081 }
82
83 @Override
84 public YdtBuilder decodeProtocolDataToYdt(String protocolData,
85 Object schemaRegistryForYdt,
86 YmsOperationType opType) {
87 // Get a new builder
88 YdtBuilder builder = ymsService.getYdtBuilder(RESTCONF_ROOT,
89 null,
90 opType,
91 schemaRegistryForYdt);
92 ParserUtils.convertJsonToYdt(getObjectNode(protocolData), builder);
93 return builder;
94 }
95
96 @Override
97 public YdtBuilder decodeCompositeProtocolDataToYdt(YangCompositeEncoding protocolData,
98 Object schemaRegistryForYdt,
99 YmsOperationType opType) {
100 // opType should be QUERY_REPLY
101 // Get a new builder
102 YdtBuilder builder = ymsService.getYdtBuilder(RESTCONF_ROOT,
103 null,
104 opType,
105 schemaRegistryForYdt);
106 // Convert the URI to ydtBuilder
107
108 // YdtContextOperationType should be NONE for URI in QUERY_RESPONSE.
109 ParserUtils.convertUriToYdt(protocolData.getResourceIdentifier(), builder, NONE);
110 // Set default operation type for the payload node, is this for resource data?
111 // NULL/EMPTY for Resource data
112 builder.setDefaultEditOperationType(null);
113
114 // Convert the payload json body to ydt
115 ParserUtils.convertJsonToYdt(getObjectNode(protocolData.getResourceInformation()), builder);
116 return builder;
117 }
118
119 // Returns an ObjectNode from s JSON string.
120 private ObjectNode getObjectNode(String json) {
121 InputStream stream = IOUtils.toInputStream(json);
122
123 ObjectNode rootNode;
124 ObjectMapper mapper = new ObjectMapper();
125 try {
126 rootNode = (ObjectNode) mapper.readTree(stream);
127 } catch (IOException e) {
128 log.error("Can't read stream as a JSON ObjectNode: {}", e);
129 return null;
130 }
131 return rootNode;
132 }
133}