From 66fe687ab56cac6a1f9ae74f38d4a1660ed8b6d4 Mon Sep 17 00:00:00 2001 From: "Benjamin, Max (mb388a)" Date: Tue, 21 Aug 2018 14:48:46 -0400 Subject: created generic HttpLookupUri class created generic HttpLookupUri abstract class Change-Id: Ie45d620af77fb58c0a7b469aba4a10f274b0de59 Issue-ID: SO-877 Signed-off-by: Benjamin, Max (mb388a) --- .../so/client/aai/entities/uri/AAIUriFactory.java | 9 +- .../entities/uri/AllottedResourceLookupUri.java | 52 +++++++++ .../so/client/aai/entities/uri/HttpLookupUri.java | 126 +++++++++++++++++++++ .../aai/entities/uri/ServiceInstanceUri.java | 80 +------------ .../aai/entities/uri/ServiceInstanceUriTest.java | 10 +- 5 files changed, 193 insertions(+), 84 deletions(-) create mode 100644 common/src/main/java/org/onap/so/client/aai/entities/uri/AllottedResourceLookupUri.java create mode 100644 common/src/main/java/org/onap/so/client/aai/entities/uri/HttpLookupUri.java diff --git a/common/src/main/java/org/onap/so/client/aai/entities/uri/AAIUriFactory.java b/common/src/main/java/org/onap/so/client/aai/entities/uri/AAIUriFactory.java index 2a7c8ee78c..192b593603 100644 --- a/common/src/main/java/org/onap/so/client/aai/entities/uri/AAIUriFactory.java +++ b/common/src/main/java/org/onap/so/client/aai/entities/uri/AAIUriFactory.java @@ -31,7 +31,12 @@ public class AAIUriFactory { /** * values are filled into the URI template specified in {@link AAIObjectType} in order - * + *
+ * There are two special lookups performed on certain types when a single value is specified: + *
+ * Service Instance and AllottedResources + *
+ * These can be retrieved without all their required keys but an HTTP call is required to do so * @param type * @param values * @return @@ -39,6 +44,8 @@ public class AAIUriFactory { public static AAIResourceUri createResourceUri(AAIObjectType type, Object... values) { if (AAIObjectType.SERVICE_INSTANCE.equals(type)) { return new ServiceInstanceUri(values); + } else if (AAIObjectType.ALLOTTED_RESOURCE.equals(type)) { + return new AllottedResourceLookupUri(values); } else { return new AAISimpleUri(type, values); } diff --git a/common/src/main/java/org/onap/so/client/aai/entities/uri/AllottedResourceLookupUri.java b/common/src/main/java/org/onap/so/client/aai/entities/uri/AllottedResourceLookupUri.java new file mode 100644 index 0000000000..e063bf8935 --- /dev/null +++ b/common/src/main/java/org/onap/so/client/aai/entities/uri/AllottedResourceLookupUri.java @@ -0,0 +1,52 @@ +/*- + * ============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.onap.so.client.aai.entities.uri; + +import java.net.URI; +import java.util.Optional; + +import javax.ws.rs.core.UriBuilder; + +import org.onap.so.client.aai.AAIObjectType; +import org.onap.so.client.aai.AAIResourcesClient; + +public class AllottedResourceLookupUri extends HttpLookupUri { + + protected AllottedResourceLookupUri(Object... values) { + super(AAIObjectType.ALLOTTED_RESOURCE, values); + } + protected AllottedResourceLookupUri(UriBuilder builder, Optional cachedValue, Object... values) { + super(AAIObjectType.ALLOTTED_RESOURCE, builder, cachedValue, values); + } + + @Override + public ServiceInstanceUri clone() { + return new ServiceInstanceUri(this.internalURI.clone(), this.getCachedValue(), values); + } + + public AAIResourcesClient getResourcesClient() { + return new AAIResourcesClient(); + } + @Override + public URI buildNoNetwork() { + return super.build(new String[]{"NONE", "NONE", "NONE", (String)this.values[0]}); + } +} diff --git a/common/src/main/java/org/onap/so/client/aai/entities/uri/HttpLookupUri.java b/common/src/main/java/org/onap/so/client/aai/entities/uri/HttpLookupUri.java new file mode 100644 index 0000000000..884f8c6ac4 --- /dev/null +++ b/common/src/main/java/org/onap/so/client/aai/entities/uri/HttpLookupUri.java @@ -0,0 +1,126 @@ +/*- + * ============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.onap.so.client.aai.entities.uri; + +import java.io.IOException; +import java.net.URI; +import java.util.Arrays; +import java.util.Map; +import java.util.Optional; + +import javax.ws.rs.BadRequestException; +import javax.ws.rs.NotFoundException; +import javax.ws.rs.core.UriBuilder; + +import org.onap.so.client.aai.AAIObjectType; +import org.onap.so.client.aai.AAIResourcesClient; +import org.onap.so.client.aai.entities.Results; +import org.onap.so.client.graphinventory.Format; +import org.onap.so.client.graphinventory.entities.uri.HttpAwareUri; +import org.onap.so.client.graphinventory.exceptions.GraphInventoryPayloadException; +import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriComputationException; +import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriNotFoundException; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; + +public abstract class HttpLookupUri extends AAISimpleUri implements HttpAwareUri { + + private Optional cachedValue = Optional.empty(); + private final AAIObjectType aaiType; + protected HttpLookupUri(AAIObjectType type, Object... values) { + super(type, values); + this.aaiType = type; + } + protected HttpLookupUri(AAIObjectType type, UriBuilder builder, Optional cachedValue, Object... values) { + super(type, builder, values); + this.cachedValue = cachedValue; + this.aaiType = type; + } + protected String getObjectById(Object id) throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException { + if (!this.getCachedValue().isPresent()) { + AAIResourceUri serviceInstanceUri = AAIUriFactory.createNodesUri(aaiType, id).format(Format.PATHED); + String resultJson; + try { + resultJson = this.getResourcesClient().get(serviceInstanceUri, NotFoundException.class).getJson(); + } catch (BadRequestException e) { + throw new GraphInventoryUriNotFoundException(aaiType.typeName() + " " + id + " not found at: " + serviceInstanceUri.build()); + + } + try { + cachedValue = extractRelatedLink(resultJson); + if (!cachedValue.isPresent()) { + throw new GraphInventoryUriNotFoundException(aaiType.typeName() + " " + id + " not found at: " + serviceInstanceUri.build()); + } + } catch (IOException e) { + throw new GraphInventoryPayloadException("could not map payload: " + resultJson, e); + } + } + Optional cachedValueOpt = this.getCachedValue(); + return cachedValueOpt.isPresent() ? cachedValueOpt.get() : ""; + } + + protected Optional extractRelatedLink(String jsonString) throws IOException { + Optional result; + ObjectMapper mapper = new ObjectMapper(); + + Results> results = mapper.readValue(jsonString, new TypeReference>>(){}); + 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 getCachedValue() { + return this.cachedValue; + } + + @Override + public URI build() { + try { + if (this.values.length == 1) { + String uri = getObjectById(this.values[0]); + Map map = getURIKeys(uri); + return super.build(map.values().toArray(values)); + } + } catch (GraphInventoryUriNotFoundException | GraphInventoryPayloadException e) { + throw new GraphInventoryUriComputationException(e); + } + return super.build(); + } + + @Override + public abstract HttpLookupUri clone(); + + public AAIResourcesClient getResourcesClient() { + return new AAIResourcesClient(); + } + @Override + public abstract URI buildNoNetwork(); +} diff --git a/common/src/main/java/org/onap/so/client/aai/entities/uri/ServiceInstanceUri.java b/common/src/main/java/org/onap/so/client/aai/entities/uri/ServiceInstanceUri.java index a132e15d1f..00a213b264 100644 --- a/common/src/main/java/org/onap/so/client/aai/entities/uri/ServiceInstanceUri.java +++ b/common/src/main/java/org/onap/so/client/aai/entities/uri/ServiceInstanceUri.java @@ -20,97 +20,21 @@ package org.onap.so.client.aai.entities.uri; -import java.io.IOException; import java.net.URI; -import java.util.Arrays; -import java.util.Map; import java.util.Optional; -import javax.ws.rs.BadRequestException; -import javax.ws.rs.NotFoundException; import javax.ws.rs.core.UriBuilder; import org.onap.so.client.aai.AAIObjectType; import org.onap.so.client.aai.AAIResourcesClient; -import org.onap.so.client.aai.entities.Results; -import org.onap.so.client.graphinventory.Format; -import org.onap.so.client.graphinventory.entities.uri.HttpAwareUri; -import org.onap.so.client.graphinventory.exceptions.GraphInventoryPayloadException; -import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriComputationException; -import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriNotFoundException; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; - -public class ServiceInstanceUri extends AAISimpleUri implements HttpAwareUri { - - private Optional cachedValue = Optional.empty(); +public class ServiceInstanceUri extends HttpLookupUri { protected ServiceInstanceUri(Object... values) { super(AAIObjectType.SERVICE_INSTANCE, values); } protected ServiceInstanceUri(UriBuilder builder, Optional cachedValue, Object... values) { - super(AAIObjectType.SERVICE_INSTANCE, builder, values); - this.cachedValue = cachedValue; - } - protected String getSerivceInstance(Object id) throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException { - if (!this.getCachedValue().isPresent()) { - AAIResourceUri serviceInstanceUri = AAIUriFactory.createNodesUri(AAIObjectType.SERVICE_INSTANCE, id).format(Format.PATHED); - String resultJson; - try { - resultJson = this.getResourcesClient().get(serviceInstanceUri, NotFoundException.class).getJson(); - } catch (BadRequestException e) { - throw new GraphInventoryUriNotFoundException("Service instance " + id + " not found at: " + serviceInstanceUri.build()); - - } - try { - cachedValue = extractRelatedLink(resultJson); - if (!cachedValue.isPresent()) { - throw new GraphInventoryUriNotFoundException("Service instance " + id + " not found at: " + serviceInstanceUri.build()); - } - } catch (IOException e) { - throw new GraphInventoryPayloadException("could not map payload: " + resultJson, e); - } - } - Optional cachedValueOpt = this.getCachedValue(); - return cachedValueOpt.isPresent() ? cachedValueOpt.get() : ""; - } - - protected Optional extractRelatedLink(String jsonString) throws IOException { - Optional result; - ObjectMapper mapper = new ObjectMapper(); - - Results> results = mapper.readValue(jsonString, new TypeReference>>(){}); - 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 getCachedValue() { - return this.cachedValue; - } - - @Override - public URI build() { - try { - if (this.values.length == 1) { - String uri = getSerivceInstance(this.values[0]); - Map map = getURIKeys(uri); - return super.build(map.values().toArray(values)); - } - } catch (GraphInventoryUriNotFoundException | GraphInventoryPayloadException e) { - throw new GraphInventoryUriComputationException(e); - } - return super.build(); + super(AAIObjectType.SERVICE_INSTANCE, builder, cachedValue, values); } @Override diff --git a/common/src/test/java/org/onap/so/client/aai/entities/uri/ServiceInstanceUriTest.java b/common/src/test/java/org/onap/so/client/aai/entities/uri/ServiceInstanceUriTest.java index 2cd7848cfa..26f9d871fd 100644 --- a/common/src/test/java/org/onap/so/client/aai/entities/uri/ServiceInstanceUriTest.java +++ b/common/src/test/java/org/onap/so/client/aai/entities/uri/ServiceInstanceUriTest.java @@ -84,7 +84,7 @@ public class ServiceInstanceUriTest { ServiceInstanceUri instance = new ServiceInstanceUri("key1"); ServiceInstanceUri spy = spy(instance); - doReturn("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3").when(spy).getSerivceInstance(any(Object.class)); + doReturn("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3").when(spy).getObjectById(any(Object.class)); final URI result = spy.build(); final URI expected = UriBuilder.fromPath("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3").build(); @@ -97,7 +97,7 @@ public class ServiceInstanceUriTest { ServiceInstanceUri instance = new ServiceInstanceUri("key1"); ServiceInstanceUri spy = spy(instance); - doReturn("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3").when(spy).getSerivceInstance(any(Object.class)); + doReturn("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3").when(spy).getObjectById(any(Object.class)); final URI result = spy.resourceVersion("1234").build(); final URI expected = UriBuilder.fromUri("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3?resource-version=1234").build(); @@ -110,7 +110,7 @@ public class ServiceInstanceUriTest { ServiceInstanceUri instance = new ServiceInstanceUri("key1"); ServiceInstanceUri spy = spy(instance); - doReturn("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%20space").when(spy).getSerivceInstance(any(Object.class)); + doReturn("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%20space").when(spy).getObjectById(any(Object.class)); final URI result = spy.build(); final URI expected = UriBuilder.fromUri("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%20space").build(); @@ -123,7 +123,7 @@ public class ServiceInstanceUriTest { ServiceInstanceUri instance = new ServiceInstanceUri("key1"); ServiceInstanceUri spy = spy(instance); - doReturn("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%28space").when(spy).getSerivceInstance(any(Object.class)); + doReturn("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%28space").when(spy).getObjectById(any(Object.class)); assertThat(spy.getURIKeys().values(), contains("key1", "key2", "key3(space")); @@ -133,7 +133,7 @@ public class ServiceInstanceUriTest { ServiceInstanceUri instance = new ServiceInstanceUri("key1"); ServiceInstanceUri spy = spy(instance); String uri = "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3"; - doReturn(uri).when(spy).getSerivceInstance(any(Object.class)); + doReturn(uri).when(spy).getObjectById(any(Object.class)); doReturn(Optional.of(uri)).when(spy).getCachedValue(); final URI result = spy.resourceVersion("1234").clone().build(); final URI expected = UriBuilder.fromUri("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3?resource-version=1234").build(); -- cgit 1.2.3-korg