blob: 6f8df8eb0c9213696d9e8d3b30fcb6c04528eb09 [file] [log] [blame]
janani b05614f12016-10-04 12:55:43 +05301/*
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
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;
23import org.onosproject.yms.ydt.YdtContext;
24import org.onosproject.yms.ydt.YmsOperationType;
25
26import java.util.List;
27
28import static org.onosproject.yms.app.ytb.YtbUtil.emptyObjErrMsg;
29import static org.onosproject.yms.ydt.YmsOperationType.NOTIFICATION;
30import 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 */
37public 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}