2 * ============LICENSE_START=======================================================
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
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.graphinventory;
23 import java.util.List;
25 import java.util.regex.Pattern;
26 import org.onap.so.client.aai.AAICommonObjectMapperPatchProvider;
27 import org.onap.so.client.aai.AAICommonObjectMapperProvider;
28 import org.onap.so.client.graphinventory.exceptions.GraphInventoryPatchDepthExceededException;
29 import org.onap.so.jsonpath.JsonPathUtil;
30 import com.fasterxml.jackson.core.JsonProcessingException;
32 public class GraphInventoryPatchConverter {
34 private static final AAICommonObjectMapperProvider standardProvider = new AAICommonObjectMapperProvider();
35 private static final AAICommonObjectMapperPatchProvider patchProvider = new AAICommonObjectMapperPatchProvider();
36 private static final Pattern LOCATE_COMPLEX_OBJECT =
37 Pattern.compile("^((?!relationship-list).)+?\\['[^\\[\\]]+?'\\]$");
40 public String convertPatchFormat(Object obj) {
41 return validatePatchObject(marshallObjectToPatchFormat(obj));
44 public String validatePatchObject(String payload) {
45 if (hasComplexObject(payload)) {
46 throw new GraphInventoryPatchDepthExceededException(payload);
53 * validates client side that json does not include any complex objects relationship-list is omitted from this
56 protected boolean hasComplexObject(String json) {
60 String complex = "$.*.*";
61 String array = "$.*.*.*";
62 List<String> result = JsonPathUtil.getInstance().getPathList(json, complex);
63 List<String> result2 = JsonPathUtil.getInstance().getPathList(json, array);
65 result.addAll(result2);
66 return result.stream().anyMatch(item -> LOCATE_COMPLEX_OBJECT.matcher(item).find());
69 protected String marshallObjectToPatchFormat(Object obj) {
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);
77 } catch (JsonProcessingException e) {
81 return (String) value;