HttpLookupUri is now serializable again
[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.io.ObjectInputStream;
25 import java.io.ObjectOutputStream;
26 import java.net.URI;
27 import java.util.Map;
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.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 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 transient Optional<String> cachedValue = Optional.empty();
48     private final AAIObjectType aaiType;
49
50     protected HttpLookupUri(AAIObjectType type, Object... values) {
51         super(type, values);
52         this.aaiType = type;
53     }
54
55     protected HttpLookupUri(AAIObjectType type, UriBuilder builder, Optional<String> cachedValue, Object... values) {
56         super(type, builder, values);
57         this.cachedValue = cachedValue;
58         this.aaiType = type;
59     }
60
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);
65             String resultJson;
66             try {
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());
71
72             }
73             try {
74                 cachedValue = extractRelatedLink(resultJson);
75                 if (!cachedValue.isPresent()) {
76                     throw new GraphInventoryUriNotFoundException(
77                             aaiType.typeName() + " " + id + " not found at: " + serviceInstanceUri.build());
78                 }
79             } catch (IOException e) {
80                 throw new GraphInventoryPayloadException("could not map payload: " + resultJson, e);
81             }
82         }
83         return cachedValue.get();
84     }
85
86     protected Optional<String> extractRelatedLink(String jsonString) throws IOException {
87         Optional<String> result;
88         ObjectMapper mapper = new ObjectMapper();
89
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();
98         } else {
99             throw new IllegalStateException("more than one result returned");
100         }
101
102         return result;
103     }
104
105     protected Optional<String> getCachedValue() {
106         return this.cachedValue;
107     }
108
109     @Override
110     public URI build() {
111         try {
112             if (this.values.length == 1) {
113                 String uri = getObjectById(this.values[0]);
114                 Map<String, String> map = getURIKeys(uri);
115                 return super.build(map.values().toArray(values));
116             }
117         } catch (GraphInventoryUriNotFoundException | GraphInventoryPayloadException e) {
118             throw new GraphInventoryUriComputationException(e);
119         }
120         return super.build();
121     }
122
123     @Override
124     public abstract HttpLookupUri clone();
125
126     @Override
127     public void validateValuesSize(String template, Object... values) {
128         try {
129             super.validateValuesSize(template, values);
130         } catch (IncorrectNumberOfUriKeys e) {
131             if (values.length == 1) {
132                 // Special case where we perform an http look up
133             } else {
134                 throw e;
135             }
136         }
137     }
138
139     public AAIResourcesClient getResourcesClient() {
140         return new AAIResourcesClient();
141     }
142
143     private void writeObject(ObjectOutputStream oos) throws IOException {
144
145         oos.writeUTF(this.cachedValue.orElse(""));
146     }
147
148     private void readObject(ObjectInputStream ois) throws IOException {
149
150         String value = ois.readUTF();
151         if ("".equals(value)) {
152             this.cachedValue = Optional.empty();
153         } else {
154             this.cachedValue = Optional.ofNullable(value);
155         }
156
157     }
158
159     @Override
160     public abstract URI buildNoNetwork();
161 }