Containerization feature of SO
[so.git] / common / src / main / java / org / onap / so / client / aai / AAIRestClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 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.aai;
22
23 import java.net.URI;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Optional;
27 import java.util.regex.Pattern;
28
29 import javax.ws.rs.core.Response;
30
31 import org.onap.so.client.ResponseExceptionMapper;
32 import org.onap.so.client.RestClientSSL;
33 import org.onap.so.client.graphinventory.exceptions.GraphInventoryPatchDepthExceededException;
34 import org.onap.so.client.policy.CommonObjectMapperProvider;
35 import org.onap.so.jsonpath.JsonPathUtil;
36 import org.onap.so.utils.TargetEntity;
37
38 import com.fasterxml.jackson.core.JsonProcessingException;
39
40 public class AAIRestClient extends RestClientSSL {
41
42         private final AAIProperties aaiProperties;
43         private static final AAICommonObjectMapperProvider standardProvider = new AAICommonObjectMapperProvider();
44         private static final AAICommonObjectMapperPatchProvider patchProvider = new AAICommonObjectMapperPatchProvider();
45         private static final Pattern LOCATE_COMPLEX_OBJECT = Pattern.compile("^((?!relationship-list).)+?\\['[^\\[\\]]+?'\\]$");
46
47         protected AAIRestClient(AAIProperties props, URI uri) {
48                 super(props, Optional.of(uri));
49                 this.aaiProperties = props;
50         }
51
52         @Override
53     public TargetEntity getTargetEntity(){
54             return TargetEntity.AAI;
55     }
56
57         @Override
58         protected void initializeHeaderMap(Map<String, String> headerMap) {
59                 headerMap.put("X-FromAppId", "MSO");
60                 headerMap.put("X-TransactionId", requestId);
61                 String auth = aaiProperties.getAuth();
62                 String key = aaiProperties.getKey();
63
64                 if (auth != null && !auth.isEmpty() && key != null && !key.isEmpty()) {
65                         addBasicAuthHeader(auth, key);
66                 }
67         }
68
69         @Override
70         protected Optional<ResponseExceptionMapper> addResponseExceptionMapper() {
71
72                 return Optional.of(new AAIClientResponseExceptionMapper());
73         }
74         
75         @Override
76         protected CommonObjectMapperProvider getCommonObjectMapperProvider() {
77                 return standardProvider;
78         }
79
80         @Override
81         public Response patch(Object obj) {
82                 String value = convertObjectToPatchFormat(obj);
83                 validatePatchObject(value);
84                 return super.patch(value);
85         }
86
87         @Override
88         public <T> T patch(Object obj, Class<T> resultClass) {
89                 String value = convertObjectToPatchFormat(obj);
90                 validatePatchObject(value);
91                 return super.patch(value, resultClass);
92         }
93         
94         protected String convertObjectToPatchFormat(Object obj) {
95                 Object value = obj;
96                 try {
97                         if (!(obj instanceof Map || obj instanceof String)) {
98                                 value = patchProvider.getMapper().writeValueAsString(obj);
99                         } else if (obj instanceof Map) {
100                                 value = standardProvider.getMapper().writeValueAsString(obj);
101                         }
102                 } catch (JsonProcessingException e) {
103                         value = "{}";
104                 }
105                 
106                 return (String)value;
107         }
108         
109         
110         protected void validatePatchObject(String payload) {
111                 if (hasComplexObject(payload)) {
112                         throw new GraphInventoryPatchDepthExceededException(payload);
113                 }
114         }
115         
116         /** validates client side that json does not include any complex objects
117          * relationship-list is omitted from this validation
118          */
119         protected boolean hasComplexObject(String json) {
120                 if (json.isEmpty()) {
121                         return false;
122                 }
123                 String complex = "$.*.*";
124                 String array = "$.*.*.*";
125                 List<String> result = JsonPathUtil.getInstance().getPathList(json, complex);
126                 List<String> result2 = JsonPathUtil.getInstance().getPathList(json, array);
127                 
128                 result.addAll(result2);
129                 return result.stream().anyMatch(item -> LOCATE_COMPLEX_OBJECT.matcher(item).find());
130         }
131 }