janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | |
| 17 | package org.onosproject.yms.app.ytb; |
| 18 | |
| 19 | import org.onosproject.yms.app.ydt.YangRequestWorkBench; |
| 20 | import org.onosproject.yms.app.ydt.YdtExtendedBuilder; |
| 21 | import org.onosproject.yms.app.ydt.YdtExtendedContext; |
| 22 | import org.onosproject.yms.app.ysr.YangSchemaRegistry; |
| 23 | import org.onosproject.yms.ydt.YdtContext; |
| 24 | import org.onosproject.yms.ydt.YmsOperationType; |
| 25 | |
| 26 | import java.util.List; |
| 27 | |
| 28 | import static org.onosproject.yms.app.ytb.YtbUtil.emptyObjErrMsg; |
| 29 | import static org.onosproject.yms.ydt.YmsOperationType.NOTIFICATION; |
| 30 | import static org.onosproject.yms.ydt.YmsOperationType.RPC_REPLY; |
| 31 | |
| 32 | /** |
| 33 | * Representation of YANG tree builder which generates YANG data tree from the |
| 34 | * class objects which are provided from the applications and return it to the |
| 35 | * protocol(s). |
| 36 | */ |
| 37 | public class DefaultYangTreeBuilder implements YangTreeBuilder { |
| 38 | |
| 39 | private static final String OBJ_LIST = "object list"; |
| 40 | private static final String EVENT_OBJ = "event object"; |
| 41 | private static final String OUTPUT_OBJ = "output object"; |
| 42 | |
| 43 | /** |
| 44 | * Creates the YANG tree builder. |
| 45 | */ |
| 46 | public DefaultYangTreeBuilder() { |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public YdtExtendedBuilder getYdtBuilderForYo( |
| 51 | List<Object> moduleObj, String rootName, |
| 52 | String rootNameSpace, YmsOperationType opType, |
| 53 | YangSchemaRegistry registry) { |
| 54 | |
| 55 | if (moduleObj == null || moduleObj.isEmpty()) { |
| 56 | throw new YtbException(emptyObjErrMsg(OBJ_LIST)); |
| 57 | } |
| 58 | |
| 59 | YdtExtendedBuilder ydtBuilder = new YangRequestWorkBench( |
| 60 | rootName, rootNameSpace, opType, registry, false); |
| 61 | |
| 62 | for (Object yangObj : moduleObj) { |
| 63 | YdtBuilderFromYo moduleBuilder = new YdtBuilderFromYo( |
| 64 | ydtBuilder, yangObj, registry); |
| 65 | |
| 66 | moduleBuilder.getModuleNodeFromYsr(yangObj); |
| 67 | moduleBuilder.createYdtFromRootObject(); |
| 68 | } |
| 69 | return ydtBuilder; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public YdtContext getYdtForNotification(Object object, String rootName, |
| 74 | YangSchemaRegistry registry) { |
| 75 | |
| 76 | if (object == null) { |
| 77 | throw new YtbException(emptyObjErrMsg(EVENT_OBJ)); |
| 78 | } |
| 79 | |
| 80 | YdtExtendedBuilder extBuilder = new YangRequestWorkBench( |
| 81 | rootName, null, NOTIFICATION, registry, false); |
| 82 | YdtBuilderFromYo moduleBuilder = new YdtBuilderFromYo( |
| 83 | extBuilder, object, registry); |
| 84 | |
| 85 | moduleBuilder.getRootNodeWithNotificationFromYsr(object); |
| 86 | /* |
| 87 | * Adds module to YDT, so that notification can further enhance the |
| 88 | * tree. |
| 89 | */ |
| 90 | moduleBuilder.createModuleInYdt(); |
| 91 | moduleBuilder.createYdtFromRootObject(); |
| 92 | return extBuilder.getRootNode(); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public YdtExtendedBuilder getYdtForRpcResponse( |
| 97 | Object outputObj, YangRequestWorkBench workBench) { |
| 98 | |
| 99 | if (outputObj == null) { |
| 100 | throw new YtbException(emptyObjErrMsg(OUTPUT_OBJ)); |
| 101 | } |
| 102 | |
| 103 | // Gets the logical root node from RPC request work bench. |
| 104 | YdtExtendedContext rootNode = workBench.getRootNode(); |
| 105 | |
| 106 | /* |
| 107 | * Creates a new work bench for RPC reply from the contents of the |
| 108 | * request work bench |
| 109 | */ |
| 110 | YdtExtendedBuilder ydtBuilder = new YangRequestWorkBench( |
| 111 | rootNode.getName(), rootNode.getNamespace(), |
| 112 | RPC_REPLY, workBench.getYangSchemaRegistry(), false); |
| 113 | YdtBuilderFromYo moduleBuilder = new YdtBuilderFromYo( |
| 114 | ydtBuilder, outputObj, |
| 115 | workBench.getYangSchemaRegistry()); |
| 116 | |
| 117 | // Forms YDT till RPC, so that output can further enhance the tree. |
| 118 | moduleBuilder.createModuleAndRpcInYdt(rootNode); |
| 119 | moduleBuilder.createYdtFromRootObject(); |
| 120 | return ydtBuilder; |
| 121 | } |
| 122 | } |