blob: 1cb17f5a220618dd844e54ba8f18ab9c0bd36056 [file] [log] [blame]
Yixiao Chen5ece00f2016-09-14 16:23:24 -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.provider.te.topology;
17
18import static org.onosproject.yms.ydt.YdtContextOperationType.NONE;
19
20import java.io.IOException;
21import java.io.InputStream;
22
23import org.apache.commons.io.IOUtils;
24import org.onosproject.protocol.restconf.server.utils.parser.json.ParserUtils;
25import org.onosproject.yms.ych.YangCompositeEncoding;
26import org.onosproject.yms.ych.YangDataTreeCodec;
27import org.onosproject.yms.ydt.YdtBuilder;
28import org.onosproject.yms.ydt.YmsOperationType;
29import org.onosproject.yms.ymsm.YmsService;
30import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
33import com.fasterxml.jackson.databind.ObjectMapper;
34import com.fasterxml.jackson.databind.node.ObjectNode;
35
36
37/**
38 * JSON/YDT Codec implementation.
39 */
40public class JsonYdtCodec implements YangDataTreeCodec {
41 private static final String RESTCONF_ROOT = "restconf/data";
42
43 protected final YmsService ymsService;
44
45 private final Logger log = LoggerFactory.getLogger(getClass());
46
47 public JsonYdtCodec(YmsService service) {
48 ymsService = service;
49 }
50
51 @Override
52 public String encodeYdtToProtocolFormat(YdtBuilder builder,
53 YmsOperationType opType) {
54 String json = ParserUtils.convertYdtToJson(builder.getRootNode().getName(),
55 builder.getRootNode(),
56 ymsService.getYdtWalker())
57 .textValue();
58 return json;
59 }
60
61 @Override
62 public YangCompositeEncoding encodeYdtToCompositeProtocolFormat(YdtBuilder builder,
63 YmsOperationType opType) {
64 // Mainly for POST/PUT operation.
65 // YdtBuilder/YdtContext has YdtContextType NONE for URI,
66 // YdtContextType CREATE/MERGE/REPLACE for Resource data.
67
68 // TODO: Implement this method in Release Ibis for TE Tunnel.
69
70 return null;
71 }
72
73 @Override
74 public YdtBuilder decodeProtocolDataToYdt(String protocolData,
75 Object schemaRegistryForYdt,
76 YmsOperationType opType) {
77 // Get a new builder
78 YdtBuilder builder = ymsService.getYdtBuilder(RESTCONF_ROOT,
79 null,
80 opType,
81 schemaRegistryForYdt);
82 ParserUtils.convertJsonToYdt(getObjectNode(protocolData), builder);
83 return builder;
84 }
85
86 @Override
87 public YdtBuilder decodeCompositeProtocolDataToYdt(YangCompositeEncoding protocolData,
88 Object schemaRegistryForYdt,
89 YmsOperationType opType) {
90 // opType should be QUERY_REPLY
91 // Get a new builder
92 YdtBuilder builder = ymsService.getYdtBuilder(RESTCONF_ROOT,
93 null,
94 opType,
95 schemaRegistryForYdt);
96 // Convert the URI to ydtBuilder
97
98 // YdtContextOperationType should be NONE for URI in QUERY_RESPONSE.
99 ParserUtils.convertUriToYdt(protocolData.getResourceIdentifier(), builder, NONE);
100 // Set default operation type for the payload node, is this for resource data?
101 // NULL/EMPTY for Resource data
102 builder.setDefaultEditOperationType(null);
103
104 // Convert the payload json body to ydt
105 ParserUtils.convertJsonToYdt(getObjectNode(protocolData.getResourceInformation()), builder);
106 return builder;
107 }
108
109 // Returns an ObjectNode from s JSON string.
110 private ObjectNode getObjectNode(String json) {
111 InputStream stream = IOUtils.toInputStream(json);
112
113 ObjectNode rootNode;
114 ObjectMapper mapper = new ObjectMapper();
115 try {
116 rootNode = (ObjectNode) mapper.readTree(stream);
117 } catch (IOException e) {
118 log.error("Can't read stream as a JSON ObjectNode: {}", e);
119 return null;
120 }
121 return rootNode;
122 }
123
124}