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;
27 import javax.ws.rs.BadRequestException;
28 import javax.ws.rs.NotFoundException;
29 import javax.ws.rs.core.UriBuilder;
30 import org.onap.so.client.aai.AAIObjectType;
31 import org.onap.so.client.aai.AAIResourcesClient;
32 import org.onap.so.client.aai.entities.Results;
33 import org.onap.so.client.graphinventory.Format;
34 import org.onap.so.client.graphinventory.entities.Pathed;
35 import org.onap.so.client.graphinventory.entities.uri.HttpAwareUri;
36 import org.onap.so.client.graphinventory.exceptions.GraphInventoryPayloadException;
37 import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriComputationException;
38 import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriNotFoundException;
39 import org.onap.so.client.graphinventory.exceptions.IncorrectNumberOfUriKeys;
40 import com.fasterxml.jackson.core.type.TypeReference;
41 import com.fasterxml.jackson.databind.ObjectMapper;
43 public abstract class HttpLookupUri extends AAISimpleUri implements HttpAwareUri {
45 private Optional<String> cachedValue = Optional.empty();
46 private final AAIObjectType aaiType;
48 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;
59 protected String getObjectById(Object id)
60 throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
61 if (!this.getCachedValue().isPresent()) {
62 AAIResourceUri serviceInstanceUri = AAIUriFactory.createNodesUri(aaiType, id).format(Format.PATHED);
65 resultJson = this.getResourcesClient().get(serviceInstanceUri, NotFoundException.class).getJson();
66 } catch (BadRequestException e) {
67 throw new GraphInventoryUriNotFoundException(
68 aaiType.typeName() + " " + id + " not found at: " + serviceInstanceUri.build());
72 cachedValue = extractRelatedLink(resultJson);
73 if (!cachedValue.isPresent()) {
74 throw new GraphInventoryUriNotFoundException(
75 aaiType.typeName() + " " + id + " not found at: " + serviceInstanceUri.build());
77 } catch (IOException e) {
78 throw new GraphInventoryPayloadException("could not map payload: " + resultJson, e);
81 Optional<String> cachedValueOpt = this.getCachedValue();
82 return cachedValueOpt.isPresent() ? cachedValueOpt.get() : "";
85 protected Optional<String> extractRelatedLink(String jsonString) throws IOException {
86 Optional<String> result;
87 ObjectMapper mapper = new ObjectMapper();
89 Results<Pathed> results = mapper.readValue(jsonString, new TypeReference<Results<Pathed>>() {});
90 if (results.getResult().size() == 1) {
91 String uriString = results.getResult().get(0).getResourceLink();
92 URI uri = UriBuilder.fromUri(uriString).build();
93 String rawPath = uri.getRawPath();
94 result = Optional.of(rawPath.replaceAll("/aai/v\\d+", ""));
95 } else if (results.getResult().isEmpty()) {
96 result = Optional.empty();
98 throw new IllegalStateException("more than one result returned");
104 protected Optional<String> getCachedValue() {
105 return this.cachedValue;
111 if (this.values.length == 1) {
112 String uri = getObjectById(this.values[0]);
113 Map<String, String> map = getURIKeys(uri);
114 return super.build(map.values().toArray(values));
116 } catch (GraphInventoryUriNotFoundException | GraphInventoryPayloadException e) {
117 throw new GraphInventoryUriComputationException(e);
119 return super.build();
123 public abstract HttpLookupUri clone();
126 public void validateValuesSize(String template, Object... values) {
128 super.validateValuesSize(template, values);
129 } catch (IncorrectNumberOfUriKeys e) {
130 if (values.length == 1) {
131 // Special case where we perform an http look up
138 public AAIResourcesClient getResourcesClient() {
139 return new AAIResourcesClient();
143 public abstract URI buildNoNetwork();