* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
import java.io.File;
import java.io.FileInputStream;
+import java.io.IOException;\r
import java.io.InputStream;
import java.io.PrintStream;
import java.lang.reflect.Constructor;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Dscp;\r
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddressBuilder;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefixBuilder;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;\r
import org.opendaylight.yangtools.yang.binding.Identifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
}
} catch (Exception e) {
LOG.error(
- "Caught exception trying to convert Yang-generated enum returned by " + fromClass
- .getName() + "." + m.getName() + "() to Properties entry", e);
+ "Caught exception trying to convert Yang-generated enum returned by "
+ + fromClass.getName() + "." + m.getName() + "() to Properties entry",
+ e);
}
} else if (isIpv4Address(returnType)) {
// Save its value
}
} catch (Exception e) {
- LOG.error(
- "Caught exception trying to convert value returned by " + fromClass.getName() + "."
- + m.getName() + "() to Properties entry", e);
+ LOG.error("Caught exception trying to convert value returned by " + fromClass.getName()
+ + "." + m.getName() + "() to Properties entry", e);
}
} else if (isIpv6Address(returnType)) {
// Save its value
}
} catch (Exception e) {
- LOG.error(
- "Caught exception trying to convert value returned by " + fromClass.getName() + "."
- + m.getName() + "() to Properties entry", e);
+ LOG.error("Caught exception trying to convert value returned by " + fromClass.getName()
+ + "." + m.getName() + "() to Properties entry", e);
}
} else if (isIpAddress(returnType)) {
// Save its value
}
} catch (Exception e) {
- LOG.error(
- "Caught exception trying to convert value returned by " + fromClass.getName() + "."
- + m.getName() + "() to Properties entry", e);
+ LOG.error("Caught exception trying to convert value returned by " + fromClass.getName()
+ + "." + m.getName() + "() to Properties entry", e);
}
} else if (isIpPrefix(returnType)) {
// Save its value
}
} catch (Exception e) {
- LOG.error(
- "Caught exception trying to convert value returned by " + fromClass.getName() + "."
- + m.getName() + "() to Properties entry", e);
+ LOG.error("Caught exception trying to convert value returned by " + fromClass.getName()
+ + "." + m.getName() + "() to Properties entry", e);
}
+ } else if (isPortNumber(returnType)) {\r
+ // Save its value\r
+ try {\r
+ String propName = propNamePfx + "." + fieldName;\r
+ boolean isAccessible = m.isAccessible();\r
+ if (!isAccessible) {\r
+ m.setAccessible(true);\r
+ }\r
+ PortNumber retValue = (PortNumber) m.invoke(fromObj);\r
+ if (!isAccessible) {\r
+ m.setAccessible(isAccessible);\r
+ }\r
+\r
+ if (retValue != null) {\r
+ propVal = "" + retValue.getValue();\r
+ LOG.debug("Setting property " + propName + " to " + propVal);\r
+ props.setProperty(propName, propVal);\r
+\r
+ }\r
+ } catch (Exception e) {\r
+ LOG.error("Caught exception trying to convert value returned by " + fromClass.getName()\r
+ + "." + m.getName() + "() to Properties entry", e);\r
+ }\r
+ } else if (isDscp(returnType)) {\r
+ // Save its value\r
+ try {\r
+ String propName = propNamePfx + "." + fieldName;\r
+ boolean isAccessible = m.isAccessible();\r
+ if (!isAccessible) {\r
+ m.setAccessible(true);\r
+ }\r
+ Dscp retValue = (Dscp) m.invoke(fromObj);\r
+ if (!isAccessible) {\r
+ m.setAccessible(isAccessible);\r
+ }\r
+\r
+ if (retValue != null) {\r
+ propVal = "" + retValue.getValue();\r
+ LOG.debug("Setting property " + propName + " to " + propVal);\r
+ props.setProperty(propName, propVal);\r
+\r
+ }\r
+ } catch (Exception e) {\r
+ LOG.error("Caught exception trying to convert value returned by " + fromClass.getName()\r
+ + "." + m.getName() + "() to Properties entry", e);\r
+ }\r
} else {
try {
boolean isAccessible = m.isAccessible();
LOG.trace(m.getName() + " returns a byte[]");
retValue = new String((byte[]) retValue, "UTF-8");
LOG.trace("Converted byte array " + propNamePfx + "." + fieldName + "to string "
- + retValue);
+ + retValue);
}
if (!isAccessible) {
m.setAccessible(isAccessible);
if (m.getName().equals("getKey")) {
LOG.trace("Caught " + e.getClass().getName()
- + " exception trying to convert results from getKey() - ignoring");
+ + " exception trying to convert results from getKey() - ignoring");
} else {
LOG.error("Caught exception trying to convert Yang-generated class returned by"
- + fromClass.getName() + "." + m.getName() + "() to Properties entry", e);
+ + fromClass.getName() + "." + m.getName() + "() to Properties entry", e);
}
}
}
Type elementType = ((ParameterizedType) paramType).getActualTypeArguments()[0];
toProperties(props, propNamePfx + "." + fieldName, retList, (Class) elementType);
} catch (Exception e) {
- LOG.error(
- "Caught exception trying to convert List returned by " + fromClass.getName() + "." + m
- .getName() + "() to Properties entry", e);
+ LOG.error("Caught exception trying to convert List returned by " + fromClass.getName() + "."
+ + m.getName() + "() to Properties entry", e);
}
} else {
LOG.trace(m.getName() + " returns a byte[]");
propVal = new String((byte[]) propValObj, "UTF-8");
LOG.trace("Converted byte array " + propNamePfx + "." + fieldName + "to string "
- + propVal);
+ + propVal);
} else {
propVal = propValObj.toString();
} catch (Exception e) {
if (m.getName().equals("getKey")) {
LOG.trace("Caught " + e.getClass().getName()
- + " exception trying to convert results from getKey() - ignoring");
+ + " exception trying to convert results from getKey() - ignoring");
} else {
- LOG.error(
- "Caught exception trying to convert value returned by" + fromClass.getName() + "."
- + m.getName() + "() to Properties entry", e);
+ LOG.error("Caught exception trying to convert value returned by" + fromClass.getName()
+ + "." + m.getName() + "() to Properties entry", e);
}
}
}
// "getValue", then
// set value identified by "prefix" to that one value.
if ((numGetters == 1) && ("getValue".equals(lastGetterName))) {
- LOG.trace(
- "getValueFIX : " + propNamePfx + " only has getValue() getter - setting " + propNamePfx + " = "
- + propVal);
+ LOG.trace("getValueFIX : " + propNamePfx + " only has getValue() getter - setting " + propNamePfx
+ + " = " + propVal);
props.setProperty(propNamePfx, propVal);
} else {
LOG.trace("getValueFIX : " + propNamePfx + " has " + numGetters + " getter(s), last one found was "
- + lastGetterName);
+ + lastGetterName);
}
String paramValue = props.getProperty(propName);
if (paramValue == null) {
LOG.trace(propName + " is unset");
+ } else if ("".equals(paramValue)) {\r
+ LOG.trace(propName + " was set to the empty string, setting it to null");\r
+ paramValue = null;\r
} else {
LOG.trace(propName + " = " + paramValue);
}
paramObj = Enum.valueOf(paramClass, toJavaEnum(paramValue));
} catch (Exception e) {
LOG.error("Caught exception trying to convert field " + propName + " to enum "
- + paramClass.getName(), e);
+ + paramClass.getName(), e);
}
try {
m.setAccessible(true);
}
- LOG.trace(
- "Calling " + toObj.getClass().getName() + "." + m.getName() + "(" + paramValue
- + ")");
+ LOG.trace("Calling " + toObj.getClass().getName() + "." + m.getName() + "("
+ + paramValue + ")");
m.invoke(toObj, paramObj);
if (!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);
+ 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 ("Ipv4Address".equals(simpleName) || "Ipv6Address".equals(simpleName)
+ || "IpAddress".equals(simpleName)) {
if ((paramValue != null) && (paramValue.length() > 0)) {
try {
}
foundValue = true;
} catch (Exception e) {
- LOG.error(
- "Caught exception calling " + toClass.getName() + "." + m.getName() + "("
- + paramValue + ")", e);
+ LOG.error("Caught exception calling " + toClass.getName() + "." + m.getName()
+ + "(" + paramValue + ")", e);
}
} else {
m.setAccessible(true);
}
LOG.trace("Calling " + toObj.getClass().getName() + "." + m.getName() + "("
- + paramValue + ")");
+ + 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);
+ LOG.error("Caught exception trying to call " + toClass.getName() + "."
+ + m.getName() + "() with Properties entry", e);
}
}
} else if ("IpPrefix".equals(simpleName)) {
m.invoke(toObj, ipPrefix);
foundValue = true;
} catch (Exception e) {
- LOG.error(
- "Caught exception calling " + toClass.getName() + "." + m.getName() + "("
- + paramValue + ")", e);
+ LOG.error("Caught exception calling " + toClass.getName() + "." + m.getName()
+ + "(" + paramValue + ")", e);
}
}
+ } else if ("PortNumber".equals(simpleName)) {\r
+ if ((paramValue != null) && (paramValue.length() > 0)) {\r
+ try {\r
+ PortNumber portNumber = PortNumber.getDefaultInstance(paramValue);\r
+ m.invoke(toObj, portNumber);\r
+ foundValue = true;\r
+ } catch (Exception e) {\r
+ LOG.error("Caught exception calling " + toClass.getName() + "." + m.getName()\r
+ + "(" + paramValue + ")", e);\r
+ }\r
+ }\r
+ } else if ("Dscp".equals(simpleName)) {\r
+ if ((paramValue != null) && (paramValue.length() > 0)) {\r
+ try {\r
+ Dscp dscp = Dscp.getDefaultInstance(paramValue);\r
+ m.invoke(toObj, dscp);\r
+ foundValue = true;\r
+ } catch (Exception e) {\r
+ LOG.error("Caught exception calling " + toClass.getName() + "." + m.getName()\r
+ + "(" + paramValue + ")", e);\r
+ }\r
+ }\r
} else {
// setter expects a yang-generated class. Need
// to
Object constObj = null;
LOG.trace(m.getName() + " expects a yang-generated class - looking for builder "
- + builderName);
+ + builderName);
try {
builderClass = Class.forName(builderName);
builderObj = builderClass.newInstance();
if (!isAccessible) {
m.setAccessible(true);
}
- LOG.trace(
- "Calling " + toObj.getClass().getName() + "." + m.getName() + "(null)");
- m.invoke(toObj, new Object[]{null});
+ 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);
+ LOG.error("Caught exception trying to cally" + toClass.getName() + "."
+ + m.getName() + "() with Properties entry", e1);
}
} else {
try {
// method
// getDefaultInstance(String)
try {
- Method gm = paramClass
- .getMethod("getDefaultInstance", String.class);
+ Method gm =
+ paramClass.getMethod("getDefaultInstance", String.class);
int gmodifier = gm.getModifiers();
if (Modifier.isStatic(gmodifier)) {
} catch (Exception gme) {
LOG.info("Unable to find static method getDefaultInstance for "
- + "class {}", paramClass.getSimpleName(), gme);
+ + "class {}", paramClass.getSimpleName(), gme);
}
}
} catch (Exception e1) {
LOG.warn(
- "Could not find a suitable constructor for " + paramClass.getName(),
- e1);
+ "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");
+ + " and could not find a String or Long constructor or static "
+ + "getDefaultInstance(String) - trying just to set passing paramValue");
}
}
try {
Method buildMethod = builderClass.getMethod("build");
- LOG.trace(
- "Calling " + paramObj.getClass().getName() + "." + buildMethod.getName()
- + "()");
+ LOG.trace("Calling " + paramObj.getClass().getName() + "."
+ + buildMethod.getName() + "()");
Object builtObj = buildMethod.invoke(paramObj);
boolean isAccessible = m.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);
+ LOG.error("Caught exception trying to set Yang-generated class expected by"
+ + toClass.getName() + "." + m.getName() + "() from Properties entry",
+ e);
}
} else {
try {
if (constObj != null) {
LOG.trace("Calling " + toObj.getClass().getName() + "." + m.getName() + "("
- + constObj.toString() + ")");
+ + constObj.toString() + ")");
m.invoke(toObj, constObj);
} else {
LOG.trace("Calling " + toObj.getClass().getName() + "." + m.getName() + "("
- + paramValue + ")");
+ + paramValue + ")");
m.invoke(toObj, paramValue);
}
foundValue = true;
} catch (Exception e) {
- LOG.error(
- "Caught exception trying to convert value returned by" + toClass.getName()
- + "." + m.getName() + "() to Properties entry", e);
+ LOG.error("Caught exception trying to convert value returned by"
+ + toClass.getName() + "." + m.getName() + "() to Properties entry", e);
}
}
}
paramObj = toList(props, propName, (List) paramObj, (Class) elementType);
} catch (Exception e) {
LOG.error("Caught exception trying to create list expected as argument to {}.{}",
- toClass.getName(), m.getName(), e);
+ toClass.getName(), m.getName(), e);
}
if (paramObj != null) {
if (!isAccessible) {
m.setAccessible(true);
}
- LOG.trace(
- "Calling " + toObj.getClass().getName() + "." + m.getName() + "(" + paramValue
- + ")");
+ 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 convert List returned by" + toClass.getName() + "."
- + m.getName() + "() to Properties entry", e);
+ LOG.error("Caught exception trying to convert List returned by" + toClass.getName()
+ + "." + m.getName() + "() to Properties entry", e);
}
}
} else {
// Setter expects something that is not a List and
// not yang-generated. Just pass the parameter value
- LOG.trace(
- "Parameter class " + paramClass.getName() + " is not a yang-generated class or a List");
+ LOG.trace("Parameter class " + paramClass.getName()
+ + " is not a yang-generated class or a List");
if ((paramValue != null) && (paramValue.length() > 0)) {
if (constObj != null) {
try {
LOG.trace("Calling " + toObj.getClass().getName() + "." + m.getName() + "("
- + constObj + ")");
+ + constObj + ")");
m.invoke(toObj, constObj);
foundValue = true;
} catch (Exception e2) {
m.setAccessible(true);
}
LOG.trace("Calling " + toObj.getClass().getName() + "." + m.getName() + "("
- + paramValue + ")");
+ + 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);
+ LOG.error("Caught exception trying to convert value returned by"
+ + toClass.getName() + "." + m.getName() + "() to Properties entry",
+ e);
}
}
} catch (Exception e1) {
String simpleName = returnClass.getSimpleName();
- if ("Ipv4Address".equals(simpleName) || "Ipv6Address".equals(simpleName) || "IpAddress"
- .equals(simpleName) || "IpPrefix".equals(simpleName)) {
+ if ("Ipv4Address".equals(simpleName) || "Ipv6Address".equals(simpleName)
+ || "IpAddress".equals(simpleName) || "IpPrefix".equals(simpleName)\r
+ || "PortNumber".equals(simpleName) || "Dscp".equals(simpleName)) {\r
LOG.trace(m.getName() + " is an " + simpleName);
pstr.print("\n\n * " + propName);
} else {
Type returnType = m.getGenericReturnType();
Type elementType = ((ParameterizedType) returnType).getActualTypeArguments()[0];
Class elementClass = (Class) elementType;
- LOG.trace(
- "Calling printPropertyList on list type (" + elementClass.getName() + "), pfx is ("
- + pfx + "), toClass is (" + toClass.getName() + ")");
+ LOG.trace("Calling printPropertyList on list type (" + elementClass.getName()
+ + "), pfx is (" + pfx + "), toClass is (" + toClass.getName() + ")");
printPropertyList(pstr,
- propNamePfx + "." + toLowerHyphen(elementClass.getSimpleName()) + "[]", elementClass);
+ propNamePfx + "." + toLowerHyphen(elementClass.getSimpleName()) + "[]",
+ elementClass);
} else if (!returnClass.equals(Class.class)) {
// not yang-generated. Just pass the parameter value
LOG.trace("Parameter class " + returnClass.getName()
- + " is not a yang-generated class or a List");
+ + " is not a yang-generated class or a List");
pstr.print("\n\n * " + propName);
}
if (c == null) {
return (false);
}
+ if (!isIetfInet(c)) {\r
+ return (false);\r
+ }\r
String simpleName = c.getSimpleName();
return ("IpPrefix".equals(simpleName));
}
if (c == null) {
return (false);
}
+ if (!isIetfInet(c)) {\r
+ return (false);\r
+ }\r
String simpleName = c.getSimpleName();
return ("Ipv4Address".equals(simpleName));
}
if (c == null) {
return (false);
}
+ if (!isIetfInet(c)) {\r
+ return (false);\r
+ }\r
String simpleName = c.getSimpleName();
return ("Ipv6Address".equals(simpleName));
}
if (c == null) {
return (false);
}
+ if (!isIetfInet(c)) {\r
+ return (false);\r
+ }\r
String simpleName = c.getSimpleName();
return ("IpAddress".equals(simpleName));
}
+ public static boolean isPortNumber(Class c) {\r
+\r
+ if (c == null) {\r
+ return (false);\r
+ }\r
+ if (!isIetfInet(c)) {\r
+ return (false);\r
+ }\r
+\r
+ String simpleName = c.getSimpleName();\r
+ return ("PortNumber".equals(simpleName));\r
+ }\r
+\r
+ public static boolean isDscp(Class c) {\r
+\r
+ if (c == null) {\r
+ return (false);\r
+ }\r
+ if (!isIetfInet(c)) {\r
+ return (false);\r
+ }\r
+ String simpleName = c.getSimpleName();\r
+ return ("Dscp".equals(simpleName));\r
+ }\r
+\r
+ public static boolean isIetfInet(Class c) {\r
+\r
+ Package p = c.getPackage();\r
+ if (p != null) {\r
+ String pkgName = p.getName();\r
+\r
+ if ((pkgName != null) && (pkgName.indexOf("yang.ietf.inet.types") > -1)) {\r
+ return (true);\r
+ }\r
+ }\r
+\r
+ return (false);\r
+ }\r
+\r
public static String toLowerHyphen(String inStr) {
if (inStr == null) {
return (null);
return (retval);
}
- //This is called when mapping the yang value back to a valid java enumeration
+ // This is called when mapping the yang value back to a valid java enumeration
public static String toJavaEnum(String inStr) {
if (inStr == null) {
return (null);
return (inStr);
}
- //This will strip out all periods, which cannot be in a java enum
+ // This will strip out all periods, which cannot be in a java enum
inStr = inStr.replaceAll("\\.", "");
String[] terms = inStr.split("-");
StringBuffer sbuff = new StringBuffer();
- //appends an _ if the string starts with a digit to make it a valid java enum
+ // appends an _ if the string starts with a digit to make it a valid java enum
if (Character.isDigit(inStr.charAt(0))) {
sbuff.append('_');
}
- //If the string contains hyphens it will convert the string to upperCamelCase without hyphens
+ // If the string contains hyphens it will convert the string to upperCamelCase without hyphens
for (String term : terms) {
sbuff.append(term.substring(0, 1).toUpperCase());
if (term.length() > 1) {
if (Modifier.isPublic(m.getModifiers()) && (m.getParameterTypes().length == 1)) {
if (m.getName().matches("^set[A-Z].*")) {
Class[] paramTypes = m.getParameterTypes();
- if (paramTypes[0].isAssignableFrom(Identifier.class) || Identifier.class
- .isAssignableFrom(paramTypes[0])) {
+ if (paramTypes[0].isAssignableFrom(Identifier.class)
+ || Identifier.class.isAssignableFrom(paramTypes[0])) {
return (false);
} else {
return (true);
return "/opt/bvc/controller/configuration/" + propertiesFileName;
}
- //This is called when mapping a valid java enumeration back to the yang model value
+ // This is called when mapping a valid java enumeration back to the yang model value
public static String mapEnumeratedValue(String propertyName, String propertyValue) {
LOG.info("mapEnumeratedValue called with propertyName=" + propertyName + " and value=" + propertyValue);
String mappingKey = "yang." + propertyName + "." + propertyValue;