Replaced all tabs with spaces in java and pom.xml
[so.git] / common / src / main / java / org / onap / so / client / aai / entities / uri / HttpLookupUri.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.client.aai.entities.uri;
22
23 import java.io.IOException;
24 import java.net.URI;
25 import java.util.Map;
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;
42
43 public abstract class HttpLookupUri extends AAISimpleUri implements HttpAwareUri {
44
45     private Optional<String> cachedValue = Optional.empty();
46     private final AAIObjectType aaiType;
47
48     protected HttpLookupUri(AAIObjectType type, Object... values) {
49         super(type, values);
50         this.aaiType = type;
51     }
52
53     protected HttpLookupUri(AAIObjectType type, UriBuilder builder, Optional<String> cachedValue, Object... values) {
54         super(type, builder, values);
55         this.cachedValue = cachedValue;
56         this.aaiType = type;
57     }
58
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);
63             String resultJson;
64             try {
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());
69
70             }
71             try {
72                 cachedValue = extractRelatedLink(resultJson);
73                 if (!cachedValue.isPresent()) {
74                     throw new GraphInventoryUriNotFoundException(
75                             aaiType.typeName() + " " + id + " not found at: " + serviceInstanceUri.build());
76                 }
77             } catch (IOException e) {
78                 throw new GraphInventoryPayloadException("could not map payload: " + resultJson, e);
79             }
80         }
81         Optional<String> cachedValueOpt = this.getCachedValue();
82         return cachedValueOpt.isPresent() ? cachedValueOpt.get() : "";
83     }
84
85     protected Optional<String> extractRelatedLink(String jsonString) throws IOException {
86         Optional<String> result;
87         ObjectMapper mapper = new ObjectMapper();
88
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();
97         } else {
98             throw new IllegalStateException("more than one result returned");
99         }
100
101         return result;
102     }
103
104     protected Optional<String> getCachedValue() {
105         return this.cachedValue;
106     }
107
108     @Override
109     public URI build() {
110         try {
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));
115             }
116         } catch (GraphInventoryUriNotFoundException | GraphInventoryPayloadException e) {
117             throw new GraphInventoryUriComputationException(e);
118         }
119         return super.build();
120     }
121
122     @Override
123     public abstract HttpLookupUri clone();
124
125     @Override
126     public void validateValuesSize(String template, Object... values) {
127         try {
128             super.validateValuesSize(template, values);
129         } catch (IncorrectNumberOfUriKeys e) {
130             if (values.length == 1) {
131                 // Special case where we perform an http look up
132             } else {
133                 throw e;
134             }
135         }
136     }
137
138     public AAIResourcesClient getResourcesClient() {
139         return new AAIResourcesClient();
140     }
141
142     @Override
143     public abstract URI buildNoNetwork();
144 }