2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.ccsdk.sli.core.sli;
25 import java.io.FileInputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.PrintStream;
29 import java.io.FileDescriptor;
30 import java.lang.reflect.Constructor;
31 import java.lang.reflect.InvocationTargetException;
32 import java.lang.reflect.Method;
33 import java.lang.reflect.Modifier;
34 import java.lang.reflect.ParameterizedType;
35 import java.lang.reflect.Type;
36 import java.util.LinkedList;
37 import java.util.List;
38 import java.util.Properties;
39 import java.util.Arrays;
40 import java.util.ArrayList;
42 import org.opendaylight.yangtools.yang.binding.Identifier;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddressBuilder;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefixBuilder;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
55 import com.google.common.base.CaseFormat;
57 public class PrintYangToProp {
59 private static final Logger LOG = LoggerFactory.getLogger(PrintYangToProp.class);
60 public static final String PROPERTIES_FILE="";
61 private static Properties properties;
63 public static Properties prop = new Properties();
64 public static ArrayList<String> propList = new ArrayList<String>();
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 //LOG.debug("Extracting properties from " + fromClass.getName()
101 if (fromObj instanceof List) {
103 // Class is a List. List should contain yang-generated classes.
104 //LOG.debug(fromClass.getName() + " is a List");
106 List fromList = (List) fromObj;
108 for (int i = 0; i < fromList.size(); i++) {
109 toProperties(props, pfx + "[" + i + "]", fromList.get(i), fromClass);
111 props.setProperty(pfx + "_length", "" + fromList.size());
113 } else if (isYangGenerated(fromClass)) {
114 // Class is yang generated.
115 //LOG.debug(fromClass.getName() + " is a Yang-generated class");
117 String propNamePfx = null;
119 // If called from a list (so prefix ends in ']'), don't
120 // add class name again
121 if (pfx.endsWith("]")) {
124 if ((pfx != null) && (pfx.length() > 0)) {
127 propNamePfx = toLowerHyphen(fromClass.getSimpleName());
130 if (propNamePfx.endsWith("-builder")) {
131 propNamePfx = propNamePfx.substring(0, propNamePfx.length()
132 - "-builder".length());
135 if (propNamePfx.endsWith("-impl")) {
136 propNamePfx = propNamePfx.substring(0, propNamePfx.length()
141 // Iterate through getter methods to figure out values we need to
144 for (Method m : fromClass.getMethods()) {
145 // LOG.debug("Checking " + m.getName() + " method");
147 // LOG.debug(m.getName() + " is a getter");
148 Class returnType = m.getReturnType();
149 String fieldName = toLowerHyphen(m.getName().substring(3));
150 if(m != null && m.getName().matches("^is[A-Z].*")){
151 fieldName = toLowerHyphen(m.getName().substring(2));
154 fieldName = fieldName.substring(0, 1).toLowerCase()
155 + fieldName.substring(1);
157 // Is the return type a yang generated class?
158 if (isYangGenerated(returnType)) {
159 //System.out.println("returnType:" +returnType);
161 if (returnType.isEnum()) {
162 // Return type is a typedef. Save its value.
164 boolean isAccessible = m.isAccessible();
166 m.setAccessible(true);
169 Object retValue = m.invoke(fromObj);
172 m.setAccessible(isAccessible);
174 if (retValue != null) {
175 String propName = propNamePfx + "."
177 String propVal = retValue.toString();
178 String yangProp = "yang." + fieldName + "." + propVal;
179 if ( properties.containsKey(yangProp)) {
180 propVal = properties.getProperty(yangProp);
181 //LOG.debug("Adjusting property " + yangProp + " " + propVal);
183 //LOG.debug("Setting property " + propName
184 // + " to " + propVal);
185 props.setProperty(propName, propVal);
187 } catch (Exception e) {
189 "Caught exception trying to convert Yang-generated enum returned by "
190 + fromClass.getName() + "."
192 + "() to Properties entry", e);
194 } else if (isIpv4Address(returnType)) {
197 String propName = propNamePfx + "." + fieldName;
198 boolean isAccessible = m.isAccessible();
200 m.setAccessible(true);
202 Ipv4Address retValue = (Ipv4Address) m.invoke(fromObj);
204 m.setAccessible(isAccessible);
207 if (retValue != null) {
208 String propVal = retValue.getValue().toString();
209 //LOG.debug("Setting property " + propName
210 // + " to " + propVal);
211 props.setProperty(propName, propVal);
214 } catch (Exception e) {
216 "Caught exception trying to convert value returned by "
217 + fromClass.getName() + "."
219 + "() to Properties entry", e);
221 } else if (isIpv6Address(returnType)) {
224 String propName = propNamePfx + "." + fieldName;
225 boolean isAccessible = m.isAccessible();
227 m.setAccessible(true);
229 Ipv6Address retValue = (Ipv6Address) m.invoke(fromObj);
231 m.setAccessible(isAccessible);
234 if (retValue != null) {
235 String propVal = retValue.getValue().toString();
236 //LOG.debug("Setting property " + propName
237 // + " to " + propVal);
238 props.setProperty(propName, propVal);
241 } catch (Exception e) {
243 "Caught exception trying to convert value returned by "
244 + fromClass.getName() + "."
246 + "() to Properties entry", e);
248 } else if (isIpv4Prefix(returnType)) {
249 //System.out.println("isIpv4Prefix");
252 String propName = propNamePfx + "." + fieldName;
253 boolean isAccessible = m.isAccessible();
255 m.setAccessible(true);
257 Ipv4Prefix retValue = (Ipv4Prefix) m.invoke(fromObj);
259 m.setAccessible(isAccessible);
262 if (retValue != null) {
263 String propVal = retValue.getValue().toString();
264 //LOG.debug("Setting property " + propName
265 // + " to " + propVal);
266 props.setProperty(propName, propVal);
269 } catch (Exception e) {
271 "Caught exception trying to convert value returned by "
272 + fromClass.getName() + "."
274 + "() to Properties entry", e);
276 } else if (isIpv6Prefix(returnType)) {
277 //System.out.println("isIpv6Prefix");
280 String propName = propNamePfx + "." + fieldName;
281 boolean isAccessible = m.isAccessible();
283 m.setAccessible(true);
285 Ipv6Prefix retValue = (Ipv6Prefix) m.invoke(fromObj);
287 m.setAccessible(isAccessible);
290 if (retValue != null) {
291 String propVal = retValue.getValue().toString();
292 //LOG.debug("Setting property " + propName
293 // + " to " + propVal);
294 props.setProperty(propName, propVal);
297 } catch (Exception e) {
299 "Caught exception trying to convert value returned by "
300 + fromClass.getName() + "."
302 + "() to Properties entry", e);
306 boolean isAccessible = m.isAccessible();
308 m.setAccessible(true);
310 Object retValue = m.invoke(fromObj);
312 m.setAccessible(isAccessible);
314 if (retValue != null) {
315 toProperties(props, propNamePfx + "." + fieldName, retValue, returnType);
317 } catch (Exception e) {
319 "Caught exception trying to convert Yang-generated class returned by"
320 + fromClass.getName() + "."
322 + "() to Properties entry", e);
325 } else if (returnType.equals(Class.class)) {
327 //LOG.debug(m.getName()
328 // + " returns a Class object - not interested");
330 } else if (List.class.isAssignableFrom(returnType)) {
332 // This getter method returns a list.
334 boolean isAccessible = m.isAccessible();
336 m.setAccessible(true);
338 Object retList = m.invoke(fromObj);
340 m.setAccessible(isAccessible);
342 // Figure out what type of elements are stored in this array.
343 Type paramType = m.getGenericReturnType();
344 Type elementType = ((ParameterizedType) paramType)
345 .getActualTypeArguments()[0];
346 toProperties(props, propNamePfx + "." + fieldName,
347 retList, (Class)elementType);
348 } catch (Exception e) {
350 "Caught exception trying to convert List returned by "
351 + fromClass.getName() + "."
353 + "() to Properties entry", e);
358 // Method returns something that is not a List and not
362 String propName = propNamePfx + "." + fieldName;
363 boolean isAccessible = m.isAccessible();
365 m.setAccessible(true);
367 Object propValObj = m.invoke(fromObj);
369 m.setAccessible(isAccessible);
372 if (propValObj != null) {
373 String propVal = propValObj.toString();
374 //LOG.debug("Setting property " + propName
375 // + " to " + propVal);
376 props.setProperty(propName, propVal);
379 } catch (Exception e) {
381 "Caught exception trying to convert value returned by "
382 + fromClass.getName() + "."
384 + "() to Properties entry", e);
392 // Class is not yang generated and not a list
400 public static Object toBuilder(Properties props, Object toObj) {
402 return (toBuilder(props, "", toObj));
405 public static List toList(Properties props, String pfx, List toObj,
409 boolean foundValue = false;
411 //LOG.debug("Saving properties to List<" + elemType.getName()
412 // + "> from " + pfx);
414 // Figure out array size
415 for (Object pNameObj : props.keySet()) {
416 String key = (String) pNameObj;
418 if (key.startsWith(pfx + "[")) {
419 String idxStr = key.substring(pfx.length() + 1);
420 int endloc = idxStr.indexOf("]");
422 idxStr = idxStr.substring(0, endloc);
426 int curIdx = Integer.parseInt(idxStr);
427 if (curIdx > maxIdx) {
430 } catch (Exception e) {
431 LOG.error("Illegal subscript in property " + key);
437 //LOG.debug(pfx + " has max index of " + maxIdx);
438 for (int i = 0; i <= maxIdx; i++) {
440 String curBase = pfx + "[" + i + "]";
442 if (isYangGenerated(elemType)) {
443 String builderName = elemType.getName() + "Builder";
445 Class builderClass = Class.forName(builderName);
446 Object builderObj = builderClass.newInstance();
447 Method buildMethod = builderClass.getMethod("build");
448 builderObj = toBuilder(props, curBase, builderObj, true);
449 if (builderObj != null) {
450 //LOG.debug("Calling " + builderObj.getClass().getName()
451 // + "." + buildMethod.getName() + "()");
452 Object builtObj = buildMethod.invoke(builderObj);
457 } catch (ClassNotFoundException e) {
458 LOG.warn("Could not find builder class " + builderName, e);
459 } catch (Exception e) {
460 LOG.error("Caught exception trying to populate list from "
475 public static Object toBuilder(Properties props, String pfx, Object toObj) {
476 return(toBuilder(props, pfx, toObj, false));
479 public static Object toBuilder(Properties props, String pfx, Object toObj, boolean preservePfx) {
480 Class toClass = toObj.getClass();
481 boolean foundValue = false;
483 //LOG.debug("Saving properties to " + toClass.getName() + " class from "
488 if (isYangGenerated(toClass)) {
489 // Class is yang generated.
490 //LOG.debug(toClass.getName() + " is a Yang-generated class");
492 String propNamePfx = null;
497 if ((pfx != null) && (pfx.length() > 0)) {
498 propNamePfx = pfx + "."
499 + toLowerHyphen(toClass.getSimpleName());
501 propNamePfx = toLowerHyphen(toClass.getSimpleName());
504 if (propNamePfx.endsWith("-builder")) {
505 propNamePfx = propNamePfx.substring(0, propNamePfx.length()
506 - "-builder".length());
509 if (propNamePfx.endsWith("-impl")) {
510 propNamePfx = propNamePfx.substring(0, propNamePfx.length()
515 if (toObj instanceof Identifier) {
516 //LOG.debug(toClass.getName() + " is a Key - skipping");
520 // Iterate through getter methods to figure out values we need to
523 for (Method m : toClass.getMethods()) {
524 // LOG.debug("Is " + m.getName() + " method a setter?");
526 // LOG.debug(m.getName() + " is a setter");
527 Class paramTypes[] = m.getParameterTypes();
528 Class paramClass = paramTypes[0];
530 String fieldName = toLowerHyphen(m.getName().substring(3));
531 fieldName = fieldName.substring(0, 1).toLowerCase()
532 + fieldName.substring(1);
534 String propName = propNamePfx + "." + fieldName;
536 String paramValue = props.getProperty(propName);
537 if (paramValue == null) {
538 //LOG.debug(propName + " is unset");
540 //LOG.debug(propName + " = " + paramValue);
543 // Is the return type a yang generated class?
544 if (isYangGenerated(paramClass)) {
546 if (paramClass.isEnum()) {
548 //LOG.debug(m.getName() + " expects an Enum");
549 // Param type is a typedef.
550 if (paramValue != null) {
551 Object paramObj = null;
554 paramObj = Enum.valueOf(paramClass,
555 toUpperCamelCase(paramValue));
556 } catch (Exception e) {
558 "Caught exception trying to convert field "
559 + propName + " to enum "
560 + paramClass.getName(), e);
564 boolean isAccessible = m.isAccessible();
566 m.setAccessible(true);
569 //LOG.debug("Calling "
570 // + toObj.getClass().getName() + "."
571 // + m.getName() + "(" + paramValue
573 m.invoke(toObj, paramObj);
576 m.setAccessible(isAccessible);
580 } catch (Exception e) {
582 "Caught exception trying to create Yang-generated enum expected by"
586 + "() from Properties entry",
592 String simpleName = paramClass.getSimpleName();
593 LOG.info("simpleName:" + simpleName);
595 if ("Ipv4Address".equals(simpleName)
596 || "Ipv6Address".equals(simpleName) || "Ipv4Prefix".equals(simpleName) || "Ipv6Prefix".equals(simpleName)) {
598 if (paramValue != null) {
599 if("Ipv4Address".equals(simpleName) || "Ipv6Address".equals(simpleName)){
601 IpAddress ipAddr = IpAddressBuilder
602 .getDefaultInstance(paramValue);
605 if ("Ipv4Address".equals(simpleName))
607 m.invoke(toObj, ipAddr.getIpv4Address());
611 m.invoke(toObj, ipAddr.getIpv6Address());
615 } catch (Exception e) {
617 "Caught exception calling "
618 + toClass.getName() + "."
620 + paramValue + ")", e);
623 }else if("Ipv4Prefix".equals(simpleName)|| "Ipv6Prefix".equals(simpleName)){
625 IpPrefix ipPrefix = IpPrefixBuilder
626 .getDefaultInstance(paramValue);
629 if ("Ipv4Prefix".equals(simpleName))
631 m.invoke(toObj, ipPrefix.getIpv4Prefix());
635 m.invoke(toObj, ipPrefix.getIpv6Prefix());
639 } catch (Exception e) {
641 "Caught exception calling "
642 + toClass.getName() + "."
644 + paramValue + ")", e);
651 // setter expects a yang-generated class. Need
653 // create a builder to set it.
655 String builderName = paramClass.getName()
657 Class builderClass = null;
658 Object builderObj = null;
659 Object paramObj = null;
661 //LOG.debug(m.getName()
662 // + " expects a yang-generated class - looking for builder "
665 builderClass = Class.forName(builderName);
666 builderObj = builderClass.newInstance();
667 paramObj = toBuilder(props, propNamePfx,
669 } catch (ClassNotFoundException e) {
670 Object constObj = null;
672 // See if I can find a constructor I can
674 Constructor[] constructors = paramClass
676 // Is there a String constructor?
677 for (Constructor c : constructors) {
679 .getParameterTypes();
681 && (cParms.length == 1)) {
683 .isAssignableFrom(cParms[0])) {
685 .newInstance(paramValue);
690 if (constObj == null) {
691 // Is there a Long constructor?
692 for (Constructor c : constructors) {
694 .getParameterTypes();
696 && (cParms.length == 1)) {
698 .isAssignableFrom(cParms[0])) {
701 .parseLong(paramValue));
708 if (constObj != null) {
710 m.invoke(toObj, constObj);
712 } catch (Exception e2) {
714 "Caught exception trying to call "
719 } catch (Exception e1) {
721 "Could not find a suitable constructor for "
722 + paramClass.getName(),
726 if (paramObj == null) {
727 LOG.warn("Could not find builder class "
729 + " and could not find a String or Long constructor - trying just to set passing paramValue");
733 } catch (Exception e) {
735 "Caught exception trying to create builder "
739 if (paramObj != null) {
743 Method buildMethod = builderClass
745 //LOG.debug("Calling "
746 // + paramObj.getClass().getName()
747 // + "." + buildMethod.getName()
749 Object builtObj = buildMethod
752 boolean isAccessible = m.isAccessible();
754 m.setAccessible(true);
757 //LOG.debug("Calling "
758 // + toObj.getClass().getName()
759 // + "." + m.getName() + "()");
760 m.invoke(toObj, builtObj);
762 m.setAccessible(isAccessible);
766 } catch (Exception e) {
768 "Caught exception trying to set Yang-generated class expected by"
772 + "() from Properties entry",
777 boolean isAccessible = m.isAccessible();
779 m.setAccessible(true);
781 //LOG.debug("Calling "
782 // + toObj.getClass().getName()
783 // + "." + m.getName() + "("
784 // + paramValue + ")");
785 m.invoke(toObj, paramValue);
787 m.setAccessible(isAccessible);
791 } catch (Exception e) {
793 "Caught exception trying to convert value returned by"
797 + "() to Properties entry",
805 // Setter's argument is not a yang-generated class. See
808 if (List.class.isAssignableFrom(paramClass)) {
810 //LOG.debug("Parameter class " + paramClass.getName()
813 // Figure out what type of args are in List and pass
816 Type paramType = m.getGenericParameterTypes()[0];
817 Type elementType = ((ParameterizedType) paramType)
818 .getActualTypeArguments()[0];
819 Object paramObj = new LinkedList();
821 paramObj = toList(props, propName,
822 (List) paramObj, (Class) elementType);
823 } catch (Exception e) {
824 LOG.error("Caught exception trying to create list expected as argument to "
825 + toClass.getName() + "." + m.getName());
828 if (paramObj != null) {
830 boolean isAccessible = m.isAccessible();
832 m.setAccessible(true);
834 //LOG.debug("Calling "
835 // + toObj.getClass().getName() + "."
836 // + m.getName() + "(" + paramValue
838 m.invoke(toObj, paramObj);
840 m.setAccessible(isAccessible);
844 } catch (Exception e) {
846 "Caught exception trying to convert List returned by"
847 + toClass.getName() + "."
849 + "() to Properties entry",
855 // Setter expects something that is not a List and
856 // not yang-generated. Just pass the parameter value
858 //LOG.debug("Parameter class "
859 // + paramClass.getName()
860 // + " is not a yang-generated class or a List");
862 if (paramValue != null) {
864 Object constObj = null;
867 // See if I can find a constructor I can use
868 Constructor[] constructors = paramClass
870 // Is there a String constructor?
871 for (Constructor c : constructors) {
872 Class[] cParms = c.getParameterTypes();
874 && (cParms.length == 1)) {
876 .isAssignableFrom(cParms[0])) {
878 .newInstance(paramValue);
883 if (constObj == null) {
884 // Is there a Long constructor?
885 for (Constructor c : constructors) {
887 .getParameterTypes();
889 && (cParms.length == 1)) {
891 .isAssignableFrom(cParms[0])) {
894 .parseLong(paramValue));
901 if (constObj != null) {
903 //LOG.debug("Calling "
904 // + toObj.getClass()
906 // + m.getName() + "("
907 // + constObj + ")");
908 m.invoke(toObj, constObj);
910 } catch (Exception e2) {
912 "Caught exception trying to call "
917 boolean isAccessible = m
920 m.setAccessible(true);
922 //LOG.debug("Calling "
923 // + toObj.getClass()
925 // + m.getName() + "("
926 // + paramValue + ")");
927 m.invoke(toObj, paramValue);
929 m.setAccessible(isAccessible);
933 } catch (Exception e) {
935 "Caught exception trying to convert value returned by"
939 + "() to Properties entry",
943 } catch (Exception e1) {
945 "Could not find a suitable constructor for "
946 + paramClass.getName(), e1);
950 * try { boolean isAccessible =
951 * m.isAccessible(); if (!isAccessible) {
952 * m.setAccessible(true); } LOG.debug("Calling "
953 * + toObj.getClass().getName() + "." +
954 * m.getName() + "(" + paramValue + ")");
955 * m.invoke(toObj, paramValue); if
957 * m.setAccessible(isAccessible); } foundValue =
960 * } catch (Exception e) { LOG.error(
961 * "Caught exception trying to convert value returned by"
962 * + toClass.getName() + "." + m.getName() +
963 * "() to Properties entry", e); }
968 } // End of section handling "setter" method
969 } // End of loop through Methods
970 } // End of section handling yang-generated class
979 public static Properties getProperties(PrintStream pstr, String pfx,
981 boolean foundValue = false;
983 //LOG.debug("Analyzing " + toClass.getName() + " class : pfx " + pfx);
985 if (isYangGenerated(toClass)
986 && (!Identifier.class.isAssignableFrom(toClass))) {
987 // Class is yang generated.
988 //LOG.debug(toClass.getName() + " is a Yang-generated class");
990 if (toClass.getName().endsWith("Key")) {
991 if (Identifier.class.isAssignableFrom(toClass)) {
992 //LOG.debug(Identifier.class.getName()
993 // + " is assignable from " + toClass.getName());
996 //LOG.debug(Identifier.class.getName()
997 // + " is NOT assignable from " + toClass.getName());
1001 String propNamePfx = null;
1002 if (pfx.endsWith("]")) {
1004 }else if(pfx.indexOf(".CLASS_FOUND") != -1){
1005 pfx = pfx.replace(".CLASS_FOUND","");
1006 propNamePfx = pfx + "."
1007 + toLowerHyphen(toClass.getSimpleName());
1010 if ((pfx != null) && (pfx.length() > 0)) {
1011 propNamePfx = pfx + "."
1012 + toLowerHyphen(toClass.getSimpleName());
1014 propNamePfx = toLowerHyphen(toClass.getSimpleName());
1017 if (propNamePfx.endsWith("-builder")) {
1018 propNamePfx = propNamePfx.substring(0, propNamePfx.length()
1019 - "-builder".length());
1022 if (propNamePfx.endsWith("-impl")) {
1023 propNamePfx = propNamePfx.substring(0, propNamePfx.length()
1024 - "-impl".length());
1028 // Iterate through getter methods to figure out values we need to
1031 for (Method m : toClass.getMethods()) {
1032 //LOG.debug("Is " + m.getName() + " method a getter?");
1034 // LOG.debug(m.getName() + " is a getter");
1035 Class returnClass = m.getReturnType();
1037 String fieldName = toLowerHyphen(m.getName().substring(3));
1038 if(m != null && m.getName().matches("^is[A-Z].*")){
1039 fieldName = toLowerHyphen(m.getName().substring(2));
1041 fieldName = fieldName.substring(0, 1).toLowerCase()
1042 + fieldName.substring(1);
1044 String propName = propNamePfx + "." + fieldName;
1045 //System.out.println("****" + propName);
1047 // Is the return type a yang generated class?
1048 if (isYangGenerated(returnClass)) {
1050 if (returnClass.isEnum()) {
1052 //LOG.debug(m.getName() + " is an Enum");
1053 //pstr.print("\n" + propName);
1054 //pstr.print("\n" + propName + ":Enum:" + Arrays.asList(returnClass.getEnumConstants()) + "\n");
1055 pstr.print("\"" + propName + ":Enum:" + Arrays.asList(returnClass.getEnumConstants()) + "\",");
1056 prop.setProperty(propName,"");
1057 propList.add(propName);
1061 String simpleName = returnClass.getSimpleName();
1062 //System.out.println("simpleName:" + simpleName);
1064 if ("Ipv4Address".equals(simpleName) || "Ipv6Address".equals(simpleName) || "IpAddress".equals(simpleName) || "Ipv4Prefix".equals(simpleName) || "Ipv6Prefix".equals(simpleName) || "IpPrefix".equals(simpleName)) {
1065 //LOG.debug(m.getName()+" is an "+simpleName);
1066 //pstr.print("\n" + propName);
1067 //pstr.print("\n" + propName + ":" + simpleName + "\n");
1068 pstr.print("\"" + propName + ":" + simpleName + "\",");
1069 prop.setProperty(propName,"");
1070 propList.add(propName);
1072 boolean isString = false;
1073 boolean isNumber = false;
1074 boolean isBoolean = false;
1075 boolean isIdentifier = false;
1076 //System.out.println("simpleName:" + simpleName);
1077 //System.out.println("propName:" + propName);
1078 for(Method mthd : returnClass.getMethods()){
1079 String methodName = mthd.getName();
1080 //System.out.println("methodName:" + methodName);
1081 if(methodName.equals("getValue")){
1082 Class retType = mthd.getReturnType();
1083 //System.out.println("retType:" + retType);
1084 isString = String.class.isAssignableFrom(retType);
1085 isNumber = Number.class.isAssignableFrom(retType);
1086 isBoolean = Boolean.class.isAssignableFrom(retType);
1087 isIdentifier = Identifier.class.isAssignableFrom(retType);
1088 //System.out.println("isString:" + isString);
1089 //System.out.println("isNumber:" + isNumber);
1090 //System.out.println("isNumber:" + isNumber);
1096 pstr.print("\"" + propName + ":String\",");
1097 prop.setProperty(propName,"");
1098 propList.add(propName);
1100 pstr.print("\"" + propName + ":Number\",");
1101 prop.setProperty(propName,"");
1102 propList.add(propName);
1103 }else if(isBoolean){
1104 pstr.print("\"" + propName + ":Boolean\",");
1105 prop.setProperty(propName,"");
1106 propList.add(propName);
1107 }else if(isIdentifier){
1108 //System.out.println("isIdentifier");
1109 //isIdentifer so skipping
1113 System.out.println("fieldName:" + fieldName);
1114 System.out.println("simpleName:" + simpleName);
1115 System.out.println("returnClass:" + returnClass);
1116 System.out.println("pstr:" + pstr);
1117 System.out.println("propNamePfx:" + propNamePfx);
1119 getProperties(pstr, propNamePfx + ".CLASS_FOUND", returnClass);
1126 // Setter's argument is not a yang-generated class. See
1129 if (List.class.isAssignableFrom(returnClass)) {
1131 //LOG.debug("Parameter class "
1132 // + returnClass.getName() + " is a List");
1134 // Figure out what type of args are in List and pass
1135 // that to toList().
1137 Type returnType = m.getGenericReturnType();
1138 Type elementType = ((ParameterizedType) returnType)
1139 .getActualTypeArguments()[0];
1140 Class elementClass = (Class) elementType;
1141 //LOG.debug("Calling printPropertyList on list type ("
1142 //+ elementClass.getName()
1145 // + "), toClass is ("
1146 // + toClass.getName() + ")");
1147 //System.out.println("List propNamePfx:" + propNamePfx+ "." + toLowerHyphen(elementClass.getSimpleName()) + "[]");
1148 if(String.class.isAssignableFrom(elementClass)){
1149 pstr.print("\"" + propName + ":[String,String,...]\",");
1150 prop.setProperty(propName,"");
1151 propList.add(propName);
1152 }else if(Number.class.isAssignableFrom(elementClass)){
1153 pstr.print("\"" + propName + ":[Number,Number,...]\",");
1154 prop.setProperty(propName,"");
1155 propList.add(propName);
1156 }else if(Boolean.class.isAssignableFrom(elementClass)){
1157 pstr.print("\"" + propName + ":[Boolean,Boolean,...]\",");
1158 prop.setProperty(propName,"");
1159 propList.add(propName);
1160 }else if(Identifier.class.isAssignableFrom(elementClass)){
1167 + toLowerHyphen(elementClass
1168 .getSimpleName()) + "[]",
1172 } else if (!returnClass.equals(Class.class)) {
1174 // Setter expects something that is not a List and
1175 // not yang-generated. Just pass the parameter value
1177 //LOG.debug("Parameter class "
1178 // + returnClass.getName()
1179 // + " is not a yang-generated class or a List");
1181 //pstr.print("\n" + propName);
1182 String className=returnClass.getName();
1183 int nClassNameIndex = className.lastIndexOf('.');
1184 String nClassName = className;
1185 if(nClassNameIndex != -1){
1186 nClassName=className.substring(nClassNameIndex+1);
1188 boolean isString = String.class.isAssignableFrom(returnClass);
1189 boolean isNumber = Number.class.isAssignableFrom(returnClass);
1190 boolean isBoolean = Boolean.class.isAssignableFrom(returnClass);
1191 //pstr.print("\n" + propName +":" + nClassName +"\n");
1192 boolean isIdentifier = Identifier.class.isAssignableFrom(returnClass);
1193 if(!isIdentifier && !nClassName.equals("[C")){
1195 pstr.print("\"" + propName +":Number\",");
1196 }else if(isBoolean){
1197 pstr.print("\"" + propName +":Boolean\",");
1199 if(nClassName.equals("[B")){
1200 pstr.print("\"" + propName +":Binary\",");
1202 pstr.print("\"" + propName +":" + nClassName +"\",");
1205 prop.setProperty(propName,"");
1206 propList.add(propName);
1211 } // End of section handling "setter" method
1212 } // End of loop through Methods
1213 } // End of section handling yang-generated class
1218 public static boolean isYangGenerated(Class c) {
1222 //System.out.println(c.getName());
1223 return (c.getName().startsWith("org.opendaylight.yang.gen."));
1227 public static boolean isIpv4Address(Class c) {
1232 String simpleName = c.getSimpleName();
1233 return ("Ipv4Address".equals(simpleName)) ;
1236 public static boolean isIpv6Address(Class c) {
1241 String simpleName = c.getSimpleName();
1242 return ("Ipv6Address".equals(simpleName)) ;
1244 public static boolean isIpv4Prefix(Class c) {
1249 String simpleName = c.getSimpleName();
1250 //System.out.println("simpleName:" + simpleName);
1251 return ("Ipv4Prefix".equals(simpleName)) ;
1254 public static boolean isIpv6Prefix(Class c) {
1259 String simpleName = c.getSimpleName();
1260 //System.out.println("simpleName:" + simpleName);
1261 return ("Ipv6Prefix".equals(simpleName)) ;
1264 public static String toLowerHyphen(String inStr) {
1265 if (inStr == null) {
1269 String str = inStr.substring(0, 1).toLowerCase();
1270 if (inStr.length() > 1) {
1271 str = str + inStr.substring(1);
1274 String regex = "(([a-z0-9])([A-Z]))";
1275 String replacement = "$2-$3";
1277 String retval = str.replaceAll(regex, replacement).toLowerCase();
1279 //LOG.debug("Converting " + inStr + " => " + str + " => " + retval);
1283 public static String toUpperCamelCase(String inStr) {
1284 if (inStr == null) {
1288 String[] terms = inStr.split("-");
1289 StringBuffer sbuff = new StringBuffer();
1290 // Check if string begins with a digit
1291 if (Character.isDigit(inStr.charAt(0))) {
1294 for (String term : terms) {
1295 sbuff.append(term.substring(0, 1).toUpperCase());
1296 if (term.length() > 1) {
1297 sbuff.append(term.substring(1));
1300 return (sbuff.toString());
1304 public static boolean isGetter(Method m) {
1305 //System.out.println(m);
1310 if (Modifier.isPublic(m.getModifiers())
1311 && (m.getParameterTypes().length == 0)) {
1312 if ((m.getName().matches("^is[A-Z].*") || m.getName().matches("^get[A-Z].*"))
1313 && m.getReturnType().equals(Boolean.class)) {
1316 if (m.getName().matches("^get[A-Z].*")
1317 && !m.getReturnType().equals(void.class)) {
1326 public static boolean isSetter(Method m) {
1331 if (Modifier.isPublic(m.getModifiers())
1332 && (m.getParameterTypes().length == 1)) {
1333 if (m.getName().matches("^set[A-Z].*")) {
1334 Class[] paramTypes = m.getParameterTypes();
1335 if (paramTypes[0].isAssignableFrom(Identifier.class)
1336 || Identifier.class.isAssignableFrom(paramTypes[0])) {
1348 public static void main(String[] args){
1350 try(PrintStream ps = new PrintStream(new FileOutputStream(FileDescriptor.out))){
1351 PrintYangToProp printYangToProp = new PrintYangToProp();
1352 String className = args[0];
1353 //ClassLoader classLoader = PrintYangToProp.class.getClassLoader();
1354 //Class aClass = classLoader.loadClass(className);
1355 Class cl = Class.forName(className);
1356 //printPropertyList(ps,"",cl);
1357 //JsonObject jsonObj = Json.createObjectBuilder().build();
1358 Properties p = getProperties(ps,"",cl);
1359 //System.out.println(p);
1361 }catch(Exception e){
1362 e.printStackTrace();