Replace GsonXxxAdapters 99/117999/4
authorJim Hahn <jrh3@att.com>
Wed, 17 Feb 2021 21:07:33 +0000 (16:07 -0500)
committerJim Hahn <jrh3@att.com>
Thu, 18 Feb 2021 12:08:43 +0000 (07:08 -0500)
The GsonXxxAdapters were removed from the models Serialization classes
and replaced with type adapters from common.  Modified the code to refer
to the new adapters.
Without this fix, examples-onap-vcpe will not compile.  Could that be
the cause of the apex-pdp standalone issue, POLICY-3066?

Allow TypeAdapter in lieu of JsonSerializer/Deserializer adapters in
config files.
Note: examples-ona-bbs refers to the following, which were removed
several releases ago (during the actor re-write?):
- appclcm.util.Serialization$RequestAdapter & Serialization$ResponseAdapter

Issue-ID: POLICY-2905
Change-Id: Ia57e0346343614cbd4a1cffd9c8393f207284244
Signed-off-by: Jim Hahn <jrh3@att.com>
14 files changed:
context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/java/JavaSchemaHelperJsonAdapterParameters.java
context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/java/JavaSchemaHelperParametersTest.java
context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/java/SupportJsonAdapter2.java [new file with mode: 0644]
examples/examples-grpc/src/main/resources/examples/config/APEXgRPC/ApexConfig.json
examples/examples-onap-bbs/src/main/resources/examples/config/ONAPBBS/NomadicONTPolicyModel_config.json
examples/examples-onap-vcpe/src/main/resources/examples/config/ONAPvCPE/ApexConfig.json
examples/examples-onap-vcpe/src/main/resources/examples/config/ONAPvCPE/ApexConfigStdin.json
examples/examples-onap-vcpe/src/main/resources/examples/config/ONAPvCPE/ApexConfig_Sim.json
examples/examples-onap-vcpe/src/main/resources/examples/config/ONAPvCPE/ApexConfig_Sim_StdIO.json
examples/examples-onap-vcpe/src/main/resources/examples/config/ONAPvCPEStandalone/ApexConfig.json
examples/examples-onap-vcpe/src/main/resources/examples/config/ONAPvCPEStandalone/ApexConfig_Sim.json
examples/examples-onap-vcpe/src/main/resources/examples/config/ONAPvCPEStandalone/ApexConfig_Sim_StdIO.json
examples/examples-onap-vcpe/src/test/java/org/onap/policy/apex/domains/onap/vcpe/AppcResponseCreator.java
examples/examples-onap-vcpe/src/test/java/org/onap/policy/apex/domains/onap/vcpe/OnapVCpeSimEndpoint.java

index 49f2cbd..0c68c42 100644 (file)
@@ -1,19 +1,20 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2018 Ericsson. All rights reserved.
+ *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * 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.
  * See the License for the specific language governing permissions and
  * limitations under the License.
- * 
+ *
  * SPDX-License-Identifier: Apache-2.0
  * ============LICENSE_END=========================================================
  */
@@ -22,6 +23,7 @@ package org.onap.policy.apex.context.impl.schema.java;
 
 import com.google.gson.JsonDeserializer;
 import com.google.gson.JsonSerializer;
+import com.google.gson.TypeAdapter;
 import org.onap.policy.common.parameters.GroupValidationResult;
 import org.onap.policy.common.parameters.ParameterGroup;
 import org.onap.policy.common.parameters.ValidationStatus;
@@ -37,7 +39,7 @@ import org.slf4j.ext.XLoggerFactory;
  * <li>adaptedClass: The name of the class being adapted.
  * <li>adapterClass: the JSON adapter class to use for the adapted class.
  * </ol>
- * 
+ *
  * @author Liam Fallon (liam.fallon@ericsson.com)
  */
 //@formatter:on
