9578d9ac2bc0dd59be4be3b947c28a70b0d9859f
[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.JsonParseException;
39 import com.google.gson.JsonPrimitive;
40 import com.google.gson.JsonSerializationContext;
41 import com.google.gson.JsonSerializer;
42
43
44 public final class Serialization {
45
46         public static class notificationTypeAdapter implements JsonSerializer<ControlLoopNotificationType>, JsonDeserializer<ControlLoopNotificationType> {
47
48
49                 @Override
50                 public JsonElement serialize(ControlLoopNotificationType src, Type typeOfSrc,
51                                 JsonSerializationContext context) {
52                         return new JsonPrimitive(src.toString());
53                 }
54
55                 @Override
56                 public ControlLoopNotificationType deserialize(JsonElement json, Type typeOfT,
57                                 JsonDeserializationContext context) throws JsonParseException {
58                         return ControlLoopNotificationType.toType(json.getAsString());
59                 }
60                 
61         }
62         
63         public static class targetTypeAdapter implements JsonSerializer<ControlLoopTargetType>, JsonDeserializer<ControlLoopTargetType> {
64
65                 @Override
66                 public JsonElement serialize(ControlLoopTargetType src, Type typeOfSrc,
67                                 JsonSerializationContext context) {
68                         return new JsonPrimitive(src.toString());
69                 }
70
71                 @Override
72                 public ControlLoopTargetType deserialize(JsonElement json, Type typeOfT,
73                                 JsonDeserializationContext context) throws JsonParseException {
74                         return ControlLoopTargetType.toType(json.getAsString());
75                 }
76                 
77         }
78         
79         public static class gsonUTCAdapter implements JsonSerializer<ZonedDateTime>, JsonDeserializer<ZonedDateTime> {
80                 private static final Logger logger = LoggerFactory.getLogger(gsonUTCAdapter.class);
81                 public static final DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSxxx");
82
83                 public ZonedDateTime deserialize(JsonElement element, Type type, JsonDeserializationContext context)
84                                 throws JsonParseException {
85                         try {
86                                 return ZonedDateTime.parse(element.getAsString(), format);
87                         } catch (Exception e) {
88                                 logger.error(e.getMessage(), e);
89                         }
90                         return null;
91                 }
92
93                 public JsonElement serialize(ZonedDateTime datetime, Type type, JsonSerializationContext context) {
94                         return new JsonPrimitive(datetime.format(format));
95                 }       
96         }
97         
98         public static class gsonInstantAdapter implements JsonSerializer<Instant>, JsonDeserializer<Instant> {
99
100                 @Override
101                 public Instant deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
102                                 throws JsonParseException {
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         final static public Gson gson = new GsonBuilder().disableHtmlEscaping()
114                         .registerTypeAdapter(ZonedDateTime.class, new gsonUTCAdapter())
115                         .registerTypeAdapter(Instant.class, new gsonInstantAdapter())
116                         .registerTypeAdapter(ControlLoopNotificationType.class, new notificationTypeAdapter())
117                         .registerTypeAdapter(ControlLoopTargetType.class, new targetTypeAdapter())
118                         .create();
119
120         
121         final static public Gson gsonPretty = new GsonBuilder().disableHtmlEscaping()
122                         .setPrettyPrinting()
123                         .registerTypeAdapter(ZonedDateTime.class, new gsonUTCAdapter())
124                         .registerTypeAdapter(Instant.class, new gsonInstantAdapter())
125                         .registerTypeAdapter(ControlLoopNotificationType.class, new notificationTypeAdapter())
126                         .registerTypeAdapter(ControlLoopTargetType.class, new targetTypeAdapter())
127                         .create();
128
129 }