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.openecomp.mso.client.aai.entities;
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;
31 import javax.ws.rs.core.UriBuilder;
33 import org.openecomp.mso.client.aai.AAICommonObjectMapperProvider;
34 import org.openecomp.mso.client.aai.AAIObjectName;
35 import org.openecomp.mso.client.aai.AAIObjectType;
36 import org.openecomp.mso.client.aai.AAIResourcesClient;
37 import org.openecomp.mso.client.aai.entities.uri.AAIResourceUri;
38 import org.openecomp.mso.client.aai.entities.uri.AAIUriFactory;
39 import org.openecomp.mso.jsonpath.JsonPathUtil;
41 import com.fasterxml.jackson.core.type.TypeReference;
42 import com.fasterxml.jackson.databind.ObjectMapper;
43 import com.google.common.base.CaseFormat;
45 public class Relationships {
47 private final ObjectMapper mapper;
48 private Map<String, Object> map;
49 private final String jsonBody;
50 public Relationships(String json) {
52 this.mapper = new AAICommonObjectMapperProvider().getMapper();
54 this.map = mapper.readValue(json, new TypeReference<Map<String, Object>>() {});
55 } catch (IOException e) {
56 this.map = new HashMap<>();
60 public List<AAIResultWrapper> getByType(AAIObjectName type) {
62 return this.getAll(Optional.of(type));
65 public List<AAIResultWrapper> getAll() {
67 return this.getAll(Optional.empty());
71 public List<String> getRelatedLinks() {
72 return this.getRelatedLinks(Optional.empty());
75 public List<String> getRelatedLinks(AAIObjectName type) {
76 return this.getRelatedLinks(Optional.of(type));
79 public List<AAIResourceUri> getRelatedAAIUris() {
80 return this.getRelatedAAIUris(x -> true);
83 public List<AAIResourceUri> getRelatedAAIUris(AAIObjectName type) {
84 return this.getRelatedAAIUris(x -> type.typeName().equals(x));
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)) {
95 type = AAIObjectType.valueOf(CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_UNDERSCORE, relatedTo));
96 } catch (IllegalArgumentException e) {
97 type = AAIObjectType.UNKNOWN;
99 final String relatedLink = (String)relationship.get("related-link");
101 result.add(AAIUriFactory.createResourceFromExistingURI(type, UriBuilder.fromPath(relatedLink).build()));
110 protected List<AAIResultWrapper> getAll(final Optional<AAIObjectName> type) {
111 List<AAIResourceUri> relatedLinks;
112 if (type.isPresent()) {
113 relatedLinks = this.getRelatedAAIUris(type.get());
115 relatedLinks = this.getRelatedAAIUris();
117 ArrayList<AAIResultWrapper> result = new ArrayList<>();
118 for (AAIResourceUri link : relatedLinks) {
119 result.add(this.get(link));
124 protected AAIResultWrapper get(AAIResourceUri uri) {
125 return new AAIResourcesClient().get(uri);
129 protected List<String> getRelatedLinks(Optional<AAIObjectName> type) {
131 if (type.isPresent()) {
132 matcher = "[?(@.related-to=='" + type.get() + "')]";
134 return JsonPathUtil.getInstance().locateResultList(this.jsonBody, String.format("$.relationship%s.related-link", matcher));
137 public String getJson() {
138 return this.jsonBody;