blob: 9101c97fb97fe51db200590f66f0c92af03df78c [file] [log] [blame]
sonu gupta1bb37b82016-11-11 16:51:18 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
sonu gupta1bb37b82016-11-11 16:51:18 +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.ydt;
18
19import org.onosproject.yangutils.datamodel.YangSchemaNode;
sonu gupta1bb37b82016-11-11 16:51:18 +053020import org.onosproject.yangutils.datamodel.YangSchemaNodeType;
sonu guptaeff184b2016-11-24 12:43:49 +053021import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
sonu gupta1bb37b82016-11-11 16:51:18 +053022import org.onosproject.yms.app.ydt.exceptions.YdtException;
sonu gupta1bb37b82016-11-11 16:51:18 +053023
24import static org.onosproject.yangutils.datamodel.YangSchemaNodeType.YANG_MULTI_INSTANCE_LEAF_NODE;
25import static org.onosproject.yangutils.datamodel.YangSchemaNodeType.YANG_MULTI_INSTANCE_NODE;
26import static org.onosproject.yangutils.datamodel.YangSchemaNodeType.YANG_SINGLE_INSTANCE_LEAF_NODE;
27import static org.onosproject.yangutils.datamodel.YangSchemaNodeType.YANG_SINGLE_INSTANCE_NODE;
sonu gupta1bb37b82016-11-11 16:51:18 +053028import static org.onosproject.yms.app.ydt.YdtConstants.errorMsg;
29
30/**
31 * Represents an YANG node factory to create different types of YANG data tree
32 * node.
33 */
34final class YdtNodeFactory {
35
sonu guptaeff184b2016-11-24 12:43:49 +053036 // YDT formatted error string
sonu gupta1bb37b82016-11-11 16:51:18 +053037 private static final String FMT_NOT_EXIST =
38 "Schema node with name %s doesn't exist.";
sonu guptaeff184b2016-11-24 12:43:49 +053039 //TODO need to handle later
40 private static final String E_MULTI_INS =
41 "Requested interface adds an instance of type list or " +
42 "leaf-list node only.";
sonu gupta1bb37b82016-11-11 16:51:18 +053043
44 // No instantiation
45 private YdtNodeFactory() {
46 }
47
48 /**
49 * Returns a YANG data tree node for a given name, set of values and
50 * instance type.
51 *
sonu guptaeff184b2016-11-24 12:43:49 +053052 * @param node data node as per YANG schema metadata
sonu gupta1bb37b82016-11-11 16:51:18 +053053 * @param cardinality requested cardinality of node
54 * @param callType identify the call type
55 * @return YANG data tree node
sonu guptaeff184b2016-11-24 12:43:49 +053056 * @throws YdtException when user requested node type doesn't exist
sonu gupta1bb37b82016-11-11 16:51:18 +053057 */
sonu guptaeff184b2016-11-24 12:43:49 +053058 static YdtNode getNode(
59 YangSchemaNode node, RequestedCardinality cardinality,
60 RequestedCallType callType) throws YdtException {
sonu gupta1bb37b82016-11-11 16:51:18 +053061
sonu guptaeff184b2016-11-24 12:43:49 +053062 YdtNode newNode;
63 YangSchemaNodeType type = node.getYangSchemaNodeType();
sonu gupta1bb37b82016-11-11 16:51:18 +053064
sonu guptaeff184b2016-11-24 12:43:49 +053065 try {
66 switch (cardinality) {
sonu gupta1bb37b82016-11-11 16:51:18 +053067
sonu guptaeff184b2016-11-24 12:43:49 +053068 case UNKNOWN:
69 /*
70 * if requested node type is UNKNOWN, check corresponding
71 * yang data node type and create respective type node.
72 */
73 newNode = getYangSchemaNodeTypeSpecificContext(node, type,
74 callType);
75 break;
76
sonu gupta1bb37b82016-11-11 16:51:18 +053077 /*
sonu guptaeff184b2016-11-24 12:43:49 +053078 * if requested node type is specified and it exist as node of
79 * some other type in data model then throw exception
sonu gupta1bb37b82016-11-11 16:51:18 +053080 */
sonu guptaeff184b2016-11-24 12:43:49 +053081 case SINGLE_INSTANCE:
82 validateNodeType(node, type, YANG_SINGLE_INSTANCE_NODE);
83 newNode = new YdtSingleInstanceNode(node);
84 break;
sonu gupta1bb37b82016-11-11 16:51:18 +053085
sonu guptaeff184b2016-11-24 12:43:49 +053086 case MULTI_INSTANCE:
sonu gupta1bb37b82016-11-11 16:51:18 +053087
sonu guptaeff184b2016-11-24 12:43:49 +053088 validateNodeType(node, type, YANG_MULTI_INSTANCE_NODE);
89 newNode = new YdtMultiInstanceNode(node);
90 break;
sonu gupta1bb37b82016-11-11 16:51:18 +053091
sonu guptaeff184b2016-11-24 12:43:49 +053092 case SINGLE_INSTANCE_LEAF:
sonu gupta1bb37b82016-11-11 16:51:18 +053093
sonu guptaeff184b2016-11-24 12:43:49 +053094 validateNodeType(node, type, YANG_SINGLE_INSTANCE_LEAF_NODE);
95 newNode = new YdtSingleInstanceLeafNode(node);
96 break;
sonu gupta1bb37b82016-11-11 16:51:18 +053097
sonu guptaeff184b2016-11-24 12:43:49 +053098 case MULTI_INSTANCE_LEAF:
sonu gupta1bb37b82016-11-11 16:51:18 +053099
sonu guptaeff184b2016-11-24 12:43:49 +0530100 validateNodeType(node, type, YANG_MULTI_INSTANCE_LEAF_NODE);
101 newNode = new YdtMultiInstanceLeafNode(node);
102 break;
sonu gupta1bb37b82016-11-11 16:51:18 +0530103
sonu guptaeff184b2016-11-24 12:43:49 +0530104 default:
105 newNode = null;
106 }
107 } catch (DataModelException | YdtException e) {
108 throw new YdtException(e.getLocalizedMessage());
sonu gupta1bb37b82016-11-11 16:51:18 +0530109 }
110
sonu guptaeff184b2016-11-24 12:43:49 +0530111 if (newNode == null) {
112 throw new YdtException(errorMsg(FMT_NOT_EXIST, node.getName()));
113 }
sonu gupta1bb37b82016-11-11 16:51:18 +0530114
115 return newNode;
116 }
117
118 /**
119 * Validates the requested ydt node type against the schema node type,
120 * if it is not equal then it will throw warning.
121 *
sonu guptaeff184b2016-11-24 12:43:49 +0530122 * @param node schema node
sonu gupta1bb37b82016-11-11 16:51:18 +0530123 * @param nodeType actual node type
124 * @param requestedType user requested node type
sonu guptaeff184b2016-11-24 12:43:49 +0530125 * @throws YdtException when user requested node type doesn't exist
sonu gupta1bb37b82016-11-11 16:51:18 +0530126 */
sonu guptaeff184b2016-11-24 12:43:49 +0530127 private static void validateNodeType(
128 YangSchemaNode node, YangSchemaNodeType nodeType,
129 YangSchemaNodeType requestedType) throws YdtException {
130
sonu gupta1bb37b82016-11-11 16:51:18 +0530131 if (nodeType != requestedType) {
sonu guptaeff184b2016-11-24 12:43:49 +0530132 throw new YdtException(errorMsg(FMT_NOT_EXIST, node.getName()));
sonu gupta1bb37b82016-11-11 16:51:18 +0530133 }
134 }
135
136 /**
137 * Creates Yang data tree node of YangSchemaNode type specific for
138 * requestedCardinality of type UNKNOWN and returns the same.
139 *
sonu guptaeff184b2016-11-24 12:43:49 +0530140 * @param node schema node
sonu gupta1bb37b82016-11-11 16:51:18 +0530141 * @param nodeType schema node type as per YANG schema metadata
142 * @param callType identify the call type
143 * @return YANG data tree node
sonu guptaeff184b2016-11-24 12:43:49 +0530144 * @throws YdtException when user requested node type doesn't exist
sonu gupta1bb37b82016-11-11 16:51:18 +0530145 */
146 private static YdtNode getYangSchemaNodeTypeSpecificContext(
sonu guptaeff184b2016-11-24 12:43:49 +0530147 YangSchemaNode node, YangSchemaNodeType nodeType,
148 RequestedCallType callType) throws YdtException, DataModelException {
sonu gupta1bb37b82016-11-11 16:51:18 +0530149 switch (callType) {
150 case LEAF:
151 switch (nodeType) {
152
153 case YANG_SINGLE_INSTANCE_LEAF_NODE:
sonu guptaeff184b2016-11-24 12:43:49 +0530154 return new YdtSingleInstanceLeafNode(node);
sonu gupta1bb37b82016-11-11 16:51:18 +0530155
156 case YANG_MULTI_INSTANCE_LEAF_NODE:
sonu guptaeff184b2016-11-24 12:43:49 +0530157 return new YdtMultiInstanceLeafNode(node);
sonu gupta1bb37b82016-11-11 16:51:18 +0530158
159 default:
sonu guptaeff184b2016-11-24 12:43:49 +0530160 return null;
sonu gupta1bb37b82016-11-11 16:51:18 +0530161 }
162
sonu guptaeff184b2016-11-24 12:43:49 +0530163 case NON_LEAF:
sonu gupta1bb37b82016-11-11 16:51:18 +0530164 switch (nodeType) {
165
166 case YANG_SINGLE_INSTANCE_NODE:
sonu guptaeff184b2016-11-24 12:43:49 +0530167 return new YdtSingleInstanceNode(node);
sonu gupta1bb37b82016-11-11 16:51:18 +0530168
169 case YANG_MULTI_INSTANCE_NODE:
sonu guptaeff184b2016-11-24 12:43:49 +0530170 return new YdtMultiInstanceNode(node);
sonu gupta1bb37b82016-11-11 16:51:18 +0530171
172 default:
sonu guptaeff184b2016-11-24 12:43:49 +0530173 return null;
sonu gupta1bb37b82016-11-11 16:51:18 +0530174 }
175
176 case MULTI_INSTANCE:
177 switch (nodeType) {
178
179 case YANG_MULTI_INSTANCE_LEAF_NODE:
sonu guptaeff184b2016-11-24 12:43:49 +0530180 return new YdtMultiInstanceLeafNode(node);
sonu gupta1bb37b82016-11-11 16:51:18 +0530181
182 case YANG_MULTI_INSTANCE_NODE:
sonu guptaeff184b2016-11-24 12:43:49 +0530183 return new YdtMultiInstanceNode(node);
sonu gupta1bb37b82016-11-11 16:51:18 +0530184
185 default:
sonu guptaeff184b2016-11-24 12:43:49 +0530186 throw new YdtException(E_MULTI_INS);
sonu gupta1bb37b82016-11-11 16:51:18 +0530187 }
188
Sean Condonfe6ceba2017-02-26 16:40:03 +0000189 case EMPTY_CONTAINER:
190 switch (nodeType) {
191
192 case YANG_SINGLE_INSTANCE_NODE:
193 return new YdtSingleInstanceNode(node);
194
195 case YANG_SINGLE_INSTANCE_LEAF_NODE:
196 return new YdtSingleInstanceLeafNode(node);
197
198 default:
199 return null;
200 }
201
sonu gupta1bb37b82016-11-11 16:51:18 +0530202 default:
sonu guptaeff184b2016-11-24 12:43:49 +0530203 return null;
sonu gupta1bb37b82016-11-11 16:51:18 +0530204 }
sonu gupta1bb37b82016-11-11 16:51:18 +0530205 }
206
207 /**
208 * Create Yang data tree node of YangSchemaNode type specific and
209 * returns the same.
210 *
sonu guptaeff184b2016-11-24 12:43:49 +0530211 * @param node schema node
sonu gupta1bb37b82016-11-11 16:51:18 +0530212 * @return YANG data tree node
sonu guptaeff184b2016-11-24 12:43:49 +0530213 * @throws YdtException when user requested node type doesn't exist
sonu gupta1bb37b82016-11-11 16:51:18 +0530214 */
sonu guptaeff184b2016-11-24 12:43:49 +0530215 static YdtNode getYangSchemaNodeTypeSpecificContext(YangSchemaNode node)
216 throws YdtException {
sonu gupta1bb37b82016-11-11 16:51:18 +0530217
sonu guptaeff184b2016-11-24 12:43:49 +0530218 switch (node.getYangSchemaNodeType()) {
sonu gupta1bb37b82016-11-11 16:51:18 +0530219
220 case YANG_SINGLE_INSTANCE_LEAF_NODE:
sonu guptaeff184b2016-11-24 12:43:49 +0530221 return new YdtSingleInstanceLeafNode(node);
sonu gupta1bb37b82016-11-11 16:51:18 +0530222
223 case YANG_MULTI_INSTANCE_LEAF_NODE:
sonu guptaeff184b2016-11-24 12:43:49 +0530224 return new YdtMultiInstanceLeafNode(node);
sonu gupta1bb37b82016-11-11 16:51:18 +0530225
226 case YANG_SINGLE_INSTANCE_NODE:
sonu guptaeff184b2016-11-24 12:43:49 +0530227 return new YdtSingleInstanceNode(node);
sonu gupta1bb37b82016-11-11 16:51:18 +0530228
229 case YANG_MULTI_INSTANCE_NODE:
sonu guptaeff184b2016-11-24 12:43:49 +0530230 return new YdtMultiInstanceNode(node);
sonu gupta1bb37b82016-11-11 16:51:18 +0530231
232 default:
sonu guptaeff184b2016-11-24 12:43:49 +0530233 throw new YdtException(errorMsg(FMT_NOT_EXIST, node.getName()));
sonu gupta1bb37b82016-11-11 16:51:18 +0530234 }
sonu gupta1bb37b82016-11-11 16:51:18 +0530235 }
236}