2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
22 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24 package org.onap.crud.util;
26 import org.onap.aai.db.props.AAIProperties;
27 import org.onap.aaiutils.oxm.OxmModelLoader;
28 import org.onap.crud.exception.CrudException;
29 import org.onap.schema.RelationshipSchemaLoader;
31 import com.google.gson.Gson;
32 import com.google.gson.JsonElement;
33 import com.google.gson.JsonPrimitive;
35 import java.util.AbstractMap;
36 import java.util.HashSet;
39 import java.util.Map.Entry;
41 import javax.ws.rs.core.HttpHeaders;
42 import javax.ws.rs.core.Response.Status;
44 public class CrudServiceUtil {
46 private static Gson gson = new Gson();
47 public static Object validateFieldType(String value, Class clazz) throws CrudException {
49 if (clazz.isAssignableFrom(Integer.class)) {
50 return Integer.parseInt(value);
51 } else if (clazz.isAssignableFrom(Long.class)) {
52 return Long.parseLong(value);
53 } else if (clazz.isAssignableFrom(Float.class)) {
54 return Float.parseFloat(value);
55 } else if (clazz.isAssignableFrom(Double.class)) {
56 return Double.parseDouble(value);
57 } else if (clazz.isAssignableFrom(Boolean.class)) {
59 // If the value is an IN/OUT direction, this gets seen as a boolean, so
60 // check for that first.
61 if (value.equals("OUT") || value.equals("IN")) {
65 if (!value.equals("true") && !value.equals("false")) {
66 throw new CrudException("Invalid propertry value: " + value, Status.BAD_REQUEST);
68 return Boolean.parseBoolean(value);
72 } catch (Exception e) {
73 throw new CrudException("Invalid property value: " + value, Status.BAD_REQUEST);
77 public static void loadModels() throws CrudException {
80 OxmModelLoader.loadModels();
81 } catch (Exception e) {
82 throw new CrudException(e);
84 RelationshipSchemaLoader.loadModels();
87 public static JsonElement mergeHeaderInFoToPayload(JsonElement propertiesFromRequest, HttpHeaders headers, boolean isAdd) {
88 if(!headers.getRequestHeaders().containsKey("X-FromAppId"))
89 return propertiesFromRequest;
91 String sourceOfTruth = headers.getRequestHeaders().getFirst("X-FromAppId");
92 Set<Map.Entry<String, JsonElement>> properties = new HashSet<Map.Entry<String, JsonElement>>();
93 properties.addAll(propertiesFromRequest.getAsJsonObject().entrySet());
95 Set<String> propertyKeys = new HashSet<String>();
96 for(Map.Entry<String, JsonElement> property : properties) {
97 propertyKeys.add(property.getKey());
100 if(!propertyKeys.contains(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH)) {
101 properties.add(new AbstractMap.SimpleEntry<String, JsonElement>(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH,
102 (JsonElement)(new JsonPrimitive(sourceOfTruth))));
105 if(isAdd && !propertyKeys.contains(AAIProperties.SOURCE_OF_TRUTH)) {
106 properties.add(new AbstractMap.SimpleEntry<String, JsonElement>(AAIProperties.SOURCE_OF_TRUTH,
107 (JsonElement)(new JsonPrimitive(sourceOfTruth))));
110 Object[] propArray = properties.toArray();
111 StringBuilder sb = new StringBuilder();
114 for(int i=0; i<propArray.length; i++) {
116 Map.Entry<String, JsonElement> entry = (Entry<String, JsonElement>) propArray[i];
120 sb.append("\"").append(entry.getKey()).append("\"").append(":").append(entry.getValue());
125 return gson.fromJson(sb.toString(), JsonElement.class);