blob: 9ea03f0594ebb80b09f96882f5a7e4b2c7dc5ddf [file] [log] [blame]
janani b05614f12016-10-04 12:55:43 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
janani b05614f12016-10-04 12:55:43 +05303 *
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
17package org.onosproject.yms.app.ytb;
18
19import org.onosproject.yms.app.ydt.YangRequestWorkBench;
20import org.onosproject.yms.app.ydt.YdtExtendedBuilder;
21import org.onosproject.yms.app.ydt.YdtExtendedContext;
22import org.onosproject.yms.app.ysr.YangSchemaRegistry;
janani b05614f12016-10-04 12:55:43 +053023import org.onosproject.yms.ydt.YmsOperationType;
24
25import java.util.List;
26
27import static org.onosproject.yms.app.ytb.YtbUtil.emptyObjErrMsg;
28import static org.onosproject.yms.ydt.YmsOperationType.NOTIFICATION;
29import 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 */
36public 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 b9069eb42016-11-24 17:50:08 +053049 public YdtExtendedBuilder getYdtBuilderForYo(List<Object> moduleObj, String rootName,
50 String rootNameSpace, YmsOperationType opType,
51 YangSchemaRegistry registry) {
janani b05614f12016-10-04 12:55:43 +053052
janani b9069eb42016-11-24 17:50:08 +053053 if (moduleObj == null) {
janani b05614f12016-10-04 12:55:43 +053054 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 b9069eb42016-11-24 17:50:08 +053071 public YdtExtendedContext getYdtForNotification(Object object, String rootName,
72 YangSchemaRegistry registry) {
janani b05614f12016-10-04 12:55:43 +053073
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 b9069eb42016-11-24 17:50:08 +053094 public YdtExtendedBuilder getYdtForRpcResponse(Object outputObj,
95 YdtExtendedBuilder reqBuilder) {
janani b05614f12016-10-04 12:55:43 +053096
97 if (outputObj == null) {
98 throw new YtbException(emptyObjErrMsg(OUTPUT_OBJ));
99 }
100
janani b9069eb42016-11-24 17:50:08 +0530101 YangRequestWorkBench workBench = (YangRequestWorkBench) reqBuilder;
102
janani b05614f12016-10-04 12:55:43 +0530103 // 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 b9069eb42016-11-24 17:50:08 +0530110 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 b05614f12016-10-04 12:55:43 +0530117
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}