Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Foundation |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.yms.app.ych; |
| 18 | |
Vidyashree Rama | fd339c4 | 2016-12-01 19:19:56 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.datamodel.YangSchemaNode; |
| 20 | import org.onosproject.yangutils.datamodel.YangSchemaNodeContextInfo; |
| 21 | import org.onosproject.yangutils.datamodel.YangSchemaNodeIdentifier; |
| 22 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 23 | import org.onosproject.yms.app.ych.defaultcodecs.YangCodecRegistry; |
| 24 | import org.onosproject.yms.app.ydt.YdtExtendedBuilder; |
| 25 | import org.onosproject.yms.app.ydt.YdtExtendedContext; |
| 26 | import org.onosproject.yms.app.yob.DefaultYobBuilder; |
| 27 | import org.onosproject.yms.app.ysr.YangSchemaRegistry; |
| 28 | import org.onosproject.yms.app.ytb.DefaultYangTreeBuilder; |
| 29 | import org.onosproject.yms.app.ytb.YtbException; |
| 30 | import org.onosproject.yms.ych.YangCodecHandler; |
| 31 | import org.onosproject.yms.ych.YangCompositeEncoding; |
| 32 | import org.onosproject.yms.ych.YangDataTreeCodec; |
| 33 | import org.onosproject.yms.ych.YangProtocolEncodingFormat; |
| 34 | import org.onosproject.yms.ydt.YdtBuilder; |
| 35 | import org.onosproject.yms.ydt.YdtContext; |
| 36 | import org.onosproject.yms.ydt.YmsOperationType; |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 37 | import org.onosproject.yms.ysr.YangModuleLibrary; |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 38 | |
| 39 | import java.util.ArrayList; |
| 40 | import java.util.HashMap; |
| 41 | import java.util.List; |
| 42 | import java.util.Map; |
| 43 | |
Vidyashree Rama | fd339c4 | 2016-12-01 19:19:56 +0530 | [diff] [blame] | 44 | import static org.onosproject.yms.app.yob.YobUtils.createAndSetInEventInstance; |
| 45 | import static org.onosproject.yms.app.yob.YobUtils.createAndSetInEventSubjectInstance; |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Represents implementation of YANG SBI broker interfaces. |
| 49 | * YCH acts as a broker between YMS and driver/provider. |
| 50 | */ |
| 51 | public class DefaultYangCodecHandler implements YangCodecHandler { |
| 52 | |
| 53 | private static final String E_MODULE_LIST = "The input module or " + |
| 54 | "sub-module object list cannot be null."; |
| 55 | private static final String E_DATA_TREE_CODEC = "data tree codec handler" + |
| 56 | " is null."; |
Vidyashree Rama | fd339c4 | 2016-12-01 19:19:56 +0530 | [diff] [blame] | 57 | private static final String E_DATA_MODEL_CHILD = "Unable to find the " + |
| 58 | "child node"; |
| 59 | private static final String E_NOTIFICATION_NODE = "Notification node " + |
| 60 | "should be the first child of module in YDT"; |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * Schema registry for driver. |
| 64 | */ |
| 65 | private final YangSchemaRegistry schemaRegistry; |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 66 | private YangModuleLibrary library; |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Default codecs. |
| 70 | */ |
| 71 | private final Map<YangProtocolEncodingFormat, YangDataTreeCodec> |
| 72 | defaultCodecs = new HashMap<>(); |
| 73 | |
| 74 | /** |
| 75 | * Override codec handler. |
| 76 | */ |
| 77 | private final Map<YangProtocolEncodingFormat, YangDataTreeCodec> |
| 78 | overrideCodecs = new HashMap<>(); |
| 79 | |
| 80 | /** |
| 81 | * Creates a new YANG codec handler. |
| 82 | * |
| 83 | * @param registry YANG schema registry |
| 84 | */ |
| 85 | public DefaultYangCodecHandler(YangSchemaRegistry registry) { |
| 86 | schemaRegistry = registry; |
| 87 | |
| 88 | // update the default codecs from codec registry |
| 89 | Map<YangProtocolEncodingFormat, YangDataTreeCodec> recvCodec = |
| 90 | YangCodecRegistry.getDefaultCodecs(); |
| 91 | if (!recvCodec.isEmpty()) { |
| 92 | for (Map.Entry<YangProtocolEncodingFormat, YangDataTreeCodec> |
| 93 | codecEntry : recvCodec.entrySet()) { |
| 94 | defaultCodecs.put(codecEntry.getKey(), codecEntry.getValue()); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | private YangDataTreeCodec getAppropriateCodec( |
| 100 | YangProtocolEncodingFormat dataFormat) { |
| 101 | YangDataTreeCodec codec = defaultCodecs.get(dataFormat); |
| 102 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 103 | int size = overrideCodecs.size(); |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 104 | // Check over ridden codec handler is exist or not. |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 105 | if (size != 0) { |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 106 | YangDataTreeCodec overrideCodec = overrideCodecs.get(dataFormat); |
| 107 | if (overrideCodec != null) { |
| 108 | codec = overrideCodec; |
| 109 | } |
| 110 | } |
| 111 | return codec; |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public void addDeviceSchema(Class<?> yangModule) { |
Gaurav Agrawal | fcc6c19 | 2016-09-20 14:29:15 +0530 | [diff] [blame] | 116 | schemaRegistry.registerApplication(null, yangModule); |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 117 | schemaRegistry.processModuleLibrary(yangModule.getName(), library); |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public String encodeOperation(String rootName, |
| 122 | String rootNamespace, |
| 123 | Map<String, String> tagAttrMap, |
| 124 | List<Object> moduleList, |
| 125 | YangProtocolEncodingFormat dataFormat, |
| 126 | YmsOperationType opType) { |
| 127 | |
| 128 | if (moduleList == null || moduleList.isEmpty()) { |
| 129 | throw new YchException(E_MODULE_LIST); |
| 130 | } |
| 131 | |
| 132 | // Get the default codec handler. |
| 133 | YangDataTreeCodec codec = getAppropriateCodec(dataFormat); |
| 134 | if (codec == null) { |
| 135 | throw new YchException(E_DATA_TREE_CODEC); |
| 136 | } |
| 137 | |
| 138 | // Get yang data tree from YTB for the received objects. |
| 139 | DefaultYangTreeBuilder builder = new DefaultYangTreeBuilder(); |
| 140 | YdtExtendedBuilder encodedYdt = |
| 141 | builder.getYdtBuilderForYo(moduleList, rootName, |
| 142 | rootNamespace, opType, |
| 143 | schemaRegistry); |
| 144 | |
| 145 | encodedYdt.setRootTagAttributeMap(tagAttrMap); |
| 146 | |
| 147 | // Get the xml string form codec handler. |
| 148 | return codec.encodeYdtToProtocolFormat(encodedYdt); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public YangCompositeEncoding encodeCompositeOperation( |
| 153 | String rootName, |
| 154 | String rootNamespace, |
| 155 | Object moduleObject, |
| 156 | YangProtocolEncodingFormat dataFormat, |
| 157 | YmsOperationType opType) { |
| 158 | |
| 159 | if (moduleObject == null) { |
| 160 | throw new YtbException(E_MODULE_LIST); |
| 161 | } |
| 162 | |
| 163 | // Get the default codec handler. |
| 164 | YangDataTreeCodec codec = getAppropriateCodec(dataFormat); |
| 165 | if (codec == null) { |
| 166 | throw new YchException(E_DATA_TREE_CODEC); |
| 167 | } |
| 168 | |
| 169 | List<Object> yangModuleList = new ArrayList<>(); |
| 170 | yangModuleList.add(moduleObject); |
| 171 | |
| 172 | // Get yang data tree from YTB for the received objects. |
| 173 | DefaultYangTreeBuilder builder = new DefaultYangTreeBuilder(); |
| 174 | YdtExtendedBuilder extBuilder = |
| 175 | builder.getYdtBuilderForYo(yangModuleList, |
| 176 | rootName, |
| 177 | rootNamespace, |
| 178 | opType, |
| 179 | schemaRegistry); |
| 180 | |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 181 | // Get the composite response from codec handler. |
| 182 | return codec.encodeYdtToCompositeProtocolFormat(extBuilder); |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public List<Object> decode(String inputString, |
| 187 | YangProtocolEncodingFormat dataFormat, |
| 188 | YmsOperationType opType) { |
| 189 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 190 | YdtBuilder ydtBuilder; |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 191 | YangDataTreeCodec codec = getAppropriateCodec(dataFormat); |
| 192 | if (codec == null) { |
| 193 | throw new YchException(E_DATA_TREE_CODEC); |
| 194 | } |
| 195 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 196 | try { |
| 197 | // Get the YANG data tree |
| 198 | ydtBuilder = codec.decodeProtocolDataToYdt(inputString, |
| 199 | schemaRegistry, |
| 200 | opType); |
| 201 | } catch (Exception e) { |
| 202 | throw new YchException(e.getLocalizedMessage()); |
| 203 | } |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 204 | |
| 205 | if (ydtBuilder != null) { |
| 206 | return getObjectList(ydtBuilder.getRootNode()); |
| 207 | } |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 208 | return null; |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public Object decode(YangCompositeEncoding protoData, |
| 213 | YangProtocolEncodingFormat dataFormat, |
| 214 | YmsOperationType opType) { |
| 215 | |
| 216 | YangDataTreeCodec codec = getAppropriateCodec(dataFormat); |
| 217 | if (codec == null) { |
| 218 | throw new YchException(E_DATA_TREE_CODEC); |
| 219 | } |
| 220 | |
| 221 | YdtBuilder ydtBuilder = |
| 222 | codec.decodeCompositeProtocolDataToYdt(protoData, |
| 223 | schemaRegistry, |
| 224 | opType); |
| 225 | |
nitinanand | ca2f369 | 2017-04-10 10:21:13 +0530 | [diff] [blame] | 226 | if (ydtBuilder == null) { |
| 227 | return null; |
| 228 | } |
| 229 | |
Vidyashree Rama | fd339c4 | 2016-12-01 19:19:56 +0530 | [diff] [blame] | 230 | YdtExtendedContext rootNode = ((YdtExtendedContext) ydtBuilder |
| 231 | .getRootNode()); |
| 232 | |
| 233 | if (opType == YmsOperationType.NOTIFICATION) { |
| 234 | return getNotificationObject(((YdtExtendedContext) rootNode |
| 235 | .getFirstChild())); |
| 236 | } |
| 237 | |
nitinanand | ca2f369 | 2017-04-10 10:21:13 +0530 | [diff] [blame] | 238 | // Return the module object by using YANG data tree |
| 239 | return getObjectList(rootNode); |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 240 | } |
| 241 | |
Vidyashree Rama | fd339c4 | 2016-12-01 19:19:56 +0530 | [diff] [blame] | 242 | //returns notification event object |
| 243 | private Object getNotificationObject(YdtExtendedContext rootNode) { |
| 244 | YangSchemaNode module = rootNode.getYangSchemaNode(); |
| 245 | YangSchemaNode childSchema = ((YdtExtendedContext) rootNode |
| 246 | .getFirstChild()).getYangSchemaNode(); |
| 247 | |
| 248 | YangSchemaNodeIdentifier id = new YangSchemaNodeIdentifier(); |
| 249 | id.setNameSpace(childSchema.getNameSpace()); |
| 250 | id.setName(childSchema.getName()); |
| 251 | |
| 252 | YangSchemaNodeContextInfo contextInfo; |
| 253 | try { |
| 254 | contextInfo = module.getChildSchema(id); |
| 255 | } catch (DataModelException e) { |
| 256 | throw new YchException(E_DATA_MODEL_CHILD); |
| 257 | } |
| 258 | |
| 259 | if (contextInfo == null) { |
| 260 | throw new YchException(E_NOTIFICATION_NODE); |
| 261 | } |
| 262 | |
| 263 | DefaultYobBuilder builder = new DefaultYobBuilder(); |
| 264 | Object object = builder.getYangObject(((YdtExtendedContext) rootNode |
| 265 | .getFirstChild()), |
| 266 | schemaRegistry); |
| 267 | |
| 268 | Object eventSubObj = createAndSetInEventSubjectInstance(object, |
| 269 | rootNode, |
| 270 | schemaRegistry); |
| 271 | return createAndSetInEventInstance(eventSubObj, rootNode, |
| 272 | schemaRegistry); |
| 273 | } |
| 274 | |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 275 | @Override |
| 276 | public void registerOverriddenCodec(YangDataTreeCodec overrideCodec, |
| 277 | YangProtocolEncodingFormat dataFormat) { |
| 278 | overrideCodecs.put(dataFormat, overrideCodec); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Returns the list of objects from YDT data tree. |
| 283 | * |
| 284 | * @param rootNode YDT root node |
| 285 | * @return returns list of objects |
| 286 | */ |
| 287 | private List<Object> getObjectList(YdtContext rootNode) { |
| 288 | |
| 289 | if (rootNode == null) { |
| 290 | // TODO |
| 291 | return null; |
| 292 | } |
| 293 | |
| 294 | if (rootNode.getFirstChild() == null) { |
| 295 | // TODO |
| 296 | return null; |
| 297 | } |
| 298 | |
| 299 | YdtContext curNode = rootNode.getFirstChild(); |
| 300 | DefaultYobBuilder builder = new DefaultYobBuilder(); |
| 301 | Object object = builder.getYangObject((YdtExtendedContext) curNode, |
| 302 | schemaRegistry); |
| 303 | List<Object> objectList = new ArrayList<>(); |
| 304 | objectList.add(object); |
| 305 | |
| 306 | // Check next module is exit or not. If exist get the object for that. |
| 307 | while (curNode.getNextSibling() != null) { |
| 308 | curNode = curNode.getNextSibling(); |
| 309 | object = builder.getYangObject((YdtExtendedContext) curNode, |
| 310 | schemaRegistry); |
| 311 | objectList.add(object); |
| 312 | } |
| 313 | |
| 314 | return objectList; |
| 315 | } |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 316 | |
| 317 | /** |
| 318 | * Returns module library for YSR. |
| 319 | * |
| 320 | * @return module library for YSR |
| 321 | */ |
| 322 | public YangModuleLibrary getLibrary() { |
| 323 | return library; |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Sets module library for YSR. |
| 328 | * |
| 329 | * @param library module library for YSR |
| 330 | */ |
| 331 | public void setLibrary(YangModuleLibrary library) { |
| 332 | this.library = library; |
| 333 | } |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 334 | } |