Hesam Rahimi | 1856ed9 | 2016-10-27 18:46:42 -0400 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.protocol.restconf.ctl; |
| 17 | |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Hesam Rahimi | 1856ed9 | 2016-10-27 18:46:42 -0400 | [diff] [blame] | 20 | import org.apache.commons.io.IOUtils; |
| 21 | import org.onosproject.protocol.restconf.server.utils.parser.json.ParserUtils; |
Henry Yu | 52a8a72 | 2016-12-19 14:50:50 -0500 | [diff] [blame^] | 22 | import org.onosproject.provider.te.utils.YangCompositeEncodingImpl; |
Hesam Rahimi | 1856ed9 | 2016-10-27 18:46:42 -0400 | [diff] [blame] | 23 | import org.onosproject.yms.ych.YangCompositeEncoding; |
| 24 | import org.onosproject.yms.ych.YangDataTreeCodec; |
Henry Yu | 52a8a72 | 2016-12-19 14:50:50 -0500 | [diff] [blame^] | 25 | import org.onosproject.yms.ych.YangResourceIdentifierType; |
Hesam Rahimi | 1856ed9 | 2016-10-27 18:46:42 -0400 | [diff] [blame] | 26 | import org.onosproject.yms.ydt.YdtBuilder; |
Henry Yu | 52a8a72 | 2016-12-19 14:50:50 -0500 | [diff] [blame^] | 27 | import org.onosproject.yms.ydt.YdtContext; |
Hesam Rahimi | 1856ed9 | 2016-10-27 18:46:42 -0400 | [diff] [blame] | 28 | import org.onosproject.yms.ydt.YmsOperationType; |
| 29 | import org.onosproject.yms.ymsm.YmsService; |
| 30 | import org.slf4j.Logger; |
| 31 | import org.slf4j.LoggerFactory; |
| 32 | |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 33 | import java.io.IOException; |
| 34 | import java.io.InputStream; |
| 35 | |
Henry Yu | 52a8a72 | 2016-12-19 14:50:50 -0500 | [diff] [blame^] | 36 | import static org.onosproject.protocol.restconf.server.utils.parser.json.ParserUtils.convertYdtToJson; |
| 37 | import static org.onosproject.protocol.restconf.server.utils.parser.json.ParserUtils.findTopNodeInCompositeYdt; |
| 38 | import static org.onosproject.protocol.restconf.server.utils.parser.json.ParserUtils.getJsonNameFromYdtNode; |
| 39 | import static org.onosproject.protocol.restconf.server.utils.parser.json.ParserUtils.getUriInCompositeYdt; |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 40 | import static org.onosproject.yms.ydt.YdtContextOperationType.NONE; |
Hesam Rahimi | 1856ed9 | 2016-10-27 18:46:42 -0400 | [diff] [blame] | 41 | |
| 42 | |
| 43 | /** |
| 44 | * JSON/YDT Codec implementation. |
| 45 | */ |
| 46 | public class JsonYdtCodec implements YangDataTreeCodec { |
| 47 | private static final String RESTCONF_ROOT = "restconf/data"; |
Henry Yu | 52a8a72 | 2016-12-19 14:50:50 -0500 | [diff] [blame^] | 48 | private static final String EMPTY_JSON_OBJECT = "{}"; |
Hesam Rahimi | 1856ed9 | 2016-10-27 18:46:42 -0400 | [diff] [blame] | 49 | |
| 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 Yu | 52a8a72 | 2016-12-19 14:50:50 -0500 | [diff] [blame^] | 59 | public String encodeYdtToProtocolFormat(YdtBuilder builder) { |
| 60 | return convertYdtToJson(getJsonNameFromYdtNode(builder.getRootNode()), |
| 61 | builder.getRootNode(), |
| 62 | ymsService.getYdtWalker()).textValue(); |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 63 | } |
Hesam Rahimi | 1856ed9 | 2016-10-27 18:46:42 -0400 | [diff] [blame] | 64 | |
| 65 | @Override |
Henry Yu | 52a8a72 | 2016-12-19 14:50:50 -0500 | [diff] [blame^] | 66 | 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 Rahimi | 1856ed9 | 2016-10-27 18:46:42 -0400 | [diff] [blame] | 77 | |
Henry Yu | 52a8a72 | 2016-12-19 14:50:50 -0500 | [diff] [blame^] | 78 | return new YangCompositeEncodingImpl(YangResourceIdentifierType.URI, |
| 79 | uriString, |
| 80 | EMPTY_JSON_OBJECT); |
Hesam Rahimi | 1856ed9 | 2016-10-27 18:46:42 -0400 | [diff] [blame] | 81 | } |
| 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 | } |