2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.servlets;
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;
38 import java.io.IOException;
39 import com.fasterxml.jackson.databind.SerializationFeature;
41 public class RepresentationUtils {
43 private static final Logger log = LoggerFactory.getLogger(RepresentationUtils.class);
45 public static ArtifactDefinition convertJsonToArtifactDefinitionForUpdate(String content, Class<ArtifactDefinition> clazz) {
47 JsonObject jsonElement = new JsonObject();
48 ArtifactDefinition resourceInfo = null;
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();
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);
68 resourceInfo = mapper.readValue(json, clazz);
69 resourceInfo.setPayloadData(payload);
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);
80 public static class ResourceRep {
85 * Build Representation of given Object
87 * @param elementToRepresent
91 public static <T> Object toRepresentation(T elementToRepresent) throws IOException {
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);
99 public static <T> T fromRepresentation(String json, Class<T> clazz) {
100 ObjectMapper mapper = new ObjectMapper();
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);
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
114 public static ArtifactDefinition convertJsonToArtifactDefinition(String content, Class<ArtifactDefinition> clazz) {
116 JsonObject jsonElement = new JsonObject();
117 ArtifactDefinition resourceInfo = null;
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(", ");
130 log.debug("artifactGroupType is {}. valid values are: {}", groupValueUpper, sb);
133 jsonElement.remove(Constants.ARTIFACT_GROUP_TYPE);
134 jsonElement.addProperty(Constants.ARTIFACT_GROUP_TYPE, groupValueUpper);
137 String payload = null;
138 JsonElement artifactPayload = jsonElement.get(Constants.ARTIFACT_PAYLOAD_DATA);
139 if (artifactPayload != null && !artifactPayload.isJsonNull()) {
140 payload = artifactPayload.getAsString();
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);
149 resourceInfo = mapper.readValue(json, clazz);
150 resourceInfo.setPayloadData(payload);
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);