blob: eff2f28a3e69fff2b5b7c02a630218f6cd3737ef [file] [log] [blame]
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +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.yob;
18
19import org.onosproject.yangutils.datamodel.RpcNotificationContainer;
Vidyashree Rama6160be12016-11-24 13:43:31 +053020import org.onosproject.yangutils.datamodel.YangDerivedInfo;
21import org.onosproject.yangutils.datamodel.YangIdentity;
22import org.onosproject.yangutils.datamodel.YangIdentityRef;
Vidyashree Ramaf463dc52016-10-06 12:46:33 +053023import org.onosproject.yangutils.datamodel.YangLeaf;
Vidyashree Rama6160be12016-11-24 13:43:31 +053024import org.onosproject.yangutils.datamodel.YangLeafList;
Vidyashree Ramaf463dc52016-10-06 12:46:33 +053025import org.onosproject.yangutils.datamodel.YangLeafRef;
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053026import org.onosproject.yangutils.datamodel.YangNode;
27import org.onosproject.yangutils.datamodel.YangSchemaNode;
28import org.onosproject.yangutils.datamodel.YangSchemaNodeContextInfo;
29import org.onosproject.yangutils.datamodel.YangType;
Vidyashree Rama6160be12016-11-24 13:43:31 +053030import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053031import org.onosproject.yms.app.ydt.YdtExtendedContext;
32import org.onosproject.yms.app.yob.exception.YobException;
33import org.onosproject.yms.app.ysr.YangSchemaRegistry;
34import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
36
37import java.lang.reflect.Constructor;
Vidyashree Ramafd339c42016-12-01 19:19:56 +053038import java.lang.reflect.Field;
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053039import java.lang.reflect.InvocationTargetException;
40import java.lang.reflect.Method;
41import java.math.BigDecimal;
42import java.math.BigInteger;
Vidyashree Ramaf463dc52016-10-06 12:46:33 +053043import java.util.Base64;
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053044
45import static org.onosproject.yangutils.datamodel.YangSchemaNodeType.YANG_AUGMENT_NODE;
Vidyashree Ramafd339c42016-12-01 19:19:56 +053046import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getEnumJavaAttribute;
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053047import static org.onosproject.yms.app.ydt.AppType.YOB;
48import static org.onosproject.yms.app.yob.YobConstants.DEFAULT;
Vidyashree Ramafd339c42016-12-01 19:19:56 +053049import static org.onosproject.yms.app.yob.YobConstants.EVENT;
50import static org.onosproject.yms.app.yob.YobConstants.EVENT_SUBJECT;
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053051import static org.onosproject.yms.app.yob.YobConstants.E_DATA_TYPE_NOT_SUPPORT;
Vidyashree Ramafd339c42016-12-01 19:19:56 +053052import static org.onosproject.yms.app.yob.YobConstants.E_FAIL_TO_CREATE_OBJ;
53import static org.onosproject.yms.app.yob.YobConstants.E_FAIL_TO_GET_FIELD;
54import static org.onosproject.yms.app.yob.YobConstants.E_FAIL_TO_GET_METHOD;
55import static org.onosproject.yms.app.yob.YobConstants.E_FAIL_TO_INVOKE_METHOD;
Vidyashree Rama6160be12016-11-24 13:43:31 +053056import static org.onosproject.yms.app.yob.YobConstants.E_FAIL_TO_LOAD_CLASS;
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053057import static org.onosproject.yms.app.yob.YobConstants.E_FAIL_TO_LOAD_CONSTRUCTOR;
58import static org.onosproject.yms.app.yob.YobConstants.E_INVALID_DATA_TREE;
Vidyashree Rama6160be12016-11-24 13:43:31 +053059import static org.onosproject.yms.app.yob.YobConstants.E_INVALID_EMPTY_DATA;
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053060import static org.onosproject.yms.app.yob.YobConstants.FROM_STRING;
Vidyashree Rama6160be12016-11-24 13:43:31 +053061import static org.onosproject.yms.app.yob.YobConstants.LEAF_IDENTIFIER;
Vidyashree Ramafd339c42016-12-01 19:19:56 +053062import static org.onosproject.yms.app.yob.YobConstants.L_FAIL_TO_GET_FIELD;
63import static org.onosproject.yms.app.yob.YobConstants.L_FAIL_TO_GET_METHOD;
64import static org.onosproject.yms.app.yob.YobConstants.L_FAIL_TO_INVOKE_METHOD;
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053065import static org.onosproject.yms.app.yob.YobConstants.L_FAIL_TO_LOAD_CLASS;
66import static org.onosproject.yms.app.yob.YobConstants.OF;
67import static org.onosproject.yms.app.yob.YobConstants.OP_PARAM;
68import static org.onosproject.yms.app.yob.YobConstants.PERIOD;
Vidyashree Rama6160be12016-11-24 13:43:31 +053069import static org.onosproject.yms.app.yob.YobConstants.SELECT_LEAF;
Vidyashree Ramafd339c42016-12-01 19:19:56 +053070import static org.onosproject.yms.app.yob.YobConstants.TYPE;
Vidyashree Rama6160be12016-11-24 13:43:31 +053071import static org.onosproject.yms.app.yob.YobConstants.VALUE_OF;
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053072
73/**
74 * Utils to support object creation.
75 */
Vidyashree Ramafd339c42016-12-01 19:19:56 +053076public final class YobUtils {
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053077
78 private static final Logger log = LoggerFactory.getLogger(YobUtils.class);
79
80 // no instantiation
81 private YobUtils() {
82 }
83
84 /**
85 * Sets data from string value in parent method.
86 *
87 * @param type refers to YANG type
88 * @param leafValue leafValue argument is used to set the value
89 * in method
90 * @param parentSetterMethod Invokes the underlying method represented
91 * by this parentSetterMethod
92 * @param parentBuilderObject the parentBuilderObject is to invoke the
93 * underlying method
94 * @param ydtExtendedContext ydtExtendedContext is used to get
95 * application related
96 * information maintained in YDT
97 * @throws InvocationTargetException if failed to invoke method
98 * @throws IllegalAccessException if member cannot be accessed
99 * @throws NoSuchMethodException if method is not found
100 */
Vidyashree Rama6160be12016-11-24 13:43:31 +0530101 static void setDataFromStringValue(YangDataTypes type, String leafValue,
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530102 Method parentSetterMethod,
103 Object parentBuilderObject,
104 YdtExtendedContext ydtExtendedContext)
105 throws InvocationTargetException, IllegalAccessException,
106 NoSuchMethodException {
Vidyashree Rama6160be12016-11-24 13:43:31 +0530107 switch (type) {
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530108 case INT8:
109 parentSetterMethod.invoke(parentBuilderObject,
110 Byte.parseByte(leafValue));
111 break;
112
113 case UINT8:
114 case INT16:
115 parentSetterMethod.invoke(parentBuilderObject,
116 Short.parseShort(leafValue));
117 break;
118
119 case UINT16:
120 case INT32:
121 parentSetterMethod.invoke(parentBuilderObject,
122 Integer.parseInt(leafValue));
123 break;
124
125 case UINT32:
126 case INT64:
127 parentSetterMethod.invoke(parentBuilderObject,
128 Long.parseLong(leafValue));
129 break;
130
131 case UINT64:
132 parentSetterMethod.invoke(parentBuilderObject,
133 new BigInteger(leafValue));
134 break;
135
136 case EMPTY:
Jon Halla3fcf672017-03-28 16:53:22 -0700137 if (leafValue == null || "".equals(leafValue)) {
Vidyashree Rama6160be12016-11-24 13:43:31 +0530138 parentSetterMethod.invoke(parentBuilderObject, true);
139 } else {
140 log.info(E_INVALID_EMPTY_DATA);
141 }
142 break;
143
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530144 case BOOLEAN:
145 parentSetterMethod.invoke(parentBuilderObject,
146 Boolean.parseBoolean(leafValue));
147 break;
148
149 case STRING:
150 parentSetterMethod.invoke(parentBuilderObject, leafValue);
151 break;
152
153 case BINARY:
Vidyashree Ramaf463dc52016-10-06 12:46:33 +0530154 byte[] value = Base64.getDecoder().decode(leafValue);
155 parentSetterMethod.invoke(parentBuilderObject, value);
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530156 break;
157
158 case BITS:
Vidyashree Rama6160be12016-11-24 13:43:31 +0530159 parseBitSetTypeInfo(ydtExtendedContext, parentSetterMethod,
160 parentBuilderObject, leafValue);
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530161 break;
162
163 case DECIMAL64:
164 parentSetterMethod.invoke(parentBuilderObject,
165 new BigDecimal(leafValue));
166 break;
167
168 case DERIVED:
169 parseDerivedTypeInfo(ydtExtendedContext, parentSetterMethod,
170 parentBuilderObject, leafValue, false);
171 break;
172
Vidyashree Rama6160be12016-11-24 13:43:31 +0530173 case IDENTITYREF:
174 parseIdentityRefInfo(ydtExtendedContext, parentSetterMethod,
175 parentBuilderObject, leafValue);
176 break;
177
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530178 case UNION:
Vidyashree Ramaf463dc52016-10-06 12:46:33 +0530179 parseDerivedTypeInfo(ydtExtendedContext, parentSetterMethod,
180 parentBuilderObject, leafValue, false);
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530181 break;
182
183 case LEAFREF:
Vidyashree Ramaf463dc52016-10-06 12:46:33 +0530184 parseLeafRefTypeInfo(ydtExtendedContext, parentSetterMethod,
185 parentBuilderObject, leafValue);
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530186 break;
187
188 case ENUMERATION:
189 parseDerivedTypeInfo(ydtExtendedContext, parentSetterMethod,
190 parentBuilderObject, leafValue, true);
191 break;
192
193 default:
194 log.error(E_DATA_TYPE_NOT_SUPPORT);
195 }
196 }
197
198 /**
Vidyashree Rama6160be12016-11-24 13:43:31 +0530199 * Sets the select leaf flag for leaf.
200 *
201 * @param builderClass builder in which the select leaf flag needs to be
202 * set
203 * @param leafNode YANG data tree leaf node
204 * @param schemaRegistry YANG schema registry
205 * @param builderObject the parent build object on which to invoke
206 * the method
207 * @throws InvocationTargetException if method could not be invoked
208 * @throws IllegalAccessException if method could not be accessed
209 * @throws NoSuchMethodException if method does not exist
210 */
211 static void setSelectLeaf(Class builderClass,
212 YdtExtendedContext leafNode,
213 YangSchemaRegistry schemaRegistry,
214 Object builderObject) throws NoSuchMethodException,
215 InvocationTargetException, IllegalAccessException {
216
217 YangSchemaNode parentSchema = ((YdtExtendedContext) leafNode
218 .getParent()).getYangSchemaNode();
219 while (parentSchema.getReferredSchema() != null) {
220 parentSchema = parentSchema.getReferredSchema();
221 }
222
223 while (((YangNode) parentSchema).getParent() != null) {
224 parentSchema = ((YangNode) parentSchema).getParent();
225 }
226
227 String qualName = getQualifiedinterface(parentSchema);
228 Class<?> regClass = schemaRegistry.getRegisteredClass(parentSchema);
229 if (regClass == null) {
230 throw new YobException(E_FAIL_TO_LOAD_CLASS + qualName);
231 }
232
233 Class<?> interfaceClass = null;
234 try {
235 interfaceClass = regClass.getClassLoader().loadClass(qualName);
236 } catch (ClassNotFoundException e) {
237 log.info(E_FAIL_TO_LOAD_CLASS, qualName);
rohitsharan0dee44c2017-04-17 18:42:41 +0530238 return;
Vidyashree Rama6160be12016-11-24 13:43:31 +0530239 }
240
241 Class<?>[] innerClasses = interfaceClass.getClasses();
242 for (Class<?> innerEnumClass : innerClasses) {
243 if (innerEnumClass.getSimpleName().equals(LEAF_IDENTIFIER)) {
244 Method valueOfMethod = innerEnumClass
245 .getDeclaredMethod(VALUE_OF, String.class);
246 String leafName = leafNode.getYangSchemaNode()
247 .getJavaAttributeName().toUpperCase();
248 Object obj = valueOfMethod.invoke(null, leafName);
249 Method selectLeafMethod = builderClass
250 .getDeclaredMethod(SELECT_LEAF, innerEnumClass);
251 selectLeafMethod.invoke(builderObject, obj);
252 break;
253 }
254 }
255 }
256
257 /**
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530258 * To set data into parent setter method from string value for derived type.
259 *
Vidyashree Rama6160be12016-11-24 13:43:31 +0530260 * @param leafValue value to be set in method
261 * @param parentSetterMethod the parent setter method to be invoked
262 * @param parentBuilderObject the parent build object on which to invoke the
263 * method
264 * @param ydtExtendedContext application context
265 * @param isEnum flag to check whether type is enum or derived
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530266 * @throws InvocationTargetException if failed to invoke method
267 * @throws IllegalAccessException if member cannot be accessed
268 * @throws NoSuchMethodException if the required method is not found
269 */
270 private static void parseDerivedTypeInfo(YdtExtendedContext ydtExtendedContext,
271 Method parentSetterMethod,
272 Object parentBuilderObject,
273 String leafValue, boolean isEnum)
274 throws InvocationTargetException, IllegalAccessException,
275 NoSuchMethodException {
276 Class<?> childSetClass = null;
277 Constructor<?> childConstructor = null;
278 Object childValue = null;
279 Object childObject = null;
280 Method childMethod = null;
281
282 YangSchemaNode yangJavaModule = ydtExtendedContext.getYangSchemaNode();
Vidyashree Rama6160be12016-11-24 13:43:31 +0530283 while (yangJavaModule.getReferredSchema() != null) {
284 yangJavaModule = yangJavaModule.getReferredSchema();
285 }
286
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530287 String qualifiedClassName = yangJavaModule.getJavaPackage() + PERIOD +
288 getCapitalCase(yangJavaModule.getJavaClassNameOrBuiltInType());
289 ClassLoader classLoader = getClassLoader(null, qualifiedClassName,
290 ydtExtendedContext, null);
291 try {
292 childSetClass = classLoader.loadClass(qualifiedClassName);
293 } catch (ClassNotFoundException e) {
294 log.error(L_FAIL_TO_LOAD_CLASS, qualifiedClassName);
295 }
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530296
Vidyashree Rama6160be12016-11-24 13:43:31 +0530297 if (!isEnum) {
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530298 if (childSetClass != null) {
299 childConstructor = childSetClass.getDeclaredConstructor();
300 }
301
302 if (childConstructor != null) {
303 childConstructor.setAccessible(true);
304 }
Vidyashree Rama6160be12016-11-24 13:43:31 +0530305
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530306 try {
307 if (childConstructor != null) {
308 childObject = childConstructor.newInstance();
309 }
310 } catch (InstantiationException e) {
311 log.error(E_FAIL_TO_LOAD_CONSTRUCTOR, qualifiedClassName);
312 }
313 if (childSetClass != null) {
314 childMethod = childSetClass
315 .getDeclaredMethod(FROM_STRING, String.class);
316 }
317 } else {
318 if (childSetClass != null) {
319 childMethod = childSetClass.getDeclaredMethod(OF, String.class);
320 }
Vidyashree Rama6160be12016-11-24 13:43:31 +0530321 }
322 if (childMethod != null) {
323 childValue = childMethod.invoke(childObject, leafValue);
324 }
325
326 parentSetterMethod.invoke(parentBuilderObject, childValue);
327 }
328
329 /**
330 * To set data into parent setter method from string value for bits type.
331 *
332 * @param leafValue value to be set in method
333 * @param parentSetterMethod the parent setter method to be invoked
334 * @param parentBuilderObject the parent build object on which to invoke the
335 * method
336 * @param ydtExtendedContext application context
337 * @throws InvocationTargetException if failed to invoke method
338 * @throws IllegalAccessException if member cannot be accessed
339 * @throws NoSuchMethodException if the required method is not found
340 */
341 private static void parseBitSetTypeInfo(YdtExtendedContext ydtExtendedContext,
342 Method parentSetterMethod,
343 Object parentBuilderObject,
344 String leafValue)
345 throws InvocationTargetException, IllegalAccessException,
346 NoSuchMethodException {
347 Class<?> childSetClass = null;
348 Object childValue = null;
349 Object childObject = null;
350 Method childMethod = null;
351
352 YangSchemaNode schemaNode = ydtExtendedContext.getYangSchemaNode();
353 while (schemaNode.getReferredSchema() != null) {
354 schemaNode = schemaNode.getReferredSchema();
355 }
356
357 YangSchemaNode parentSchema = ((YdtExtendedContext) ydtExtendedContext
358 .getParent()).getYangSchemaNode();
359 String qualifiedClassName = parentSchema.getJavaPackage() + PERIOD +
360 parentSchema.getJavaAttributeName().toLowerCase() +
361 PERIOD + getCapitalCase(schemaNode.getJavaAttributeName());
362
363 ClassLoader classLoader = getClassLoader(null, qualifiedClassName,
364 ydtExtendedContext, null);
365
366 try {
367 childSetClass = classLoader.loadClass(qualifiedClassName);
368 } catch (ClassNotFoundException e) {
369 log.error(L_FAIL_TO_LOAD_CLASS, qualifiedClassName);
370 }
371
372 if (childSetClass != null) {
373 childMethod = childSetClass.getDeclaredMethod(FROM_STRING, String.class);
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530374 }
375 if (childMethod != null) {
376 childValue = childMethod.invoke(childObject, leafValue);
377 }
378
379 parentSetterMethod.invoke(parentBuilderObject, childValue);
380 }
381
382 /**
Vidyashree Ramaf463dc52016-10-06 12:46:33 +0530383 * To set data into parent setter method from string value for leafref type.
384 *
385 * @param leafValue leaf value to be set
386 * @param parentSetterMethod the parent setter method to be invoked
387 * @param parentBuilderObject the parent build object on which to invoke
388 * the method
389 * @param ydtExtendedContext application context
390 * @throws InvocationTargetException if method could not be invoked
391 * @throws IllegalAccessException if method could not be accessed
392 * @throws NoSuchMethodException if method does not exist
393 */
394 private static void parseLeafRefTypeInfo(YdtExtendedContext ydtExtendedContext,
395 Method parentSetterMethod,
396 Object parentBuilderObject,
397 String leafValue)
398 throws InvocationTargetException, IllegalAccessException,
399 NoSuchMethodException {
Vidyashree Rama6160be12016-11-24 13:43:31 +0530400
Vidyashree Ramaf463dc52016-10-06 12:46:33 +0530401 YangSchemaNode schemaNode = ydtExtendedContext.getYangSchemaNode();
Vidyashree Rama6160be12016-11-24 13:43:31 +0530402 while (schemaNode.getReferredSchema() != null) {
403 schemaNode = schemaNode.getReferredSchema();
404 }
405
406 YangLeafRef leafRef;
407 if (schemaNode instanceof YangLeaf) {
408 leafRef = (YangLeafRef) ((YangLeaf) schemaNode)
409 .getDataType().getDataTypeExtendedInfo();
410 } else {
411 leafRef = (YangLeafRef) ((YangLeafList) schemaNode)
412 .getDataType().getDataTypeExtendedInfo();
413 }
414
415 YangType type = leafRef.getEffectiveDataType();
416 if (type.getDataType() == YangDataTypes.DERIVED &&
417 schemaNode.getJavaPackage().equals(YobConstants.JAVA_LANG)) {
418 /*
419 * If leaf is inside grouping, then its return type will be of type
420 * Object and if its actual type is derived type then get the
421 * effective built-in type and set the value.
422 */
423 YangDerivedInfo derivedInfo = (YangDerivedInfo) leafRef
424 .getEffectiveDataType()
425 .getDataTypeExtendedInfo();
426 YobUtils.setDataFromStringValue(derivedInfo.getEffectiveBuiltInType(),
427 leafValue, parentSetterMethod,
428 parentBuilderObject,
429 ydtExtendedContext);
430 } else {
431 YobUtils.setDataFromStringValue(type.getDataType(),
432 leafValue, parentSetterMethod,
433 parentBuilderObject,
434 ydtExtendedContext);
435 }
Vidyashree Ramaf463dc52016-10-06 12:46:33 +0530436 }
437
438 /**
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530439 * Updates class loader for all the classes.
440 *
441 * @param registry YANG schema registry
442 * @param qualifiedClassName qualified class name
443 * @param curNode YDT context
444 * @param rootNode application root node
445 * @return current class loader
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530446 */
447 static ClassLoader getClassLoader(YangSchemaRegistry registry,
448 String qualifiedClassName,
449 YdtExtendedContext curNode,
450 YdtExtendedContext rootNode) {
Vidyashree Rama6160be12016-11-24 13:43:31 +0530451
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530452 if (rootNode != null && curNode == rootNode) {
453 YangSchemaNode curSchemaNode = curNode.getYangSchemaNode();
454 while (!(curSchemaNode instanceof RpcNotificationContainer)) {
455 curNode = (YdtExtendedContext) curNode.getParent();
456 if (curNode == null) {
457 throw new YobException(E_INVALID_DATA_TREE);
458 }
459 curSchemaNode = curNode.getYangSchemaNode();
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530460 }
461
Vidyashree Rama6160be12016-11-24 13:43:31 +0530462 Class<?> regClass = registry.getRegisteredClass(curSchemaNode);
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530463 return regClass.getClassLoader();
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530464 }
465
Vidyashree Rama6160be12016-11-24 13:43:31 +0530466 YdtExtendedContext parent = (YdtExtendedContext) curNode.getParent();
467 YobWorkBench parentBuilderContainer = (YobWorkBench) parent.getAppInfo(YOB);
468 Object parentObj = parentBuilderContainer.getParentBuilder(curNode,
469 registry);
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530470 return parentObj.getClass().getClassLoader();
471 }
472
473 /**
474 * Returns the class loader to be used for the switched context schema node.
475 *
476 * @param curLoader current context class loader
477 * @param context switched context
478 * @param registry schema registry
479 * @return class loader to be used for the switched context schema node
480 */
Vidyashree Rama6160be12016-11-24 13:43:31 +0530481 static ClassLoader getTargetClassLoader(ClassLoader curLoader,
482 YangSchemaNodeContextInfo context,
483 YangSchemaRegistry registry) {
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530484 YangSchemaNode augmentSchemaNode = context.getContextSwitchedNode();
485 if (augmentSchemaNode.getYangSchemaNodeType() == YANG_AUGMENT_NODE) {
Vidyashree Rama6160be12016-11-24 13:43:31 +0530486 YangSchemaNode moduleNode = ((YangNode) augmentSchemaNode).getParent();
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530487
Vidyashree Rama6160be12016-11-24 13:43:31 +0530488 Class<?> moduleClass = registry.getRegisteredClass(moduleNode);
489 if (moduleClass == null) {
490 throw new YobException(E_FAIL_TO_LOAD_CLASS + moduleNode
491 .getJavaClassNameOrBuiltInType());
492 }
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530493 return moduleClass.getClassLoader();
494 }
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530495 return curLoader;
496 }
497
498 /**
Vidyashree Rama6160be12016-11-24 13:43:31 +0530499 * Returns the schema node's module interface.
500 *
501 * @param schemaNode YANG schema node
502 * @param schemaRegistry YANG schema registry
503 * @return schema node's module interface
504 */
505 public static Class<?> getModuleInterface(YangSchemaNode schemaNode,
506 YangSchemaRegistry schemaRegistry) {
507
508 YangNode yangNode = (YangNode) schemaNode;
509 while (yangNode.getReferredSchema() != null) {
510 yangNode = (YangNode) yangNode.getReferredSchema();
511 }
512
513 while (yangNode.getParent() != null) {
514 yangNode = yangNode.getParent();
515 }
516
517 String qualName = getQualifiedinterface(yangNode);
518 Class<?> regClass = schemaRegistry.getRegisteredClass(yangNode);
519 if (regClass == null) {
520 throw new YobException(E_FAIL_TO_LOAD_CLASS + qualName);
521 }
522
523 try {
524 return regClass.getClassLoader().loadClass(qualName);
525 } catch (ClassNotFoundException e) {
526 log.error(L_FAIL_TO_LOAD_CLASS, qualName);
527 }
528
529 return null;
530 }
531
532 /**
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530533 * Returns the qualified default / op param class.
534 *
535 * @param schemaNode schema node of the required class
536 * @return qualified default / op param class name
537 */
538 static String getQualifiedDefaultClass(YangSchemaNode schemaNode) {
539 String packageName = schemaNode.getJavaPackage();
540 String className = getCapitalCase(
541 schemaNode.getJavaClassNameOrBuiltInType());
542
543 if (schemaNode instanceof RpcNotificationContainer) {
544 return packageName + PERIOD + className + OP_PARAM;
545 }
546
547 return packageName + PERIOD + DEFAULT + className;
548 }
549
550 /**
551 * Returns the qualified interface name.
552 *
553 * @param schemaNode schema node of the required class
554 * @return qualified interface name
555 */
556 static String getQualifiedinterface(YangSchemaNode schemaNode) {
557 String packageName = schemaNode.getJavaPackage();
558 String className = getCapitalCase(
559 schemaNode.getJavaClassNameOrBuiltInType());
560
561 return packageName + PERIOD + className;
562 }
Vidyashree Ramaf463dc52016-10-06 12:46:33 +0530563
564 /**
Vidyashree Rama6160be12016-11-24 13:43:31 +0530565 * Returns the capital cased first letter of the given string.
Vidyashree Ramaf463dc52016-10-06 12:46:33 +0530566 *
Vidyashree Rama6160be12016-11-24 13:43:31 +0530567 * @param name string to be capital cased
568 * @return capital cased string
Vidyashree Ramaf463dc52016-10-06 12:46:33 +0530569 */
Vidyashree Rama6160be12016-11-24 13:43:31 +0530570 public static String getCapitalCase(String name) {
571 // TODO: It will be removed if common util is committed.
572 return name.substring(0, 1).toUpperCase() +
573 name.substring(1);
574 }
575
576 /**
577 * To set data into parent setter method from string value for identity ref.
578 *
579 * @param leafValue leaf value to be set
580 * @param parentSetterMethod the parent setter method to be invoked
581 * @param parentBuilderObject the parent build object on which to invoke
582 * the method
583 * @param ydtExtendedContext application context
584 * @throws InvocationTargetException if method could not be invoked
585 * @throws IllegalAccessException if method could not be accessed
586 * @throws NoSuchMethodException if method does not exist
587 */
588 private static void parseIdentityRefInfo(YdtExtendedContext
589 ydtExtendedContext,
590 Method parentSetterMethod,
591 Object parentBuilderObject,
592 String leafValue)
593 throws InvocationTargetException, IllegalAccessException,
594 NoSuchMethodException {
595 Class<?> childSetClass = null;
596 Object childValue = null;
597 Method childMethod = null;
598
599 YangSchemaNode yangJavaModule = ydtExtendedContext.getYangSchemaNode();
600 while (yangJavaModule.getReferredSchema() != null) {
601 yangJavaModule = yangJavaModule.getReferredSchema();
Vidyashree Ramaf463dc52016-10-06 12:46:33 +0530602 }
Vidyashree Rama6160be12016-11-24 13:43:31 +0530603
604 String qualifiedClassName = null;
605 YangType type;
606 if (yangJavaModule instanceof YangLeaf) {
607 type = ((YangLeaf) yangJavaModule).getDataType();
608 } else {
609 type = ((YangLeafList) yangJavaModule).getDataType();
610 }
611
612 if (type.getDataType() == YangDataTypes.LEAFREF && yangJavaModule
613 .getJavaPackage().equals(YobConstants.JAVA_LANG)) {
614 YangLeafRef leafref = ((YangLeafRef) type.getDataTypeExtendedInfo());
615 YangType effectiveType = leafref.getEffectiveDataType();
616 if (effectiveType.getDataType() == YangDataTypes.IDENTITYREF) {
617 YangIdentityRef identityref = ((YangIdentityRef) effectiveType
618 .getDataTypeExtendedInfo());
619 YangIdentity identity = identityref.getReferredIdentity();
620 qualifiedClassName = identity.getJavaPackage() + PERIOD +
621 getCapitalCase(identity.getJavaClassNameOrBuiltInType());
622 }
623 } else {
624 qualifiedClassName = yangJavaModule.getJavaPackage() + PERIOD +
625 getCapitalCase(yangJavaModule.getJavaClassNameOrBuiltInType());
626 }
627
628 ClassLoader classLoader = getClassLoader(null, qualifiedClassName,
629 ydtExtendedContext, null);
630 try {
631 childSetClass = classLoader.loadClass(qualifiedClassName);
632 } catch (ClassNotFoundException e) {
633 log.error(L_FAIL_TO_LOAD_CLASS, qualifiedClassName);
634 }
635
636 if (childSetClass != null) {
637 childMethod = childSetClass
638 .getDeclaredMethod(FROM_STRING, String.class);
639 }
640
641 if (childMethod != null) {
642 childValue = childMethod.invoke(null, leafValue);
643 }
644
645 parentSetterMethod.invoke(parentBuilderObject, childValue);
Vidyashree Ramaf463dc52016-10-06 12:46:33 +0530646 }
Vidyashree Ramafd339c42016-12-01 19:19:56 +0530647
648 /**
649 * Creates and sets default notification object in event subject object.
650 *
651 * @param defaultObj default notification object
652 * @param curNode application context
653 * @param registry YANG schema registry
654 * @return notification event subject object
655 */
656 public static Object createAndSetInEventSubjectInstance(Object defaultObj,
657 YdtExtendedContext curNode,
658 YangSchemaRegistry registry) {
659 YangSchemaNode childSchema = ((YdtExtendedContext) curNode
660 .getFirstChild()).getYangSchemaNode();
661 String packageName = childSchema.getJavaPackage();
662 String className = getCapitalCase(curNode.getYangSchemaNode()
663 .getJavaClassNameOrBuiltInType());
664 String qualName = packageName + PERIOD + className + EVENT_SUBJECT;
665
666 ClassLoader classLoader = YobUtils.getClassLoader(registry, qualName,
667 curNode, curNode);
668
669 Object eventSubObj;
670 Class<?> eventSubjectClass = null;
671 try {
672 eventSubjectClass = classLoader.loadClass(qualName);
673 eventSubObj = eventSubjectClass.newInstance();
674 } catch (ClassNotFoundException e) {
675 log.error(E_FAIL_TO_LOAD_CLASS, className);
676 throw new YobException(E_FAIL_TO_LOAD_CLASS +
rohitsharan2dc43072017-04-13 12:23:12 +0530677 qualName);
Vidyashree Ramafd339c42016-12-01 19:19:56 +0530678 } catch (InstantiationException e) {
679 log.error(E_FAIL_TO_CREATE_OBJ, className);
680 throw new YobException(E_FAIL_TO_CREATE_OBJ +
681 eventSubjectClass.getName());
682 } catch (IllegalAccessException e) {
683 log.error(L_FAIL_TO_INVOKE_METHOD, className);
684 throw new YobException(E_FAIL_TO_INVOKE_METHOD +
685 eventSubjectClass.getName());
686 }
687
688 setInEventSubject(((YdtExtendedContext) curNode.getFirstChild()),
689 eventSubObj, defaultObj);
690 return eventSubObj;
691 }
692
693 /**
694 * Sets the default notification object in event subject class.
695 *
696 * @param ydtNode application context
697 * @param eventSubObj notification event subject instance
698 * @param defaultObj default notification instance
699 */
700 public static void setInEventSubject(YdtExtendedContext ydtNode,
701 Object eventSubObj,
702 Object defaultObj) {
703
704 Class<?> eventSubjectClass = eventSubObj.getClass();
705 String className = eventSubjectClass.getName();
706 String setter = ydtNode.getYangSchemaNode().getJavaAttributeName();
707
708 try {
709 Class<?> type = null;
710 Field fieldName = eventSubjectClass.getDeclaredField(setter);
711 if (fieldName != null) {
712 type = fieldName.getType();
713 }
714
715 Method method;
716 method = eventSubjectClass.getDeclaredMethod(setter, type);
717 method.invoke(eventSubObj, defaultObj);
718 } catch (NoSuchFieldException e) {
719 log.error(L_FAIL_TO_GET_FIELD, className);
720 throw new YobException(E_FAIL_TO_GET_FIELD + className);
721 } catch (NoSuchMethodException e) {
722 log.error(L_FAIL_TO_GET_METHOD, className);
723 throw new YobException(E_FAIL_TO_GET_METHOD + className);
724 } catch (InvocationTargetException | IllegalAccessException e) {
725 log.error(L_FAIL_TO_INVOKE_METHOD, className);
726 throw new YobException(E_FAIL_TO_INVOKE_METHOD + className);
727 }
728 }
729
730 /**
731 * Creates an object of notification event class and sets event subject
732 * in event class.
733 *
734 * @param eventSubObj instance of event subject class
735 * @param curNode current YDT node
736 * @param registry YANG schema registry
737 * @return notification event object
738 */
739 public static Object createAndSetInEventInstance(Object eventSubObj,
740 YdtExtendedContext curNode,
741 YangSchemaRegistry registry) {
742 YangSchemaNode childSchema = ((YdtExtendedContext) curNode
743 .getFirstChild()).getYangSchemaNode();
744 String packageName = childSchema.getJavaPackage();
745 String className = getCapitalCase(curNode.getYangSchemaNode()
746 .getJavaClassNameOrBuiltInType());
747 String qualName = packageName + PERIOD + className + EVENT;
748
749 try {
750 ClassLoader classLoader = YobUtils.getClassLoader(registry, qualName,
751 curNode, curNode);
752 Class<?> eventClass = classLoader.loadClass(qualName);
753 Class<?>[] innerClasses = eventClass.getClasses();
754 Object typeObj = null;
755 for (Class<?> innerEnumClass : innerClasses) {
756 if (innerEnumClass.getSimpleName().equals(TYPE)) {
757 Method valueOfMethod = innerEnumClass
758 .getDeclaredMethod(VALUE_OF, String.class);
759 String eventType = getEnumJavaAttribute(childSchema.getName())
760 .toUpperCase();
761 typeObj = valueOfMethod.invoke(null, eventType);
762 break;
763 }
764 }
765
766 Constructor constructor = eventClass
767 .getDeclaredConstructor(typeObj.getClass(),
768 eventSubObj.getClass());
769 constructor.setAccessible(true);
770 return constructor.newInstance(typeObj, eventSubObj);
771 } catch (ClassNotFoundException e) {
772 log.error(L_FAIL_TO_INVOKE_METHOD, className);
773 throw new YobException(E_FAIL_TO_INVOKE_METHOD + className);
774 } catch (InstantiationException e) {
775 log.error(E_FAIL_TO_CREATE_OBJ, className);
776 throw new YobException(E_FAIL_TO_CREATE_OBJ + className);
777 } catch (NoSuchMethodException e) {
778 log.error(L_FAIL_TO_GET_METHOD, className);
779 throw new YobException(E_FAIL_TO_GET_METHOD + className);
780 } catch (InvocationTargetException | IllegalAccessException e) {
781 log.error(L_FAIL_TO_INVOKE_METHOD, className);
782 throw new YobException(E_FAIL_TO_INVOKE_METHOD + className);
783 }
784 }
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530785}