Merge "Removed deprecated Matcher imports"
[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
28 import javax.ws.rs.BadRequestException;
29 import javax.ws.rs.NotFoundException;
30 import javax.ws.rs.core.UriBuilder;
31
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;
41
42 import com.fasterxml.jackson.core.type.TypeReference;
43 import com.fasterxml.jackson.databind.ObjectMapper;
44
45 public abstract class HttpLookupUri extends AAISimpleUri implements HttpAwareUri {
46
47         private Optional<String> cachedValue = Optional.empty();
48         private final AAIObjectType aaiType;
49         protected HttpLookupUri(AAIObjectType type, Object... values) {
50                 super(type, values);
51                 this.aaiType = type;
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         protected String getObjectById(Object id) throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
59                 if (!this.getCachedValue().isPresent()) {
60                         AAIResourceUri serviceInstanceUri = AAIUriFactory.createNodesUri(aaiType, id).format(Format.PATHED);
61                         String resultJson;
62                         try {
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());
66                                 
67                         }
68                         try {
69                                 cachedValue = extractRelatedLink(resultJson);
70                                 if (!cachedValue.isPresent()) {
71                                         throw new GraphInventoryUriNotFoundException(aaiType.typeName() + " " + id + " not found at: " + serviceInstanceUri.build());
72                                 }
73                         } catch (IOException e) {
74                                 throw new GraphInventoryPayloadException("could not map payload: " + resultJson, e);
75                         }
76                 }
77                 Optional<String> cachedValueOpt = this.getCachedValue();
78                 return cachedValueOpt.isPresent() ? cachedValueOpt.get() : "";
79         }
80         
81         protected Optional<String> extractRelatedLink(String jsonString) throws IOException {
82                 Optional<String> result;
83                 ObjectMapper mapper = new ObjectMapper();
84                 
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();
93                 } else {
94                         throw new IllegalStateException("more than one result returned");
95                 }
96         
97                 return result;
98         }
99         
100         protected Optional<String> getCachedValue() {
101                 return this.cachedValue;
102         }
103         
104         @Override
105         public URI build() {
106                 try {
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));
111                         }
112                 } catch (GraphInventoryUriNotFoundException | GraphInventoryPayloadException e) {
113                         throw new GraphInventoryUriComputationException(e);
114                 }
115                 return super.build();
116         }
117         
118         @Override
119         public abstract HttpLookupUri clone();
120         
121         @Override
122         public void validateValuesSize(String template, Object... values) {
123                 try {
124                         super.validateValuesSize(template, values);
125                 } catch (IncorrectNumberOfUriKeys e) {
126                         if (values.length == 1) {
127                                 //Special case where we perform an http look up
128                         } else {
129                                 throw e;
130                         }
131                 }
132         }
133         public AAIResourcesClient getResourcesClient() {
134                 return new AAIResourcesClient();
135         }
136         @Override
137         public abstract URI buildNoNetwork();
138 }