3 import java.io.FileInputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.PrintStream;
7 import java.io.FileDescriptor;
8 import java.lang.reflect.Constructor;
9 import java.lang.reflect.InvocationTargetException;
10 import java.lang.reflect.Method;
11 import java.lang.reflect.Modifier;
12 import java.lang.reflect.ParameterizedType;
13 import java.lang.reflect.Type;
14 import java.util.LinkedList;
15 import java.util.List;
16 import java.util.Properties;
17 import java.util.Arrays;
18 import java.util.ArrayList;
21 import org.opendaylight.yangtools.yang.binding.Identifier;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddressBuilder;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 import com.google.common.base.CaseFormat;
32 public class PrintYangToProp {
34 private static final Logger LOG = LoggerFactory.getLogger(PrintYangToProp.class);
35 public static final String PROPERTIES_FILE="/opt/bvc/controller/configuration/flowred.properties";
36 private static Properties properties;
40 // Trick class loader into loading builders. Some of
41 // these will be needed later by Reflection classes, but need
42 // to explicitly "new" them here to get class loader to load them.
44 ServiceInformationBuilder b2 = new ServiceInformationBuilder();
45 ServiceDataBuilder b3 = new ServiceDataBuilder();
46 SdncRequestHeaderBuilder b4 = new SdncRequestHeaderBuilder();
47 RequestInformationBuilder b6 = new RequestInformationBuilder();
49 FlowredGroupInformationDataBuilder b29 = new FlowredGroupInformationDataBuilder();
50 FlowredInformationDataBuilder b48 = new FlowredInformationDataBuilder();
52 OperStatusBuilder b41 = new OperStatusBuilder();
58 public static void loadProperties() {
61 File file = new File(PROPERTIES_FILE);
62 properties = new Properties();
63 InputStream input = null;
64 if (file.isFile() && file.canRead()) {
66 input = new FileInputStream(file);
67 properties.load(input);
68 LOG.info("Loaded properties from " + PROPERTIES_FILE );
69 } catch (Exception e) {
70 LOG.error("Failed to load properties " + PROPERTIES_FILE +"\n",e);
75 } catch (IOException e) {
76 LOG.error("Failed to close properties file " + PROPERTIES_FILE +"\n",e);
84 public static Properties toProperties(Properties props, Object fromObj) {
85 Class fromClass = null;
89 fromClass = fromObj.getClass();
91 return (toProperties(props, "", fromObj, fromClass));
94 public static Properties toProperties(Properties props, String pfx, Object fromObj)
96 Class fromClass = null;
100 fromClass = fromObj.getClass();
103 return(toProperties(props, pfx, fromObj, fromClass));
106 public static Properties toProperties(Properties props, String pfx,
107 Object fromObj, Class fromClass) {
109 if (fromObj == null) {
114 String simpleName = fromClass.getSimpleName();
116 //LOG.debug("Extracting properties from " + fromClass.getName()
118 if (fromObj instanceof List) {
120 // Class is a List. List should contain yang-generated classes.
121 //LOG.debug(fromClass.getName() + " is a List");
123 List fromList = (List) fromObj;
125 for (int i = 0; i < fromList.size(); i++) {
126 toProperties(props, pfx + "[" + i + "]", fromList.get(i), fromClass);
128 props.setProperty(pfx + "_length", "" + fromList.size());
130 } else if (isYangGenerated(fromClass)) {
131 // Class is yang generated.
132 //LOG.debug(fromClass.getName() + " is a Yang-generated class");
134 String propNamePfx = null;
136 // If called from a list (so prefix ends in ']'), don't
137 // add class name again
138 if (pfx.endsWith("]")) {
141 if ((pfx != null) && (pfx.length() > 0)) {
144 propNamePfx = toLowerHyphen(fromClass.getSimpleName());
147 if (propNamePfx.endsWith("-builder")) {
148 propNamePfx = propNamePfx.substring(0, propNamePfx.length()
149 - "-builder".length());
152 if (propNamePfx.endsWith("-impl")) {
153 propNamePfx = propNamePfx.substring(0, propNamePfx.length()
158 // Iterate through getter methods to figure out values we need to
161 for (Method m : fromClass.getMethods()) {
162 // LOG.debug("Checking " + m.getName() + " method");
164 // LOG.debug(m.getName() + " is a getter");
165 Class returnType = m.getReturnType();
166 String fieldName = toLowerHyphen(m.getName().substring(3));
167 if(m != null && m.getName().matches("^is[A-Z].*")){
168 fieldName = toLowerHyphen(m.getName().substring(2));
171 fieldName = fieldName.substring(0, 1).toLowerCase()
172 + fieldName.substring(1);
174 // Is the return type a yang generated class?
175 if (isYangGenerated(returnType)) {
177 if (returnType.isEnum()) {
178 // Return type is a typedef. Save its value.
180 boolean isAccessible = m.isAccessible();
182 m.setAccessible(true);
185 Object retValue = m.invoke(fromObj);
188 m.setAccessible(isAccessible);
190 if (retValue != null) {
191 String propName = propNamePfx + "."
193 String propVal = retValue.toString();
194 String yangProp = "yang." + fieldName + "." + propVal;
195 if ( properties.containsKey(yangProp)) {
196 propVal = properties.getProperty(yangProp);
197 //LOG.debug("Adjusting property " + yangProp + " " + propVal);
199 //LOG.debug("Setting property " + propName
200 // + " to " + propVal);
201 props.setProperty(propName, propVal);
203 } catch (Exception e) {
205 "Caught exception trying to convert Yang-generated enum returned by "
206 + fromClass.getName() + "."
208 + "() to Properties entry", e);
210 } else if (isIpv4Address(returnType)) {
213 String propName = propNamePfx + "." + fieldName;
214 boolean isAccessible = m.isAccessible();
216 m.setAccessible(true);
218 Ipv4Address retValue = (Ipv4Address) m.invoke(fromObj);
220 m.setAccessible(isAccessible);
223 if (retValue != null) {
224 String propVal = retValue.getValue().toString();
225 //LOG.debug("Setting property " + propName
226 // + " to " + propVal);
227 props.setProperty(propName, propVal);
230 } catch (Exception e) {
232 "Caught exception trying to convert value returned by "
233 + fromClass.getName() + "."
235 + "() to Properties entry", e);
237 } else if (isIpv6Address(returnType)) {
240 String propName = propNamePfx + "." + fieldName;
241 boolean isAccessible = m.isAccessible();
243 m.setAccessible(true);
245 Ipv6Address retValue = (Ipv6Address) m.invoke(fromObj);
247 m.setAccessible(isAccessible);
250 if (retValue != null) {
251 String propVal = retValue.getValue().toString();
252 //LOG.debug("Setting property " + propName
253 // + " to " + propVal);
254 props.setProperty(propName, propVal);
257 } catch (Exception e) {
259 "Caught exception trying to convert value returned by "
260 + fromClass.getName() + "."
262 + "() to Properties entry", e);
266 boolean isAccessible = m.isAccessible();
268 m.setAccessible(true);
270 Object retValue = m.invoke(fromObj);
272 m.setAccessible(isAccessible);
274 if (retValue != null) {
275 toProperties(props, propNamePfx + "." + fieldName, retValue, returnType);
277 } catch (Exception e) {
279 "Caught exception trying to convert Yang-generated class returned by"
280 + fromClass.getName() + "."
282 + "() to Properties entry", e);
285 } else if (returnType.equals(Class.class)) {
287 //LOG.debug(m.getName()
288 // + " returns a Class object - not interested");
290 } else if (List.class.isAssignableFrom(returnType)) {
292 // This getter method returns a list.
294 boolean isAccessible = m.isAccessible();
296 m.setAccessible(true);
298 Object retList = m.invoke(fromObj);
300 m.setAccessible(isAccessible);
302 // Figure out what type of elements are stored in this array.
303 Type paramType = m.getGenericReturnType();
304 Type elementType = ((ParameterizedType) paramType)
305 .getActualTypeArguments()[0];
306 toProperties(props, propNamePfx + "." + fieldName,
307 retList, (Class)elementType);
308 } catch (Exception e) {
310 "Caught exception trying to convert List returned by "
311 + fromClass.getName() + "."
313 + "() to Properties entry", e);
318 // Method returns something that is not a List and not
322 String propName = propNamePfx + "." + fieldName;
323 boolean isAccessible = m.isAccessible();
325 m.setAccessible(true);
327 Object propValObj = m.invoke(fromObj);
329 m.setAccessible(isAccessible);
332 if (propValObj != null) {
333 String propVal = propValObj.toString();
334 //LOG.debug("Setting property " + propName
335 // + " to " + propVal);
336 props.setProperty(propName, propVal);
339 } catch (Exception e) {
341 "Caught exception trying to convert value returned by "
342 + fromClass.getName() + "."
344 + "() to Properties entry", e);
352 // Class is not yang generated and not a list
360 public static Object toBuilder(Properties props, Object toObj) {
362 return (toBuilder(props, "", toObj));
365 public static List toList(Properties props, String pfx, List toObj,
369 boolean foundValue = false;
371 //LOG.debug("Saving properties to List<" + elemType.getName()
372 // + "> from " + pfx);
374 // Figure out array size
375 for (Object pNameObj : props.keySet()) {
376 String key = (String) pNameObj;
378 if (key.startsWith(pfx + "[")) {
379 String idxStr = key.substring(pfx.length() + 1);
380 int endloc = idxStr.indexOf("]");
382 idxStr = idxStr.substring(0, endloc);
386 int curIdx = Integer.parseInt(idxStr);
387 if (curIdx > maxIdx) {
390 } catch (Exception e) {
391 LOG.error("Illegal subscript in property " + key);
397 //LOG.debug(pfx + " has max index of " + maxIdx);
398 for (int i = 0; i <= maxIdx; i++) {
400 String curBase = pfx + "[" + i + "]";
402 if (isYangGenerated(elemType)) {
403 String builderName = elemType.getName() + "Builder";
405 Class builderClass = Class.forName(builderName);
406 Object builderObj = builderClass.newInstance();
407 Method buildMethod = builderClass.getMethod("build");
408 builderObj = toBuilder(props, curBase, builderObj, true);
409 if (builderObj != null) {
410 //LOG.debug("Calling " + builderObj.getClass().getName()
411 // + "." + buildMethod.getName() + "()");
412 Object builtObj = buildMethod.invoke(builderObj);
417 } catch (ClassNotFoundException e) {
418 LOG.warn("Could not find builder class " + builderName, e);
419 } catch (Exception e) {
420 LOG.error("Caught exception trying to populate list from "
435 public static Object toBuilder(Properties props, String pfx, Object toObj) {
436 return(toBuilder(props, pfx, toObj, false));
439 public static Object toBuilder(Properties props, String pfx, Object toObj, boolean preservePfx) {
440 Class toClass = toObj.getClass();
441 boolean foundValue = false;
443 //LOG.debug("Saving properties to " + toClass.getName() + " class from "
448 if (isYangGenerated(toClass)) {
449 // Class is yang generated.
450 //LOG.debug(toClass.getName() + " is a Yang-generated class");
452 String propNamePfx = null;
457 if ((pfx != null) && (pfx.length() > 0)) {
458 propNamePfx = pfx + "."
459 + toLowerHyphen(toClass.getSimpleName());
461 propNamePfx = toLowerHyphen(toClass.getSimpleName());
464 if (propNamePfx.endsWith("-builder")) {
465 propNamePfx = propNamePfx.substring(0, propNamePfx.length()
466 - "-builder".length());
469 if (propNamePfx.endsWith("-impl")) {
470 propNamePfx = propNamePfx.substring(0, propNamePfx.length()
475 if (toObj instanceof Identifier) {
476 //LOG.debug(toClass.getName() + " is a Key - skipping");
480 // Iterate through getter methods to figure out values we need to
483 for (Method m : toClass.getMethods()) {
484 // LOG.debug("Is " + m.getName() + " method a setter?");
486 // LOG.debug(m.getName() + " is a setter");
487 Class paramTypes[] = m.getParameterTypes();
488 Class paramClass = paramTypes[0];
490 String fieldName = toLowerHyphen(m.getName().substring(3));
491 fieldName = fieldName.substring(0, 1).toLowerCase()
492 + fieldName.substring(1);
494 String propName = propNamePfx + "." + fieldName;
496 String paramValue = props.getProperty(propName);
497 if (paramValue == null) {
498 //LOG.debug(propName + " is unset");
500 //LOG.debug(propName + " = " + paramValue);
503 // Is the return type a yang generated class?
504 if (isYangGenerated(paramClass)) {
506 if (paramClass.isEnum()) {
508 //LOG.debug(m.getName() + " expects an Enum");
509 // Param type is a typedef.
510 if (paramValue != null) {
511 Object paramObj = null;
514 paramObj = Enum.valueOf(paramClass,
515 toUpperCamelCase(paramValue));
516 } catch (Exception e) {
518 "Caught exception trying to convert field "
519 + propName + " to enum "
520 + paramClass.getName(), e);
524 boolean isAccessible = m.isAccessible();
526 m.setAccessible(true);
529 //LOG.debug("Calling "
530 // + toObj.getClass().getName() + "."
531 // + m.getName() + "(" + paramValue
533 m.invoke(toObj, paramObj);
536 m.setAccessible(isAccessible);
540 } catch (Exception e) {
542 "Caught exception trying to create Yang-generated enum expected by"
546 + "() from Properties entry",
552 String simpleName = paramClass.getSimpleName();
554 if ("Ipv4Address".equals(simpleName)
555 || "Ipv6Address".equals(simpleName)) {
557 if (paramValue != null) {
559 IpAddress ipAddr = IpAddressBuilder
560 .getDefaultInstance(paramValue);
563 if ("Ipv4Address".equals(simpleName))
565 m.invoke(toObj, ipAddr.getIpv4Address());
569 m.invoke(toObj, ipAddr.getIpv6Address());
573 } catch (Exception e) {
575 "Caught exception calling "
576 + toClass.getName() + "."
578 + paramValue + ")", e);
584 // setter expects a yang-generated class. Need
586 // create a builder to set it.
588 String builderName = paramClass.getName()
590 Class builderClass = null;
591 Object builderObj = null;
592 Object paramObj = null;
594 //LOG.debug(m.getName()
595 // + " expects a yang-generated class - looking for builder "
598 builderClass = Class.forName(builderName);
599 builderObj = builderClass.newInstance();
600 paramObj = toBuilder(props, propNamePfx,
602 } catch (ClassNotFoundException e) {
603 Object constObj = null;
605 // See if I can find a constructor I can
607 Constructor[] constructors = paramClass
609 // Is there a String constructor?
610 for (Constructor c : constructors) {
612 .getParameterTypes();
614 && (cParms.length == 1)) {
616 .isAssignableFrom(cParms[0])) {
618 .newInstance(paramValue);
623 if (constObj == null) {
624 // Is there a Long constructor?
625 for (Constructor c : constructors) {
627 .getParameterTypes();
629 && (cParms.length == 1)) {
631 .isAssignableFrom(cParms[0])) {
634 .parseLong(paramValue));
641 if (constObj != null) {
643 m.invoke(toObj, constObj);
645 } catch (Exception e2) {
647 "Caught exception trying to call "
652 } catch (Exception e1) {
654 "Could not find a suitable constructor for "
655 + paramClass.getName(),
659 if (paramObj == null) {
660 LOG.warn("Could not find builder class "
662 + " and could not find a String or Long constructor - trying just to set passing paramValue");
666 } catch (Exception e) {
668 "Caught exception trying to create builder "
672 if (paramObj != null) {
676 Method buildMethod = builderClass
678 //LOG.debug("Calling "
679 // + paramObj.getClass().getName()
680 // + "." + buildMethod.getName()
682 Object builtObj = buildMethod
685 boolean isAccessible = m.isAccessible();
687 m.setAccessible(true);
690 //LOG.debug("Calling "
691 // + toObj.getClass().getName()
692 // + "." + m.getName() + "()");
693 m.invoke(toObj, builtObj);
695 m.setAccessible(isAccessible);
699 } catch (Exception e) {
701 "Caught exception trying to set Yang-generated class expected by"
705 + "() from Properties entry",
710 boolean isAccessible = m.isAccessible();
712 m.setAccessible(true);
714 //LOG.debug("Calling "
715 // + toObj.getClass().getName()
716 // + "." + m.getName() + "("
717 // + paramValue + ")");
718 m.invoke(toObj, paramValue);
720 m.setAccessible(isAccessible);
724 } catch (Exception e) {
726 "Caught exception trying to convert value returned by"
730 + "() to Properties entry",
738 // Setter's argument is not a yang-generated class. See
741 if (List.class.isAssignableFrom(paramClass)) {
743 //LOG.debug("Parameter class " + paramClass.getName()
746 // Figure out what type of args are in List and pass
749 Type paramType = m.getGenericParameterTypes()[0];
750 Type elementType = ((ParameterizedType) paramType)
751 .getActualTypeArguments()[0];
752 Object paramObj = new LinkedList();
754 paramObj = toList(props, propName,
755 (List) paramObj, (Class) elementType);
756 } catch (Exception e) {
757 LOG.error("Caught exception trying to create list expected as argument to "
758 + toClass.getName() + "." + m.getName());
761 if (paramObj != null) {
763 boolean isAccessible = m.isAccessible();
765 m.setAccessible(true);
767 //LOG.debug("Calling "
768 // + toObj.getClass().getName() + "."
769 // + m.getName() + "(" + paramValue
771 m.invoke(toObj, paramObj);
773 m.setAccessible(isAccessible);
777 } catch (Exception e) {
779 "Caught exception trying to convert List returned by"
780 + toClass.getName() + "."
782 + "() to Properties entry",
788 // Setter expects something that is not a List and
789 // not yang-generated. Just pass the parameter value
791 //LOG.debug("Parameter class "
792 // + paramClass.getName()
793 // + " is not a yang-generated class or a List");
795 if (paramValue != null) {
797 Object constObj = null;
800 // See if I can find a constructor I can use
801 Constructor[] constructors = paramClass
803 // Is there a String constructor?
804 for (Constructor c : constructors) {
805 Class[] cParms = c.getParameterTypes();
807 && (cParms.length == 1)) {
809 .isAssignableFrom(cParms[0])) {
811 .newInstance(paramValue);
816 if (constObj == null) {
817 // Is there a Long constructor?
818 for (Constructor c : constructors) {
820 .getParameterTypes();
822 && (cParms.length == 1)) {
824 .isAssignableFrom(cParms[0])) {
827 .parseLong(paramValue));
834 if (constObj != null) {
836 //LOG.debug("Calling "
837 // + toObj.getClass()
839 // + m.getName() + "("
840 // + constObj + ")");
841 m.invoke(toObj, constObj);
843 } catch (Exception e2) {
845 "Caught exception trying to call "
850 boolean isAccessible = m
853 m.setAccessible(true);
855 //LOG.debug("Calling "
856 // + toObj.getClass()
858 // + m.getName() + "("
859 // + paramValue + ")");
860 m.invoke(toObj, paramValue);
862 m.setAccessible(isAccessible);
866 } catch (Exception e) {
868 "Caught exception trying to convert value returned by"
872 + "() to Properties entry",
876 } catch (Exception e1) {
878 "Could not find a suitable constructor for "
879 + paramClass.getName(), e1);
883 * try { boolean isAccessible =
884 * m.isAccessible(); if (!isAccessible) {
885 * m.setAccessible(true); } LOG.debug("Calling "
886 * + toObj.getClass().getName() + "." +
887 * m.getName() + "(" + paramValue + ")");
888 * m.invoke(toObj, paramValue); if
890 * m.setAccessible(isAccessible); } foundValue =
893 * } catch (Exception e) { LOG.error(
894 * "Caught exception trying to convert value returned by"
895 * + toClass.getName() + "." + m.getName() +
896 * "() to Properties entry", e); }
901 } // End of section handling "setter" method
902 } // End of loop through Methods
903 } // End of section handling yang-generated class
913 public static void printPropertyList(PrintStream pstr, String pfx,
915 boolean foundValue = false;
917 //LOG.debug("Analyzing " + toClass.getName() + " class : pfx " + pfx);
919 if (isYangGenerated(toClass)
920 && (!Identifier.class.isAssignableFrom(toClass))) {
921 // Class is yang generated.
922 //LOG.debug(toClass.getName() + " is a Yang-generated class");
924 if (toClass.getName().endsWith("Key")) {
925 if (Identifier.class.isAssignableFrom(toClass)) {
926 //LOG.debug(Identifier.class.getName()
927 // + " is assignable from " + toClass.getName());
930 //LOG.debug(Identifier.class.getName()
931 // + " is NOT assignable from " + toClass.getName());
935 String propNamePfx = null;
936 if (pfx.endsWith("]")) {
940 if ((pfx != null) && (pfx.length() > 0)) {
941 propNamePfx = pfx + "."
942 + toLowerHyphen(toClass.getSimpleName());
944 propNamePfx = toLowerHyphen(toClass.getSimpleName());
947 if (propNamePfx.endsWith("-builder")) {
948 propNamePfx = propNamePfx.substring(0, propNamePfx.length()
949 - "-builder".length());
952 if (propNamePfx.endsWith("-impl")) {
953 propNamePfx = propNamePfx.substring(0, propNamePfx.length()
958 // Iterate through getter methods to figure out values we need to
961 for (Method m : toClass.getMethods()) {
962 //LOG.debug("Is " + m.getName() + " method a getter?");
964 //LOG.debug(m.getName() + " is a getter");
965 Class returnClass = m.getReturnType();
967 String fieldName = toLowerHyphen(m.getName().substring(3));
968 fieldName = fieldName.substring(0, 1).toLowerCase()
969 + fieldName.substring(1);
971 String propName = propNamePfx + "." + fieldName;
973 // Is the return type a yang generated class?
974 if (isYangGenerated(returnClass)) {
976 if (returnClass.isEnum()) {
977 //System.out.println(returnClass.getSimpleName());
978 //System.out.println(Arrays.asList(returnClass.getEnumConstants()));
980 //LOG.debug(m.getName() + " is an Enum");
981 pstr.print("\n" + propName + ":Enum:" + Arrays.asList(returnClass.getEnumConstants()) + "\n");
985 String simpleName = returnClass.getSimpleName();
987 if ("Ipv4Address".equals(simpleName) || "Ipv6Address".equals(simpleName)) {
988 //LOG.debug(m.getName()+" is an "+simpleName);
989 pstr.print("\n" + propName + ":" + simpleName + "\n");
991 printPropertyList(pstr, propNamePfx, returnClass);
997 // Setter's argument is not a yang-generated class. See
1000 if (List.class.isAssignableFrom(returnClass)) {
1002 //LOG.debug("Parameter class "
1003 //+ returnClass.getName() + " is a List");
1005 // Figure out what type of args are in List and pass
1006 // that to toList().
1008 Type returnType = m.getGenericReturnType();
1009 Type elementType = ((ParameterizedType) returnType)
1010 .getActualTypeArguments()[0];
1011 Class elementClass = (Class) elementType;
1012 //LOG.debug("Calling printPropertyList on list type ("
1013 //+ elementClass.getName()
1016 // + "), toClass is ("
1017 // + toClass.getName() + ")");
1022 + toLowerHyphen(elementClass
1023 .getSimpleName()) + "[]",
1026 } else if (!returnClass.equals(Class.class)) {
1028 // Setter expects something that is not a List and
1029 // not yang-generated. Just pass the parameter value
1031 //LOG.debug("Parameter class "
1032 // + returnClass.getName()
1033 // + " is not a yang-generated class or a List");
1034 String className=returnClass.getName();
1036 //"org.opendaylight.yangtools.yang.binding.Identifier"
1037 int nClassNameIndex = className.lastIndexOf('.');
1038 String nClassName = className;
1039 if(nClassNameIndex != -1){
1040 nClassName=className.substring(nClassNameIndex+1);
1042 pstr.print("\n" + propName +":" + nClassName +"\n");
1046 } // End of section handling "setter" method
1047 } // End of loop through Methods
1048 } // End of section handling yang-generated class
1053 public static Properties prop = new Properties();
1054 public static ArrayList<String> propList = new ArrayList<String>();
1055 public static Properties getProperties(PrintStream pstr, String pfx,
1057 boolean foundValue = false;
1059 //LOG.debug("Analyzing " + toClass.getName() + " class : pfx " + pfx);
1061 if (isYangGenerated(toClass)
1062 && (!Identifier.class.isAssignableFrom(toClass))) {
1063 // Class is yang generated.
1064 //LOG.debug(toClass.getName() + " is a Yang-generated class");
1066 if (toClass.getName().endsWith("Key")) {
1067 if (Identifier.class.isAssignableFrom(toClass)) {
1068 //LOG.debug(Identifier.class.getName()
1069 // + " is assignable from " + toClass.getName());
1072 //LOG.debug(Identifier.class.getName()
1073 // + " is NOT assignable from " + toClass.getName());
1077 String propNamePfx = null;
1078 if (pfx.endsWith("]")) {
1080 }else if(pfx.indexOf(".CLASS_FOUND") != -1){
1081 pfx = pfx.replace(".CLASS_FOUND","");
1082 propNamePfx = pfx + "."
1083 + toLowerHyphen(toClass.getSimpleName());
1086 if ((pfx != null) && (pfx.length() > 0)) {
1087 propNamePfx = pfx + "."
1088 + toLowerHyphen(toClass.getSimpleName());
1090 propNamePfx = toLowerHyphen(toClass.getSimpleName());
1093 if (propNamePfx.endsWith("-builder")) {
1094 propNamePfx = propNamePfx.substring(0, propNamePfx.length()
1095 - "-builder".length());
1098 if (propNamePfx.endsWith("-impl")) {
1099 propNamePfx = propNamePfx.substring(0, propNamePfx.length()
1100 - "-impl".length());
1104 // Iterate through getter methods to figure out values we need to
1107 for (Method m : toClass.getMethods()) {
1108 //LOG.debug("Is " + m.getName() + " method a getter?");
1110 // LOG.debug(m.getName() + " is a getter");
1111 Class returnClass = m.getReturnType();
1113 String fieldName = toLowerHyphen(m.getName().substring(3));
1114 if(m != null && m.getName().matches("^is[A-Z].*")){
1115 fieldName = toLowerHyphen(m.getName().substring(2));
1117 fieldName = fieldName.substring(0, 1).toLowerCase()
1118 + fieldName.substring(1);
1120 String propName = propNamePfx + "." + fieldName;
1121 //System.out.println("****" + propName);
1123 // Is the return type a yang generated class?
1124 if (isYangGenerated(returnClass)) {
1126 if (returnClass.isEnum()) {
1128 //LOG.debug(m.getName() + " is an Enum");
1129 //pstr.print("\n" + propName);
1130 //pstr.print("\n" + propName + ":Enum:" + Arrays.asList(returnClass.getEnumConstants()) + "\n");
1131 pstr.print("\"" + propName + ":Enum:" + Arrays.asList(returnClass.getEnumConstants()) + "\",");
1132 prop.setProperty(propName,"");
1133 propList.add(propName);
1137 String simpleName = returnClass.getSimpleName();
1138 //System.out.println("simpleName:" + simpleName);
1140 if ("Ipv4Address".equals(simpleName) || "Ipv6Address".equals(simpleName) || "IpAddress".equals(simpleName)) {
1141 //LOG.debug(m.getName()+" is an "+simpleName);
1142 //pstr.print("\n" + propName);
1143 //pstr.print("\n" + propName + ":" + simpleName + "\n");
1144 pstr.print("\"" + propName + ":" + simpleName + "\",");
1145 prop.setProperty(propName,"");
1146 propList.add(propName);
1148 boolean isString = false;
1149 boolean isNumber = false;
1150 boolean isBoolean = false;
1151 boolean isIdentifier = false;
1152 //System.out.println("simpleName:" + simpleName);
1153 //System.out.println("propName:" + propName);
1154 for(Method mthd : returnClass.getMethods()){
1155 String methodName = mthd.getName();
1156 //System.out.println("methodName:" + methodName);
1157 if(methodName.equals("getValue")){
1158 Class retType = mthd.getReturnType();
1159 //System.out.println("retType:" + retType);
1160 isString = String.class.isAssignableFrom(retType);
1161 isNumber = Number.class.isAssignableFrom(retType);
1162 isBoolean = Boolean.class.isAssignableFrom(retType);
1163 isIdentifier = Identifier.class.isAssignableFrom(retType);
1164 //System.out.println("isString:" + isString);
1165 //System.out.println("isNumber:" + isNumber);
1166 //System.out.println("isNumber:" + isNumber);
1172 pstr.print("\"" + propName + ":String\",");
1173 prop.setProperty(propName,"");
1174 propList.add(propName);
1176 pstr.print("\"" + propName + ":Number\",");
1177 prop.setProperty(propName,"");
1178 propList.add(propName);
1179 }else if(isBoolean){
1180 pstr.print("\"" + propName + ":Boolean\",");
1181 prop.setProperty(propName,"");
1182 propList.add(propName);
1183 }else if(isIdentifier){
1184 //System.out.println("isIdentifier");
1185 //isIdentifer so skipping
1189 System.out.println("fieldName:" + fieldName);
1190 System.out.println("simpleName:" + simpleName);
1191 System.out.println("returnClass:" + returnClass);
1192 System.out.println("pstr:" + pstr);
1193 System.out.println("propNamePfx:" + propNamePfx);
1195 getProperties(pstr, propNamePfx + ".CLASS_FOUND", returnClass);
1202 // Setter's argument is not a yang-generated class. See
1205 if (List.class.isAssignableFrom(returnClass)) {
1207 //LOG.debug("Parameter class "
1208 // + returnClass.getName() + " is a List");
1210 // Figure out what type of args are in List and pass
1211 // that to toList().
1213 Type returnType = m.getGenericReturnType();
1214 Type elementType = ((ParameterizedType) returnType)
1215 .getActualTypeArguments()[0];
1216 Class elementClass = (Class) elementType;
1217 //LOG.debug("Calling printPropertyList on list type ("
1218 //+ elementClass.getName()
1221 // + "), toClass is ("
1222 // + toClass.getName() + ")");
1223 //System.out.println("List propNamePfx:" + propNamePfx+ "." + toLowerHyphen(elementClass.getSimpleName()) + "[]");
1224 if(String.class.isAssignableFrom(elementClass)){
1225 pstr.print("\"" + propName + ":[String,String,...]\",");
1226 prop.setProperty(propName,"");
1227 propList.add(propName);
1228 }else if(Number.class.isAssignableFrom(elementClass)){
1229 pstr.print("\"" + propName + ":[Number,Number,...]\",");
1230 prop.setProperty(propName,"");
1231 propList.add(propName);
1232 }else if(Boolean.class.isAssignableFrom(elementClass)){
1233 pstr.print("\"" + propName + ":[Boolean,Boolean,...]\",");
1234 prop.setProperty(propName,"");
1235 propList.add(propName);
1236 }else if(Identifier.class.isAssignableFrom(elementClass)){
1243 + toLowerHyphen(elementClass
1244 .getSimpleName()) + "[]",
1248 } else if (!returnClass.equals(Class.class)) {
1250 // Setter expects something that is not a List and
1251 // not yang-generated. Just pass the parameter value
1253 //LOG.debug("Parameter class "
1254 // + returnClass.getName()
1255 // + " is not a yang-generated class or a List");
1257 //pstr.print("\n" + propName);
1258 String className=returnClass.getName();
1259 int nClassNameIndex = className.lastIndexOf('.');
1260 String nClassName = className;
1261 if(nClassNameIndex != -1){
1262 nClassName=className.substring(nClassNameIndex+1);
1264 boolean isString = String.class.isAssignableFrom(returnClass);
1265 boolean isNumber = Number.class.isAssignableFrom(returnClass);
1266 boolean isBoolean = Boolean.class.isAssignableFrom(returnClass);
1267 //pstr.print("\n" + propName +":" + nClassName +"\n");
1268 boolean isIdentifier = Identifier.class.isAssignableFrom(returnClass);
1269 if(!isIdentifier && !nClassName.equals("[C")){
1271 pstr.print("\"" + propName +":Number\",");
1272 }else if(isBoolean){
1273 pstr.print("\"" + propName +":Boolean\",");
1275 if(nClassName.equals("[B")){
1276 pstr.print("\"" + propName +":Binary\",");
1278 pstr.print("\"" + propName +":" + nClassName +"\",");
1281 prop.setProperty(propName,"");
1282 propList.add(propName);
1287 } // End of section handling "setter" method
1288 } // End of loop through Methods
1289 } // End of section handling yang-generated class
1294 public static boolean isYangGenerated(Class c) {
1298 //System.out.println(c.getName());
1299 return (c.getName().startsWith("org.opendaylight.yang.gen."));
1303 public static boolean isIpv4Address(Class c) {
1308 String simpleName = c.getSimpleName();
1309 return ("Ipv4Address".equals(simpleName)) ;
1312 public static boolean isIpv6Address(Class c) {
1317 String simpleName = c.getSimpleName();
1318 return ("Ipv6Address".equals(simpleName)) ;
1321 public static String toLowerHyphen(String inStr) {
1322 if (inStr == null) {
1326 String str = inStr.substring(0, 1).toLowerCase();
1327 if (inStr.length() > 1) {
1328 str = str + inStr.substring(1);
1331 String regex = "(([a-z0-9])([A-Z]))";
1332 String replacement = "$2-$3";
1334 String retval = str.replaceAll(regex, replacement).toLowerCase();
1336 //LOG.debug("Converting " + inStr + " => " + str + " => " + retval);
1340 public static String toUpperCamelCase(String inStr) {
1341 if (inStr == null) {
1345 String[] terms = inStr.split("-");
1346 StringBuffer sbuff = new StringBuffer();
1347 // Check if string begins with a digit
1348 if (Character.isDigit(inStr.charAt(0))) {
1351 for (String term : terms) {
1352 sbuff.append(term.substring(0, 1).toUpperCase());
1353 if (term.length() > 1) {
1354 sbuff.append(term.substring(1));
1357 return (sbuff.toString());
1361 public static boolean isGetter(Method m) {
1362 //System.out.println(m);
1367 if (Modifier.isPublic(m.getModifiers())
1368 && (m.getParameterTypes().length == 0)) {
1369 if ((m.getName().matches("^is[A-Z].*") || m.getName().matches("^get[A-Z].*"))
1370 && m.getReturnType().equals(Boolean.class)) {
1373 if (m.getName().matches("^get[A-Z].*")
1374 && !m.getReturnType().equals(void.class)) {
1383 public static boolean isSetter(Method m) {
1388 if (Modifier.isPublic(m.getModifiers())
1389 && (m.getParameterTypes().length == 1)) {
1390 if (m.getName().matches("^set[A-Z].*")) {
1391 Class[] paramTypes = m.getParameterTypes();
1392 if (paramTypes[0].isAssignableFrom(Identifier.class)
1393 || Identifier.class.isAssignableFrom(paramTypes[0])) {
1405 public static void main(String[] args){
1407 PrintStream ps = new PrintStream(new FileOutputStream(FileDescriptor.out));
1408 PrintYangToProp printYangToProp = new PrintYangToProp();
1409 String className = args[0];
1410 //ClassLoader classLoader = PrintYangToProp.class.getClassLoader();
1411 //Class aClass = classLoader.loadClass(className);
1412 Class cl = Class.forName(className);
1413 //printPropertyList(ps,"",cl);
1414 //JsonObject jsonObj = Json.createObjectBuilder().build();
1415 Properties p = getProperties(ps,"",cl);
1416 //System.out.println(p);
1418 }catch(Exception e){
1419 e.printStackTrace();