move aai-client into its own project
[so.git] / graph-inventory / aai-client / src / main / java / org / onap / so / client / graphinventory / entities / GraphInventoryRelationships.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.graphinventory.entities;
22
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Optional;
29 import java.util.function.Predicate;
30 import org.onap.so.client.graphinventory.GraphInventoryCommonObjectMapperProvider;
31 import org.onap.so.client.graphinventory.GraphInventoryObjectName;
32 import org.onap.so.client.graphinventory.GraphInventoryObjectType;
33 import org.onap.so.client.graphinventory.entities.uri.GraphInventorySingleResourceUri;
34 import org.onap.so.jsonpath.JsonPathUtil;
35 import com.fasterxml.jackson.core.type.TypeReference;
36 import com.fasterxml.jackson.databind.ObjectMapper;
37
38 public abstract class GraphInventoryRelationships<Wrapper extends GraphInventoryResultWrapper<?>, Uri extends GraphInventorySingleResourceUri<?, ?, ?, ?>, Type extends GraphInventoryObjectType> {
39
40     protected final ObjectMapper mapper;
41     protected Map<String, Object> map;
42     protected final String jsonBody;
43
44     public GraphInventoryRelationships(String json) {
45         this.jsonBody = json;
46         this.mapper = new GraphInventoryCommonObjectMapperProvider().getMapper();
47         try {
48             this.map = mapper.readValue(json, new TypeReference<Map<String, Object>>() {});
49         } catch (IOException e) {
50             this.map = new HashMap<>();
51         }
52     }
53
54     public List<Wrapper> getByType(GraphInventoryObjectName type) {
55
56         return this.getAll(Optional.of(type));
57     }
58
59     public List<Wrapper> getAll() {
60
61         return this.getAll(Optional.empty());
62     }
63
64
65     public List<String> getRelatedLinks() {
66         return this.getRelatedLinks(Optional.empty());
67     }
68
69     public List<String> getRelatedLinks(GraphInventoryObjectName type) {
70         return this.getRelatedLinks(Optional.of(type));
71     }
72
73     public List<Uri> getRelatedUris() {
74         return this.getRelatedUris(x -> true);
75     }
76
77     public List<Uri> getRelatedUris(GraphInventoryObjectName type) {
78         return this.getRelatedUris(x -> type.typeName().equals(x));
79     }
80
81     protected List<Uri> getRelatedUris(Predicate<String> p) {
82         List<Uri> result = new ArrayList<>();
83         if (map.containsKey("relationship")) {
84             List<Map<String, Object>> relationships = (List<Map<String, Object>>) map.get("relationship");
85             for (Map<String, Object> relationship : relationships) {
86                 final String relatedTo = (String) relationship.get("related-to");
87                 if (p.test(relatedTo)) {
88                     Type type;
89                     type = fromTypeName(relatedTo);
90                     final String relatedLink = (String) relationship.get("related-link");
91
92                     result.add(createUri(type, relatedLink));
93                 }
94             }
95         }
96         return result;
97     }
98
99
100
101     protected List<Wrapper> getAll(final Optional<GraphInventoryObjectName> type) {
102         List<Uri> relatedLinks;
103         if (type.isPresent()) {
104             relatedLinks = this.getRelatedUris(type.get());
105         } else {
106             relatedLinks = this.getRelatedUris();
107         }
108         ArrayList<Wrapper> result = new ArrayList<>();
109         for (Uri link : relatedLinks) {
110             result.add(this.get(link));
111         }
112         return result;
113     }
114
115     protected abstract Wrapper get(Uri uri);
116
117     protected abstract Uri createUri(Type type, String relatedLink);
118
119     protected abstract Type fromTypeName(String name);
120
121     protected List<String> getRelatedLinks(Optional<GraphInventoryObjectName> type) {
122         String matcher = "";
123         if (type.isPresent()) {
124             matcher = "[?(@.related-to=='" + type.get().typeName() + "')]";
125         }
126         return JsonPathUtil.getInstance().locateResultList(this.jsonBody,
127                 String.format("$.relationship%s.related-link", matcher));
128     }
129
130     public String getJson() {
131         return this.jsonBody;
132     }
133 }