Merge "Reorder modifiers"
[so.git] / common / src / main / java / org / openecomp / mso / 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.openecomp.mso.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.client.ResponseProcessingException;
31 import javax.ws.rs.core.UriBuilder;
32
33 import org.openecomp.mso.client.aai.AAIObjectType;
34 import org.openecomp.mso.client.aai.AAIQueryClient;
35 import org.openecomp.mso.client.aai.Format;
36 import org.openecomp.mso.client.aai.entities.CustomQuery;
37 import org.openecomp.mso.client.aai.entities.Results;
38 import org.openecomp.mso.client.aai.exceptions.AAIPayloadException;
39 import org.openecomp.mso.client.aai.exceptions.AAIUriComputationException;
40 import org.openecomp.mso.client.aai.exceptions.AAIUriNotFoundException;
41
42 import com.fasterxml.jackson.core.type.TypeReference;
43 import com.fasterxml.jackson.databind.ObjectMapper;
44
45 public class ServiceInstanceUri extends SimpleUri {
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 AAIUriNotFoundException, AAIPayloadException {
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 (ResponseProcessingException e) {
64                                 if (e.getCause() instanceof BadRequestException) {
65                                         throw new AAIUriNotFoundException("Service instance " + id + " not found at: " + serviceInstanceUri.build());
66                                 } else {
67                                         throw e;
68                                 }
69                         }
70                         try {
71                                 cachedValue = extractRelatedLink(resultJson);
72                                 if (!cachedValue.isPresent()) {
73                                         throw new AAIUriNotFoundException("Service instance " + id + " not found at: " + serviceInstanceUri.build());
74                                 }
75                         } catch (IOException e) {
76                                 throw new AAIPayloadException("could not map payload: " + resultJson, e);
77                         }
78                         
79                 }
80                 
81                 return this.getCachedValue().get();
82         }
83         
84         protected Optional<String> extractRelatedLink(String jsonString) throws IOException {
85                 Optional<String> result;
86                 ObjectMapper mapper = new ObjectMapper();
87                 
88                         Results<Map<String, String>> results = mapper.readValue(jsonString, new TypeReference<Results<Map<String, String>>>(){});
89                         if (results.getResult().size() == 1) {
90                                 String uriString = results.getResult().get(0).get("resource-link");
91                                 URI uri = UriBuilder.fromUri(uriString).build();
92                                 String rawPath = uri.getRawPath();
93                         result = Optional.of(rawPath.replaceAll("/aai/v\\d+", ""));
94                         } else if (results.getResult().isEmpty()) {
95                         result = Optional.empty();
96                         } else {
97                                 throw new IllegalStateException("more than one result returned");
98                         }
99         
100                 return result;
101         }
102         
103         protected Optional<String> getCachedValue() {
104                 return this.cachedValue;
105         }
106
107         @Override
108         public URI build() {
109                 try {
110                 if (this.values.length == 1) {
111                         String uri = getSerivceInstance(this.values[0]);
112                         Map<String, String> map = getURIKeys(uri);
113                         return super.build(map.values().toArray(values));
114                 }
115                 } catch (AAIUriNotFoundException | AAIPayloadException e) {
116                         throw new AAIUriComputationException(e);
117                 }
118                 return super.build();
119         }
120         
121         @Override
122         public ServiceInstanceUri clone() {
123                 return new ServiceInstanceUri(this.internalURI.clone(), this.getCachedValue(), values);
124         }
125         
126         protected AAIQueryClient getQueryClient() {
127                 return new AAIQueryClient();
128         }
129 }