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.onap.so.client.aai.entities.uri;
23 import java.io.IOException;
25 import java.util.Collections;
27 import java.util.Optional;
29 import javax.ws.rs.BadRequestException;
30 import javax.ws.rs.core.UriBuilder;
32 import org.onap.so.client.aai.AAIObjectType;
33 import org.onap.so.client.aai.AAIQueryClient;
34 import org.onap.so.client.graphinventory.Format;
35 import org.onap.so.client.aai.entities.CustomQuery;
36 import org.onap.so.client.aai.entities.Results;
37 import org.onap.so.client.graphinventory.entities.uri.SimpleUri;
38 import org.onap.so.client.graphinventory.exceptions.GraphInventoryPayloadException;
39 import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriComputationException;
40 import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriNotFoundException;
42 import com.fasterxml.jackson.core.type.TypeReference;
43 import com.fasterxml.jackson.databind.ObjectMapper;
45 public class ServiceInstanceUri extends AAISimpleUri {
47 private Optional<String> cachedValue = Optional.empty();
49 protected ServiceInstanceUri(Object... values) {
50 super(AAIObjectType.SERVICE_INSTANCE, values);
52 protected ServiceInstanceUri(UriBuilder builder, Optional<String> cachedValue, Object... values) {
53 super(AAIObjectType.SERVICE_INSTANCE, builder, values);
54 this.cachedValue = cachedValue;
56 protected String getSerivceInstance(Object id) throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
57 if (!this.getCachedValue().isPresent()) {
58 AAIResourceUri serviceInstanceUri = AAIUriFactory.createNodesUri(AAIObjectType.SERVICE_INSTANCE, id);
59 CustomQuery query = new CustomQuery(Collections.singletonList(serviceInstanceUri));
62 resultJson = this.getQueryClient().query(Format.PATHED, query);
63 } catch (BadRequestException e) {
64 throw new GraphInventoryUriNotFoundException("Service instance " + id + " not found at: " + serviceInstanceUri.build());
68 cachedValue = extractRelatedLink(resultJson);
69 if (!cachedValue.isPresent()) {
70 throw new GraphInventoryUriNotFoundException("Service instance " + id + " not found at: " + serviceInstanceUri.build());
72 } catch (IOException e) {
73 throw new GraphInventoryPayloadException("could not map payload: " + resultJson, e);
76 Optional<String> cachedValueOpt = this.getCachedValue();
77 return cachedValueOpt.isPresent() ? cachedValueOpt.get() : "";
80 protected Optional<String> extractRelatedLink(String jsonString) throws IOException {
81 Optional<String> result;
82 ObjectMapper mapper = new ObjectMapper();
84 Results<Map<String, String>> results = mapper.readValue(jsonString, new TypeReference<Results<Map<String, String>>>(){});
85 if (results.getResult().size() == 1) {
86 String uriString = results.getResult().get(0).get("resource-link");
87 URI uri = UriBuilder.fromUri(uriString).build();
88 String rawPath = uri.getRawPath();
89 result = Optional.of(rawPath.replaceAll("/aai/v\\d+", ""));
90 } else if (results.getResult().isEmpty()) {
91 result = Optional.empty();
93 throw new IllegalStateException("more than one result returned");
99 protected Optional<String> getCachedValue() {
100 return this.cachedValue;
106 if (this.values.length == 1) {
107 String uri = getSerivceInstance(this.values[0]);
108 Map<String, String> map = getURIKeys(uri);
109 return super.build(map.values().toArray(values));
111 } catch (GraphInventoryUriNotFoundException | GraphInventoryPayloadException e) {
112 throw new GraphInventoryUriComputationException(e);
114 return super.build();
118 public ServiceInstanceUri clone() {
119 return new ServiceInstanceUri(this.internalURI.clone(), this.getCachedValue(), values);
122 protected AAIQueryClient getQueryClient() {
123 AAIResourceUri uri = AAIUriFactory.createNodesUri(AAIObjectType.ALLOTTED_RESOURCE, "").clone();
124 return new AAIQueryClient();