[POLICY-22] Reorganizing drools-apps
[policy/drools-applications.git] / controlloop / common / model-impl / events / src / main / java / org / onap / policy / controlloop / util / Serialization.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * controlloop
4  * ================================================================================
5  * Copyright (C) 2017 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.controlloop.util;
22
23 import java.lang.reflect.Type;
24 import java.time.Instant;
25 import java.time.ZonedDateTime;
26 import java.time.format.DateTimeFormatter;
27
28 import org.onap.policy.controlloop.ControlLoopNotificationType;
29 import org.onap.policy.controlloop.ControlLoopTargetType;
30
31 import com.google.gson.Gson;
32 import com.google.gson.GsonBuilder;
33 import com.google.gson.JsonDeserializationContext;
34 import com.google.gson.JsonDeserializer;
35 import com.google.gson.JsonElement;
36 import com.google.gson.JsonParseException;
37 import com.google.gson.JsonPrimitive;
38 import com.google.gson.JsonSerializationContext;
39 import com.google.gson.JsonSerializer;
40
41 public final class Serialization {
42
43         public static class notificationTypeAdapter implements JsonSerializer<ControlLoopNotificationType>, JsonDeserializer<ControlLoopNotificationType> {
44
45                 @Override
46                 public JsonElement serialize(ControlLoopNotificationType src, Type typeOfSrc,
47                                 JsonSerializationContext context) {
48                         return new JsonPrimitive(src.toString());
49                 }
50
51                 @Override
52                 public ControlLoopNotificationType deserialize(JsonElement json, Type typeOfT,
53                                 JsonDeserializationContext context) throws JsonParseException {
54                         return ControlLoopNotificationType.toType(json.getAsString());
55                 }
56                 
57         }
58         
59         public static class targetTypeAdapter implements JsonSerializer<ControlLoopTargetType>, JsonDeserializer<ControlLoopTargetType> {
60
61                 @Override
62                 public JsonElement serialize(ControlLoopTargetType src, Type typeOfSrc,
63                                 JsonSerializationContext context) {
64                         return new JsonPrimitive(src.toString());
65                 }
66
67                 @Override
68                 public ControlLoopTargetType deserialize(JsonElement json, Type typeOfT,
69                                 JsonDeserializationContext context) throws JsonParseException {
70                         return ControlLoopTargetType.toType(json.getAsString());
71                 }
72                 
73         }
74         
75         public static class gsonUTCAdapter implements JsonSerializer<ZonedDateTime>, JsonDeserializer<ZonedDateTime> {
76                 public static DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSxxx");
77
78                 public ZonedDateTime deserialize(JsonElement element, Type type, JsonDeserializationContext context)
79                                 throws JsonParseException {
80                         try {
81                                 return ZonedDateTime.parse(element.getAsString(), format);
82                         } catch (Exception e) {
83                                 System.err.println(e);
84                         }
85                         return null;
86                 }
87
88                 public JsonElement serialize(ZonedDateTime datetime, Type type, JsonSerializationContext context) {
89                         return new JsonPrimitive(datetime.format(format));
90                 }       
91         }
92         
93         public static class gsonInstantAdapter implements JsonSerializer<Instant>, JsonDeserializer<Instant> {
94
95                 @Override
96                 public Instant deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
97                                 throws JsonParseException {
98                         return Instant.ofEpochMilli(json.getAsLong());
99                 }
100
101                 @Override
102                 public JsonElement serialize(Instant src, Type typeOfSrc, JsonSerializationContext context) {
103                         return new JsonPrimitive(src.toEpochMilli());
104                 }
105                 
106         }
107         
108         final static public Gson gson = new GsonBuilder().disableHtmlEscaping()
109                         .registerTypeAdapter(ZonedDateTime.class, new gsonUTCAdapter())
110                         .registerTypeAdapter(Instant.class, new gsonInstantAdapter())
111                         .registerTypeAdapter(ControlLoopNotificationType.class, new notificationTypeAdapter())
112                         .registerTypeAdapter(ControlLoopTargetType.class, new targetTypeAdapter())
113                         .create();
114
115         
116         final static public Gson gsonPretty = new GsonBuilder().disableHtmlEscaping()
117                         .setPrettyPrinting()
118                         .registerTypeAdapter(ZonedDateTime.class, new gsonUTCAdapter())
119                         .registerTypeAdapter(Instant.class, new gsonInstantAdapter())
120                         .registerTypeAdapter(ControlLoopNotificationType.class, new notificationTypeAdapter())
121                         .registerTypeAdapter(ControlLoopTargetType.class, new targetTypeAdapter())
122                         .create();
123
124 }