af172f9adb7d72334b0bec4cf824ad592f933b41
[sdnc/core.git] / sli / provider / src / main / java / org / openecomp / sdnc / sli / provider / MdsalHelper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                         reserved.
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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=========================================================
20  */
21
22 package org.openecomp.sdnc.sli.provider;
23
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.PrintStream;
29 import java.lang.reflect.Constructor;
30 import java.lang.reflect.Method;
31 import java.lang.reflect.Modifier;
32 import java.lang.reflect.ParameterizedType;
33 import java.lang.reflect.Type;
34 import java.util.LinkedList;
35 import java.util.List;
36 import java.util.Properties;
37
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddressBuilder;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefixBuilder;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
44 import org.opendaylight.yangtools.yang.binding.Identifier;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 public class MdsalHelper {
49
50         private static final Logger LOG = LoggerFactory.getLogger(MdsalHelper.class);
51         public static final String PROPERTIES_FILE="/opt/bvc/controller/configuration/l3sdn.properties";
52         private static Properties properties = new Properties();
53         
54         
55         public static void setProperties(Properties properties) {
56                 
57                 for (Object propNameObj: properties.keySet()) {
58                         String propName = (String) propNameObj;
59                         MdsalHelper.properties.setProperty(propName, properties.getProperty(propName));
60                 }
61         }
62
63         public static void loadProperties() {
64
65                 File file = new File(PROPERTIES_FILE); 
66                 Properties properties = new Properties();
67                 InputStream input = null;
68                 if (file.isFile() && file.canRead()) {
69                         try     {
70                                 input = new FileInputStream(file);
71                                 properties.load(input);
72                                 MdsalHelper.setProperties(properties);
73                                 LOG.info("Loaded properties from " + PROPERTIES_FILE );
74                         } catch (Exception e) {
75                                 LOG.error("Failed to load properties " + PROPERTIES_FILE +"\n",e);
76                         } finally {
77                                 if (input != null) {
78                                         try {
79                                                 input.close();
80                                         } catch (IOException e) {
81                                                 LOG.error("Failed to close properties file " + PROPERTIES_FILE +"\n",e);
82                                         }
83                                 }
84                         }
85                 }
86         }
87         
88         public static Properties toProperties(Properties props, Object fromObj) {
89                 Class fromClass = null;
90                 
91                 if (fromObj != null)
92                 {
93                         fromClass = fromObj.getClass();
94                 }
95                 return (toProperties(props, "", fromObj, fromClass));
96         }
97         
98         public static Properties toProperties(Properties props, String pfx, Object fromObj)
99         {
100                 Class fromClass = null;
101                 
102                 if (fromObj != null)
103                 {
104                         fromClass = fromObj.getClass();
105                 }
106                 
107                 return(toProperties(props, pfx, fromObj, fromClass));
108         }
109
110         public static Properties toProperties(Properties props, String pfx,
111                         Object fromObj, Class fromClass) {
112
113                 if (fromObj == null) {
114                         return (props);
115                 }
116         
117                 
118                 String simpleName = fromClass.getSimpleName();
119
120                 LOG.trace("Extracting properties from " + fromClass.getName()
121                                 + " class");
122                 if (fromObj instanceof List) {
123
124                         // Class is a List. List should contain yang-generated classes.
125                         LOG.trace(fromClass.getName() + " is a List");
126
127                         List fromList = (List) fromObj;
128
129                         for (int i = 0; i < fromList.size(); i++) {
130                                 toProperties(props, pfx + "[" + i + "]", fromList.get(i), fromClass);
131                         }
132                         props.setProperty(pfx + "_length", "" + fromList.size());
133
134                 } else if (isYangGenerated(fromClass)) {
135                         // Class is yang generated.
136                         LOG.trace(fromClass.getName() + " is a Yang-generated class");
137
138                         String propNamePfx = null;
139
140                         // If called from a list (so prefix ends in ']'), don't
141                         // add class name again
142                         if (pfx.endsWith("]")) {
143                                 propNamePfx = pfx;
144                         } else {
145                                 if ((pfx != null) && (pfx.length() > 0)) {
146                                         propNamePfx = pfx ;
147                                 } else {
148                                         propNamePfx = toLowerHyphen(fromClass.getSimpleName());
149                                 }
150
151                                 if (propNamePfx.endsWith("-builder")) {
152                                         propNamePfx = propNamePfx.substring(0, propNamePfx.length()
153                                                         - "-builder".length());
154                                 }
155
156                                 if (propNamePfx.endsWith("-impl")) {
157                                         propNamePfx = propNamePfx.substring(0, propNamePfx.length()
158                                                         - "-impl".length());
159                                 }
160                         }
161                         
162                         // Iterate through getter methods to figure out values we need to
163                         // save from
164
165                         int numGetters = 0;
166                         String lastGetterName = null;
167                         String propVal = null;
168                         
169                         for (Method m : fromClass.getMethods()) {
170                                 if (isGetter(m)) {
171                                         
172                                         numGetters++;
173                                         lastGetterName = m.getName();
174                                         
175                                         Class returnType = m.getReturnType();
176                                         String fieldName;
177                                         if (m.getName().startsWith("get")) {
178                                                 fieldName = toLowerHyphen(m.getName().substring(3));
179                                         } else {
180
181                                                 fieldName = toLowerHyphen(m.getName().substring(2));
182                                         }
183                                         
184                                         fieldName = fieldName.substring(0, 1).toLowerCase()
185                                                         + fieldName.substring(1);
186
187                                         // Is the return type a yang generated class?
188                                         if (isYangGenerated(returnType)) {
189                                                 // Is it an enum?
190                                                 if (returnType.isEnum()) {
191                                                         // Return type is a typedef. Save its value.
192                                                         try {
193                                                                 boolean isAccessible = m.isAccessible();
194                                                                 if (!isAccessible) {
195                                                                         m.setAccessible(true);
196                                                                 }
197
198                                                                 Object retValue = m.invoke(fromObj);
199
200                                                                 if (!isAccessible) {
201                                                                         m.setAccessible(isAccessible);
202                                                                 }
203                                                                 if (retValue != null) {
204                                                                         String propName = propNamePfx + "."
205                                                                                         + fieldName;
206                                                                         propVal = retValue.toString();
207                                                                         String yangProp = "yang." + fieldName + "." + propVal;
208                                                                         if ( properties.containsKey(yangProp)) {
209                                                                                 propVal = properties.getProperty(yangProp);
210                                                                                 LOG.trace("Adjusting property " + yangProp + " " + propVal);
211                                                                         }
212                                                                         LOG.debug("Setting property " + propName
213                                                                                         + " to " + propVal);
214                                                                         props.setProperty(propName, propVal);
215                                                                 }
216                                                         } catch (Exception e) {
217                                                                 LOG.error(
218                                                                                 "Caught exception trying to convert Yang-generated enum returned by "
219                                                                                                 + fromClass.getName() + "."
220                                                                                                 + m.getName()
221                                                                                                 + "() to Properties entry", e);
222                                                         }
223                                                 } else if (isIpv4Address(returnType)) {
224                                                         // Save its value
225                                                         try {
226                                                                 String propName = propNamePfx + "." + fieldName;
227                                                                 boolean isAccessible = m.isAccessible();
228                                                                 if (!isAccessible) {
229                                                                         m.setAccessible(true);
230                                                                 }
231                                                                 Ipv4Address retValue = (Ipv4Address) m.invoke(fromObj);
232                                                                 if (!isAccessible) {
233                                                                         m.setAccessible(isAccessible);
234                                                                 }
235
236                                                                 if (retValue != null) {
237                                                                         propVal = retValue.getValue().toString();
238                                                                         LOG.debug("Setting property " + propName
239                                                                                         + " to " + propVal);
240                                                                         props.setProperty(propName, propVal);
241
242                                                                 }
243                                                         } catch (Exception e) {
244                                                                 LOG.error(
245                                                                                 "Caught exception trying to convert value returned by "
246                                                                                                 + fromClass.getName() + "."
247                                                                                                 + m.getName()
248                                                                                                 + "() to Properties entry", e);
249                                                         }
250                                                 } else if (isIpv6Address(returnType)) {
251                                                         // Save its value
252                                                         try {
253                                                                 String propName = propNamePfx + "." + fieldName;
254                                                                 boolean isAccessible = m.isAccessible();
255                                                                 if (!isAccessible) {
256                                                                         m.setAccessible(true);
257                                                                 }
258                                                                 Ipv6Address retValue = (Ipv6Address) m.invoke(fromObj);
259                                                                 if (!isAccessible) {
260                                                                         m.setAccessible(isAccessible);
261                                                                 }
262
263                                                                 if (retValue != null) {
264                                                                         propVal = retValue.getValue().toString();
265                                                                         LOG.debug("Setting property " + propName
266                                                                                         + " to " + propVal);
267                                                                         props.setProperty(propName, propVal);
268
269                                                                 }
270                                                         } catch (Exception e) {
271                                                                 LOG.error(
272                                                                                 "Caught exception trying to convert value returned by "
273                                                                                                 + fromClass.getName() + "."
274                                                                                                 + m.getName()
275                                                                                                 + "() to Properties entry", e);
276                                                         }
277                                                 } else if (isIpAddress(returnType)) {
278                                                         // Save its value
279                                                         try {
280                                                                 String propName = propNamePfx + "." + fieldName;
281                                                                 boolean isAccessible = m.isAccessible();
282                                                                 if (!isAccessible) {
283                                                                         m.setAccessible(true);
284                                                                 }
285                                                                 IpAddress retValue = (IpAddress) m.invoke(fromObj);
286                                                                 if (!isAccessible) {
287                                                                         m.setAccessible(isAccessible);
288                                                                 }
289
290                                                                 if (retValue != null) {
291                                                                         propVal = new String(retValue.getValue());
292                                                                         LOG.debug("Setting property " + propName
293                                                                                         + " to " + propVal);
294                                                                         props.setProperty(propName, propVal);
295
296                                                                 }
297                                                         } catch (Exception e) {
298                                                                 LOG.error(
299                                                                                 "Caught exception trying to convert value returned by "
300                                                                                                 + fromClass.getName() + "."
301                                                                                                 + m.getName()
302                                                                                                 + "() to Properties entry", e);
303                                                         }
304                                                 } else if (isIpPrefix(returnType)) {
305                                                         // Save its value
306                                                         try {
307                                                                 String propName = propNamePfx + "." + fieldName;
308                                                                 boolean isAccessible = m.isAccessible();
309                                                                 if (!isAccessible) {
310                                                                         m.setAccessible(true);
311                                                                 }
312                                                                 IpPrefix retValue = (IpPrefix) m.invoke(fromObj);
313                                                                 if (!isAccessible) {
314                                                                         m.setAccessible(isAccessible);
315                                                                 }
316
317                                                                 if (retValue != null) {
318                                                                         propVal = new String(retValue.getValue());
319                                                                         LOG.debug("Setting property " + propName
320                                                                                         + " to " + propVal);
321                                                                         props.setProperty(propName, propVal);
322
323                                                                 }
324                                                         } catch (Exception e) {
325                                                                 LOG.error(
326                                                                                 "Caught exception trying to convert value returned by "
327                                                                                                 + fromClass.getName() + "."
328                                                                                                 + m.getName()
329                                                                                                 + "() to Properties entry", e);
330                                                         }
331                                                 } else {
332                                                         try {
333                                                                 boolean isAccessible = m.isAccessible();
334                                                                 if (!isAccessible) {
335                                                                         m.setAccessible(true);
336                                                                 }
337                                                                 Object retValue = m.invoke(fromObj);
338                                                                 
339                                                                 if (retValue instanceof byte[]) {
340                                                                         LOG.trace(m.getName()+" returns a byte[]");
341                                                                         retValue = new String((byte[]) retValue, "UTF-8");
342                                                                         LOG.trace("Converted byte array "+propNamePfx+"."+fieldName+"to string "+ retValue );
343                                                                 }
344                                                                 if (!isAccessible) {
345                                                                         m.setAccessible(isAccessible);
346                                                                 }
347                                                                 if (retValue != null) {
348                                                                         toProperties(props, propNamePfx + "." + fieldName, retValue, returnType);
349                                                                 }
350                                                         } catch (Exception e) {
351                                                                 
352                                                                 if (m.getName().equals("getKey")) {
353                                                                         LOG.trace("Caught "+e.getClass().getName()+" exception trying to convert results from getKey() - ignoring");
354                                                                 } else {
355                                                                         LOG.error(
356                                                                                         "Caught exception trying to convert Yang-generated class returned by"
357                                                                                                         + fromClass.getName() + "."
358                                                                                                         + m.getName()
359                                                                                                         + "() to Properties entry", e);
360                                                                 }
361                                                         }
362                                                 }
363                                         } else if (returnType.equals(Class.class)) {
364
365                                                 LOG.trace(m.getName()
366                                                                 + " returns a Class object - not interested");
367
368                                         } else if (List.class.isAssignableFrom(returnType)) {
369
370                                                 // This getter method returns a list.
371                                                 try {
372                                                         boolean isAccessible = m.isAccessible();
373                                                         if (!isAccessible) {
374                                                                 m.setAccessible(true);
375                                                         }
376                                                         Object retList = m.invoke(fromObj);
377                                                         if (!isAccessible) {
378                                                                 m.setAccessible(isAccessible);
379                                                         }
380                                                         // Figure out what type of elements are stored in this array.
381                                                         Type paramType = m.getGenericReturnType();
382                                                         Type elementType = ((ParameterizedType) paramType)
383                                                                         .getActualTypeArguments()[0];
384                                                         toProperties(props, propNamePfx + "." + fieldName,
385                                                                         retList, (Class)elementType);
386                                                 } catch (Exception e) {
387                                                         LOG.error(
388                                                                         "Caught exception trying to convert List returned by "
389                                                                                         + fromClass.getName() + "."
390                                                                                         + m.getName()
391                                                                                         + "() to Properties entry", e);
392                                                 }
393
394                                         } else {
395
396                                                 // Method returns something that is not a List and not
397                                                 // yang-generated.
398                                                 // Save its value
399                                                 try {
400                                                         String propName = propNamePfx + "." + fieldName;
401                                                         boolean isAccessible = m.isAccessible();
402                                                         if (!isAccessible) {
403                                                                 m.setAccessible(true);
404                                                         }
405                                                         Object propValObj = m.invoke(fromObj);
406                                                         if (!isAccessible) {
407                                                                 m.setAccessible(isAccessible);
408                                                         }
409
410                                                         if (propValObj != null) {
411                                                                 if (propValObj instanceof byte[]) {
412                                                                         LOG.trace(m.getName()+" returns a byte[]");
413                                                                         propVal = new String((byte[]) propValObj, "UTF-8");
414                                                                         LOG.trace("Converted byte array "+propNamePfx+"."+fieldName+"to string "+ propVal );
415
416                                                                 } else {
417                                                                         propVal = propValObj.toString();
418                                                                 }
419                                                                 LOG.debug("Setting property " + propName
420                                                                                 + " to " + propVal);
421                                                                 props.setProperty(propName, propVal);
422
423                                                         }
424                                                 } catch (Exception e) {
425                                                         if (m.getName().equals("getKey")) {
426                                                                 LOG.trace("Caught "+e.getClass().getName()+" exception trying to convert results from getKey() - ignoring");
427                                                         } else {
428                                                                 LOG.error(
429                                                                                 "Caught exception trying to convert value returned by"
430                                                                                                 + fromClass.getName() + "."
431                                                                                                 + m.getName()
432                                                                                                 + "() to Properties entry", e);
433                                                         }
434                                                 }
435                                         }
436
437                                 }
438                         }
439                         
440                         // End of method loop.  If there was only one getter, named "getValue", then
441                         // set value identified by "prefix" to that one value.
442                         if ((numGetters == 1) && ("getValue".equals(lastGetterName))) {
443                                 LOG.trace("getValueFIX : "+ propNamePfx+" only has getValue() getter - setting "+propNamePfx+" = "+propVal);
444                                 props.setProperty(propNamePfx, propVal);
445                         } else {
446                                 LOG.trace("getValueFIX : " + propNamePfx+" has "+numGetters+" getter(s), last one found was "+lastGetterName);
447                                 
448                         }
449
450                 } else {
451                         // Class is not yang generated and not a list
452                         // It must be an element of a leaf list - set "prefix" to value
453                         String fromVal = null;
454                         if (fromObj instanceof byte[]) {
455                                 try {
456                                 fromVal = new String((byte[]) fromObj, "UTF-8");
457                                 LOG.trace("Converted byte array "+pfx+"to string "+ fromVal );
458                                 } catch (Exception e) {
459                                         LOG.warn("Caught exception trying to convert "+pfx+" from byte[] to String", e);
460                                         fromVal = fromObj.toString();
461                                 }
462
463                         } else {
464                                 fromVal = fromObj.toString();
465                         }
466                         LOG.debug("Setting property " + pfx
467                                         + " to " + fromVal);
468                         props.setProperty(pfx, fromVal);
469                 }
470
471                 return (props);
472         }
473
474         public static Object toBuilder(Properties props, Object toObj) {
475
476                 return (toBuilder(props, "", toObj));
477         }
478
479         public static List toList(Properties props, String pfx, List toObj,
480                         Class elemType) {
481
482                 int maxIdx = -1;
483                 boolean foundValue = false;
484
485                 LOG.trace("Saving properties to List<" + elemType.getName()
486                                 + ">  from " + pfx);
487
488                 if (props.contains(pfx+"_length")) {
489                         try {
490                                 int listLength = Integer.parseInt(props.getProperty(pfx+"_length"));
491                                 
492                                 if (listLength > 0) {
493                                         maxIdx = listLength - 1;
494                                 }
495                         } catch (Exception e) {
496                                 // Ignore exception
497                         }
498                 }
499                 
500                 if (maxIdx == -1) {
501                         // Figure out array size
502                         for (Object pNameObj : props.keySet()) {
503                                 String key = (String) pNameObj;
504
505                                 if (key.startsWith(pfx + "[")) {
506                                         String idxStr = key.substring(pfx.length() + 1);
507                                         int endloc = idxStr.indexOf("]");
508                                         if (endloc != -1) {
509                                                 idxStr = idxStr.substring(0, endloc);
510                                         }
511
512                                         try {
513                                                 int curIdx = Integer.parseInt(idxStr);
514                                                 if (curIdx > maxIdx) {
515                                                         maxIdx = curIdx;
516                                                 }
517                                         } catch (Exception e) {
518                                                 LOG.error("Illegal subscript in property " + key);
519                                         }
520
521                                 }
522                         }
523                 }
524                 
525
526                 LOG.trace(pfx + " has max index of " + maxIdx);
527                 for (int i = 0; i <= maxIdx; i++) {
528
529                         String curBase = pfx + "[" + i + "]";
530
531                         if (isYangGenerated(elemType)) {
532                                 String builderName = elemType.getName() + "Builder";
533                                 try {
534                                         Class builderClass = Class.forName(builderName);
535                                         Object builderObj = builderClass.newInstance();
536                                         Method buildMethod = builderClass.getMethod("build");
537                                         builderObj = toBuilder(props, curBase, builderObj, true);
538                                         if (builderObj != null) {
539                                                 LOG.trace("Calling " + builderObj.getClass().getName()
540                                                                 + "." + buildMethod.getName() + "()");
541                                                 Object builtObj = buildMethod.invoke(builderObj);
542                                                 toObj.add(builtObj);
543                                                 foundValue = true;
544                                         }
545
546                                 } catch (ClassNotFoundException e) {
547                                         LOG.warn("Could not find builder class " + builderName, e);
548                                 } catch (Exception e) {
549                                         LOG.error("Caught exception trying to populate list from "
550                                                         + pfx);
551                                 }
552                         } else {
553                                 // Must be a leaf list
554                                 String curValue = props.getProperty(curBase, "");
555                                 
556                                 toObj.add(curValue);
557                                 
558                                 if ((curValue != null) && (curValue.length() > 0)) {
559                                         foundValue = true;
560                                 }
561                         }
562
563                 }
564
565                 if (foundValue) {
566                         return (toObj);
567                 } else {
568                         return (null);
569                 }
570
571         }
572         
573         public static Object toBuilder(Properties props, String pfx, Object toObj) {
574                 return(toBuilder(props, pfx, toObj, false));
575         }
576
577         public static Object toBuilder(Properties props, String pfx, Object toObj, boolean preservePfx) {
578                 Class toClass = toObj.getClass();
579                 boolean foundValue = false;
580
581                 LOG.trace("Saving properties to " + toClass.getName() + " class from "
582                                 + pfx);
583
584                 Ipv4Address addr;
585
586                 if (isYangGenerated(toClass)) {
587                         // Class is yang generated.
588                         LOG.trace(toClass.getName() + " is a Yang-generated class");
589
590                         String propNamePfx = null;
591                         if (preservePfx) {
592                                 propNamePfx = pfx;
593                         } else {
594
595                                 if ((pfx != null) && (pfx.length() > 0)) {
596                                         propNamePfx = pfx + "."
597                                                         + toLowerHyphen(toClass.getSimpleName());
598                                 } else {
599                                         propNamePfx = toLowerHyphen(toClass.getSimpleName());
600                                 }
601
602                                 if (propNamePfx.endsWith("-builder")) {
603                                         propNamePfx = propNamePfx.substring(0, propNamePfx.length()
604                                                         - "-builder".length());
605                                 }
606
607                                 if (propNamePfx.endsWith("-impl")) {
608                                         propNamePfx = propNamePfx.substring(0, propNamePfx.length()
609                                                         - "-impl".length());
610                                 }
611                         }
612
613                         if (toObj instanceof Identifier) {
614                                 LOG.trace(toClass.getName() + " is a Key - skipping");
615                                 return (toObj);
616                         }
617
618                         // Iterate through getter methods to figure out values we need to
619                         // set
620
621                         for (Method m : toClass.getMethods()) {
622                                 if (isSetter(m)) {
623                                         Class paramTypes[] = m.getParameterTypes();
624                                         Class paramClass = paramTypes[0];
625
626                                         String fieldName = toLowerHyphen(m.getName().substring(3));
627                                         fieldName = fieldName.substring(0, 1).toLowerCase()
628                                                         + fieldName.substring(1);
629
630                                         String propName = propNamePfx + "." + fieldName;
631
632                                         String paramValue = props.getProperty(propName);
633                                         if (paramValue == null) {
634                                                 LOG.trace(propName + " is unset");
635                                         } else {
636                                                 LOG.trace(propName + " = " + paramValue);
637                                         }
638
639                                         // Is the return type a yang generated class?
640                                         if (isYangGenerated(paramClass)) {
641                                                 // Is it an enum?
642                                                 if (paramClass.isEnum()) {
643
644                                                         LOG.trace(m.getName() + " expects an Enum");
645                                                         // Param type is a typedef.
646                                                         if ((paramValue != null)  && (paramValue.length() > 0)) {
647                                                                 Object paramObj = null;
648
649                                                                 try {
650                                                                         paramObj = Enum.valueOf(paramClass,
651                                                                                         toUpperCamelCase(paramValue));
652                                                                 } catch (Exception e) {
653                                                                         LOG.error(
654                                                                                         "Caught exception trying to convert field "
655                                                                                                         + propName + " to enum "
656                                                                                                         + paramClass.getName(), e);
657                                                                 }
658
659                                                                 try {
660                                                                         boolean isAccessible = m.isAccessible();
661                                                                         if (!isAccessible) {
662                                                                                 m.setAccessible(true);
663                                                                         }
664
665                                                                         LOG.trace("Calling "
666                                                                                         + toObj.getClass().getName() + "."
667                                                                                         + m.getName() + "(" + paramValue
668                                                                                         + ")");
669                                                                         m.invoke(toObj, paramObj);
670
671                                                                         if (!isAccessible) {
672                                                                                 m.setAccessible(isAccessible);
673                                                                         }
674                                                                         foundValue = true;
675
676                                                                 } catch (Exception e) {
677                                                                         LOG.error(
678                                                                                         "Caught exception trying to create Yang-generated enum expected by"
679                                                                                                         + toClass.getName()
680                                                                                                         + "."
681                                                                                                         + m.getName()
682                                                                                                         + "() from Properties entry",
683                                                                                         e);
684                                                                 }
685                                                         }
686                                                 } else {
687
688                                                         String simpleName = paramClass.getSimpleName();
689
690                                                         if ("Ipv4Address".equals(simpleName)
691                                                                         || "Ipv6Address".equals(simpleName) || "IpAddress".equals(simpleName)) {
692                                                                 
693                                                                 if ((paramValue != null) && (paramValue.length() > 0)) {
694                                                                         try {
695                                                                                 IpAddress ipAddr = IpAddressBuilder
696                                                                                                 .getDefaultInstance(paramValue);
697
698
699                                                                                 if ("Ipv4Address".equals(simpleName))
700                                                                                 {
701                                                                                         m.invoke(toObj, ipAddr.getIpv4Address());
702                                                                                 }
703                                                                                 else if ("Ipv6Address".equals(simpleName))
704                                                                                 {
705                                                                                         m.invoke(toObj, ipAddr.getIpv6Address());
706
707                                                                                 }
708                                                                                 else
709                                                                                 {
710                                                                                         m.invoke(toObj, ipAddr);
711                                                                                 }
712                                                                                 foundValue = true;
713                                                                         } catch (Exception e) {
714                                                                                 LOG.error(
715                                                                                                 "Caught exception calling "
716                                                                                                                 + toClass.getName() + "."
717                                                                                                                 + m.getName() + "("
718                                                                                                                 + paramValue + ")", e);
719
720                                                                         }
721                                                                 } else {
722                                                                         try {
723                                                                                 boolean isAccessible = m.isAccessible();
724                                                                                 if (!isAccessible) {
725                                                                                         m.setAccessible(true);
726                                                                                 }
727                                                                                 LOG.trace("Calling "
728                                                                                                 + toObj.getClass().getName()
729                                                                                                 + "." + m.getName() + "("
730                                                                                                 + paramValue + ")");
731                                                                                 m.invoke(toObj, paramValue);
732                                                                                 if (!isAccessible) {
733                                                                                         m.setAccessible(isAccessible);
734                                                                                 }
735                                                                                 foundValue = true;
736
737                                                                         } catch (Exception e) {
738                                                                                 LOG.error(
739                                                                                                 "Caught exception trying to call "
740                                                                                                                 + toClass.getName()
741                                                                                                                 + "."
742                                                                                                                 + m.getName()
743                                                                                                                 + "() with Properties entry",
744                                                                                                 e);
745                                                                         }
746                                                                 }
747                                                         } else if ("IpPrefix".equals(simpleName)) {
748                                                                         if ((paramValue != null) && (paramValue.length() > 0)) {
749                                                                                 try {
750                                                                                         IpPrefix ipPrefix = IpPrefixBuilder.getDefaultInstance(paramValue);
751                                                                                         m.invoke(toObj, ipPrefix);
752                                                                                         foundValue = true;
753                                                                                 } catch (Exception e) {
754                                                                                         LOG.error(
755                                                                                                         "Caught exception calling "
756                                                                                                                         + toClass.getName() + "."
757                                                                                                                         + m.getName() + "("
758                                                                                                                         + paramValue + ")", e);
759                                                                                 }
760                                                                         }
761                                                         } else {
762                                                                 // setter expects a yang-generated class. Need
763                                                                 // to
764                                                                 // create a builder to set it.
765
766                                                                 String builderName = paramClass.getName()
767                                                                                 + "Builder";
768                                                                 Class builderClass = null;
769                                                                 Object builderObj = null;
770                                                                 Object paramObj = null;
771
772                                                                 Object constObj = null;
773                                                                 
774                                                                 LOG.trace(m.getName()
775                                                                                 + " expects a yang-generated class - looking for builder "
776                                                                                 + builderName);
777                                                                 try {
778                                                                         builderClass = Class.forName(builderName);
779                                                                         builderObj = builderClass.newInstance();
780                                                                         paramObj = toBuilder(props, propNamePfx,
781                                                                                         builderObj);
782                                                                 } catch (ClassNotFoundException e) {
783                                                                         
784                                                                         if (paramValue == null) {
785                                                                                 try {
786                                                                                         boolean isAccessible = m
787                                                                                                         .isAccessible();
788                                                                                         if (!isAccessible) {
789                                                                                                 m.setAccessible(true);
790                                                                                         }
791                                                                                         LOG.trace("Calling "
792                                                                                                         + toObj.getClass()
793                                                                                                                         .getName() + "."
794                                                                                                         + m.getName() + "(null)");
795                                                                                         m.invoke(toObj, new Object[]{null});
796                                                                                         if (!isAccessible) {
797                                                                                                 m.setAccessible(isAccessible);
798                                                                                         }
799                                                                                         foundValue = true;
800
801                                                                                 } catch (Exception e1) {
802                                                                                         LOG.error(
803                                                                                                         "Caught exception trying to cally"
804                                                                                                                         + toClass.getName()
805                                                                                                                         + "."
806                                                                                                                         + m.getName()
807                                                                                                                         + "() with Properties entry",
808                                                                                                         e1);
809                                                                                 }
810                                                                         } else {
811                                                                                 try {
812                                                                                         // See if I can find a constructor I
813                                                                                         // can
814                                                                                         // use
815                                                                                         Constructor[] constructors = paramClass
816                                                                                                         .getConstructors();
817                                                                                         // Is there a String constructor?
818                                                                                         for (Constructor c : constructors) {
819                                                                                                 Class[] cParms = c
820                                                                                                                 .getParameterTypes();
821                                                                                                 if ((cParms != null)
822                                                                                                                 && (cParms.length == 1)) {
823                                                                                                         if (String.class
824                                                                                                                         .isAssignableFrom(cParms[0])) {
825                                                                                                                 constObj = c
826                                                                                                                                 .newInstance(paramValue);
827                                                                                                         }
828                                                                                                 }
829                                                                                         }
830
831                                                                                         if (constObj == null) {
832                                                                                                 // Is there a Long constructor?
833                                                                                                 for (Constructor c : constructors) {
834                                                                                                         Class[] cParms = c
835                                                                                                                         .getParameterTypes();
836                                                                                                         if ((cParms != null)
837                                                                                                                         && (cParms.length == 1)) {
838                                                                                                                 if (Long.class
839                                                                                                                                 .isAssignableFrom(cParms[0])) {
840                                                                                                                         constObj = c
841                                                                                                                                         .newInstance(Long
842                                                                                                                                                         .parseLong(paramValue));
843                                                                                                                 }
844                                                                                                         }
845                                                                                                 }
846
847                                                                                         }
848
849                                                                                         if (constObj == null) {
850
851                                                                                                 // Last chance - see if
852                                                                                                 // parameter class has a static
853                                                                                                 // method
854                                                                                                 // getDefaultInstance(String)
855                                                                                                 try {
856                                                                                                         Method gm = paramClass
857                                                                                                                         .getMethod(
858                                                                                                                                         "getDefaultInstance",
859                                                                                                                                         String.class);
860
861                                                                                                         int gmodifier = gm
862                                                                                                                         .getModifiers();
863                                                                                                         if (Modifier
864                                                                                                                         .isStatic(gmodifier)) {
865                                                                                                                 // Invoke static
866                                                                                                                 // getDefaultInstance(String)
867                                                                                                                 paramObj = gm.invoke(
868                                                                                                                                 null,
869                                                                                                                                 paramValue);
870                                                                                                         }
871
872                                                                                                 } catch (Exception gme) {
873                                                                                                         // Ignore exceptions
874                                                                                                 }
875                                                                                         }
876                                                                                         
877                                                                                         
878                                                                                 } catch (Exception e1) {
879                                                                                         LOG.warn(
880                                                                                                         "Could not find a suitable constructor for "
881                                                                                                                         + paramClass
882                                                                                                                                         .getName(),
883                                                                                                         e1);
884                                                                                 }
885
886                                                                                 if (constObj == null) {
887                                                                                         LOG.warn("Could not find builder class "
888                                                                                                         + builderName
889                                                                                                         + " and could not find a String or Long constructor or static getDefaultInstance(String) - trying just to set passing paramValue");
890
891                                                                                 }
892                                                                         }
893                                                                 } catch (Exception e) {
894                                                                         LOG.error(
895                                                                                         "Caught exception trying to create builder "
896                                                                                                         + builderName, e);
897                                                                 }
898
899                                                                 if (paramObj != null) {
900
901                                                                         try {
902
903                                                                                 Method buildMethod = builderClass
904                                                                                                 .getMethod("build");
905                                                                                 LOG.trace("Calling "
906                                                                                                 + paramObj.getClass().getName()
907                                                                                                 + "." + buildMethod.getName()
908                                                                                                 + "()");
909                                                                                 Object builtObj = buildMethod
910                                                                                                 .invoke(paramObj);
911
912                                                                                 boolean isAccessible = m.isAccessible();
913                                                                                 if (!isAccessible) {
914                                                                                         m.setAccessible(true);
915                                                                                 }
916
917                                                                                 LOG.trace("Calling "
918                                                                                                 + toObj.getClass().getName()
919                                                                                                 + "." + m.getName() + "()");
920                                                                                 m.invoke(toObj, builtObj);
921                                                                                 if (!isAccessible) {
922                                                                                         m.setAccessible(isAccessible);
923                                                                                 }
924                                                                                 foundValue = true;
925
926                                                                         } catch (Exception e) {
927                                                                                 LOG.error(
928                                                                                                 "Caught exception trying to set Yang-generated class expected by"
929                                                                                                                 + toClass.getName()
930                                                                                                                 + "."
931                                                                                                                 + m.getName()
932                                                                                                                 + "() from Properties entry",
933                                                                                                 e);
934                                                                         }
935                                                                 } else {
936                                                                         try {
937                                                                                 boolean isAccessible = m.isAccessible();
938                                                                                 if (!isAccessible) {
939                                                                                         m.setAccessible(true);
940                                                                                 }
941                                                                                 
942                                                                                 if (constObj != null) {
943
944                                                                                         LOG.trace("Calling "
945                                                                                                         + toObj.getClass()
946                                                                                                                         .getName() + "."
947                                                                                                         + m.getName() + "("
948                                                                                                         + constObj.toString() + ")");
949                                                                                         m.invoke(toObj, constObj);
950                                                                                 } else {
951                                                                                         LOG.trace("Calling "
952                                                                                                         + toObj.getClass()
953                                                                                                                         .getName() + "."
954                                                                                                         + m.getName() + "("
955                                                                                                         + paramValue + ")");
956                                                                                         m.invoke(toObj, paramValue);
957
958                                                                                 }
959                                                                                 if (!isAccessible) {
960                                                                                         m.setAccessible(isAccessible);
961                                                                                 }
962                                                                                 foundValue = true;
963
964                                                                         } catch (Exception e) {
965                                                                                 LOG.error(
966                                                                                                 "Caught exception trying to convert value returned by"
967                                                                                                                 + toClass.getName()
968                                                                                                                 + "."
969                                                                                                                 + m.getName()
970                                                                                                                 + "() to Properties entry",
971                                                                                                 e);
972                                                                         }
973                                                                 }
974                                                         }
975                                                 }
976                                         } else {
977
978                                                 // Setter's argument is not a yang-generated class. See
979                                                 // if it is a List.
980
981                                                 if (List.class.isAssignableFrom(paramClass)) {
982
983                                                         LOG.trace("Parameter class " + paramClass.getName()
984                                                                         + " is a List");
985
986                                                         // Figure out what type of args are in List and pass
987                                                         // that to toList().
988
989                                                         Type paramType = m.getGenericParameterTypes()[0];
990                                                         Type elementType = ((ParameterizedType) paramType)
991                                                                         .getActualTypeArguments()[0];
992                                                         Object paramObj = new LinkedList();
993                                                         try {
994                                                                 paramObj = toList(props, propName,
995                                                                                 (List) paramObj, (Class) elementType);
996                                                         } catch (Exception e) {
997                                                                 LOG.error("Caught exception trying to create list expected as argument to "
998                                                                                 + toClass.getName() + "." + m.getName());
999                                                         }
1000
1001                                                         if (paramObj != null) {
1002                                                                 try {
1003                                                                         boolean isAccessible = m.isAccessible();
1004                                                                         if (!isAccessible) {
1005                                                                                 m.setAccessible(true);
1006                                                                         }
1007                                                                         LOG.trace("Calling "
1008                                                                                         + toObj.getClass().getName() + "."
1009                                                                                         + m.getName() + "(" + paramValue
1010                                                                                         + ")");
1011                                                                         m.invoke(toObj, paramObj);
1012                                                                         if (!isAccessible) {
1013                                                                                 m.setAccessible(isAccessible);
1014                                                                         }
1015                                                                         foundValue = true;
1016
1017                                                                 } catch (Exception e) {
1018                                                                         LOG.error(
1019                                                                                         "Caught exception trying to convert List returned by"
1020                                                                                                         + toClass.getName() + "."
1021                                                                                                         + m.getName()
1022                                                                                                         + "() to Properties entry",
1023                                                                                         e);
1024                                                                 }
1025                                                         }
1026                                                 } else {
1027
1028                                                         // Setter expects something that is not a List and
1029                                                         // not yang-generated. Just pass the parameter value
1030
1031                                                         LOG.trace("Parameter class "
1032                                                                         + paramClass.getName()
1033                                                                         + " is not a yang-generated class or a List");
1034
1035                                                         if ((paramValue != null) && (paramValue.length() > 0)) {
1036
1037                                                                 Object constObj = null;
1038
1039                                                                 try {
1040                                                                         // See if I can find a constructor I can use
1041                                                                         Constructor[] constructors = paramClass
1042                                                                                         .getConstructors();
1043                                                                         // Is there a String constructor?
1044                                                                         for (Constructor c : constructors) {
1045                                                                                 Class[] cParms = c.getParameterTypes();
1046                                                                                 if ((cParms != null)
1047                                                                                                 && (cParms.length == 1)) {
1048                                                                                         if (String.class
1049                                                                                                         .isAssignableFrom(cParms[0])) {
1050                                                                                                 constObj = c
1051                                                                                                                 .newInstance(paramValue);
1052                                                                                         }
1053                                                                                 }
1054                                                                         }
1055
1056                                                                         if (constObj == null) {
1057                                                                                 // Is there a Long constructor?
1058                                                                                 for (Constructor c : constructors) {
1059                                                                                         Class[] cParms = c
1060                                                                                                         .getParameterTypes();
1061                                                                                         if ((cParms != null)
1062                                                                                                         && (cParms.length == 1)) {
1063                                                                                                 if (Long.class
1064                                                                                                                 .isAssignableFrom(cParms[0])) {
1065                                                                                                         constObj = c
1066                                                                                                                         .newInstance(Long
1067                                                                                                                                         .parseLong(paramValue));
1068                                                                                                 }
1069                                                                                         }
1070                                                                                 }
1071
1072                                                                         }
1073
1074                                                                         if (constObj != null) {
1075                                                                                 try {
1076                                                                                         LOG.trace("Calling "
1077                                                                                                         + toObj.getClass()
1078                                                                                                                         .getName() + "."
1079                                                                                                         + m.getName() + "("
1080                                                                                                         + constObj + ")");
1081                                                                                         m.invoke(toObj, constObj);
1082                                                                                         foundValue = true;
1083                                                                                 } catch (Exception e2) {
1084                                                                                         LOG.error(
1085                                                                                                         "Caught exception trying to call "
1086                                                                                                                         + m.getName(), e2);
1087                                                                                 }
1088                                                                         } else {
1089                                                                                 try {
1090                                                                                         boolean isAccessible = m
1091                                                                                                         .isAccessible();
1092                                                                                         if (!isAccessible) {
1093                                                                                                 m.setAccessible(true);
1094                                                                                         }
1095                                                                                         LOG.trace("Calling "
1096                                                                                                         + toObj.getClass()
1097                                                                                                                         .getName() + "."
1098                                                                                                         + m.getName() + "("
1099                                                                                                         + paramValue + ")");
1100                                                                                         m.invoke(toObj, paramValue);
1101                                                                                         if (!isAccessible) {
1102                                                                                                 m.setAccessible(isAccessible);
1103                                                                                         }
1104                                                                                         foundValue = true;
1105
1106                                                                                 } catch (Exception e) {
1107                                                                                         LOG.error(
1108                                                                                                         "Caught exception trying to convert value returned by"
1109                                                                                                                         + toClass.getName()
1110                                                                                                                         + "."
1111                                                                                                                         + m.getName()
1112                                                                                                                         + "() to Properties entry",
1113                                                                                                         e);
1114                                                                                 }
1115                                                                         }
1116                                                                 } catch (Exception e1) {
1117                                                                         LOG.warn(
1118                                                                                         "Could not find a suitable constructor for "
1119                                                                                                         + paramClass.getName(), e1);
1120                                                                 }
1121
1122
1123                                                         }
1124                                                 }
1125                                         }
1126                                 } // End of section handling "setter" method
1127                         } // End of loop through Methods
1128                 } // End of section handling yang-generated class
1129
1130                 if (foundValue) {
1131                         return (toObj);
1132                 } else {
1133                         return (null);
1134                 }
1135         }
1136
1137         public static void printPropertyList(PrintStream pstr, String pfx,
1138                         Class toClass) {
1139                 boolean foundValue = false;
1140
1141                 LOG.trace("Analyzing " + toClass.getName() + " class : pfx " + pfx);
1142
1143                 if (isYangGenerated(toClass)
1144                                 && (!Identifier.class.isAssignableFrom(toClass))) {
1145                         // Class is yang generated.
1146                         LOG.trace(toClass.getName() + " is a Yang-generated class");
1147
1148                         if (toClass.getName().endsWith("Key")) {
1149                                 if (Identifier.class.isAssignableFrom(toClass)) {
1150                                         LOG.trace(Identifier.class.getName()
1151                                                         + " is assignable from " + toClass.getName());
1152                                 } else {
1153
1154                                         LOG.trace(Identifier.class.getName()
1155                                                         + " is NOT assignable from " + toClass.getName());
1156                                 }
1157                         }
1158
1159                         String propNamePfx = null;
1160                         if (pfx.endsWith("]")) {
1161                                 propNamePfx = pfx;
1162                         } else {
1163
1164                                 if ((pfx != null) && (pfx.length() > 0)) {
1165                                         propNamePfx = pfx + "."
1166                                                         + toLowerHyphen(toClass.getSimpleName());
1167                                 } else {
1168                                         propNamePfx = toLowerHyphen(toClass.getSimpleName());
1169                                 }
1170
1171                                 if (propNamePfx.endsWith("-builder")) {
1172                                         propNamePfx = propNamePfx.substring(0, propNamePfx.length()
1173                                                         - "-builder".length());
1174                                 }
1175
1176                                 if (propNamePfx.endsWith("-impl")) {
1177                                         propNamePfx = propNamePfx.substring(0, propNamePfx.length()
1178                                                         - "-impl".length());
1179                                 }
1180                         }
1181
1182                         // Iterate through getter methods to figure out values we need to
1183                         // set
1184
1185                         for (Method m : toClass.getMethods()) {
1186                                 LOG.trace("Is " + m.getName() + " method a getter?");
1187                                 if (isGetter(m)) {
1188                                         LOG.trace(m.getName() + " is a getter");
1189                                         Class returnClass = m.getReturnType();
1190
1191                                         String fieldName = toLowerHyphen(m.getName().substring(3));
1192                                         fieldName = fieldName.substring(0, 1).toLowerCase()
1193                                                         + fieldName.substring(1);
1194
1195                                         String propName = propNamePfx + "." + fieldName;
1196
1197                                         // Is the return type a yang generated class?
1198                                         if (isYangGenerated(returnClass)) {
1199                                                 // Is it an enum?
1200                                                 if (returnClass.isEnum()) {
1201
1202                                                         LOG.trace(m.getName() + " is an Enum");
1203                                                         pstr.print("\n\n     * " + propName);
1204
1205                                                 } else {
1206                                                         
1207                                                         String simpleName = returnClass.getSimpleName();
1208                                                         
1209                                                         if ("Ipv4Address".equals(simpleName) || "Ipv6Address".equals(simpleName) || "IpAddress".equals(simpleName) || "IpPrefix".equals(simpleName)) {
1210                                                                 LOG.trace(m.getName()+" is an "+simpleName);
1211                                                                 pstr.print("\n\n     * " + propName);
1212                                                         } else {
1213                                                                 printPropertyList(pstr, propNamePfx, returnClass);
1214                                                         }
1215
1216                                                 }
1217                                         } else {
1218
1219                                                 // Setter's argument is not a yang-generated class. See
1220                                                 // if it is a List.
1221
1222                                                 if (List.class.isAssignableFrom(returnClass)) {
1223
1224                                                         LOG.trace("Parameter class "
1225                                                                         + returnClass.getName() + " is a List");
1226
1227                                                         // Figure out what type of args are in List and pass
1228                                                         // that to toList().
1229
1230                                                         Type returnType = m.getGenericReturnType();
1231                                                         Type elementType = ((ParameterizedType) returnType)
1232                                                                         .getActualTypeArguments()[0];
1233                                                         Class elementClass = (Class) elementType;
1234                                                         LOG.trace("Calling printPropertyList on list type ("
1235                                                                         + elementClass.getName()
1236                                                                         + "), pfx is ("
1237                                                                         + pfx
1238                                                                         + "), toClass is ("
1239                                                                         + toClass.getName() + ")");
1240                                                         printPropertyList(
1241                                                                         pstr,
1242                                                                         propNamePfx
1243                                                                                         + "."
1244                                                                                         + toLowerHyphen(elementClass
1245                                                                                                         .getSimpleName()) + "[]",
1246                                                                         elementClass);
1247
1248                                                 } else if (!returnClass.equals(Class.class)) {
1249
1250                                                         // Setter expects something that is not a List and
1251                                                         // not yang-generated. Just pass the parameter value
1252
1253                                                         LOG.trace("Parameter class "
1254                                                                         + returnClass.getName()
1255                                                                         + " is not a yang-generated class or a List");
1256
1257                                                         pstr.print("\n\n     * " + propName);
1258
1259                                                 }
1260                                         }
1261                                 } // End of section handling "setter" method
1262                         } // End of loop through Methods
1263                 } // End of section handling yang-generated class
1264
1265         }
1266
1267         public static boolean isYangGenerated(Class c) {
1268                 if (c == null) {
1269                         return (false);
1270                 } else {
1271                         return (c.getName().startsWith("org.opendaylight.yang.gen."));
1272                 }
1273         }
1274         
1275         public static boolean isIpPrefix(Class c) {
1276                 
1277                 if (c == null ) {
1278                         return (false);
1279                 }
1280                 String simpleName = c.getSimpleName();
1281                 return ("IpPrefix".equals(simpleName)) ;
1282         }
1283         
1284         
1285         
1286         public static boolean isIpv4Address(Class c) {
1287                 
1288                 if (c == null ) {
1289                         return (false);
1290                 }
1291                 String simpleName = c.getSimpleName();
1292                 return ("Ipv4Address".equals(simpleName)) ;
1293         }
1294         
1295         public static boolean isIpv6Address(Class c) {
1296                 
1297                 if (c == null ) {
1298                         return (false);
1299                 } 
1300                 String simpleName = c.getSimpleName();
1301                 return ("Ipv6Address".equals(simpleName)) ;
1302         }
1303         
1304         public static boolean isIpAddress(Class c) {
1305                 
1306                 if (c == null ) {
1307                         return (false);
1308                 } 
1309                 String simpleName = c.getSimpleName();
1310                 return ("IpAddress".equals(simpleName)) ;
1311         }
1312
1313         public static String toLowerHyphen(String inStr) {
1314                 if (inStr == null) {
1315                         return (null);
1316                 }
1317
1318                 String str = inStr.substring(0, 1).toLowerCase();
1319                 if (inStr.length() > 1) {
1320                         str = str + inStr.substring(1);
1321                 }
1322
1323                 String regex = "(([a-z0-9])([A-Z]))";
1324                 String replacement = "$2-$3";
1325
1326                 String retval = str.replaceAll(regex, replacement).toLowerCase();
1327
1328                 LOG.trace("Converting " + inStr + " => " + str + " => " + retval);
1329                 return (retval);
1330         }
1331
1332         public static String toUpperCamelCase(String inStr) {
1333                 if (inStr == null) {
1334                         return (null);
1335                 } else if (inStr.length() == 0) {
1336                         return(inStr);
1337                 }
1338
1339                 String[] terms = inStr.split("-");
1340                 StringBuffer sbuff = new StringBuffer();
1341                 // Check if string begins with a digit
1342                 if (Character.isDigit(inStr.charAt(0))) {
1343                         sbuff.append('_');
1344                 }
1345                 for (String term : terms) {
1346                         sbuff.append(term.substring(0, 1).toUpperCase());
1347                         if (term.length() > 1) {
1348                                 sbuff.append(term.substring(1));
1349                         }
1350                 }
1351                 return (sbuff.toString());
1352
1353         }
1354
1355         public static boolean isGetter(Method m) {
1356                 if (m == null) {
1357                         return (false);
1358                 }
1359
1360                 if (Modifier.isPublic(m.getModifiers())
1361                                 && (m.getParameterTypes().length == 0)) {
1362                         if (m.getName().matches("^get[A-Z].*")
1363                                         && !m.getReturnType().equals(void.class)) {
1364                                 if (!"getClass".equals(m.getName())) {
1365                                         return (true);
1366                                 }
1367                         }
1368
1369                         if (m.getName().matches("^get[A-Z].*")
1370                                         && m.getReturnType().equals(boolean.class)) {
1371                                 return (true);
1372                         }
1373                         
1374                         if (m.getName().matches("^is[A-Z].*")
1375                                         && m.getReturnType().equals(Boolean.class)) {
1376                                 return(true);
1377                         }
1378                 }
1379
1380                 return (false);
1381         }
1382
1383         public static boolean isSetter(Method m) {
1384                 if (m == null) {
1385                         return (false);
1386                 }
1387
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])) {
1394                                         return (false);
1395                                 } else {
1396                                         return (true);
1397                                 }
1398                         }
1399
1400                 }
1401
1402                 return (false);
1403         }
1404
1405 }