7943218f4091234df7d657ecff110e3d6f2b990a
[aai/gizmo.git] / src / main / java / org / onap / crud / util / CrudServiceUtil.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.crud.util;
22
23 import org.onap.aai.db.props.AAIProperties;
24 import org.onap.aaiutils.oxm.OxmModelLoader;
25 import org.onap.crud.exception.CrudException;
26 import org.onap.schema.RelationshipSchemaLoader;
27
28 import com.google.gson.Gson;
29 import com.google.gson.JsonElement;
30 import com.google.gson.JsonPrimitive;
31
32 import java.util.AbstractMap;
33 import java.util.HashSet;
34 import java.util.Map;
35 import java.util.Set;
36 import java.util.Map.Entry;
37
38 import javax.ws.rs.core.HttpHeaders;
39 import javax.ws.rs.core.Response.Status;
40
41 public class CrudServiceUtil {
42
43   private static Gson gson = new Gson();
44   public static Object validateFieldType(String value, Class clazz) throws CrudException {
45     try {
46       if (clazz.isAssignableFrom(Integer.class)) {
47         return Integer.parseInt(value);
48       } else if (clazz.isAssignableFrom(Long.class)) {
49         return Long.parseLong(value);
50       } else if (clazz.isAssignableFrom(Float.class)) {
51         return Float.parseFloat(value);
52       } else if (clazz.isAssignableFrom(Double.class)) {
53         return Double.parseDouble(value);
54       } else if (clazz.isAssignableFrom(Boolean.class)) {
55                   
56                 // If the value is an IN/OUT direction, this gets seen as a boolean, so
57         // check for that first.
58         if (value.equals("OUT") || value.equals("IN")) {
59           return value;
60         }
61                 
62         if (!value.equals("true") && !value.equals("false")) {
63           throw new CrudException("Invalid propertry value: " + value, Status.BAD_REQUEST);
64         }
65         return Boolean.parseBoolean(value);
66       } else {
67         return value;
68       }
69     } catch (Exception e) {
70       throw new CrudException("Invalid property value: " + value, Status.BAD_REQUEST);
71     }
72   }
73
74   public static void loadModels() throws CrudException {
75     // load the schemas
76     try {
77       OxmModelLoader.loadModels();
78     } catch (Exception e) {
79       throw new CrudException(e);
80     }
81     RelationshipSchemaLoader.loadModels();
82   }
83   
84   /**
85    * This method will merge header property from app id in request payload if not already populated
86    * @param propertiesFromRequest
87    * @param headers
88    * @param isAdd
89    * @return
90    */
91   public static JsonElement mergeHeaderInFoToPayload(JsonElement propertiesFromRequest,  HttpHeaders headers, boolean isAdd) {
92     String sourceOfTruth = headers.getRequestHeaders().getFirst("X-FromAppId");  
93     Set<Map.Entry<String, JsonElement>> properties = new HashSet<Map.Entry<String, JsonElement>>();
94     properties.addAll(propertiesFromRequest.getAsJsonObject().entrySet());
95     
96     Set<String> propertyKeys = new HashSet<String>();
97     for(Map.Entry<String, JsonElement> property : properties) {
98       propertyKeys.add(property.getKey());
99     }
100     
101     if(!propertyKeys.contains(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH)) {
102         properties.add(new AbstractMap.SimpleEntry<String, JsonElement>(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH,
103             (JsonElement)(new JsonPrimitive(sourceOfTruth))));
104     }
105    
106     if(isAdd && !propertyKeys.contains(AAIProperties.SOURCE_OF_TRUTH)) {
107         properties.add(new AbstractMap.SimpleEntry<String, JsonElement>(AAIProperties.SOURCE_OF_TRUTH,
108             (JsonElement)(new JsonPrimitive(sourceOfTruth))));
109     }
110
111     Object[] propArray = properties.toArray();
112     StringBuilder sb = new StringBuilder();
113     sb.append("{");
114     boolean first=true;
115     for(int i=0; i<propArray.length; i++) {
116       
117       Map.Entry<String, JsonElement> entry = (Entry<String, JsonElement>) propArray[i];
118       if(!first) {
119         sb.append(",");
120       }
121       sb.append("\"").append(entry.getKey()).append("\"").append(":").append(entry.getValue());
122       first=false;
123     }
124     sb.append("}");
125     
126     return gson.fromJson(sb.toString(), JsonElement.class);
127   }
128 }