From afe5abed95c6cbfe239066e267d9c10a71f20623 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Fri, 15 May 2020 09:52:35 -0400 Subject: Cannot parse finishTime in SO responses The timestamp in SO responses appears to follow RFC-1123/RFC-822 format instead of ISO format. Added a type adapter for SO. In Guilin, we may want to consider combining the two type adapters for LocalDateTime so that either format is accepted. Also modified the SO simulator to return responses that are actual samples from SO. As part of that work, discovered that the legacy SO actor is not able to parse these timestamps either. However, as that code is now deprecated, left it as is. Issue-ID: POLICY-2570 Change-Id: I322318d1007e36eef47bb8867fd8ed01cb60223a Signed-off-by: Jim Hahn --- .../policy/controlloop/actor/so/SoOperation.java | 85 +++++++++++++++++++++- 1 file changed, 84 insertions(+), 1 deletion(-) (limited to 'models-interactions/model-actors/actor.so/src/main/java') diff --git a/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoOperation.java b/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoOperation.java index a4c802c9a..2a00edda4 100644 --- a/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoOperation.java +++ b/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoOperation.java @@ -20,6 +20,17 @@ package org.onap.policy.controlloop.actor.so; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonParseException; +import com.google.gson.TypeAdapter; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonToken; +import com.google.gson.stream.JsonWriter; +import java.io.IOException; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -39,9 +50,11 @@ import org.onap.policy.aai.AaiConstants; import org.onap.policy.aai.AaiCqResponse; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType; +import org.onap.policy.common.gson.GsonMessageBodyHandler; import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.coder.StandardCoderObject; import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperation; import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; @@ -64,7 +77,7 @@ import org.slf4j.LoggerFactory; */ public abstract class SoOperation extends HttpOperation { private static final Logger logger = LoggerFactory.getLogger(SoOperation.class); - private static final Coder coder = new StandardCoder(); + private static final Coder coder = new SoCoder(); public static final String PAYLOAD_KEY_VF_COUNT = "vfCount"; public static final String FAILED = "FAILED"; @@ -479,4 +492,74 @@ public abstract class SoOperation extends HttpOperation { public int getWaitSecGet() { return config.getWaitSecGet(); } + + @Override + protected Coder makeCoder() { + return coder; + } + + /* + * TODO: combine this adapter with existing LocalDateTimeTypeAdapter and eliminate the + * following two classes. + */ + + /** + * GSON Type Adapter for "LocalDateTime" fields, that uses the standard + * RFC_1123_DATE_TIME formatter. + */ + private static class SoLocalDateTimeTypeAdapter extends TypeAdapter { + private static final DateTimeFormatter FORMATTER = DateTimeFormatter.RFC_1123_DATE_TIME; + + @Override + public LocalDateTime read(JsonReader in) throws IOException { + try { + if (in.peek() == JsonToken.NULL) { + in.nextNull(); + return null; + } else { + return LocalDateTime.parse(in.nextString(), FORMATTER); + } + + } catch (DateTimeParseException e) { + throw new JsonParseException("invalid date", e); + } + } + + @Override + public void write(JsonWriter out, LocalDateTime value) throws IOException { + if (value == null) { + out.nullValue(); + } else { + String text = value.format(FORMATTER); + out.value(text); + } + } + } + + private static class SoCoder extends StandardCoder { + + /** + * Gson object used to encode and decode messages. + */ + private static final Gson SO_GSON; + + /** + * Gson object used to encode messages in "pretty" format. + */ + private static final Gson SO_GSON_PRETTY; + + static { + GsonBuilder builder = GsonMessageBodyHandler + .configBuilder(new GsonBuilder().registerTypeAdapter(StandardCoderObject.class, + new StandardTypeAdapter())) + .registerTypeAdapter(LocalDateTime.class, new SoLocalDateTimeTypeAdapter()); + + SO_GSON = builder.create(); + SO_GSON_PRETTY = builder.setPrettyPrinting().create(); + } + + public SoCoder() { + super(SO_GSON, SO_GSON_PRETTY); + } + } } -- cgit 1.2.3-korg