janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.yms.app.ytb; |
| 18 | |
| 19 | import org.onosproject.yangutils.datamodel.YangAugment; |
| 20 | import org.onosproject.yangutils.datamodel.YangCase; |
janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.datamodel.YangIdentity; |
| 22 | import org.onosproject.yangutils.datamodel.YangIdentityRef; |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.datamodel.YangLeafRef; |
| 24 | import org.onosproject.yangutils.datamodel.YangNode; |
| 25 | import org.onosproject.yangutils.datamodel.YangNotification; |
| 26 | import org.onosproject.yangutils.datamodel.YangOutput; |
| 27 | import org.onosproject.yangutils.datamodel.YangRpc; |
janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.datamodel.YangSchemaNode; |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.datamodel.YangType; |
| 30 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes; |
| 31 | import org.onosproject.yangutils.translator.tojava.javamodel.JavaLeafInfoContainer; |
| 32 | import org.onosproject.yms.app.utils.TraversalType; |
| 33 | import org.onosproject.yms.app.ysr.YangSchemaRegistry; |
| 34 | import org.onosproject.yms.ydt.YdtContextOperationType; |
| 35 | |
| 36 | import java.lang.reflect.InvocationTargetException; |
| 37 | import java.lang.reflect.Method; |
| 38 | import java.util.Arrays; |
| 39 | import java.util.Base64; |
janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 40 | import java.util.Collection; |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 41 | import java.util.HashSet; |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 42 | import java.util.Set; |
| 43 | |
| 44 | import static org.onosproject.yangutils.datamodel.YangSchemaNodeType.YANG_AUGMENT_NODE; |
| 45 | import static org.onosproject.yangutils.datamodel.YangSchemaNodeType.YANG_MULTI_INSTANCE_NODE; |
| 46 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.BOOLEAN; |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 47 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.EMPTY; |
| 48 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT16; |
| 49 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT32; |
| 50 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT64; |
| 51 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT8; |
| 52 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.LEAFREF; |
| 53 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.UINT16; |
| 54 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.UINT32; |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 55 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.UINT8; |
janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 56 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase; |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 57 | import static org.onosproject.yms.app.utils.TraversalType.PARENT; |
| 58 | |
| 59 | /** |
| 60 | * Representation of utility for YANG tree builder. |
| 61 | */ |
| 62 | public final class YtbUtil { |
| 63 | |
| 64 | /** |
| 65 | * Static attribute for string value having null. |
| 66 | */ |
| 67 | public static final String STR_NULL = "null"; |
| 68 | |
| 69 | /** |
| 70 | * Static attribute for a dot string. |
| 71 | */ |
| 72 | public static final String PERIOD = "."; |
| 73 | |
| 74 | private static final int ONE = 1; |
janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 75 | private static final String YANG = "yang"; |
| 76 | private static final String OP_TYPE = "OpType"; |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 77 | private static final String STR_NONE = "NONE"; |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 78 | private static final String ENUM_LEAF_IDENTIFIER = "$LeafIdentifier"; |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 79 | private static final Set<YangDataTypes> PRIMITIVE_TYPES = |
| 80 | new HashSet<>(Arrays.asList(INT8, INT16, INT32, INT64, UINT8, |
janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 81 | UINT16, UINT32, BOOLEAN, EMPTY)); |
| 82 | private static final String TO_STRING = "toString"; |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 83 | |
| 84 | // No instantiation. |
| 85 | private YtbUtil() { |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Returns the object of the node from the node info. Getting object for |
| 90 | * augment and case differs from other node. |
| 91 | * |
| 92 | * @param nodeInfo node info of the holder |
| 93 | * @param yangNode YANG node of the holder |
| 94 | * @return object of the parent |
| 95 | */ |
| 96 | public static Object getParentObjectOfNode(YtbNodeInfo nodeInfo, |
| 97 | YangNode yangNode) { |
| 98 | Object object; |
| 99 | if (yangNode instanceof YangCase) { |
| 100 | object = nodeInfo.getCaseObject(); |
| 101 | } else if (yangNode instanceof YangAugment) { |
| 102 | object = nodeInfo.getAugmentObject(); |
| 103 | } else { |
| 104 | object = nodeInfo.getYangObject(); |
| 105 | } |
| 106 | return object; |
| 107 | } |
| 108 | |
| 109 | /** |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 110 | * Returns the value of an attribute, in a class object. The attribute |
| 111 | * name is taken from the YANG node java name. |
| 112 | * |
| 113 | * @param nodeObj object of the node |
| 114 | * @param fieldName name of the attribute |
| 115 | * @return object of the attribute |
| 116 | * @throws NoSuchMethodException method not found exception |
| 117 | */ |
| 118 | public static Object getAttributeOfObject(Object nodeObj, String fieldName) |
| 119 | throws NoSuchMethodException { |
| 120 | Class<?> nodeClass = nodeObj.getClass(); |
| 121 | Method getterMethod; |
| 122 | try { |
| 123 | getterMethod = nodeClass.getDeclaredMethod(fieldName); |
| 124 | return getterMethod.invoke(nodeObj); |
| 125 | } catch (InvocationTargetException | IllegalAccessException e) { |
| 126 | throw new YtbException(e); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Returns the object of the declared method in parent class by invoking |
| 132 | * through the child class object. |
| 133 | * |
janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 134 | * @param childClass child class which inherits the parent class |
| 135 | * @param methodName name of the declared method |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 136 | * @return value of the method |
| 137 | */ |
janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 138 | public static Object getAttributeFromInheritance(Object childClass, |
| 139 | String methodName) { |
| 140 | Class<?> parentClass = childClass.getClass().getSuperclass(); |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 141 | Method getterMethod; |
| 142 | try { |
| 143 | getterMethod = parentClass.getDeclaredMethod(methodName); |
| 144 | return getterMethod.invoke(childClass); |
| 145 | } catch (InvocationTargetException | NoSuchMethodException | |
| 146 | IllegalAccessException e) { |
| 147 | throw new YtbException(e); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Returns interface class from an implementation class object. |
| 153 | * |
janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 154 | * @param obj implementation class object |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 155 | * @return interface class |
| 156 | */ |
janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 157 | public static Class<?> getInterfaceClassFromImplClass(Object obj) { |
| 158 | Class<?>[] interfaces = obj.getClass().getInterfaces(); |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 159 | if (interfaces.length > ONE) { |
| 160 | // TODO: Need to handle when impl class has more than one interface. |
| 161 | throw new YtbException("Implementation class having more than one" + |
| 162 | " interface is not handled"); |
| 163 | } |
| 164 | return interfaces[0]; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Returns the operation type value for a class object. If the operation |
| 169 | * type is not set, then none type is returned. |
| 170 | * |
janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 171 | * @param nodeObj node object |
| 172 | * @param typeName data type name |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 173 | * @return operation type of the class |
| 174 | */ |
janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 175 | public static YdtContextOperationType getNodeOpType(Object nodeObj, |
| 176 | String typeName) { |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 177 | Object opTypeObj; |
| 178 | try { |
janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 179 | opTypeObj = getAttributeOfObject(nodeObj, typeName); |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 180 | } catch (NoSuchMethodException e) { |
| 181 | return YdtContextOperationType.valueOf(STR_NONE); |
| 182 | } |
| 183 | String opTypeValue = String.valueOf(opTypeObj); |
| 184 | if (opTypeValue.equals(STR_NULL)) { |
janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 185 | return null; |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 186 | } |
| 187 | return YdtContextOperationType.valueOf(opTypeValue); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Returns true, if data type of leaf is primitive data type; false |
| 192 | * otherwise. |
| 193 | * |
| 194 | * @param yangType leaf type |
| 195 | * @return true if data type is primitive; false otherwise |
| 196 | */ |
| 197 | public static boolean isTypePrimitive(YangType yangType) { |
| 198 | if (yangType.getDataType() == LEAFREF) { |
| 199 | YangLeafRef leafRef = |
| 200 | (YangLeafRef) yangType.getDataTypeExtendedInfo(); |
| 201 | return isPrimitiveDataType(leafRef.getEffectiveDataType() |
| 202 | .getDataType()); |
| 203 | } |
| 204 | return isPrimitiveDataType(yangType.getDataType()); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Returns the registered class from the YSR of the module node where |
| 209 | * augment is present. |
| 210 | * |
| 211 | * @param curNode current augment node |
| 212 | * @param registry schema registry |
| 213 | * @return class loader of module |
| 214 | */ |
| 215 | public static Class<?> getClassLoaderForAugment( |
| 216 | YangNode curNode, YangSchemaRegistry registry) { |
| 217 | YangNode moduleNode = curNode.getParent(); |
| 218 | String moduleName = moduleNode.getJavaClassNameOrBuiltInType(); |
| 219 | String modulePackage = moduleNode.getJavaPackage(); |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 220 | return registry.getRegisteredClass(moduleNode |
| 221 | ); |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Returns the string true, if the leaf data is actually set; false |
| 226 | * otherwise. |
| 227 | * |
janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 228 | * @param holder leaf holder |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 229 | * @param nodeObj object if the node |
| 230 | * @param javaName java name of the leaf |
| 231 | * @param methodName getter method name |
| 232 | * @return string value of the boolean method |
janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 233 | * @throws NoSuchMethodException if the method is not present |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 234 | */ |
janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 235 | public static String isValueOrSelectLeafSet(YangSchemaNode holder, Object nodeObj, |
| 236 | String javaName, String methodName) |
| 237 | throws NoSuchMethodException { |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 238 | |
| 239 | Class<?> nodeClass = nodeObj.getClass(); |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 240 | |
| 241 | // Appends the enum inner package to the interface class package. |
janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 242 | String enumPackage = holder.getJavaPackage() + PERIOD + |
| 243 | getCapitalCase(holder.getJavaClassNameOrBuiltInType()) + |
| 244 | ENUM_LEAF_IDENTIFIER; |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 245 | |
janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 246 | ClassLoader classLoader = nodeClass.getClassLoader(); |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 247 | Class leafEnum; |
| 248 | try { |
| 249 | leafEnum = classLoader.loadClass(enumPackage); |
| 250 | Method getterMethod = nodeClass.getMethod(methodName, leafEnum); |
| 251 | // Gets the value of the enum. |
| 252 | Enum<?> value = Enum.valueOf(leafEnum, javaName.toUpperCase()); |
| 253 | // Invokes the method with the value of enum as param. |
| 254 | return String.valueOf(getterMethod.invoke(nodeObj, value)); |
| 255 | } catch (IllegalAccessException | InvocationTargetException | |
janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 256 | ClassNotFoundException e) { |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 257 | throw new YtbException(e); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Returns the string value from the respective data types of the |
| 263 | * leaf/leaf-list. |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 264 | * |
janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 265 | * @param holder leaf/leaf-list holder |
| 266 | * @param holderObj leaf/leaf-list holder object |
| 267 | * @param name leaf/leaf-list name |
| 268 | * @param fieldObj object of the leaf/leaf-list field |
| 269 | * @param dataType type of the leaf/leaf-list |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 270 | * @return string value from the type |
| 271 | */ |
janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 272 | public static String getStringFromType(YangSchemaNode holder, Object holderObj, |
| 273 | String name, Object fieldObj, YangType dataType) { |
| 274 | |
| 275 | if (fieldObj == null) { |
| 276 | throw new YtbException("Value of " + holder.getName() + " is null"); |
| 277 | } |
| 278 | |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 279 | YangDataTypes type = dataType.getDataType(); |
| 280 | switch (type) { |
| 281 | case INT8: |
| 282 | case INT16: |
| 283 | case INT32: |
| 284 | case INT64: |
| 285 | case UINT8: |
| 286 | case UINT16: |
| 287 | case UINT32: |
| 288 | case UINT64: |
| 289 | case EMPTY: |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 290 | case STRING: |
| 291 | case DECIMAL64: |
| 292 | case INSTANCE_IDENTIFIER: |
| 293 | case DERIVED: |
| 294 | case UNION: |
janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 295 | case ENUMERATION: |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 296 | case BOOLEAN: |
janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 297 | return String.valueOf(fieldObj).trim(); |
| 298 | |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 299 | case BITS: |
janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 300 | return getBitsValue(holder, holderObj, name, fieldObj).trim(); |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 301 | |
| 302 | case BINARY: |
| 303 | return Base64.getEncoder().encodeToString((byte[]) fieldObj); |
| 304 | |
janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 305 | case IDENTITYREF: |
| 306 | YangIdentityRef ir = |
| 307 | (YangIdentityRef) dataType.getDataTypeExtendedInfo(); |
| 308 | if (ir.isInGrouping()) { |
| 309 | return String.valueOf(fieldObj).trim(); |
| 310 | } |
| 311 | return getIdentityRefValue(fieldObj, ir, holderObj); |
| 312 | |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 313 | case LEAFREF: |
| 314 | YangLeafRef leafRef = |
| 315 | (YangLeafRef) dataType.getDataTypeExtendedInfo(); |
janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 316 | return getStringFromType(holder, holderObj, name, fieldObj, |
| 317 | leafRef.getEffectiveDataType()); |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 318 | |
| 319 | default: |
| 320 | throw new YtbException("Unsupported data type. Cannot be " + |
| 321 | "processed."); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | /** |
janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 326 | * Returns the string values for the data type bits. |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 327 | * |
janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 328 | * @param holder leaf/leaf-list holder |
| 329 | * @param holderObj leaf/leaf-list holder object |
| 330 | * @param name leaf/leaf-list name |
| 331 | * @param fieldObj object of the leaf/leaf-list field |
| 332 | * @return string value for bits type |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 333 | */ |
janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 334 | private static String getBitsValue(YangSchemaNode holder, Object holderObj, |
| 335 | String name, Object fieldObj) { |
| 336 | |
| 337 | Class<?> holderClass = holderObj.getClass(); |
| 338 | String interfaceName = holder.getJavaClassNameOrBuiltInType(); |
| 339 | String className = interfaceName.toLowerCase() + PERIOD + |
| 340 | getCapitalCase(name); |
| 341 | String pkgName = holder.getJavaPackage() + PERIOD + className; |
| 342 | ClassLoader classLoader = holderClass.getClassLoader(); |
| 343 | |
| 344 | Class<?> bitClass; |
| 345 | try { |
| 346 | bitClass = classLoader.loadClass(pkgName); |
| 347 | Method getterMethod = bitClass.getDeclaredMethod( |
| 348 | TO_STRING, fieldObj.getClass()); |
| 349 | return String.valueOf(getterMethod.invoke(null, fieldObj)); |
| 350 | } catch (ClassNotFoundException | NoSuchMethodException | |
| 351 | InvocationTargetException | IllegalAccessException e) { |
| 352 | throw new YtbException(e); |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 353 | } |
janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Returns the string value of the type identity-ref. |
| 358 | * |
| 359 | * @param fieldObj object of the leaf/leaf-list field |
| 360 | * @param ir YANG identity ref |
| 361 | * @param holderObj leaf/leaf-list holder object |
| 362 | * @return string value for identity ref type |
| 363 | */ |
| 364 | private static String getIdentityRefValue(Object fieldObj, YangIdentityRef ir, |
| 365 | Object holderObj) { |
| 366 | |
| 367 | YangIdentity id = ir.getReferredIdentity(); |
| 368 | String idName = id.getJavaClassNameOrBuiltInType(); |
| 369 | String idPkg = id.getJavaPackage() + PERIOD + getCapitalCase(idName); |
| 370 | String methodName = idName + getCapitalCase(TO_STRING); |
| 371 | |
| 372 | Class<?> holderClass = holderObj.getClass(); |
| 373 | ClassLoader classLoader = holderClass.getClassLoader(); |
| 374 | Class<?> idClass; |
| 375 | try { |
| 376 | idClass = classLoader.loadClass(idPkg); |
| 377 | Method method = idClass.getDeclaredMethod(methodName, null); |
| 378 | return String.valueOf(method.invoke(fieldObj, null)).trim(); |
| 379 | } catch (ClassNotFoundException | NoSuchMethodException | |
| 380 | InvocationTargetException | IllegalAccessException e) { |
| 381 | throw new YtbException(e); |
| 382 | } |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | /** |
| 386 | * Returns true, if the data type is primitive; false otherwise. |
| 387 | * |
| 388 | * @param dataType data type |
| 389 | * @return true if the data type is primitive; false otherwise |
| 390 | */ |
| 391 | private static boolean isPrimitiveDataType(YangDataTypes dataType) { |
| 392 | return PRIMITIVE_TYPES.contains(dataType); |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * Returns true, if processing of the node is not required; false otherwise. |
| 397 | * For the nodes such as notification, RPC, augment there is a different |
| 398 | * flow, so these nodes are skipped in normal conditions. |
| 399 | * |
| 400 | * @param yangNode node to be checked |
| 401 | * @return true if node processing is not required; false otherwise. |
| 402 | */ |
| 403 | public static boolean isNonProcessableNode(YangNode yangNode) { |
| 404 | return yangNode != null && (yangNode instanceof YangNotification) || |
| 405 | (yangNode instanceof YangRpc) || (yangNode instanceof YangAugment); |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * Returns true, if multi instance node; false otherwise. |
| 410 | * |
| 411 | * @param yangNode YANG node |
| 412 | * @return true, if multi instance node; false otherwise. |
| 413 | */ |
| 414 | public static boolean isMultiInstanceNode(YangNode yangNode) { |
| 415 | return yangNode.getYangSchemaNodeType() == YANG_MULTI_INSTANCE_NODE; |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * Returns true, if augment node; false otherwise. |
| 420 | * |
| 421 | * @param yangNode YANG node |
| 422 | * @return true, if augment node; false otherwise. |
| 423 | */ |
| 424 | public static boolean isAugmentNode(YangNode yangNode) { |
| 425 | return yangNode.getYangSchemaNodeType() == YANG_AUGMENT_NODE; |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * Returns string for throwing error when empty object is given as input |
| 430 | * to YTB. |
| 431 | * |
| 432 | * @param objName name of the object |
| 433 | * @return error message |
| 434 | */ |
| 435 | public static String emptyObjErrMsg(String objName) { |
| 436 | return "The " + objName + " given for tree creation cannot be null"; |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * Returns the java name for the nodes, leaf/leaf-list. |
| 441 | * |
| 442 | * @param node YANG node |
| 443 | * @return node java name |
| 444 | */ |
| 445 | public static String getJavaName(Object node) { |
| 446 | return ((JavaLeafInfoContainer) node).getJavaName(null); |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Returns true, if the list is not null and non-empty; false otherwise. |
| 451 | * |
janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 452 | * @param c collection object |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 453 | * @return true, if the list is not null and non-empty; false otherwise |
| 454 | */ |
janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 455 | public static boolean nonEmpty(Collection<?> c) { |
| 456 | return c != null && !c.isEmpty(); |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | /** |
| 460 | * Returns true, if the string is not null and non-empty; false otherwise. |
| 461 | * |
| 462 | * @param str string value |
| 463 | * @return true, if the string is not null and non-empty; false otherwise. |
| 464 | */ |
janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 465 | public static boolean nonEmpty(String str) { |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 466 | return str != null && !str.isEmpty(); |
| 467 | } |
| 468 | |
| 469 | /** |
| 470 | * Returns true when the node processing of RPC and notification is |
| 471 | * completed; false otherwise. For RPC and notification, processing of |
| 472 | * other nodes are invalid, so once node gets completed, it must be stopped. |
| 473 | * |
| 474 | * @param curNode current node |
| 475 | * @param curTraversal current traversal of the node |
| 476 | * @return true, if the node processing is completed; false otherwise. |
| 477 | */ |
| 478 | public static boolean isNodeProcessCompleted( |
| 479 | YangNode curNode, TraversalType curTraversal) { |
| 480 | return (curTraversal == PARENT && |
| 481 | curNode instanceof YangNotification) || |
| 482 | curNode instanceof YangOutput; |
| 483 | } |
| 484 | |
janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 485 | /** |
| 486 | * Returns the name of the operation type variable from the yang name. |
| 487 | * |
| 488 | * @param curNode YANG node |
| 489 | * @return name of operation type |
| 490 | */ |
| 491 | public static String getOpTypeName(YangNode curNode) { |
| 492 | return YANG + getCapitalCase(curNode.getJavaClassNameOrBuiltInType()) + |
| 493 | OP_TYPE; |
| 494 | } |
janani b | 05614f1 | 2016-10-04 12:55:43 +0530 | [diff] [blame] | 495 | } |