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