OXM which tracks provenance
[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   public static JsonElement mergeHeaderInFoToPayload(JsonElement propertiesFromRequest,  HttpHeaders headers, boolean isAdd) {
88     if(!headers.getRequestHeaders().containsKey("X-FromAppId"))  
89         return propertiesFromRequest;
90     
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());
94     
95     Set<String> propertyKeys = new HashSet<String>();
96     for(Map.Entry<String, JsonElement> property : properties) {
97       propertyKeys.add(property.getKey());
98     }
99     
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))));
103     }
104    
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))));
108     }
109
110     Object[] propArray = properties.toArray();
111     StringBuilder sb = new StringBuilder();
112     sb.append("{");
113     boolean first=true;
114     for(int i=0; i<propArray.length; i++) {
115       
116       Map.Entry<String, JsonElement> entry = (Entry<String, JsonElement>) propArray[i];
117       if(!first) {
118         sb.append(",");
119       }
120       sb.append("\"").append(entry.getKey()).append("\"").append(":").append(entry.getValue());
121       first=false;
122     }
123     sb.append("}");
124     
125     return gson.fromJson(sb.toString(), JsonElement.class);
126   }
127   
128 }