d2a51f7707b52698e9903ac6cb520aec466927a0
[policy/drools-applications.git] /
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 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import com.google.gson.Gson;
34 import com.google.gson.GsonBuilder;
35 import com.google.gson.JsonDeserializationContext;
36 import com.google.gson.JsonDeserializer;
37 import com.google.gson.JsonElement;
38 import com.google.gson.JsonPrimitive;
39 import com.google.gson.JsonSerializationContext;
40 import com.google.gson.JsonSerializer;
41
42 public final class Serialization {
43         private Serialization() {
44         }
45
46         public static class NotificationTypeAdapter implements JsonSerializer<ControlLoopNotificationType>, JsonDeserializer<ControlLoopNotificationType> {
47                 @Override
48                 public JsonElement serialize(ControlLoopNotificationType src, Type typeOfSrc,
49                                 JsonSerializationContext context) {
50                         return new JsonPrimitive(src.toString());
51                 }
52
53                 @Override
54                 public ControlLoopNotificationType deserialize(JsonElement json, Type typeOfT,
55                                 JsonDeserializationContext context) {
56                         return ControlLoopNotificationType.toType(json.getAsString());
57                 }
58         }
59
60         public static class TargetTypeAdapter implements JsonSerializer<ControlLoopTargetType>, JsonDeserializer<ControlLoopTargetType> {
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) {
70                         return ControlLoopTargetType.toType(json.getAsString());
71                 }
72         }
73
74         public static class GSONUTCAdapter implements JsonSerializer<ZonedDateTime>, JsonDeserializer<ZonedDateTime> {
75                 private static final Logger logger = LoggerFactory.getLogger(GSONUTCAdapter.class);
76                 public static final DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSxxx");
77
78                 @Override
79                 public ZonedDateTime deserialize(JsonElement element, Type type, JsonDeserializationContext context) {
80                         try {
81                                 return ZonedDateTime.parse(element.getAsString(), format);
82                         } catch (Exception e) {
83                                 logger.error(e.getMessage(), e);
84                         }
85                         return null;
86                 }
87
88                 @Override
89                 public JsonElement serialize(ZonedDateTime datetime, Type type, JsonSerializationContext context) {
90                         return new JsonPrimitive(datetime.format(format));
91                 }       
92         }
93
94         public static class GSONInstantAdapter implements JsonSerializer<Instant>, JsonDeserializer<Instant> {
95
96                 @Override
97                 public Instant deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
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         public static final 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         public static final 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         public static final Gson gsonJunit = new GsonBuilder().disableHtmlEscaping()
125                         .setPrettyPrinting()
126                         .registerTypeAdapter(ZonedDateTime.class, new GSONUTCAdapter())
127                         .registerTypeAdapter(Instant.class, new GSONInstantAdapter())
128                         .registerTypeAdapter(ControlLoopTargetType.class, new TargetTypeAdapter())
129                         .create();
130
131 }