345a1cf217cb27fe996835e9e443011133f60ca4
[aai/gizmo.git] / src / main / java / org / onap / crud / util / CrudServiceUtil.java
1 /**
2  * ============LICENSE_START=======================================================
3  * Gizmo
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
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
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  *
22  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23  */
24 package org.onap.crud.util;
25
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;
30
31 import com.google.gson.Gson;
32 import com.google.gson.JsonElement;
33 import com.google.gson.JsonPrimitive;
34
35 import java.util.AbstractMap;
36 import java.util.HashSet;
37 import java.util.Map;
38 import java.util.Set;
39 import java.util.Map.Entry;
40
41 import javax.ws.rs.core.HttpHeaders;
42 import javax.ws.rs.core.Response.Status;
43
44 public class CrudServiceUtil {
45
46   private static Gson gson = new Gson();
47   public static Object validateFieldType(String value, Class clazz) throws CrudException {
48     try {
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)) {
58                   
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")) {
62           return value;
63         }
64                 
65         if (!value.equals("true") && !value.equals("false")) {
66           throw new CrudException("Invalid propertry value: " + value, Status.BAD_REQUEST);
67         }
68         return Boolean.parseBoolean(value);
69       } else {
70         return value;
71       }
72     } catch (Exception e) {
73       throw new CrudException("Invalid property value: " + value, Status.BAD_REQUEST);
74     }
75   }
76
77   public static void loadModels() throws CrudException {
78     // load the schemas
79     try {
80       OxmModelLoader.loadModels();
81     } catch (Exception e) {
82       throw new CrudException(e);
83     }
84     RelationshipSchemaLoader.loadModels();
85   }
86   
87   /**
88    * This method will merge header property from app id in request payload if not already populated
89    * @param propertiesFromRequest
90    * @param headers
91    * @param isAdd
92    * @return
93    */
94   public static JsonElement mergeHeaderInFoToPayload(JsonElement propertiesFromRequest,  HttpHeaders headers, boolean isAdd) {
95     String sourceOfTruth = headers.getRequestHeaders().getFirst("X-FromAppId");  
96     Set<Map.Entry<String, JsonElement>> properties = new HashSet<Map.Entry<String, JsonElement>>();
97     properties.addAll(propertiesFromRequest.getAsJsonObject().entrySet());
98     
99     Set<String> propertyKeys = new HashSet<String>();
100     for(Map.Entry<String, JsonElement> property : properties) {
101       propertyKeys.add(property.getKey());
102     }
103     
104     if(!propertyKeys.contains(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH)) {
105         properties.add(new AbstractMap.SimpleEntry<String, JsonElement>(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH,
106             (JsonElement)(new JsonPrimitive(sourceOfTruth))));
107     }
108    
109     if(isAdd && !propertyKeys.contains(AAIProperties.SOURCE_OF_TRUTH)) {
110         properties.add(new AbstractMap.SimpleEntry<String, JsonElement>(AAIProperties.SOURCE_OF_TRUTH,
111             (JsonElement)(new JsonPrimitive(sourceOfTruth))));
112     }
113
114     Object[] propArray = properties.toArray();
115     StringBuilder sb = new StringBuilder();
116     sb.append("{");
117     boolean first=true;
118     for(int i=0; i<propArray.length; i++) {
119       
120       Map.Entry<String, JsonElement> entry = (Entry<String, JsonElement>) propArray[i];
121       if(!first) {
122         sb.append(",");
123       }
124       sb.append("\"").append(entry.getKey()).append("\"").append(":").append(entry.getValue());
125       first=false;
126     }
127     sb.append("}");
128     
129     return gson.fromJson(sb.toString(), JsonElement.class);
130   }
131 }