2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.mso.client.aai.entities.uri;
23 import java.io.IOException;
25 import java.util.Collections;
27 import java.util.Optional;
29 import javax.ws.rs.BadRequestException;
30 import javax.ws.rs.client.ResponseProcessingException;
31 import javax.ws.rs.core.UriBuilder;
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;
42 import com.fasterxml.jackson.core.type.TypeReference;
43 import com.fasterxml.jackson.databind.ObjectMapper;
45 public class ServiceInstanceUri extends SimpleUri {
47 private Optional<String> cachedValue = Optional.empty();
49 protected ServiceInstanceUri(Object... values) {
50 super(AAIObjectType.SERVICE_INSTANCE, values);
52 protected ServiceInstanceUri(UriBuilder builder, Optional<String> cachedValue, Object... values) {
53 super(AAIObjectType.SERVICE_INSTANCE, builder, values);
54 this.cachedValue = cachedValue;
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));
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());
71 cachedValue = extractRelatedLink(resultJson);
72 if (!cachedValue.isPresent()) {
73 throw new AAIUriNotFoundException("Service instance " + id + " not found at: " + serviceInstanceUri.build());
75 } catch (IOException e) {
76 throw new AAIPayloadException("could not map payload: " + resultJson, e);
81 return this.getCachedValue().get();
84 protected Optional<String> extractRelatedLink(String jsonString) throws IOException {
85 Optional<String> result;
86 ObjectMapper mapper = new ObjectMapper();
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();
97 throw new IllegalStateException("more than one result returned");
103 protected Optional<String> getCachedValue() {
104 return this.cachedValue;
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));
115 } catch (AAIUriNotFoundException | AAIPayloadException e) {
116 throw new AAIUriComputationException(e);
118 return super.build();
122 public ServiceInstanceUri clone() {
123 return new ServiceInstanceUri(this.internalURI.clone(), this.getCachedValue(), values);
126 protected AAIQueryClient getQueryClient() {
127 return new AAIQueryClient();