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.Pathed;
37 import org.onap.so.client.graphinventory.entities.uri.HttpAwareUri;
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;
41 import org.onap.so.client.graphinventory.exceptions.IncorrectNumberOfUriKeys;
43 import com.fasterxml.jackson.core.type.TypeReference;
44 import com.fasterxml.jackson.databind.ObjectMapper;
46 public abstract class HttpLookupUri extends AAISimpleUri implements HttpAwareUri {
48 private Optional<String> cachedValue = Optional.empty();
49 private final AAIObjectType aaiType;
50 protected HttpLookupUri(AAIObjectType type, Object... values) {
54 protected HttpLookupUri(AAIObjectType type, UriBuilder builder, Optional<String> cachedValue, Object... values) {
55 super(type, builder, values);
56 this.cachedValue = cachedValue;
59 protected String getObjectById(Object id) throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
60 if (!this.getCachedValue().isPresent()) {
61 AAIResourceUri serviceInstanceUri = AAIUriFactory.createNodesUri(aaiType, id).format(Format.PATHED);
64 resultJson = this.getResourcesClient().get(serviceInstanceUri, NotFoundException.class).getJson();
65 } catch (BadRequestException e) {
66 throw new GraphInventoryUriNotFoundException(aaiType.typeName() + " " + id + " not found at: " + serviceInstanceUri.build());
70 cachedValue = extractRelatedLink(resultJson);
71 if (!cachedValue.isPresent()) {
72 throw new GraphInventoryUriNotFoundException(aaiType.typeName() + " " + id + " not found at: " + serviceInstanceUri.build());
74 } catch (IOException e) {
75 throw new GraphInventoryPayloadException("could not map payload: " + resultJson, e);
78 Optional<String> cachedValueOpt = this.getCachedValue();
79 return cachedValueOpt.isPresent() ? cachedValueOpt.get() : "";
82 protected Optional<String> extractRelatedLink(String jsonString) throws IOException {
83 Optional<String> result;
84 ObjectMapper mapper = new ObjectMapper();
86 Results<Pathed> results = mapper.readValue(jsonString, new TypeReference<Results<Pathed>>(){});
87 if (results.getResult().size() == 1) {
88 String uriString = results.getResult().get(0).getResourceLink();
89 URI uri = UriBuilder.fromUri(uriString).build();
90 String rawPath = uri.getRawPath();
91 result = Optional.of(rawPath.replaceAll("/aai/v\\d+", ""));
92 } else if (results.getResult().isEmpty()) {
93 result = Optional.empty();
95 throw new IllegalStateException("more than one result returned");
101 protected Optional<String> getCachedValue() {
102 return this.cachedValue;
108 if (this.values.length == 1) {
109 String uri = getObjectById(this.values[0]);
110 Map<String, String> map = getURIKeys(uri);
111 return super.build(map.values().toArray(values));
113 } catch (GraphInventoryUriNotFoundException | GraphInventoryPayloadException e) {
114 throw new GraphInventoryUriComputationException(e);
116 return super.build();
120 public abstract HttpLookupUri clone();
123 public void validateValuesSize(String template, Object... values) {
125 super.validateValuesSize(template, values);
126 } catch (IncorrectNumberOfUriKeys e) {
127 if (values.length == 1) {
128 //Special case where we perform an http look up
134 public AAIResourcesClient getResourcesClient() {
135 return new AAIResourcesClient();
138 public abstract URI buildNoNetwork();