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.aaiclient.client.aai.entities.uri;
 
  23 import java.io.IOException;
 
  24 import java.io.ObjectInputStream;
 
  25 import java.io.ObjectOutputStream;
 
  28 import java.util.Optional;
 
  29 import javax.ws.rs.BadRequestException;
 
  30 import javax.ws.rs.NotFoundException;
 
  31 import javax.ws.rs.core.UriBuilder;
 
  32 import org.onap.aaiclient.client.aai.AAIObjectType;
 
  33 import org.onap.aaiclient.client.aai.AAIResourcesClient;
 
  34 import org.onap.aaiclient.client.aai.entities.Results;
 
  35 import org.onap.aaiclient.client.graphinventory.Format;
 
  36 import org.onap.aaiclient.client.graphinventory.entities.Pathed;
 
  37 import org.onap.aaiclient.client.graphinventory.entities.uri.HttpAwareUri;
 
  38 import org.onap.aaiclient.client.graphinventory.exceptions.GraphInventoryPayloadException;
 
  39 import org.onap.aaiclient.client.graphinventory.exceptions.GraphInventoryUriComputationException;
 
  40 import org.onap.aaiclient.client.graphinventory.exceptions.GraphInventoryUriNotFoundException;
 
  41 import org.onap.aaiclient.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 transient Optional<String> cachedValue = Optional.empty();
 
  48     private final AAIObjectType aaiType;
 
  50     protected HttpLookupUri(AAIObjectType type, Object... values) {
 
  55     protected HttpLookupUri(AAIObjectType type, UriBuilder builder, Optional<String> cachedValue, Object... values) {
 
  56         super(type, builder, values);
 
  57         this.cachedValue = cachedValue;
 
  61     protected String getObjectById(Object id)
 
  62             throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
 
  63         if (!this.getCachedValue().isPresent()) {
 
  64             AAIResourceUri serviceInstanceUri = AAIUriFactory.createNodesUri(aaiType, id).format(Format.PATHED);
 
  67                 resultJson = this.getResourcesClient().get(serviceInstanceUri, NotFoundException.class).getJson();
 
  68             } catch (BadRequestException e) {
 
  69                 throw new GraphInventoryUriNotFoundException(
 
  70                         aaiType.typeName() + " " + id + " not found at: " + serviceInstanceUri.build());
 
  74                 cachedValue = extractRelatedLink(resultJson);
 
  75                 if (!cachedValue.isPresent()) {
 
  76                     throw new GraphInventoryUriNotFoundException(
 
  77                             aaiType.typeName() + " " + id + " not found at: " + serviceInstanceUri.build());
 
  79             } catch (IOException e) {
 
  80                 throw new GraphInventoryPayloadException("could not map payload: " + resultJson, e);
 
  83         return cachedValue.get();
 
  86     protected Optional<String> extractRelatedLink(String jsonString) throws IOException {
 
  87         Optional<String> result;
 
  88         ObjectMapper mapper = new ObjectMapper();
 
  90         Results<Pathed> results = mapper.readValue(jsonString, new TypeReference<Results<Pathed>>() {});
 
  91         if (results.getResult().size() == 1) {
 
  92             String uriString = results.getResult().get(0).getResourceLink();
 
  93             URI uri = UriBuilder.fromUri(uriString).build();
 
  94             String rawPath = uri.getRawPath();
 
  95             result = Optional.of(rawPath.replaceAll("/aai/v\\d+", ""));
 
  96         } else if (results.getResult().isEmpty()) {
 
  97             result = Optional.empty();
 
  99             throw new IllegalStateException("more than one result returned");
 
 105     protected Optional<String> getCachedValue() {
 
 106         return this.cachedValue;
 
 112             if (this.values.length == 1) {
 
 113                 String uri = getObjectById(this.values[0]);
 
 114                 Map<String, String> map = super.getURIKeys(uri);
 
 115                 this.values = map.values().toArray(values);
 
 116                 return super.build(values);
 
 118         } catch (GraphInventoryUriNotFoundException | GraphInventoryPayloadException e) {
 
 119             throw new GraphInventoryUriComputationException(e);
 
 121         return super.build();
 
 125     public URI locateAndBuild() {
 
 127             if (this.values.length == 1) {
 
 128                 String uri = getObjectById(this.values[0]);
 
 129                 Map<String, String> map = super.getURIKeys(uri);
 
 130                 this.values = map.values().toArray(values);
 
 131                 return super.build(values);
 
 133         } catch (GraphInventoryUriNotFoundException | GraphInventoryPayloadException e) {
 
 134             throw new GraphInventoryUriComputationException(e);
 
 136         return super.build();
 
 140     public abstract HttpLookupUri clone();
 
 143     public void validateValuesSize(String template, Object... values) {
 
 145             super.validateValuesSize(template, values);
 
 146         } catch (IncorrectNumberOfUriKeys e) {
 
 147             if (values.length == 1) {
 
 148                 // Special case where we perform an http look up
 
 155     public AAIResourcesClient getResourcesClient() {
 
 156         return new AAIResourcesClient();
 
 159     private void writeObject(ObjectOutputStream oos) throws IOException {
 
 161         oos.writeUTF(this.cachedValue.orElse(""));
 
 164     private void readObject(ObjectInputStream ois) throws IOException {
 
 166         String value = ois.readUTF();
 
 167         if ("".equals(value)) {
 
 168             this.cachedValue = Optional.empty();
 
 170             this.cachedValue = Optional.ofNullable(value);