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; |
| 22 | import org.onosproject.yms.ych.YangCompositeEncoding; |
| 23 | import org.onosproject.yms.ych.YangDataTreeCodec; |
| 24 | import org.onosproject.yms.ydt.YdtBuilder; |
| 25 | import org.onosproject.yms.ydt.YmsOperationType; |
| 26 | import org.onosproject.yms.ymsm.YmsService; |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame^] | 30 | import java.io.IOException; |
| 31 | import java.io.InputStream; |
| 32 | |
| 33 | import static org.onosproject.yms.ydt.YdtContextOperationType.NONE; |
Hesam Rahimi | 1856ed9 | 2016-10-27 18:46:42 -0400 | [diff] [blame] | 34 | |
| 35 | |
| 36 | /** |
| 37 | * JSON/YDT Codec implementation. |
| 38 | */ |
| 39 | public 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-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame^] | 51 | public String encodeYdtToProtocolFormat(YdtBuilder ydtBuilder) { |
| 52 | String json = ParserUtils.convertYdtToJson(ydtBuilder.getRootNode().getName(), |
| 53 | ydtBuilder.getRootNode(), |
Hesam Rahimi | 1856ed9 | 2016-10-27 18:46:42 -0400 | [diff] [blame] | 54 | ymsService.getYdtWalker()) |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame^] | 55 | .textValue(); |
Hesam Rahimi | 1856ed9 | 2016-10-27 18:46:42 -0400 | [diff] [blame] | 56 | return json; |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame^] | 57 | } |
Hesam Rahimi | 1856ed9 | 2016-10-27 18:46:42 -0400 | [diff] [blame] | 58 | |
| 59 | @Override |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame^] | 60 | public YangCompositeEncoding encodeYdtToCompositeProtocolFormat(YdtBuilder ydtBuilder) { |
Hesam Rahimi | 1856ed9 | 2016-10-27 18:46:42 -0400 | [diff] [blame] | 61 | // 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 | } |