diff options
author | Rob Daugherty <rd472p@att.com> | 2018-03-14 02:07:32 -0400 |
---|---|---|
committer | Rob Daugherty <rd472p@att.com> | 2018-03-14 04:08:41 -0400 |
commit | 38f720752af4d4aad8c4e467a288d9048659f688 (patch) | |
tree | e81066a8b5c77272e30fb57a64999573c4db4d86 /common/src/main/java/org/openecomp/mso/client/aai/entities/uri/ServiceInstanceUri.java | |
parent | aee3d223f92a6f250f43e17558a2dfd576ff7294 (diff) |
AT&T 1712 and 1802 release code
This is code from AT&T's 1712 and 1802 releases.
Change-Id: Ie1e85851e94bc66c4d9514a0226c221939531a04
Issue-ID: SO-425
Signed-off-by: Rob Daugherty <rd472p@att.com>
Diffstat (limited to 'common/src/main/java/org/openecomp/mso/client/aai/entities/uri/ServiceInstanceUri.java')
-rw-r--r-- | common/src/main/java/org/openecomp/mso/client/aai/entities/uri/ServiceInstanceUri.java | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/common/src/main/java/org/openecomp/mso/client/aai/entities/uri/ServiceInstanceUri.java b/common/src/main/java/org/openecomp/mso/client/aai/entities/uri/ServiceInstanceUri.java new file mode 100644 index 0000000000..cdaecedfa9 --- /dev/null +++ b/common/src/main/java/org/openecomp/mso/client/aai/entities/uri/ServiceInstanceUri.java @@ -0,0 +1,129 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.mso.client.aai.entities.uri; + +import java.io.IOException; +import java.net.URI; +import java.util.Collections; +import java.util.Map; +import java.util.Optional; + +import javax.ws.rs.BadRequestException; +import javax.ws.rs.client.ResponseProcessingException; +import javax.ws.rs.core.UriBuilder; + +import org.openecomp.mso.client.aai.AAIObjectType; +import org.openecomp.mso.client.aai.AAIQueryClient; +import org.openecomp.mso.client.aai.Format; +import org.openecomp.mso.client.aai.entities.CustomQuery; +import org.openecomp.mso.client.aai.entities.Results; +import org.openecomp.mso.client.aai.exceptions.AAIPayloadException; +import org.openecomp.mso.client.aai.exceptions.AAIUriComputationException; +import org.openecomp.mso.client.aai.exceptions.AAIUriNotFoundException; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class ServiceInstanceUri extends SimpleUri { + + private Optional<String> cachedValue = Optional.empty(); + + protected ServiceInstanceUri(Object... values) { + super(AAIObjectType.SERVICE_INSTANCE, values); + } + protected ServiceInstanceUri(UriBuilder builder, Optional<String> cachedValue, Object... values) { + super(AAIObjectType.SERVICE_INSTANCE, builder, values); + this.cachedValue = cachedValue; + } + protected String getSerivceInstance(Object id) throws AAIUriNotFoundException, AAIPayloadException { + if (!this.getCachedValue().isPresent()) { + AAIResourceUri serviceInstanceUri = AAIUriFactory.createNodesUri(AAIObjectType.SERVICE_INSTANCE, id); + CustomQuery query = new CustomQuery(Collections.singletonList(serviceInstanceUri)); + String resultJson; + try { + resultJson = this.getQueryClient().query(Format.PATHED, query); + } catch (ResponseProcessingException e) { + if (e.getCause() instanceof BadRequestException) { + throw new AAIUriNotFoundException("Service instance " + id + " not found at: " + serviceInstanceUri.build()); + } else { + throw e; + } + } + try { + cachedValue = extractRelatedLink(resultJson); + if (!cachedValue.isPresent()) { + throw new AAIUriNotFoundException("Service instance " + id + " not found at: " + serviceInstanceUri.build()); + } + } catch (IOException e) { + throw new AAIPayloadException("could not map payload: " + resultJson, e); + } + + } + + return this.getCachedValue().get(); + } + + protected Optional<String> extractRelatedLink(String jsonString) throws IOException { + Optional<String> result; + ObjectMapper mapper = new ObjectMapper(); + + Results<Map<String, String>> results = mapper.readValue(jsonString, new TypeReference<Results<Map<String, String>>>(){}); + if (results.getResult().size() == 1) { + String uriString = results.getResult().get(0).get("resource-link"); + URI uri = UriBuilder.fromUri(uriString).build(); + String rawPath = uri.getRawPath(); + result = Optional.of(rawPath.replaceAll("/aai/v\\d+", "")); + } else if (results.getResult().isEmpty()) { + result = Optional.empty(); + } else { + throw new IllegalStateException("more than one result returned"); + } + + return result; + } + + protected Optional<String> getCachedValue() { + return this.cachedValue; + } + + @Override + public URI build() { + try { + if (this.values.length == 1) { + String uri = getSerivceInstance(this.values[0]); + Map<String, String> map = getURIKeys(uri); + return super.build(map.values().toArray(values)); + } + } catch (AAIUriNotFoundException | AAIPayloadException e) { + throw new AAIUriComputationException(e); + } + return super.build(); + } + + @Override + public ServiceInstanceUri clone() { + return new ServiceInstanceUri(this.internalURI.clone(), this.getCachedValue(), values); + } + + protected AAIQueryClient getQueryClient() { + return new AAIQueryClient(); + } +}
\ No newline at end of file |