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;
26 import java.util.Optional;
28 import javax.ws.rs.BadRequestException;
29 import javax.ws.rs.NotFoundException;
30 import javax.ws.rs.core.UriBuilder;
32 import org.onap.so.client.aai.AAIObjectType;
33 import org.onap.so.client.aai.AAIResourcesClient;
34 import org.onap.so.client.aai.entities.Results;
35 import org.onap.so.client.graphinventory.Format;
36 import org.onap.so.client.graphinventory.entities.uri.HttpAwareUri;
37 import org.onap.so.client.graphinventory.exceptions.GraphInventoryPayloadException;
38 import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriComputationException;
39 import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriNotFoundException;
40 import org.onap.so.client.graphinventory.exceptions.IncorrectNumberOfUriKeys;
42 import com.fasterxml.jackson.core.type.TypeReference;
43 import com.fasterxml.jackson.databind.ObjectMapper;
45 public abstract class HttpLookupUri extends AAISimpleUri implements HttpAwareUri {
47 private Optional<String> cachedValue = Optional.empty();
48 private final AAIObjectType aaiType;
49 protected HttpLookupUri(AAIObjectType type, Object... values) {
53 protected HttpLookupUri(AAIObjectType type, UriBuilder builder, Optional<String> cachedValue, Object... values) {
54 super(type, builder, values);
55 this.cachedValue = cachedValue;
58 protected String getObjectById(Object id) throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
59 if (!this.getCachedValue().isPresent()) {
60 AAIResourceUri serviceInstanceUri = AAIUriFactory.createNodesUri(aaiType, id).format(Format.PATHED);
63 resultJson = this.getResourcesClient().get(serviceInstanceUri, NotFoundException.class).getJson();
64 } catch (BadRequestException e) {
65 throw new GraphInventoryUriNotFoundException(aaiType.typeName() + " " + id + " not found at: " + serviceInstanceUri.build());
69 cachedValue = extractRelatedLink(resultJson);
70 if (!cachedValue.isPresent()) {
71 throw new GraphInventoryUriNotFoundException(aaiType.typeName() + " " + id + " not found at: " + serviceInstanceUri.build());
73 } catch (IOException e) {
74 throw new GraphInventoryPayloadException("could not map payload: " + resultJson, e);
77 Optional<String> cachedValueOpt = this.getCachedValue();
78 return cachedValueOpt.isPresent() ? cachedValueOpt.get() : "";
81 protected Optional<String> extractRelatedLink(String jsonString) throws IOException {
82 Optional<String> result;
83 ObjectMapper mapper = new ObjectMapper();
85 Results<Map<String, String>> results = mapper.readValue(jsonString, new TypeReference<Results<Map<String, String>>>(){});
86 if (results.getResult().size() == 1) {
87 String uriString = results.getResult().get(0).get("resource-link");
88 URI uri = UriBuilder.fromUri(uriString).build();
89 String rawPath = uri.getRawPath();
90 result = Optional.of(rawPath.replaceAll("/aai/v\\d+", ""));
91 } else if (results.getResult().isEmpty()) {
92 result = Optional.empty();
94 throw new IllegalStateException("more than one result returned");
100 protected Optional<String> getCachedValue() {
101 return this.cachedValue;
107 if (this.values.length == 1) {
108 String uri = getObjectById(this.values[0]);
109 Map<String, String> map = getURIKeys(uri);
110 return super.build(map.values().toArray(values));
112 } catch (GraphInventoryUriNotFoundException | GraphInventoryPayloadException e) {
113 throw new GraphInventoryUriComputationException(e);
115 return super.build();
119 public abstract HttpLookupUri clone();
122 public void validateValuesSize(String template, Object... values) {
124 super.validateValuesSize(template, values);
125 } catch (IncorrectNumberOfUriKeys e) {
126 if (values.length == 1) {
127 //Special case where we perform an http look up
133 public AAIResourcesClient getResourcesClient() {
134 return new AAIResourcesClient();
137 public abstract URI buildNoNetwork();