Containerization feature of SO
[so.git] / common / src / main / java / org / onap / so / client / aai / entities / Relationships.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.aai.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 javax.ws.rs.core.UriBuilder;
32
33 import org.onap.so.client.aai.AAICommonObjectMapperProvider;
34 import org.onap.so.client.aai.AAIObjectType;
35 import org.onap.so.client.aai.AAIResourcesClient;
36 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
37 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
38 import org.onap.so.client.graphinventory.GraphInventoryObjectName;
39 import org.onap.so.jsonpath.JsonPathUtil;
40
41 import com.fasterxml.jackson.core.type.TypeReference;
42 import com.fasterxml.jackson.databind.ObjectMapper;
43 import com.google.common.base.CaseFormat;
44
45 public class Relationships {
46
47         private final ObjectMapper mapper;
48         private Map<String, Object> map;
49         private final String jsonBody;
50         public Relationships(String json) {
51                 this.jsonBody = json;
52                 this.mapper = new AAICommonObjectMapperProvider().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<AAIResultWrapper> getByType(GraphInventoryObjectName type) {
61                 
62                 return this.getAll(Optional.of(type));
63         }
64         
65         public List<AAIResultWrapper> 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<AAIResourceUri> getRelatedAAIUris() {
80                 return this.getRelatedAAIUris(x -> true);
81         }
82         
83         public List<AAIResourceUri> getRelatedAAIUris(GraphInventoryObjectName type) {
84                 return this.getRelatedAAIUris(x -> type.typeName().equals(x));
85         }
86         protected List<AAIResourceUri> getRelatedAAIUris(Predicate<String> p) {
87                 List<AAIResourceUri> 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                                         AAIObjectType type;
94                                         try {
95                                                 type = AAIObjectType.valueOf(CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_UNDERSCORE, relatedTo));
96                                         } catch (IllegalArgumentException e) {
97                                                 type = AAIObjectType.UNKNOWN;
98                                         }
99                                         final String relatedLink = (String)relationship.get("related-link");
100                                         
101                                         result.add(AAIUriFactory.createResourceFromExistingURI(type, UriBuilder.fromPath(relatedLink).build()));
102                                 }
103                         }
104                 }
105                 return result;
106         }
107         
108         
109         
110         protected List<AAIResultWrapper> getAll(final Optional<GraphInventoryObjectName> type) {
111                 List<AAIResourceUri> relatedLinks;
112                 if (type.isPresent()) {
113                         relatedLinks = this.getRelatedAAIUris(type.get());
114                 } else {
115                         relatedLinks = this.getRelatedAAIUris();
116                 }
117                 ArrayList<AAIResultWrapper> result = new ArrayList<>();
118                 for (AAIResourceUri link : relatedLinks) {
119                         result.add(this.get(link));
120                 }
121                 return result;
122         }
123         
124         protected AAIResultWrapper get(AAIResourceUri uri) {
125                 return new AAIResourcesClient().get(uri);
126                 
127         }
128         
129         protected List<String> getRelatedLinks(Optional<GraphInventoryObjectName> type) {
130                 String matcher = "";
131                 if (type.isPresent()) {
132                         matcher = "[?(@.related-to=='" + type.get() + "')]";
133                 }
134                 return JsonPathUtil.getInstance().locateResultList(this.jsonBody, String.format("$.relationship%s.related-link", matcher));
135         }
136         
137         public String getJson() {
138                 return this.jsonBody;
139         }
140 }