- // Is the return type a yang generated class?
- if (isYangGenerated(paramClass)) {
- // Is it an enum?
- if (paramClass.isEnum()) {
-
- LOG.trace(m.getName() + " expects an Enum");
- // Param type is a typedef.
- if ((paramValue != null) && (paramValue.length() > 0)) {
- Object paramObj = null;
-
- try {
- paramObj = Enum.valueOf(paramClass,
- toUpperCamelCase(paramValue));
- } catch (Exception e) {
- LOG.error(
- "Caught exception trying to convert field "
- + propName + " to enum "
- + paramClass.getName(), e);
- }
-
- try {
- boolean isAccessible = m.isAccessible();
- if (!isAccessible) {
- m.setAccessible(true);
- }
-
- LOG.trace("Calling "
- + toObj.getClass().getName() + "."
- + m.getName() + "(" + paramValue
- + ")");
- m.invoke(toObj, paramObj);
-
- if (!isAccessible) {
- m.setAccessible(isAccessible);
- }
- foundValue = true;
-
- } catch (Exception e) {
- LOG.error(
- "Caught exception trying to create Yang-generated enum expected by"
- + toClass.getName()
- + "."
- + m.getName()
- + "() from Properties entry",
- e);
- }
- }
- } else {
-
- String simpleName = paramClass.getSimpleName();
-
- if ("Ipv4Address".equals(simpleName)
- || "Ipv6Address".equals(simpleName) || "IpAddress".equals(simpleName)) {
-
- if ((paramValue != null) && (paramValue.length() > 0)) {
- try {
- IpAddress ipAddr = IpAddressBuilder
- .getDefaultInstance(paramValue);
-
-
- if ("Ipv4Address".equals(simpleName))
- {
- m.invoke(toObj, ipAddr.getIpv4Address());
- }
- else if ("Ipv6Address".equals(simpleName))
- {
- m.invoke(toObj, ipAddr.getIpv6Address());
-
- }
- else
- {
- m.invoke(toObj, ipAddr);
- }
- foundValue = true;
- } catch (Exception e) {
- LOG.error(
- "Caught exception calling "
- + toClass.getName() + "."
- + m.getName() + "("
- + paramValue + ")", e);
-
- }
- } else {
- try {
- boolean isAccessible = m.isAccessible();
- if (!isAccessible) {
- m.setAccessible(true);
- }
- LOG.trace("Calling "
- + toObj.getClass().getName()
- + "." + m.getName() + "("
- + paramValue + ")");
- m.invoke(toObj, paramValue);
- if (!isAccessible) {
- m.setAccessible(isAccessible);
- }
- foundValue = true;
-
- } catch (Exception e) {
- LOG.error(
- "Caught exception trying to call "
- + toClass.getName()
- + "."
- + m.getName()
- + "() with Properties entry",
- e);
- }
- }
- } else if ("IpPrefix".equals(simpleName)) {
- if ((paramValue != null) && (paramValue.length() > 0)) {
- try {
- IpPrefix ipPrefix = IpPrefixBuilder.getDefaultInstance(paramValue);
- m.invoke(toObj, ipPrefix);
- foundValue = true;
- } catch (Exception e) {
- LOG.error(
- "Caught exception calling "
- + toClass.getName() + "."
- + m.getName() + "("
- + paramValue + ")", e);
- }
- }
- } else {
- // setter expects a yang-generated class. Need
- // to
- // create a builder to set it.
-
- String builderName = paramClass.getName()
- + "Builder";
- Class builderClass = null;
- Object builderObj = null;
- Object paramObj = null;
-
- Object constObj = null;
-
- LOG.trace(m.getName()
- + " expects a yang-generated class - looking for builder "
- + builderName);
- try {
- builderClass = Class.forName(builderName);
- builderObj = builderClass.newInstance();
- paramObj = toBuilder(props, propNamePfx,
- builderObj);
- } catch (ClassNotFoundException e) {
-
- if (paramValue == null) {
- try {
- boolean isAccessible = m
- .isAccessible();
- if (!isAccessible) {
- m.setAccessible(true);
- }
- LOG.trace("Calling "
- + toObj.getClass()
- .getName() + "."
- + m.getName() + "(null)");
- m.invoke(toObj, new Object[]{null});
- if (!isAccessible) {
- m.setAccessible(isAccessible);
- }
- foundValue = true;
-
- } catch (Exception e1) {
- LOG.error(
- "Caught exception trying to cally"
- + toClass.getName()
- + "."
- + m.getName()
- + "() with Properties entry",
- e1);
- }
- } else {
- try {
- // See if I can find a constructor I
- // can
- // use
- Constructor[] constructors = paramClass
- .getConstructors();
- // Is there a String constructor?
- for (Constructor c : constructors) {
- Class[] cParms = c
- .getParameterTypes();
- if ((cParms != null)
- && (cParms.length == 1)) {
- if (String.class
- .isAssignableFrom(cParms[0])) {
- constObj = c
- .newInstance(paramValue);
- }
- }
- }
-
- if (constObj == null) {
- // Is there a Long constructor?
- for (Constructor c : constructors) {
- Class[] cParms = c
- .getParameterTypes();
- if ((cParms != null)
- && (cParms.length == 1)) {
- if (Long.class
- .isAssignableFrom(cParms[0])) {
- constObj = c
- .newInstance(Long
- .parseLong(paramValue));
- }
- }
- }
-
- }
-
- if (constObj == null) {
-
- // Last chance - see if
- // parameter class has a static
- // method
- // getDefaultInstance(String)
- try {
- Method gm = paramClass
- .getMethod(
- "getDefaultInstance",
- String.class);
-
- int gmodifier = gm
- .getModifiers();
- if (Modifier
- .isStatic(gmodifier)) {
- // Invoke static
- // getDefaultInstance(String)
- paramObj = gm.invoke(
- null,
- paramValue);
- }
-
- } catch (Exception gme) {
- // Ignore exceptions
- }
- }
-
-
- } catch (Exception e1) {
- LOG.warn(
- "Could not find a suitable constructor for "
- + paramClass
- .getName(),
- e1);
- }
-
- if (constObj == null) {
- LOG.warn("Could not find builder class "
- + builderName
- + " and could not find a String or Long constructor or static getDefaultInstance(String) - trying just to set passing paramValue");
-
- }
- }
- } catch (Exception e) {
- LOG.error(
- "Caught exception trying to create builder "
- + builderName, e);
- }
-
- if (paramObj != null) {
-
- try {
-
- Method buildMethod = builderClass
- .getMethod("build");
- LOG.trace("Calling "
- + paramObj.getClass().getName()
- + "." + buildMethod.getName()
- + "()");
- Object builtObj = buildMethod
- .invoke(paramObj);
-
- boolean isAccessible = m.isAccessible();
- if (!isAccessible) {
- m.setAccessible(true);
- }
-
- LOG.trace("Calling "
- + toObj.getClass().getName()
- + "." + m.getName() + "()");
- m.invoke(toObj, builtObj);
- if (!isAccessible) {
- m.setAccessible(isAccessible);
- }
- foundValue = true;
-
- } catch (Exception e) {
- LOG.error(
- "Caught exception trying to set Yang-generated class expected by"
- + toClass.getName()
- + "."
- + m.getName()
- + "() from Properties entry",
- e);
- }
- } else {
- try {
- boolean isAccessible = m.isAccessible();
- if (!isAccessible) {
- m.setAccessible(true);
- }
-
- if (constObj != null) {
-
- LOG.trace("Calling "
- + toObj.getClass()
- .getName() + "."
- + m.getName() + "("
- + constObj.toString() + ")");
- m.invoke(toObj, constObj);
- } else {
- LOG.trace("Calling "
- + toObj.getClass()
- .getName() + "."
- + m.getName() + "("
- + paramValue + ")");
- m.invoke(toObj, paramValue);
-
- }
- if (!isAccessible) {
- m.setAccessible(isAccessible);
- }
- foundValue = true;
-
- } catch (Exception e) {
- LOG.error(
- "Caught exception trying to convert value returned by"
- + toClass.getName()
- + "."
- + m.getName()
- + "() to Properties entry",
- e);
- }
- }
- }
- }
- } else {