blob: 43680da2841f65b1017e6ee918c3454b4ef6eb31 [file] [log] [blame]
sonu gupta1bb37b82016-11-11 16:51:18 +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.ydt;
18
19import org.onosproject.yangutils.datamodel.YangSchemaNode;
20import org.onosproject.yangutils.datamodel.YangSchemaNodeIdentifier;
21import org.onosproject.yangutils.datamodel.YangSchemaNodeType;
22import org.onosproject.yms.app.ydt.exceptions.YdtException;
23import org.onosproject.yms.ydt.YdtContextOperationType;
24
25import static org.onosproject.yangutils.datamodel.YangSchemaNodeType.YANG_MULTI_INSTANCE_LEAF_NODE;
26import static org.onosproject.yangutils.datamodel.YangSchemaNodeType.YANG_MULTI_INSTANCE_NODE;
27import static org.onosproject.yangutils.datamodel.YangSchemaNodeType.YANG_SINGLE_INSTANCE_LEAF_NODE;
28import static org.onosproject.yangutils.datamodel.YangSchemaNodeType.YANG_SINGLE_INSTANCE_NODE;
29import static org.onosproject.yms.app.ydt.YdtAppNodeOperationType.DELETE_ONLY;
30import static org.onosproject.yms.app.ydt.YdtAppNodeOperationType.OTHER_EDIT;
31import static org.onosproject.yms.app.ydt.YdtConstants.errorMsg;
32
33/**
34 * Represents an YANG node factory to create different types of YANG data tree
35 * node.
36 */
37final class YdtNodeFactory {
38
39 // ydt formatted error string
40 private static final String FMT_NOT_EXIST =
41 "Schema node with name %s doesn't exist.";
42
43 // No instantiation
44 private YdtNodeFactory() {
45 }
46
47 /**
48 * Returns a YANG data tree node for a given name, set of values and
49 * instance type.
50 *
51 * @param id dataNodeIdentifier of data tree node
52 * @param schemaNode data node as per YANG schema metadata
53 * @param cardinality requested cardinality of node
54 * @param callType identify the call type
55 * @return YANG data tree node
56 */
57 protected static YdtNode getNode(YangSchemaNodeIdentifier id,
58 YangSchemaNode schemaNode,
59 RequestedCardinality cardinality,
60 RequestedCallType callType) {
61
62 YdtNode newNode = null;
63 YangSchemaNodeType nodeType = schemaNode.getYangSchemaNodeType();
64
65 switch (cardinality) {
66
67 case UNKNOWN:
68 /*
69 * if requested node type is UNKNOWN, check corresponding
70 * yang data node type and create respective type node.
71 */
72 newNode = getYangSchemaNodeTypeSpecificContext(id, nodeType,
73 callType);
74 break;
75
76 /*
77 * if requested node type is specified and it exist as node of some
78 * other type in data model then throw exception
79 */
80 case SINGLE_INSTANCE:
81 validateNodeType(id, nodeType, YANG_SINGLE_INSTANCE_NODE);
82 newNode = new YdtSingleInstanceNode(id);
83 break;
84
85 case MULTI_INSTANCE:
86
87 validateNodeType(id, nodeType, YANG_MULTI_INSTANCE_NODE);
88 newNode = new YdtMultiInstanceNode(id);
89 break;
90
91 case SINGLE_INSTANCE_LEAF:
92
93 validateNodeType(id, nodeType, YANG_SINGLE_INSTANCE_LEAF_NODE);
94 newNode = new YdtSingleInstanceLeafNode(id);
95 break;
96
97 case MULTI_INSTANCE_LEAF:
98
99 validateNodeType(id, nodeType, YANG_MULTI_INSTANCE_LEAF_NODE);
100 newNode = new YdtMultiInstanceLeafNode(id);
101 break;
102
103 default:
104 throwNotExistError(id);
105 }
106
107 // set reference of yang data node in the requested node.
108 newNode.setYangSchemaNode(schemaNode);
109
110 return newNode;
111 }
112
113 /**
114 * Validates the requested ydt node type against the schema node type,
115 * if it is not equal then it will throw warning.
116 *
117 * @param id dataNodeIdentifier of data tree node
118 * @param nodeType actual node type
119 * @param requestedType user requested node type
120 */
121 private static void validateNodeType(YangSchemaNodeIdentifier id,
122 YangSchemaNodeType nodeType,
123 YangSchemaNodeType requestedType) {
124 if (nodeType != requestedType) {
125 throwNotExistError(id);
126 }
127 }
128
129 /**
130 * Creates Yang data tree node of YangSchemaNode type specific for
131 * requestedCardinality of type UNKNOWN and returns the same.
132 *
133 * @param id node identifier of data tree node
134 * @param nodeType schema node type as per YANG schema metadata
135 * @param callType identify the call type
136 * @return YANG data tree node
137 */
138 private static YdtNode getYangSchemaNodeTypeSpecificContext(
139 YangSchemaNodeIdentifier id,
140 YangSchemaNodeType nodeType,
141 RequestedCallType callType) {
142 switch (callType) {
143 case LEAF:
144 switch (nodeType) {
145
146 case YANG_SINGLE_INSTANCE_LEAF_NODE:
147 return new YdtSingleInstanceLeafNode(id);
148
149 case YANG_MULTI_INSTANCE_LEAF_NODE:
150 return new YdtMultiInstanceLeafNode(id);
151
152 default:
153 throwNotExistError(id);
154 }
155
156 case OTHER:
157 switch (nodeType) {
158
159 case YANG_SINGLE_INSTANCE_NODE:
160 return new YdtSingleInstanceNode(id);
161
162 case YANG_MULTI_INSTANCE_NODE:
163 return new YdtMultiInstanceNode(id);
164
165 default:
166 throwNotExistError(id);
167 }
168
169 case MULTI_INSTANCE:
170 switch (nodeType) {
171
172 case YANG_MULTI_INSTANCE_LEAF_NODE:
173 return new YdtMultiInstanceLeafNode(id);
174
175 case YANG_MULTI_INSTANCE_NODE:
176 return new YdtMultiInstanceNode(id);
177
178 default:
179 throwNotExistError(id);
180 }
181
182 default:
183 throwNotExistError(id);
184 }
185
186 return null;
187 }
188
189 /**
190 * Create Yang data tree node of YangSchemaNode type specific and
191 * returns the same.
192 *
193 * @param id node identifier of data tree node
194 * @param nodeType schema node type as per YANG schema metadata
195 * @return YANG data tree node
196 */
197 protected static YdtNode getYangSchemaNodeTypeSpecificContext(
198 YangSchemaNodeIdentifier id,
199 YangSchemaNodeType nodeType) {
200
201 switch (nodeType) {
202
203 case YANG_SINGLE_INSTANCE_LEAF_NODE:
204 return new YdtSingleInstanceLeafNode(id);
205
206 case YANG_MULTI_INSTANCE_LEAF_NODE:
207 return new YdtMultiInstanceLeafNode(id);
208
209 case YANG_SINGLE_INSTANCE_NODE:
210 return new YdtSingleInstanceNode(id);
211
212 case YANG_MULTI_INSTANCE_NODE:
213 return new YdtMultiInstanceNode(id);
214
215 default:
216 throwNotExistError(id);
217 }
218
219 return null;
220 }
221
222 /**
223 * Returns the app tree operation type with the help of YdtOperation type.
224 *
225 * @param opType ydt operation type
226 * @return app tree operation type
227 */
228 protected static YdtAppNodeOperationType getAppOpTypeFromYdtOpType(
229 YdtContextOperationType opType) {
230 // Get the app tree operation type.
231 switch (opType) {
232 case CREATE:
233 case MERGE:
234 case REPLACE:
235 return OTHER_EDIT;
236
237 case DELETE:
238 case REMOVE:
239 return DELETE_ONLY;
240
241 default:
242 return null;
243 //TODO handle the default data type.
244 }
245 }
246
247 /**
248 * Throws exception for requested ydt node by preparing error message with
249 * given node identifier.
250 *
251 * @param id node identifier
252 */
253 private static void throwNotExistError(YangSchemaNodeIdentifier id) {
254 throw new YdtException(errorMsg(FMT_NOT_EXIST, id.getName()));
255 }
256}