2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.client.aai;
24 import java.util.List;
26 import java.util.Optional;
27 import java.util.regex.Pattern;
29 import javax.ws.rs.core.Response;
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;
38 import com.fasterxml.jackson.core.JsonProcessingException;
40 public class AAIRestClient extends RestClientSSL {
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).)+?\\['[^\\[\\]]+?'\\]$");
47 protected AAIRestClient(AAIProperties props, URI uri) {
48 super(props, Optional.of(uri));
49 this.aaiProperties = props;
53 public TargetEntity getTargetEntity(){
54 return TargetEntity.AAI;
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();
64 if (auth != null && !auth.isEmpty() && key != null && !key.isEmpty()) {
65 addBasicAuthHeader(auth, key);
70 protected Optional<ResponseExceptionMapper> addResponseExceptionMapper() {
72 return Optional.of(new AAIClientResponseExceptionMapper());
76 protected CommonObjectMapperProvider getCommonObjectMapperProvider() {
77 return standardProvider;
81 public Response patch(Object obj) {
82 String value = convertObjectToPatchFormat(obj);
83 validatePatchObject(value);
84 return super.patch(value);
88 public <T> T patch(Object obj, Class<T> resultClass) {
89 String value = convertObjectToPatchFormat(obj);
90 validatePatchObject(value);
91 return super.patch(value, resultClass);
94 protected String convertObjectToPatchFormat(Object obj) {
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);
102 } catch (JsonProcessingException e) {
106 return (String)value;
110 protected void validatePatchObject(String payload) {
111 if (hasComplexObject(payload)) {
112 throw new GraphInventoryPatchDepthExceededException(payload);
116 /** validates client side that json does not include any complex objects
117 * relationship-list is omitted from this validation
119 protected boolean hasComplexObject(String json) {
120 if (json.isEmpty()) {
123 String complex = "$.*.*";
124 String array = "$.*.*.*";
125 List<String> result = JsonPathUtil.getInstance().getPathList(json, complex);
126 List<String> result2 = JsonPathUtil.getInstance().getPathList(json, array);
128 result.addAll(result2);
129 return result.stream().anyMatch(item -> LOCATE_COMPLEX_OBJECT.matcher(item).find());