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;
24 import com.fasterxml.jackson.annotation.JsonFilter;
25 import com.fasterxml.jackson.annotation.JsonInclude;
26 import com.fasterxml.jackson.databind.DeserializationFeature;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import com.fasterxml.jackson.databind.SerializationFeature;
29 import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter.SerializeExceptFilter;
30 import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
31 import com.google.common.collect.ImmutableMap;
32 import com.google.gson.Gson;
33 import com.google.gson.JsonElement;
34 import com.google.gson.JsonObject;
35 import java.io.IOException;
36 import java.util.ArrayList;
37 import java.util.Collection;
38 import java.util.HashMap;
39 import java.util.HashSet;
40 import java.util.List;
42 import org.openecomp.sdc.be.config.BeEcompErrorManager;
43 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
44 import org.openecomp.sdc.be.model.ArtifactDefinition;
45 import org.openecomp.sdc.be.model.InterfaceDefinition;
46 import org.openecomp.sdc.be.model.Resource;
47 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
48 import org.openecomp.sdc.common.api.Constants;
49 import org.openecomp.sdc.common.log.wrappers.Logger;
51 public class RepresentationUtils {
53 private static final Logger log = Logger.getLogger(RepresentationUtils.class);
55 public static ArtifactDefinition convertJsonToArtifactDefinitionForUpdate(String content, Class<ArtifactDefinition> clazz) {
57 JsonObject jsonElement = new JsonObject();
58 ArtifactDefinition resourceInfo = null;
61 Gson gson = new Gson();
62 jsonElement = gson.fromJson(content, jsonElement.getClass());
63 String payload = null;
64 jsonElement.remove(Constants.ARTIFACT_GROUP_TYPE);
65 //in update the group type is ignored but this spagheti code makes it too complex to remove this field.
66 jsonElement.addProperty(Constants.ARTIFACT_GROUP_TYPE, ArtifactGroupTypeEnum.INFORMATIONAL.getType());
67 JsonElement artifactPayload = jsonElement.get(Constants.ARTIFACT_PAYLOAD_DATA);
68 if (artifactPayload != null && !artifactPayload.isJsonNull()) {
69 payload = artifactPayload.getAsString();
71 jsonElement.remove(Constants.ARTIFACT_PAYLOAD_DATA);
72 String json = gson.toJson(jsonElement);
73 ObjectMapper mapper = new ObjectMapper();
74 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
75 mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
76 mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
78 resourceInfo = mapper.readValue(json, clazz);
79 resourceInfo.setPayloadData(payload);
81 } catch (Exception e) {
82 BeEcompErrorManager.getInstance().logBeArtifactInformationInvalidError("Artifact Upload / Update");
83 log.debug("Failed to convert the content {} to object.", content.substring(0, Math.min(50, content.length())), e);
90 public static class ResourceRep {
95 * Build Representation of given Object
97 * @param elementToRepresent
101 public static <T> Object toRepresentation(T elementToRepresent) throws IOException {
103 ObjectMapper mapper = new ObjectMapper();
104 mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
105 mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
106 return mapper.writeValueAsString(elementToRepresent);
109 public static <T> T fromRepresentation(String json, Class<T> clazz) {
110 ObjectMapper mapper = new ObjectMapper();
112 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
113 mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
114 mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
116 object = mapper.readValue(json, clazz);
117 } catch (Exception e) {
118 log.error("Error when parsing JSON of object of type {}", clazz.getSimpleName(), e);
119 } // return null in case of exception
124 public static ArtifactDefinition convertJsonToArtifactDefinition(String content, Class<ArtifactDefinition> clazz) {
126 JsonObject jsonElement = new JsonObject();
127 ArtifactDefinition resourceInfo = null;
130 Gson gson = new Gson();
131 jsonElement = gson.fromJson(content, jsonElement.getClass());
132 JsonElement artifactGroupValue = jsonElement.get(Constants.ARTIFACT_GROUP_TYPE);
133 if (artifactGroupValue != null && !artifactGroupValue.isJsonNull()) {
134 String groupValueUpper = artifactGroupValue.getAsString().toUpperCase();
135 if (!ArtifactGroupTypeEnum.getAllTypes().contains(groupValueUpper)) {
136 StringBuilder sb = new StringBuilder();
137 for (String value : ArtifactGroupTypeEnum.getAllTypes()) {
138 sb.append(value).append(", ");
140 log.debug("artifactGroupType is {}. valid values are: {}", groupValueUpper, sb);
143 jsonElement.remove(Constants.ARTIFACT_GROUP_TYPE);
144 jsonElement.addProperty(Constants.ARTIFACT_GROUP_TYPE, groupValueUpper);
147 String payload = null;
148 JsonElement artifactPayload = jsonElement.get(Constants.ARTIFACT_PAYLOAD_DATA);
149 if (artifactPayload != null && !artifactPayload.isJsonNull()) {
150 payload = artifactPayload.getAsString();
152 jsonElement.remove(Constants.ARTIFACT_PAYLOAD_DATA);
153 String json = gson.toJson(jsonElement);
154 ObjectMapper mapper = new ObjectMapper();
155 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
156 mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
157 mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
159 resourceInfo = mapper.readValue(json, clazz);
160 resourceInfo.setPayloadData(payload);
162 } catch (Exception e) {
163 BeEcompErrorManager.getInstance().logBeArtifactInformationInvalidError("Artifact Upload / Update");
164 log.debug("Failed to convert the content {} to object.", content.substring(0, Math.min(50, content.length())), e);
170 public static <T> Object toFilteredRepresentation(T elementToRepresent) throws IOException {
171 ObjectMapper mapper = new ObjectMapper();
172 mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
173 mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
174 mapper.setMixIns(IS_EMPTY_FILTER_MIXIN);
175 return mapper.writer(new SimpleFilterProvider().addFilter(REMOVE_IS_EMPTY_FROM_COLLECTIONS_FILTER,
176 SerializeExceptFilter.serializeAllExcept(EMPTY))).writeValueAsString(elementToRepresent);
179 @JsonFilter(REMOVE_IS_EMPTY_FROM_COLLECTIONS_FILTER)
180 private static class IsEmptyFilterMixIn {}
182 private static final String EMPTY = "empty";
183 private static final String REMOVE_IS_EMPTY_FROM_COLLECTIONS_FILTER = "removeIsEmptyFromCollections";
184 private static final ImmutableMap<Class<?>,Class<?>> IS_EMPTY_FILTER_MIXIN =
185 ImmutableMap.<Class<?>,Class<?>>builder()
186 .put(Collection.class,IsEmptyFilterMixIn.class)
187 .put(List.class,IsEmptyFilterMixIn.class)
188 .put(Set.class,IsEmptyFilterMixIn.class)
189 .put(HashMap.class,IsEmptyFilterMixIn.class)
190 .put(ArrayList.class,IsEmptyFilterMixIn.class)
191 .put(HashSet.class,IsEmptyFilterMixIn.class)
192 .put(InterfaceDefinition.class,IsEmptyFilterMixIn.class)
193 .put(Resource.class,IsEmptyFilterMixIn.class)
194 .put(ToscaDataDefinition.class,IsEmptyFilterMixIn.class)