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