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.aaiclient.client.graphinventory;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
28 import java.util.Optional;
29 import java.util.function.Predicate;
30 import java.util.function.UnaryOperator;
31 import org.onap.aaiclient.client.graphinventory.entities.uri.GraphInventorySingleResourceUri;
32 import org.onap.so.jsonpath.JsonPathUtil;
33 import com.ctc.wstx.shaded.msv_core.util.Uri;
34 import com.fasterxml.jackson.core.type.TypeReference;
35 import com.fasterxml.jackson.databind.ObjectMapper;
37 public abstract class GraphInventoryRelationships<Wrapper extends GraphInventoryResultWrapper<?>, Uri extends GraphInventorySingleResourceUri<?, ?, ?, ?, ?, ?>, Type extends GraphInventoryObjectType> {
39 protected final ObjectMapper mapper;
40 protected Map<String, Object> map;
41 protected final String jsonBody;
43 public GraphInventoryRelationships(String json) {
45 this.mapper = new GraphInventoryCommonObjectMapperProvider().getMapper();
47 this.map = mapper.readValue(json, new TypeReference<Map<String, Object>>() {});
48 } catch (IOException e) {
49 this.map = new HashMap<>();
53 public List<Wrapper> getByType(GraphInventoryObjectName type) {
55 return this.getAll(Optional.of(type));
58 public List<Wrapper> getByType(GraphInventoryObjectName type, UnaryOperator<Uri> func) {
60 return this.getAll(Optional.of(type), func);
63 public List<Wrapper> getAll() {
65 return this.getAll(Optional.empty());
69 public List<String> getRelatedLinks() {
70 return this.getRelatedLinks(Optional.empty());
73 public List<String> getRelatedLinks(GraphInventoryObjectName type) {
74 return this.getRelatedLinks(Optional.of(type));
77 public List<Uri> getRelatedUris() {
78 return this.getRelatedUris(x -> true);
81 public List<Uri> getRelatedUris(GraphInventoryObjectName type) {
82 return this.getRelatedUris(x -> type.typeName().equals(x));
85 protected List<Uri> getRelatedUris(Predicate<String> p) {
86 List<Uri> result = new ArrayList<>();
87 if (map.containsKey("relationship")) {
88 List<Map<String, Object>> relationships = (List<Map<String, Object>>) map.get("relationship");
89 for (Map<String, Object> relationship : relationships) {
90 final String relatedTo = (String) relationship.get("related-to");
91 if (p.test(relatedTo)) {
93 final String relatedLink = (String) relationship.get("related-link");
94 type = fromTypeName(relatedTo, relatedLink);
96 result.add(createUri(type, relatedLink));
105 protected List<Wrapper> getAll(final Optional<GraphInventoryObjectName> type) {
106 return getAll(type, UnaryOperator.identity());
109 protected List<Wrapper> getAll(final Optional<GraphInventoryObjectName> type, UnaryOperator<Uri> func) {
110 List<Uri> relatedLinks;
111 if (type.isPresent()) {
112 relatedLinks = this.getRelatedUris(type.get());
114 relatedLinks = this.getRelatedUris();
116 ArrayList<Wrapper> result = new ArrayList<>();
117 for (Uri link : relatedLinks) {
118 result.add(this.get(func.apply(link)));
123 protected abstract Wrapper get(Uri uri);
125 protected abstract Uri createUri(Type type, String relatedLink);
127 protected abstract Type fromTypeName(String name, String uri);
129 protected List<String> getRelatedLinks(Optional<GraphInventoryObjectName> type) {
131 if (type.isPresent()) {
132 matcher = "[?(@.related-to=='" + type.get().typeName() + "')]";
134 return JsonPathUtil.getInstance().locateResultList(this.jsonBody,
135 String.format("$.relationship%s.related-link", matcher));
138 public String getJson() {
139 return this.jsonBody;