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