752a743247a96e9ee706636d4f974b2f85516044
[so.git] / common / 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
31 import org.onap.so.client.aai.AAICommonObjectMapperProvider;
32 import org.onap.so.client.aai.AAIObjectType;
33 import org.onap.so.client.aai.entities.AAIResultWrapper;
34 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
35 import org.onap.so.client.graphinventory.GraphInventoryCommonObjectMapperProvider;
36 import org.onap.so.client.graphinventory.GraphInventoryObjectName;
37 import org.onap.so.client.graphinventory.GraphInventoryObjectType;
38 import org.onap.so.client.graphinventory.entities.uri.GraphInventoryResourceUri;
39 import org.onap.so.jsonpath.JsonPathUtil;
40
41 import com.fasterxml.jackson.core.type.TypeReference;
42 import com.fasterxml.jackson.databind.ObjectMapper;
43
44 public abstract class GraphInventoryRelationships<Wrapper extends GraphInventoryResultWrapper, Uri extends GraphInventoryResourceUri, Type extends GraphInventoryObjectType> {
45
46         protected final ObjectMapper mapper;
47         protected Map<String, Object> map;
48         protected final String jsonBody;
49         
50         public GraphInventoryRelationships(String json) {
51                 this.jsonBody = json;
52                 this.mapper = new GraphInventoryCommonObjectMapperProvider().getMapper();
53                 try {
54                         this.map = mapper.readValue(json, new TypeReference<Map<String, Object>>() {});
55                 } catch (IOException e) {
56                         this.map = new HashMap<>();
57                 }
58         }
59         
60         public List<Wrapper> getByType(GraphInventoryObjectName type) {
61                 
62                 return this.getAll(Optional.of(type));
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         protected List<Uri> getRelatedUris(Predicate<String> p) {
87                 List<Uri> result = new ArrayList<>();
88                 if (map.containsKey("relationship")) {
89                         List<Map<String, Object>> relationships = (List<Map<String, Object>>)map.get("relationship");
90                         for (Map<String, Object> relationship : relationships) {
91                                 final String relatedTo = (String)relationship.get("related-to");
92                                 if (p.test(relatedTo)) {
93                                         Type type;
94                                         type = fromTypeName(relatedTo);
95                                         final String relatedLink = (String)relationship.get("related-link");
96                                         
97                                         result.add(createUri(type, relatedLink));
98                                 }
99                         }
100                 }
101                 return result;
102         }
103         
104         
105         
106         protected List<Wrapper> getAll(final Optional<GraphInventoryObjectName> type) {
107                 List<Uri> relatedLinks;
108                 if (type.isPresent()) {
109                         relatedLinks = this.getRelatedUris(type.get());
110                 } else {
111                         relatedLinks = this.getRelatedUris();
112                 }
113                 ArrayList<Wrapper> result = new ArrayList<>();
114                 for (Uri link : relatedLinks) {
115                         result.add(this.get(link));
116                 }
117                 return result;
118         }
119         
120         protected abstract Wrapper get(Uri uri);
121         
122         protected abstract Uri createUri(Type type, String relatedLink);
123         
124         protected abstract Type fromTypeName(String name);
125         
126         protected List<String> getRelatedLinks(Optional<GraphInventoryObjectName> type) {
127                 String matcher = "";
128                 if (type.isPresent()) {
129                         matcher = "[?(@.related-to=='" + type.get().typeName() + "')]";
130                 }
131                 return JsonPathUtil.getInstance().locateResultList(this.jsonBody, String.format("$.relationship%s.related-link", matcher));
132         }
133         
134         public String getJson() {
135                 return this.jsonBody;
136         }
137 }