Sync Integ to Master
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / servlets / RepresentationUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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.openecomp.sdc.be.servlets;
22
23 import com.fasterxml.jackson.annotation.JsonInclude;
24 import com.fasterxml.jackson.databind.DeserializationFeature;
25 import com.fasterxml.jackson.databind.SerializationFeature;
26 import com.google.gson.Gson;
27 import com.google.gson.JsonElement;
28 import com.google.gson.JsonObject;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import org.openecomp.sdc.be.config.BeEcompErrorManager;
31 import org.openecomp.sdc.be.model.ArtifactDefinition;
32 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
33 import org.openecomp.sdc.common.api.Constants;
34 import org.openecomp.sdc.common.config.EcompErrorName;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import java.io.IOException;
39 import com.fasterxml.jackson.databind.SerializationFeature;
40
41 public class RepresentationUtils {
42
43     private static final Logger log = LoggerFactory.getLogger(RepresentationUtils.class);
44
45     public static ArtifactDefinition convertJsonToArtifactDefinitionForUpdate(String content, Class<ArtifactDefinition> clazz) {
46
47         JsonObject jsonElement = new JsonObject();
48         ArtifactDefinition resourceInfo = null;
49
50         try {
51             Gson gson = new Gson();
52             jsonElement = gson.fromJson(content, jsonElement.getClass());
53             String payload = null;
54             jsonElement.remove(Constants.ARTIFACT_GROUP_TYPE);
55             //in update the group type is ignored but this spagheti code makes it too complex to remove this field.
56             jsonElement.addProperty(Constants.ARTIFACT_GROUP_TYPE, ArtifactGroupTypeEnum.INFORMATIONAL.getType());
57             JsonElement artifactPayload = jsonElement.get(Constants.ARTIFACT_PAYLOAD_DATA);
58             if (artifactPayload != null && !artifactPayload.isJsonNull()) {
59                 payload = artifactPayload.getAsString();
60             }
61             jsonElement.remove(Constants.ARTIFACT_PAYLOAD_DATA);
62             String json = gson.toJson(jsonElement);
63             ObjectMapper mapper = new ObjectMapper();
64             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
65             mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
66             mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
67
68             resourceInfo = mapper.readValue(json, clazz);
69             resourceInfo.setPayloadData(payload);
70
71         } catch (Exception e) {
72             BeEcompErrorManager.getInstance().logBeArtifactInformationInvalidError("Artifact Upload / Update");
73             log.debug("Failed to convert the content {} to object.", content.substring(0, Math.min(50, content.length())), e);
74         }
75
76         return resourceInfo;
77     }
78
79
80     public static class ResourceRep {
81
82     }
83
84     /**
85      * Build Representation of given Object
86      *
87      * @param elementToRepresent
88      * @return
89      * @throws IOException
90      */
91     public static <T> Object toRepresentation(T elementToRepresent) throws IOException {
92
93         ObjectMapper mapper = new ObjectMapper();
94         mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
95         mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
96         return mapper.writeValueAsString(elementToRepresent);
97     }
98
99     public static <T> T fromRepresentation(String json, Class<T> clazz) {
100         ObjectMapper mapper = new ObjectMapper();
101         T object = null;
102         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
103         mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
104         mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
105         try {
106             object = mapper.readValue(json, clazz);
107         } catch (Exception e) {
108             log.error("Error when parsing JSON of object of type {}", clazz.getSimpleName(), e);
109         } // return null in case of exception
110
111         return object;
112     }
113
114     public static ArtifactDefinition convertJsonToArtifactDefinition(String content, Class<ArtifactDefinition> clazz) {
115
116         JsonObject jsonElement = new JsonObject();
117         ArtifactDefinition resourceInfo = null;
118
119         try {
120             Gson gson = new Gson();
121             jsonElement = gson.fromJson(content, jsonElement.getClass());
122             JsonElement artifactGroupValue = jsonElement.get(Constants.ARTIFACT_GROUP_TYPE);
123             if (artifactGroupValue != null && !artifactGroupValue.isJsonNull()) {
124                 String groupValueUpper = artifactGroupValue.getAsString().toUpperCase();
125                 if (!ArtifactGroupTypeEnum.getAllTypes().contains(groupValueUpper)) {
126                     StringBuilder sb = new StringBuilder();
127                     for (String value : ArtifactGroupTypeEnum.getAllTypes()) {
128                         sb.append(value).append(", ");
129                     }
130                     log.debug("artifactGroupType is {}. valid values are: {}", groupValueUpper, sb);
131                     return null;
132                 } else {
133                     jsonElement.remove(Constants.ARTIFACT_GROUP_TYPE);
134                     jsonElement.addProperty(Constants.ARTIFACT_GROUP_TYPE, groupValueUpper);
135                 }
136             }
137             String payload = null;
138             JsonElement artifactPayload = jsonElement.get(Constants.ARTIFACT_PAYLOAD_DATA);
139             if (artifactPayload != null && !artifactPayload.isJsonNull()) {
140                 payload = artifactPayload.getAsString();
141             }
142             jsonElement.remove(Constants.ARTIFACT_PAYLOAD_DATA);
143             String json = gson.toJson(jsonElement);
144             ObjectMapper mapper = new ObjectMapper();
145             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
146             mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
147             mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
148
149             resourceInfo = mapper.readValue(json, clazz);
150             resourceInfo.setPayloadData(payload);
151
152         } catch (Exception e) {
153             BeEcompErrorManager.getInstance().logBeArtifactInformationInvalidError("Artifact Upload / Update");
154             log.debug("Failed to convert the content {} to object.", content.substring(0, Math.min(50, content.length())), e);
155         }
156
157         return resourceInfo;
158     }
159
160 }