2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Modifications Copyright (C) 2018 IBM.
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.ccsdk.sli.core.sli;
26 import java.io.PrintStream;
27 import java.io.FileDescriptor;
28 import java.lang.reflect.Constructor;
29 import java.lang.reflect.Method;
30 import java.lang.reflect.Modifier;
31 import java.lang.reflect.ParameterizedType;
32 import java.lang.reflect.Type;
33 import java.util.LinkedList;
34 import java.util.List;
35 import java.util.Properties;
36 import java.util.Arrays;
37 import java.util.ArrayList;
39 import org.opendaylight.yangtools.yang.binding.Identifier;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressBuilder;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefixBuilder;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51 import com.google.common.base.Strings;
54 public class PrintYangToProp {
56 private static final Logger LOG = LoggerFactory.getLogger(PrintYangToProp.class);
57 public static final String PROPERTIES_FILE="";
58 private static Properties properties;
59 private static final String BUILDER="-builder";
60 private static final String IMPL="-impl";
61 private static final String TO_PROPERTIES_STRING="() to Properties entry";
62 private static final String CAUGHT_EXCEPTION_MSG="Caught exception trying to convert value returned by ";
63 public static Properties prop = new Properties();
64 public static ArrayList<String> propList = new ArrayList<>();
67 public static Properties toProperties(Properties props, Object fromObj) {
68 Class fromClass = null;
72 fromClass = fromObj.getClass();
74 return (toProperties(props, "", fromObj, fromClass));
77 public static Properties toProperties(Properties props, String pfx, Object fromObj)
79 Class fromClass = null;
83 fromClass = fromObj.getClass();
86 return(toProperties(props, pfx, fromObj, fromClass));
89 public static Properties toProperties(Properties props, String pfx,
90 Object fromObj, Class fromClass) {
92 if (fromObj == null) {
97 String simpleName = fromClass.getSimpleName();
99 if (fromObj instanceof List) {
102 List fromList = (List) fromObj;
104 for (int i = 0; i < fromList.size(); i++) {
105 toProperties(props, pfx + "[" + i + "]", fromList.get(i), fromClass);
107 props.setProperty(pfx + "_length", "" + fromList.size());
109 } else if (isYangGenerated(fromClass)) {
111 String propNamePfx = null;
113 // If called from a list (so prefix ends in ']'), don't
114 // add class name again
115 if (pfx.endsWith("]")) {
118 if ((pfx != null) && (pfx.length() > 0)) {
121 propNamePfx = toLowerHyphen(fromClass.getSimpleName());
124 if (propNamePfx.endsWith(BUILDER)) {
125 propNamePfx = propNamePfx.substring(0, propNamePfx.length()
129 if (propNamePfx.endsWith(IMPL)) {
130 propNamePfx = propNamePfx.substring(0, propNamePfx.length()
135 // Iterate through getter methods to figure out values we need to
138 for (Method m : fromClass.getMethods()) {
142 Class returnType = m.getReturnType();
143 String fieldName = toLowerHyphen(m.getName().substring(3));
144 if(m != null && m.getName().matches("^is[A-Z].*")){
145 fieldName = toLowerHyphen(m.getName().substring(2));
148 if(Strings.isNullOrEmpty(fieldName)) fieldName = fieldName.substring(0, 1).toLowerCase()+ fieldName.substring(1);
151 // Is the return type a yang generated class?
152 if (isYangGenerated(returnType)) {
155 if (returnType.isEnum()) {
156 // Return type is a typedef. Save its value.
158 boolean isAccessible = m.isAccessible();
160 m.setAccessible(true);
163 Object retValue = m.invoke(fromObj);
166 m.setAccessible(isAccessible);
168 if (retValue != null) {
169 String propName = propNamePfx + "."
171 String propVal = retValue.toString();
172 String yangProp = "yang." + fieldName + "." + propVal;
173 if ( properties.containsKey(yangProp)) {
174 propVal = properties.getProperty(yangProp);
176 props.setProperty(propName, propVal);
178 } catch (Exception e) {
180 "Caught exception trying to convert Yang-generated enum returned by "
181 + fromClass.getName() + "."
183 + TO_PROPERTIES_STRING, e);
185 } else if (isIpv4Address(returnType)) {
188 String propName = propNamePfx + "." + fieldName;
189 boolean isAccessible = m.isAccessible();
191 m.setAccessible(true);
193 Ipv4Address retValue = (Ipv4Address) m.invoke(fromObj);
195 m.setAccessible(isAccessible);
198 if (retValue != null) {
199 String propVal = retValue.getValue();
201 props.setProperty(propName, propVal);
204 } catch (Exception e) {
207 + fromClass.getName() + "."
209 + TO_PROPERTIES_STRING, e);
211 } else if (isIpv6Address(returnType)) {
214 String propName = propNamePfx + "." + fieldName;
215 boolean isAccessible = m.isAccessible();
217 m.setAccessible(true);
219 Ipv6Address retValue = (Ipv6Address) m.invoke(fromObj);
221 m.setAccessible(isAccessible);
224 if (retValue != null) {
225 String propVal = retValue.getValue();
227 props.setProperty(propName, propVal);
230 } catch (Exception e) {
233 + fromClass.getName() + "."
235 + TO_PROPERTIES_STRING, e);
237 } else if (isIpv4Prefix(returnType)) {
241 String propName = propNamePfx + "." + fieldName;
242 boolean isAccessible = m.isAccessible();
244 m.setAccessible(true);
246 Ipv4Prefix retValue = (Ipv4Prefix) m.invoke(fromObj);
248 m.setAccessible(isAccessible);
251 if (retValue != null) {
252 String propVal = retValue.getValue();
254 props.setProperty(propName, propVal);
257 } catch (Exception e) {
260 + fromClass.getName() + "."
262 + TO_PROPERTIES_STRING, e);
264 } else if (isIpv6Prefix(returnType)) {
265 //System.out.println("isIpv6Prefix");
268 String propName = propNamePfx + "." + fieldName;
269 boolean isAccessible = m.isAccessible();
271 m.setAccessible(true);
273 Ipv6Prefix retValue = (Ipv6Prefix) m.invoke(fromObj);
275 m.setAccessible(isAccessible);
278 if (retValue != null) {
279 String propVal = retValue.getValue().toString();
280 //LOG.debug("Setting property " + propName
281 // + " to " + propVal);
282 props.setProperty(propName, propVal);
285 } catch (Exception e) {
288 + fromClass.getName() + "."
290 + TO_PROPERTIES_STRING, e);
294 boolean isAccessible = m.isAccessible();
296 m.setAccessible(true);
298 Object retValue = m.invoke(fromObj);
300 m.setAccessible(isAccessible);
302 if (retValue != null) {
303 toProperties(props, propNamePfx + "." + fieldName, retValue, returnType);
305 } catch (Exception e) {
307 "Caught exception trying to convert Yang-generated class returned by"
308 + fromClass.getName() + "."
310 + TO_PROPERTIES_STRING, e);
313 } else if (returnType.equals(Class.class)) {
315 //LOG.debug(m.getName()
316 // + " returns a Class object - not interested");
318 } else if (List.class.isAssignableFrom(returnType)) {
320 // This getter method returns a list.
322 boolean isAccessible = m.isAccessible();
324 m.setAccessible(true);
326 Object retList = m.invoke(fromObj);
328 m.setAccessible(isAccessible);
330 // Figure out what type of elements are stored in this array.
331 Type paramType = m.getGenericReturnType();
332 Type elementType = ((ParameterizedType) paramType)
333 .getActualTypeArguments()[0];
334 toProperties(props, propNamePfx + "." + fieldName,
335 retList, (Class)elementType);
336 } catch (Exception e) {
338 "Caught exception trying to convert List returned by "
339 + fromClass.getName() + "."
341 + TO_PROPERTIES_STRING, e);
346 // Method returns something that is not a List and not
350 String propName = propNamePfx + "." + fieldName;
351 boolean isAccessible = m.isAccessible();
353 m.setAccessible(true);
355 Object propValObj = m.invoke(fromObj);
357 m.setAccessible(isAccessible);
360 if (propValObj != null) {
361 String propVal = propValObj.toString();
362 //LOG.debug("Setting property " + propName
363 // + " to " + propVal);
364 props.setProperty(propName, propVal);
367 } catch (Exception e) {
370 + fromClass.getName() + "."
372 + TO_PROPERTIES_STRING, e);
380 // Class is not yang generated and not a list
388 public static Object toBuilder(Properties props, Object toObj) {
390 return (toBuilder(props, "", toObj));
393 public static List toList(Properties props, String pfx, List toObj,
397 boolean foundValue = false;
399 //LOG.debug("Saving properties to List<" + elemType.getName()
400 // + "> from " + pfx);
402 // Figure out array size
403 for (Object pNameObj : props.keySet()) {
404 String key = (String) pNameObj;
406 if (key.startsWith(pfx + "[")) {
407 String idxStr = key.substring(pfx.length() + 1);
408 int endloc = idxStr.indexOf("]");
410 idxStr = idxStr.substring(0, endloc);
414 int curIdx = Integer.parseInt(idxStr);
415 if (curIdx > maxIdx) {
418 } catch (Exception e) {
419 LOG.error("Illegal subscript in property " + key);
425 //LOG.debug(pfx + " has max index of " + maxIdx);
426 for (int i = 0; i <= maxIdx; i++) {
428 String curBase = pfx + "[" + i + "]";
430 if (isYangGenerated(elemType)) {
431 String builderName = elemType.getName() + "Builder";
433 Class builderClass = Class.forName(builderName);
434 Object builderObj = builderClass.newInstance();
435 Method buildMethod = builderClass.getMethod("build");
436 builderObj = toBuilder(props, curBase, builderObj, true);
437 if (builderObj != null) {
438 //LOG.debug("Calling " + builderObj.getClass().getName()
439 // + "." + buildMethod.getName() + "()");
440 Object builtObj = buildMethod.invoke(builderObj);
445 } catch (ClassNotFoundException e) {
446 LOG.warn("Could not find builder class " + builderName, e);
447 } catch (Exception e) {
448 LOG.error("Caught exception trying to populate list from "
463 public static Object toBuilder(Properties props, String pfx, Object toObj) {
464 return(toBuilder(props, pfx, toObj, false));
467 public static Object toBuilder(Properties props, String pfx, Object toObj, boolean preservePfx) {
468 Class toClass = toObj.getClass();
469 boolean foundValue = false;
471 //LOG.debug("Saving properties to " + toClass.getName() + " class from "
476 if (isYangGenerated(toClass)) {
477 // Class is yang generated.
478 //LOG.debug(toClass.getName() + " is a Yang-generated class");
480 String propNamePfx = null;
485 if ((pfx != null) && (pfx.length() > 0)) {
486 propNamePfx = pfx + "."
487 + toLowerHyphen(toClass.getSimpleName());
489 propNamePfx = toLowerHyphen(toClass.getSimpleName());
492 if (propNamePfx.endsWith(BUILDER)) {
493 propNamePfx = propNamePfx.substring(0, propNamePfx.length()
497 if (propNamePfx.endsWith(IMPL)) {
498 propNamePfx = propNamePfx.substring(0, propNamePfx.length()
503 if (toObj instanceof Identifier) {
504 //LOG.debug(toClass.getName() + " is a Key - skipping");
508 // Iterate through getter methods to figure out values we need to
511 for (Method m : toClass.getMethods()) {
512 // LOG.debug("Is " + m.getName() + " method a setter?");
514 // LOG.debug(m.getName() + " is a setter");
515 Class paramTypes[] = m.getParameterTypes();
516 Class paramClass = paramTypes[0];
518 String fieldName = toLowerHyphen(m.getName().substring(3));
519 fieldName = fieldName.substring(0, 1).toLowerCase()
520 + fieldName.substring(1);
522 String propName = propNamePfx + "." + fieldName;
524 String paramValue = props.getProperty(propName);
525 if (paramValue == null) {
526 //LOG.debug(propName + " is unset");
528 //LOG.debug(propName + " = " + paramValue);
531 // Is the return type a yang generated class?
532 if (isYangGenerated(paramClass)) {
534 if (paramClass.isEnum()) {
536 //LOG.debug(m.getName() + " expects an Enum");
537 // Param type is a typedef.
538 if (paramValue != null) {
539 Object paramObj = null;
542 paramObj = Enum.valueOf(paramClass,
543 toUpperCamelCase(paramValue));
544 } catch (Exception e) {
546 "Caught exception trying to convert field "
547 + propName + " to enum "
548 + paramClass.getName(), e);
552 boolean isAccessible = m.isAccessible();
554 m.setAccessible(true);
557 //LOG.debug("Calling "
558 // + toObj.getClass().getName() + "."
559 // + m.getName() + "(" + paramValue
561 m.invoke(toObj, paramObj);
564 m.setAccessible(isAccessible);
568 } catch (Exception e) {
570 "Caught exception trying to create Yang-generated enum expected by"
574 + "() from Properties entry",
580 String simpleName = paramClass.getSimpleName();
581 LOG.info("simpleName:" + simpleName);
583 if ("Ipv4Address".equals(simpleName)
584 || "Ipv6Address".equals(simpleName) || "Ipv4Prefix".equals(simpleName) || "Ipv6Prefix".equals(simpleName)) {
586 if (paramValue != null) {
587 if("Ipv4Address".equals(simpleName) || "Ipv6Address".equals(simpleName)){
589 IpAddress ipAddr = IpAddressBuilder
590 .getDefaultInstance(paramValue);
593 if ("Ipv4Address".equals(simpleName))
595 m.invoke(toObj, ipAddr.getIpv4Address());
599 m.invoke(toObj, ipAddr.getIpv6Address());
603 } catch (Exception e) {
605 "Caught exception calling "
606 + toClass.getName() + "."
608 + paramValue + ")", e);
611 }else if("Ipv4Prefix".equals(simpleName)|| "Ipv6Prefix".equals(simpleName)){
613 IpPrefix ipPrefix = IpPrefixBuilder
614 .getDefaultInstance(paramValue);
617 if ("Ipv4Prefix".equals(simpleName))
619 m.invoke(toObj, ipPrefix.getIpv4Prefix());
623 m.invoke(toObj, ipPrefix.getIpv6Prefix());
627 } catch (Exception e) {
629 "Caught exception calling "
630 + toClass.getName() + "."
632 + paramValue + ")", e);
639 // setter expects a yang-generated class. Need
641 // create a builder to set it.
643 String builderName = paramClass.getName()
645 Class builderClass = null;
646 Object builderObj = null;
647 Object paramObj = null;
649 //LOG.debug(m.getName()
650 // + " expects a yang-generated class - looking for builder "
653 builderClass = Class.forName(builderName);
654 builderObj = builderClass.newInstance();
655 paramObj = toBuilder(props, propNamePfx,
657 } catch (ClassNotFoundException e) {
658 Object constObj = null;
660 // See if I can find a constructor I can
662 Constructor[] constructors = paramClass
664 // Is there a String constructor?
665 for (Constructor c : constructors) {
667 .getParameterTypes();
669 && (cParms.length == 1)) {
671 .isAssignableFrom(cParms[0])) {
673 .newInstance(paramValue);
678 if (constObj == null) {
679 // Is there a Long constructor?
680 for (Constructor c : constructors) {
682 .getParameterTypes();
684 && (cParms.length == 1)) {
686 .isAssignableFrom(cParms[0])) {
689 .parseLong(paramValue));
696 if (constObj != null) {
698 m.invoke(toObj, constObj);
700 } catch (Exception e2) {
702 "Caught exception trying to call "
707 } catch (Exception e1) {
709 "Could not find a suitable constructor for "
710 + paramClass.getName(),
714 if (paramObj == null) {
715 LOG.warn("Could not find builder class "
717 + " and could not find a String or Long constructor - trying just to set passing paramValue");
721 } catch (Exception e) {
723 "Caught exception trying to create builder "
727 if (paramObj != null) {
731 Method buildMethod = builderClass
733 //LOG.debug("Calling "
734 // + paramObj.getClass().getName()
735 // + "." + buildMethod.getName()
737 Object builtObj = buildMethod
740 boolean isAccessible = m.isAccessible();
742 m.setAccessible(true);
745 //LOG.debug("Calling "
746 // + toObj.getClass().getName()
747 // + "." + m.getName() + "()");
748 m.invoke(toObj, builtObj);
750 m.setAccessible(isAccessible);
754 } catch (Exception e) {
756 "Caught exception trying to set Yang-generated class expected by"
760 + "() from Properties entry",
765 boolean isAccessible = m.isAccessible();
767 m.setAccessible(true);
769 //LOG.debug("Calling "
770 // + toObj.getClass().getName()
771 // + "." + m.getName() + "("
772 // + paramValue + ")");
773 m.invoke(toObj, paramValue);
775 m.setAccessible(isAccessible);
779 } catch (Exception e) {
781 "Caught exception trying to convert value returned by"
785 + TO_PROPERTIES_STRING,
793 // Setter's argument is not a yang-generated class. See
796 if (List.class.isAssignableFrom(paramClass)) {
798 //LOG.debug("Parameter class " + paramClass.getName()
801 // Figure out what type of args are in List and pass
804 Type paramType = m.getGenericParameterTypes()[0];
805 Type elementType = ((ParameterizedType) paramType)
806 .getActualTypeArguments()[0];
807 Object paramObj = new LinkedList();
809 paramObj = toList(props, propName,
810 (List) paramObj, (Class) elementType);
811 } catch (Exception e) {
812 LOG.error("Caught exception trying to create list expected as argument to "
813 + toClass.getName() + "." + m.getName());
816 if (paramObj != null) {
818 boolean isAccessible = m.isAccessible();
820 m.setAccessible(true);
822 //LOG.debug("Calling "
823 // + toObj.getClass().getName() + "."
824 // + m.getName() + "(" + paramValue
826 m.invoke(toObj, paramObj);
828 m.setAccessible(isAccessible);
832 } catch (Exception e) {
834 "Caught exception trying to convert List returned by"
835 + toClass.getName() + "."
837 + TO_PROPERTIES_STRING,
843 // Setter expects something that is not a List and
844 // not yang-generated. Just pass the parameter value
846 //LOG.debug("Parameter class "
847 // + paramClass.getName()
848 // + " is not a yang-generated class or a List");
850 if (paramValue != null) {
852 Object constObj = null;
855 // See if I can find a constructor I can use
856 Constructor[] constructors = paramClass
858 // Is there a String constructor?
859 for (Constructor c : constructors) {
860 Class[] cParms = c.getParameterTypes();
862 && (cParms.length == 1)) {
864 .isAssignableFrom(cParms[0])) {
866 .newInstance(paramValue);
871 if (constObj == null) {
872 // Is there a Long constructor?
873 for (Constructor c : constructors) {
875 .getParameterTypes();
877 && (cParms.length == 1)) {
879 .isAssignableFrom(cParms[0])) {
882 .parseLong(paramValue));
889 if (constObj != null) {
891 //LOG.debug("Calling "
892 // + toObj.getClass()
894 // + m.getName() + "("
895 // + constObj + ")");
896 m.invoke(toObj, constObj);
898 } catch (Exception e2) {
900 "Caught exception trying to call "
905 boolean isAccessible = m
908 m.setAccessible(true);
910 //LOG.debug("Calling "
911 // + toObj.getClass()
913 // + m.getName() + "("
914 // + paramValue + ")");
915 m.invoke(toObj, paramValue);
917 m.setAccessible(isAccessible);
921 } catch (Exception e) {
923 "Caught exception trying to convert value returned by"
927 + TO_PROPERTIES_STRING,
931 } catch (Exception e1) {
933 "Could not find a suitable constructor for "
934 + paramClass.getName(), e1);
938 * try { boolean isAccessible =
939 * m.isAccessible(); if (!isAccessible) {
940 * m.setAccessible(true); } LOG.debug("Calling "
941 * + toObj.getClass().getName() + "." +
942 * m.getName() + "(" + paramValue + ")");
943 * m.invoke(toObj, paramValue); if
945 * m.setAccessible(isAccessible); } foundValue =
948 * } catch (Exception e) { LOG.error(
949 * "Caught exception trying to convert value returned by"
950 * + toClass.getName() + "." + m.getName() +
951 * "() to Properties entry", e); }
956 } // End of section handling "setter" method
957 } // End of loop through Methods
958 } // End of section handling yang-generated class
967 public static Properties getProperties(PrintStream pstr, String pfx,
969 boolean foundValue = false;
971 //LOG.debug("Analyzing " + toClass.getName() + " class : pfx " + pfx);
973 if (isYangGenerated(toClass)
974 && (!Identifier.class.isAssignableFrom(toClass))) {
975 // Class is yang generated.
976 //LOG.debug(toClass.getName() + " is a Yang-generated class");
978 if (toClass.getName().endsWith("Key")) {
979 if (Identifier.class.isAssignableFrom(toClass)) {
980 //LOG.debug(Identifier.class.getName()
981 // + " is assignable from " + toClass.getName());
984 //LOG.debug(Identifier.class.getName()
985 // + " is NOT assignable from " + toClass.getName());
989 String propNamePfx = null;
990 if (pfx.endsWith("]")) {
992 }else if(pfx.indexOf(".CLASS_FOUND") != -1){
993 pfx = pfx.replace(".CLASS_FOUND","");
994 propNamePfx = pfx + "."
995 + toLowerHyphen(toClass.getSimpleName());
998 if ((pfx != null) && (pfx.length() > 0)) {
999 propNamePfx = pfx + "."
1000 + toLowerHyphen(toClass.getSimpleName());
1002 propNamePfx = toLowerHyphen(toClass.getSimpleName());
1005 if (propNamePfx.endsWith(BUILDER)) {
1006 propNamePfx = propNamePfx.substring(0, propNamePfx.length()
1007 - BUILDER.length());
1010 if (propNamePfx.endsWith(IMPL)) {
1011 propNamePfx = propNamePfx.substring(0, propNamePfx.length()
1016 // Iterate through getter methods to figure out values we need to
1019 for (Method m : toClass.getMethods()) {
1020 //LOG.debug("Is " + m.getName() + " method a getter?");
1022 // LOG.debug(m.getName() + " is a getter");
1023 Class returnClass = m.getReturnType();
1025 String fieldName = toLowerHyphen(m.getName().substring(3));
1026 if(m != null && m.getName().matches("^is[A-Z].*")){
1027 fieldName = toLowerHyphen(m.getName().substring(2));
1029 fieldName = fieldName.substring(0, 1).toLowerCase()
1030 + fieldName.substring(1);
1032 String propName = propNamePfx + "." + fieldName;
1033 //System.out.println("****" + propName);
1035 // Is the return type a yang generated class?
1036 if (isYangGenerated(returnClass)) {
1038 if (returnClass.isEnum()) {
1040 //LOG.debug(m.getName() + " is an Enum");
1041 //pstr.print("\n" + propName);
1042 //pstr.print("\n" + propName + ":Enum:" + Arrays.asList(returnClass.getEnumConstants()) + "\n");
1043 pstr.print("\"" + propName + ":Enum:" + Arrays.asList(returnClass.getEnumConstants()) + "\",");
1044 prop.setProperty(propName,"");
1045 propList.add(propName);
1049 String simpleName = returnClass.getSimpleName();
1050 //System.out.println("simpleName:" + simpleName);
1052 if ("Ipv4Address".equals(simpleName) || "Ipv6Address".equals(simpleName) || "IpAddress".equals(simpleName) || "Ipv4Prefix".equals(simpleName) || "Ipv6Prefix".equals(simpleName) || "IpPrefix".equals(simpleName)) {
1053 //LOG.debug(m.getName()+" is an "+simpleName);
1054 //pstr.print("\n" + propName);
1055 //pstr.print("\n" + propName + ":" + simpleName + "\n");
1056 pstr.print("\"" + propName + ":" + simpleName + "\",");
1057 prop.setProperty(propName,"");
1058 propList.add(propName);
1060 boolean isString = false;
1061 boolean isNumber = false;
1062 boolean isBoolean = false;
1063 boolean isIdentifier = false;
1064 //System.out.println("simpleName:" + simpleName);
1065 //System.out.println("propName:" + propName);
1066 for(Method mthd : returnClass.getMethods()){
1067 String methodName = mthd.getName();
1068 //System.out.println("methodName:" + methodName);
1069 if(methodName.equals("getValue")){
1070 Class retType = mthd.getReturnType();
1071 //System.out.println("retType:" + retType);
1072 isString = String.class.isAssignableFrom(retType);
1073 isNumber = Number.class.isAssignableFrom(retType);
1074 isBoolean = Boolean.class.isAssignableFrom(retType);
1075 isIdentifier = Identifier.class.isAssignableFrom(retType);
1076 //System.out.println("isString:" + isString);
1077 //System.out.println("isNumber:" + isNumber);
1078 //System.out.println("isNumber:" + isNumber);
1084 pstr.print("\"" + propName + ":String\",");
1085 prop.setProperty(propName,"");
1086 propList.add(propName);
1088 pstr.print("\"" + propName + ":Number\",");
1089 prop.setProperty(propName,"");
1090 propList.add(propName);
1091 }else if(isBoolean){
1092 pstr.print("\"" + propName + ":Boolean\",");
1093 prop.setProperty(propName,"");
1094 propList.add(propName);
1095 }else if(isIdentifier){
1096 //System.out.println("isIdentifier");
1097 //isIdentifer so skipping
1101 System.out.println("fieldName:" + fieldName);
1102 System.out.println("simpleName:" + simpleName);
1103 System.out.println("returnClass:" + returnClass);
1104 System.out.println("pstr:" + pstr);
1105 System.out.println("propNamePfx:" + propNamePfx);
1107 getProperties(pstr, propNamePfx + ".CLASS_FOUND", returnClass);
1114 // Setter's argument is not a yang-generated class. See
1117 if (List.class.isAssignableFrom(returnClass)) {
1119 //LOG.debug("Parameter class "
1120 // + returnClass.getName() + " is a List");
1122 // Figure out what type of args are in List and pass
1123 // that to toList().
1125 Type returnType = m.getGenericReturnType();
1126 Type elementType = ((ParameterizedType) returnType)
1127 .getActualTypeArguments()[0];
1128 Class elementClass = (Class) elementType;
1129 //LOG.debug("Calling printPropertyList on list type ("
1130 //+ elementClass.getName()
1133 // + "), toClass is ("
1134 // + toClass.getName() + ")");
1135 //System.out.println("List propNamePfx:" + propNamePfx+ "." + toLowerHyphen(elementClass.getSimpleName()) + "[]");
1136 if(String.class.isAssignableFrom(elementClass)){
1137 pstr.print("\"" + propName + ":[String,String,...]\",");
1138 prop.setProperty(propName,"");
1139 propList.add(propName);
1140 }else if(Number.class.isAssignableFrom(elementClass)){
1141 pstr.print("\"" + propName + ":[Number,Number,...]\",");
1142 prop.setProperty(propName,"");
1143 propList.add(propName);
1144 }else if(Boolean.class.isAssignableFrom(elementClass)){
1145 pstr.print("\"" + propName + ":[Boolean,Boolean,...]\",");
1146 prop.setProperty(propName,"");
1147 propList.add(propName);
1148 }else if(Identifier.class.isAssignableFrom(elementClass)){
1155 + toLowerHyphen(elementClass
1156 .getSimpleName()) + "[]",
1160 } else if (!returnClass.equals(Class.class)) {
1162 // Setter expects something that is not a List and
1163 // not yang-generated. Just pass the parameter value
1165 //LOG.debug("Parameter class "
1166 // + returnClass.getName()
1167 // + " is not a yang-generated class or a List");
1169 //pstr.print("\n" + propName);
1170 String className=returnClass.getName();
1171 int nClassNameIndex = className.lastIndexOf('.');
1172 String nClassName = className;
1173 if(nClassNameIndex != -1){
1174 nClassName=className.substring(nClassNameIndex+1);
1176 boolean isString = String.class.isAssignableFrom(returnClass);
1177 boolean isNumber = Number.class.isAssignableFrom(returnClass);
1178 boolean isBoolean = Boolean.class.isAssignableFrom(returnClass);
1179 //pstr.print("\n" + propName +":" + nClassName +"\n");
1180 boolean isIdentifier = Identifier.class.isAssignableFrom(returnClass);
1181 if(!isIdentifier && !nClassName.equals("[C")){
1183 pstr.print("\"" + propName +":Number\",");
1184 }else if(isBoolean){
1185 pstr.print("\"" + propName +":Boolean\",");
1187 if(nClassName.equals("[B")){
1188 pstr.print("\"" + propName +":Binary\",");
1190 pstr.print("\"" + propName +":" + nClassName +"\",");
1193 prop.setProperty(propName,"");
1194 propList.add(propName);
1199 } // End of section handling "setter" method
1200 } // End of loop through Methods
1201 } // End of section handling yang-generated class
1206 public static boolean isYangGenerated(Class c) {
1210 //System.out.println(c.getName());
1211 return (c.getName().startsWith("org.opendaylight.yang.gen."));
1215 public static boolean isIpv4Address(Class c) {
1220 String simpleName = c.getSimpleName();
1221 return ("Ipv4Address".equals(simpleName)) ;
1224 public static boolean isIpv6Address(Class c) {
1229 String simpleName = c.getSimpleName();
1230 return ("Ipv6Address".equals(simpleName)) ;
1232 public static boolean isIpv4Prefix(Class c) {
1237 String simpleName = c.getSimpleName();
1238 //System.out.println("simpleName:" + simpleName);
1239 return ("Ipv4Prefix".equals(simpleName)) ;
1242 public static boolean isIpv6Prefix(Class c) {
1247 String simpleName = c.getSimpleName();
1248 //System.out.println("simpleName:" + simpleName);
1249 return ("Ipv6Prefix".equals(simpleName)) ;
1252 public static String toLowerHyphen(String inStr) {
1253 if (inStr == null) {
1257 String str = inStr.substring(0, 1).toLowerCase();
1258 if (inStr.length() > 1) {
1259 str = str + inStr.substring(1);
1262 String regex = "(([a-z0-9])([A-Z]))";
1263 String replacement = "$2-$3";
1265 String retval = str.replaceAll(regex, replacement).toLowerCase();
1267 //LOG.debug("Converting " + inStr + " => " + str + " => " + retval);
1271 public static String toUpperCamelCase(String inStr) {
1272 if (inStr == null) {
1276 String[] terms = inStr.split("-");
1277 StringBuffer sbuff = new StringBuffer();
1278 // Check if string begins with a digit
1279 if (Character.isDigit(inStr.charAt(0))) {
1282 for (String term : terms) {
1283 sbuff.append(term.substring(0, 1).toUpperCase());
1284 if (term.length() > 1) {
1285 sbuff.append(term.substring(1));
1288 return (sbuff.toString());
1292 public static boolean isGetter(Method m) {
1293 //System.out.println(m);
1298 if (Modifier.isPublic(m.getModifiers())
1299 && (m.getParameterTypes().length == 0)) {
1300 if ((m.getName().matches("^is[A-Z].*") || m.getName().matches("^get[A-Z].*"))
1301 && m.getReturnType().equals(Boolean.class)) {
1304 if (m.getName().matches("^get[A-Z].*")
1305 && !m.getReturnType().equals(void.class)) {
1314 public static boolean isSetter(Method m) {
1319 if (Modifier.isPublic(m.getModifiers())
1320 && (m.getParameterTypes().length == 1)) {
1321 if (m.getName().matches("^set[A-Z].*")) {
1322 Class[] paramTypes = m.getParameterTypes();
1323 if (paramTypes[0].isAssignableFrom(Identifier.class)
1324 || Identifier.class.isAssignableFrom(paramTypes[0])) {
1336 public static void main(String[] args){
1338 try(PrintStream ps = new PrintStream(new FileOutputStream(FileDescriptor.out))){
1339 PrintYangToProp printYangToProp = new PrintYangToProp();
1340 String className = args[0];
1341 //ClassLoader classLoader = PrintYangToProp.class.getClassLoader();
1342 //Class aClass = classLoader.loadClass(className);
1343 Class cl = Class.forName(className);
1344 //printPropertyList(ps,"",cl);
1345 //JsonObject jsonObj = Json.createObjectBuilder().build();
1346 Properties p = getProperties(ps,"",cl);
1347 //System.out.println(p);
1349 }catch(Exception e){
1350 e.printStackTrace();