diff options
Diffstat (limited to 'sdnr/wt/common-yang/utils/src/main')
2 files changed, 59 insertions, 8 deletions
diff --git a/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/YangToolsMapperHelper.java b/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/YangToolsMapperHelper.java index f443fd615..d468e075c 100644 --- a/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/YangToolsMapperHelper.java +++ b/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/YangToolsMapperHelper.java @@ -278,6 +278,12 @@ public class YangToolsMapperHelper { return notification instanceof DOMEvent; } + /** + * Get time instant from notification if available or default + * @param notification + * @param defaultValue + * @return DateAndTime + */ public static DateAndTime getTime(Notification notification, Instant defaultValue) { Instant time; if (hasTime(notification)) { // If notification class extends/implements the EventInstantAware @@ -290,6 +296,21 @@ public class YangToolsMapperHelper { return DateAndTime.getDefaultInstance(ZonedDateTime.ofInstant(time, ZoneOffset.UTC).format(formatterOutput)); } + /** + * Get time instant from notification if available or actual time + * @param notification + * @return DateAndTime + */ + public static DateAndTime getTime(Notification notification) { + return getTime(notification, Instant.now()); + } + + /** + * Get time instant from DOM notification if available or default + * @param DOM notification + * @param defaultValue + * @return DateAndTime + */ public static DateAndTime getTime(DOMNotification notification, Instant defaultValue) { Instant time; if (hasTime(notification)) { // If notification class extends/implements the EventInstantAware @@ -301,4 +322,13 @@ public class YangToolsMapperHelper { } return DateAndTime.getDefaultInstance(ZonedDateTime.ofInstant(time, ZoneOffset.UTC).format(formatterOutput)); } + + /** + * Get time instant from notification if available or actual time + * @param DOM notification + * @return DateAndTime + */ + public static DateAndTime getTime(DOMNotification notification) { + return getTime(notification, Instant.now()); + } } diff --git a/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/serialize/TypeObjectSerializer.java b/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/serialize/TypeObjectSerializer.java index b43e6c100..4fb39415e 100644 --- a/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/serialize/TypeObjectSerializer.java +++ b/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/serialize/TypeObjectSerializer.java @@ -31,22 +31,43 @@ import org.opendaylight.yangtools.yang.binding.TypeObject; public class TypeObjectSerializer extends JsonSerializer<TypeObject> { + /** + * serialize typeobject values + * prefer stringValue() method over getValue() method + */ @Override public void serialize(TypeObject value, JsonGenerator gen, SerializerProvider serializers) throws IOException { - //stringValue Method[] methods = value.getClass().getDeclaredMethods(); String name; + Method getValueMethod = null; for (Method method : methods) { name = method.getName(); - if (method.getParameterCount()==0 && (name.equals("stringValue") || name.equals("getValue"))) { - try { - gen.writeString((String)method.invoke(value)); - break; - } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException - | IOException e) { - throw new IOException("No String getter method supported TypeObject for "+value.getClass(),e); + if (method.getParameterCount() == 0) { + if (name.equals("getValue")) { + getValueMethod = method; + } else if (name.equals("stringValue")) { + try { + gen.writeString((String) method.invoke(value)); + break; + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException + | IOException e) { + throw new IOException("No String getter method supported TypeObject for " + value.getClass(), + e); + } } } } + if (getValueMethod != null) { + try { + if (String.class.equals(getValueMethod.getReturnType())) { + gen.writeString((String) getValueMethod.invoke(value)); + } else { + gen.writeObject(getValueMethod.invoke(value)); + } + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | IOException e) { + throw new IOException("No String getter method supported TypeObject for " + value.getClass(), e); + } + + } } } |