Containerization feature of SO
[so.git] / common / src / main / java / org / onap / so / client / aai / entities / uri / ServiceInstanceUri.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.Collections;
26 import java.util.Map;
27 import java.util.Optional;
28
29 import javax.ws.rs.BadRequestException;
30 import javax.ws.rs.core.UriBuilder;
31
32 import org.onap.so.client.aai.AAIObjectType;
33 import org.onap.so.client.aai.AAIQueryClient;
34 import org.onap.so.client.graphinventory.Format;
35 import org.onap.so.client.aai.entities.CustomQuery;
36 import org.onap.so.client.aai.entities.Results;
37 import org.onap.so.client.graphinventory.entities.uri.SimpleUri;
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
42 import com.fasterxml.jackson.core.type.TypeReference;
43 import com.fasterxml.jackson.databind.ObjectMapper;
44
45 public class ServiceInstanceUri extends AAISimpleUri {
46
47         private Optional<String> cachedValue = Optional.empty();
48
49         protected ServiceInstanceUri(Object... values) {
50                 super(AAIObjectType.SERVICE_INSTANCE, values);
51         }
52         protected ServiceInstanceUri(UriBuilder builder, Optional<String> cachedValue, Object... values) {
53                 super(AAIObjectType.SERVICE_INSTANCE, builder, values);
54                 this.cachedValue = cachedValue;
55         }
56         protected String getSerivceInstance(Object id) throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
57                 if (!this.getCachedValue().isPresent()) {
58                         AAIResourceUri serviceInstanceUri = AAIUriFactory.createNodesUri(AAIObjectType.SERVICE_INSTANCE, id);
59                         CustomQuery query = new CustomQuery(Collections.singletonList(serviceInstanceUri));
60                         String resultJson;
61                         try {
62                                 resultJson = this.getQueryClient().query(Format.PATHED, query);
63                         } catch (BadRequestException e) {
64                                 throw new GraphInventoryUriNotFoundException("Service instance " + id + " not found at: " + serviceInstanceUri.build());
65                                 
66                         }
67                         try {
68                                 cachedValue = extractRelatedLink(resultJson);
69                                 if (!cachedValue.isPresent()) {
70                                         throw new GraphInventoryUriNotFoundException("Service instance " + id + " not found at: " + serviceInstanceUri.build());
71                                 }
72                         } catch (IOException e) {
73                                 throw new GraphInventoryPayloadException("could not map payload: " + resultJson, e);
74                         }
75                 }
76                 Optional<String> cachedValueOpt = this.getCachedValue();
77                 return cachedValueOpt.isPresent() ? cachedValueOpt.get() : "";
78         }
79         
80         protected Optional<String> extractRelatedLink(String jsonString) throws IOException {
81                 Optional<String> result;
82                 ObjectMapper mapper = new ObjectMapper();
83                 
84                 Results<Map<String, String>> results = mapper.readValue(jsonString, new TypeReference<Results<Map<String, String>>>(){});
85                 if (results.getResult().size() == 1) {
86                         String uriString = results.getResult().get(0).get("resource-link");
87                         URI uri = UriBuilder.fromUri(uriString).build();
88                         String rawPath = uri.getRawPath();
89                         result = Optional.of(rawPath.replaceAll("/aai/v\\d+", ""));
90                 } else if (results.getResult().isEmpty()) {
91                         result = Optional.empty();
92                 } else {
93                         throw new IllegalStateException("more than one result returned");
94                 }
95         
96                 return result;
97         }
98         
99         protected Optional<String> getCachedValue() {
100                 return this.cachedValue;
101         }
102
103         @Override
104         public URI build() {
105                 try {
106                         if (this.values.length == 1) {
107                                 String uri = getSerivceInstance(this.values[0]);
108                                 Map<String, String> map = getURIKeys(uri);
109                                 return super.build(map.values().toArray(values));
110                         }
111                 } catch (GraphInventoryUriNotFoundException | GraphInventoryPayloadException e) {
112                         throw new GraphInventoryUriComputationException(e);
113                 }
114                 return super.build();
115         }
116         
117         @Override
118         public ServiceInstanceUri clone() {
119                 return new ServiceInstanceUri(this.internalURI.clone(), this.getCachedValue(), values);
120         }
121         
122         protected AAIQueryClient getQueryClient() {
123                 AAIResourceUri uri = AAIUriFactory.createNodesUri(AAIObjectType.ALLOTTED_RESOURCE, "").clone();
124                 return new AAIQueryClient();
125         }
126 }