Merge "Add debugging of REST call"
[policy/drools-applications.git] / controlloop / common / model-impl / appclcm / src / main / java / org / onap / policy / appclcm / util / Serialization.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * appc
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.appclcm.util;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import com.google.gson.JsonDeserializationContext;
26 import com.google.gson.JsonDeserializer;
27 import com.google.gson.JsonElement;
28 import com.google.gson.JsonObject;
29 import com.google.gson.JsonPrimitive;
30 import com.google.gson.JsonSerializationContext;
31 import com.google.gson.JsonSerializer;
32
33 import java.lang.reflect.Type;
34 import java.time.Instant;
35
36 import org.onap.policy.appclcm.LcmRequest;
37 import org.onap.policy.appclcm.LcmResponse;
38
39 public final class Serialization {
40     public static final Gson gsonPretty = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting()
41             .registerTypeAdapter(Instant.class, new InstantAdapter()).create();
42
43     public static final Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting()
44             .registerTypeAdapter(LcmRequest.class, new RequestAdapter())
45             .registerTypeAdapter(LcmResponse.class, new ResponseAdapter()).create();
46
47     public static final Gson gsonJunit = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting()
48             .registerTypeAdapter(Instant.class, new InstantJunitAdapter()).create();
49
50     private Serialization() {}
51
52     public static class RequestAdapter implements JsonSerializer<LcmRequest>, JsonDeserializer<LcmRequest> {
53
54         @Override
55         public JsonElement serialize(LcmRequest src, Type typeOfSrc, JsonSerializationContext context) {
56             JsonElement requestJson = gsonPretty.toJsonTree(src, LcmRequest.class);
57             JsonObject input = new JsonObject();
58             input.add("input", requestJson);
59
60             return input;
61         }
62
63         @Override
64         public LcmRequest deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
65             return gsonPretty.fromJson(json.getAsJsonObject().get("input"), LcmRequest.class);
66         }
67     }
68
69     public static class ResponseAdapter implements JsonSerializer<LcmResponse>, JsonDeserializer<LcmResponse> {
70
71         @Override
72         public JsonElement serialize(LcmResponse src, Type typeOfSrc, JsonSerializationContext context) {
73             JsonElement responseJson = gsonPretty.toJsonTree(src, LcmResponse.class);
74             JsonObject output = new JsonObject();
75             output.add("output", responseJson);
76             return output;
77         }
78
79         @Override
80         public LcmResponse deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
81             return gsonPretty.fromJson(json.getAsJsonObject().get("output"), LcmResponse.class);
82         }
83     }
84
85     public static class InstantAdapter implements JsonSerializer<Instant>, JsonDeserializer<Instant> {
86
87         @Override
88         public Instant deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
89             return Instant.parse(json.getAsString());
90         }
91
92         @Override
93         public JsonElement serialize(Instant src, Type typeOfSrc, JsonSerializationContext context) {
94             return new JsonPrimitive(src.toString());
95         }
96
97     }
98
99     public static class InstantJunitAdapter implements JsonSerializer<Instant>, JsonDeserializer<Instant> {
100
101         @Override
102         public Instant deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
103             return Instant.ofEpochMilli(json.getAsLong());
104         }
105
106         @Override
107         public JsonElement serialize(Instant src, Type typeOfSrc, JsonSerializationContext context) {
108             return new JsonPrimitive(src.toEpochMilli());
109         }
110
111     }
112
113 }