00e597b189ca6ce6b53e548dcd194a33c09b5f57
[so.git] / common / src / main / java / org / onap / so / client / graphinventory / GraphInventoryPatchConverter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.client.graphinventory;
22
23 import java.util.List;
24 import java.util.Map;
25 import java.util.regex.Pattern;
26
27 import org.onap.so.client.aai.AAICommonObjectMapperPatchProvider;
28 import org.onap.so.client.aai.AAICommonObjectMapperProvider;
29 import org.onap.so.client.graphinventory.exceptions.GraphInventoryPatchDepthExceededException;
30 import org.onap.so.jsonpath.JsonPathUtil;
31
32 import com.fasterxml.jackson.core.JsonProcessingException;
33
34 public class GraphInventoryPatchConverter {
35
36         private static final AAICommonObjectMapperProvider standardProvider = new AAICommonObjectMapperProvider();
37         private static final AAICommonObjectMapperPatchProvider patchProvider = new AAICommonObjectMapperPatchProvider();
38         private static final Pattern LOCATE_COMPLEX_OBJECT = Pattern.compile("^((?!relationship-list).)+?\\['[^\\[\\]]+?'\\]$");
39
40         
41         public String convertPatchFormat(Object obj) {
42                 return validatePatchObject(marshallObjectToPatchFormat(obj));
43         }
44         
45         public String validatePatchObject(String payload) {
46                 if (hasComplexObject(payload)) {
47                         throw new GraphInventoryPatchDepthExceededException(payload);
48                 }
49                 
50                 return payload;
51         }
52         
53         /** validates client side that json does not include any complex objects
54          * relationship-list is omitted from this validation
55          */
56         protected boolean hasComplexObject(String json) {
57                 if (json.isEmpty()) {
58                         return false;
59                 }
60                 String complex = "$.*.*";
61                 String array = "$.*.*.*";
62                 List<String> result = JsonPathUtil.getInstance().getPathList(json, complex);
63                 List<String> result2 = JsonPathUtil.getInstance().getPathList(json, array);
64                 
65                 result.addAll(result2);
66                 return result.stream().anyMatch(item -> LOCATE_COMPLEX_OBJECT.matcher(item).find());
67         }
68         
69         protected String marshallObjectToPatchFormat(Object obj) {
70                 Object value = obj;
71                 try {
72                         if (!(obj instanceof Map || obj instanceof String)) {
73                                 value = patchProvider.getMapper().writeValueAsString(obj);
74                         } else if (obj instanceof Map) {
75                                 value = standardProvider.getMapper().writeValueAsString(obj);
76                         }
77                 } catch (JsonProcessingException e) {
78                         value = "{}";
79                 }
80                 
81                 return (String)value;
82         }
83 }