@@ -85,7 +87,7 @@ public class JavaSchemaHelperJsonAdapterParameters implements ParameterGroup {
         if (adaptedClass == null) {
             return null;
         }
-        
+
         try {
             return Class.forName(adaptedClass);
         } catch (final ClassNotFoundException e) {
@@ -121,7 +123,7 @@ public class JavaSchemaHelperJsonAdapterParameters implements ParameterGroup {
         if (adaptorClass == null) {
             return null;
         }
-        
+
         try {
             return Class.forName(adaptorClass);
         } catch (final ClassNotFoundException e) {
@@ -147,15 +149,20 @@ public class JavaSchemaHelperJsonAdapterParameters implements ParameterGroup {
         final GroupValidationResult result = new GroupValidationResult(this);
 
         getClass(ADAPTED_CLASS, adaptedClass, result);
-        
+
         Class<?> adaptorClazz = getClass(ADAPTOR_CLASS, adaptorClass, result);
+
         if (adaptorClazz != null) {
             String errorMessage = null;
-            
+
+            if (TypeAdapter.class.isAssignableFrom(adaptorClazz)) {
+                return result;
+            }
+
             if (!JsonSerializer.class.isAssignableFrom(adaptorClazz)) {
                 errorMessage = "class is not a JsonSerializer";
             }
-            
+
             if (!JsonDeserializer.class.isAssignableFrom(adaptorClazz)) {
                 if (errorMessage == null) {
                     errorMessage = "class is not a JsonDeserializer";
@@ -174,9 +181,9 @@ public class JavaSchemaHelperJsonAdapterParameters implements ParameterGroup {
 
     /**
      * Check a class exists.
-     * 
-     * @param parameterName the parameter name of the class to check for existence 
-     * @param classToCheck the class to check for existence 
+     *
+     * @param parameterName the parameter name of the class to check for existence
+     * @param classToCheck the class to check for existence
      * @param result the result of the check
      */
     private Class<?> getClass(String parameterName, String classToCheck, final GroupValidationResult result) {
@@ -184,7 +191,7 @@ public class JavaSchemaHelperJsonAdapterParameters implements ParameterGroup {
             result.setResult(parameterName, ValidationStatus.INVALID, "parameter is null or blank");
             return null;
         }
-        
+
         // Get the class for the event protocol
         try {
             return Class.forName(classToCheck);
index 611eb7f..65b2ed6 100644 (file)
@@ -1,19 +1,20 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2018 Ericsson. All rights reserved.
+ *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * 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.
  * See the License for the specific language governing permissions and
  * limitations under the License.
- * 
+ *
  * SPDX-License-Identifier: Apache-2.0
  * ============LICENSE_END=========================================================
  */
@@ -72,6 +73,10 @@ public class JavaSchemaHelperParametersTest {
         jsonPars.setAdaptorClass("org.onap.policy.apex.context.impl.schema.java.SupportJsonAdapter");
         assertTrue(pars.validate().isValid());
 
+        jsonPars.setAdaptedClass("java.lang.String");
+        jsonPars.setAdaptorClass("org.onap.policy.apex.context.impl.schema.java.SupportJsonAdapter2");
+        assertTrue(pars.validate().isValid());
+
         Map<String, JavaSchemaHelperJsonAdapterParameters> adapterMap = new LinkedHashMap<>();
 
         pars.setJsonAdapters(adapterMap);
diff --git a/context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/java/SupportJsonAdapter2.java b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/java/SupportJsonAdapter2.java
new file mode 100644 (file)
index 0000000..627cde4
--- /dev/null
@@ -0,0 +1,50 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.apex.context.impl.schema.java;
+
+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;
+
+public class SupportJsonAdapter2 extends TypeAdapter<String> {
+
+    @Override
+    public void write(JsonWriter out, String value) throws IOException {
+        if (value == null) {
+            out.nullValue();
+
+        } else {
+            out.value(value);
+        }
+    }
+
+    @Override
+    public String read(JsonReader in) throws IOException {
+        if (in.peek() == JsonToken.NULL) {
+            in.nextNull();
+            return null;
+        } else {
+            return in.nextString();
+        }
+    }
+}
index 553f16b..fb1c380 100644 (file)
@@ -22,7 +22,7 @@
             "jsonAdapters": {
               "Instant": {
                 "adaptedClass": "java.time.Instant",
-                "adaptorClass": "org.onap.policy.controlloop.util.Serialization$GsonInstantAdapter"
+                "adaptorClass": "org.onap.policy.common.gson.InstantAsMillisTypeAdapter"
               }
             }
           }
index 1c84dc5..1f07987 100644 (file)
@@ -25,7 +25,7 @@
                         "jsonAdapters": {
                             "Instant": {
                                 "adaptedClass": "java.time.Instant",
-                                "adaptorClass": "org.onap.policy.controlloop.util.Serialization$GsonInstantAdapter"
+                                "adaptorClass": "org.onap.policy.common.gson.InstantAsMillisTypeAdapter"
                             },
                             "APPC_LCM_REQUEST": {
                                 "adaptedClass": "org.onap.policy.appclcm.LcmRequest",
index 3b386b9..4b00e0b 100644 (file)
@@ -22,7 +22,7 @@
                         "jsonAdapters": {
                             "Instant": {
                                 "adaptedClass": "java.time.Instant",
-                                "adaptorClass": "org.onap.policy.controlloop.util.Serialization$GsonInstantAdapter"
+                                "adaptorClass": "org.onap.policy.common.gson.InstantAsMillisTypeAdapter"
                             }
                         }
                     }
index 2a99d9a..edf8165 100644 (file)
@@ -22,7 +22,7 @@
                         "jsonAdapters": {
                             "Instant": {
                                 "adaptedClass": "java.time.Instant",
-                                "adaptorClass": "org.onap.policy.controlloop.util.Serialization$GsonInstantAdapter"
+                                "adaptorClass": "org.onap.policy.common.gson.InstantAsMillisTypeAdapter"
                             }
                         }
                     }
index c2193b1..df923cd 100644 (file)
@@ -22,7 +22,7 @@
                         "jsonAdapters": {
                             "Instant": {
                                 "adaptedClass": "java.time.Instant",
-                                "adaptorClass": "org.onap.policy.controlloop.util.Serialization$GsonInstantAdapter"
+                                "adaptorClass": "org.onap.policy.common.gson.InstantAsMillisTypeAdapter"
                             }
                         }
                     }
index 8380e8f..1ed872d 100644 (file)
@@ -22,7 +22,7 @@
                         "jsonAdapters": {
                             "Instant": {
                                 "adaptedClass": "java.time.Instant",
-                                "adaptorClass": "org.onap.policy.controlloop.util.Serialization$GsonInstantAdapter"
+                                "adaptorClass": "org.onap.policy.common.gson.InstantAsMillisTypeAdapter"
                             }
                         }
                     }
index 665de0e..7813e47 100644 (file)
@@ -22,7 +22,7 @@
                         "jsonAdapters": {
                             "Instant": {
                                 "adaptedClass": "java.time.Instant",
-                                "adaptorClass": "org.onap.policy.controlloop.util.Serialization$GsonInstantAdapter"
+                                "adaptorClass": "org.onap.policy.common.gson.InstantAsMillisTypeAdapter"
                             }
                         }
                     }
index 3646959..f9a5de0 100644 (file)
@@ -22,7 +22,7 @@
                         "jsonAdapters": {
                             "Instant": {
                                 "adaptedClass": "java.time.Instant",
-                                "adaptorClass": "org.onap.policy.controlloop.util.Serialization$GsonInstantAdapter"
+                                "adaptorClass": "org.onap.policy.common.gson.InstantAsMillisTypeAdapter"
                             }
                         }
                     }
index 104b641..b9fb66f 100644 (file)
@@ -22,7 +22,7 @@
                         "jsonAdapters": {
                             "Instant": {
                                 "adaptedClass": "java.time.Instant",
-                                "adaptorClass": "org.onap.policy.controlloop.util.Serialization$GsonInstantAdapter"
+                                "adaptorClass": "org.onap.policy.common.gson.InstantAsMillisTypeAdapter"
                             }
                         }
                     }
index afbc3f2..e6d913b 100644 (file)
@@ -2,6 +2,7 @@
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2018 Ericsson. All rights reserved.
  *  Modifications Copyright (C) 2019-2020 Nordix Foundation.
+ *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -31,7 +32,7 @@ import org.onap.policy.appclcm.AppcLcmBody;
 import org.onap.policy.appclcm.AppcLcmDmaapWrapper;
 import org.onap.policy.appclcm.AppcLcmInput;
 import org.onap.policy.appclcm.AppcLcmOutput;
-import org.onap.policy.controlloop.util.Serialization;
+import org.onap.policy.common.gson.InstantAsMillisTypeAdapter;
 
 /**
  * Respond to an APPC request with a given delay.
@@ -47,7 +48,7 @@ public class AppcResponseCreator {
     private final Timer appcTimer;
 
     private static final Gson gson = new GsonBuilder()
-            .registerTypeAdapter(Instant.class, new Serialization.GsonInstantAdapter()).setPrettyPrinting().create();
+            .registerTypeAdapter(Instant.class, new InstantAsMillisTypeAdapter()).setPrettyPrinting().create();
 
     private static Integer nextResponseCode = 400;
 
index c275df1..8b5d2ef 100644 (file)
@@ -2,6 +2,7 @@
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
  *  Modifications Copyright (C) 2019-2020 Nordix Foundation.
+ *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -43,8 +44,8 @@ import javax.ws.rs.Path;
 import javax.ws.rs.QueryParam;
 import javax.ws.rs.core.Response;
 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
+import org.onap.policy.common.gson.InstantAsMillisTypeAdapter;
 import org.onap.policy.common.utils.resources.TextFileUtils;
-import org.onap.policy.controlloop.util.Serialization;
 import org.slf4j.ext.XLogger;
 import org.slf4j.ext.XLoggerFactory;
 
@@ -66,7 +67,7 @@ public class OnapVCpeSimEndpoint {
     private static final Random randomDelayInc = new Random();
 
     private static final Gson gson = new GsonBuilder()
-            .registerTypeAdapter(Instant.class, new Serialization.GsonInstantAdapter()).setPrettyPrinting().create();
+            .registerTypeAdapter(Instant.class, new InstantAsMillisTypeAdapter()).setPrettyPrinting().create();
 
     private static final AtomicInteger nextVnfId = new AtomicInteger(0);
     private static Boolean nextControlLoopMessageIsOnset = true;