diff options
Diffstat (limited to 'graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri')
17 files changed, 779 insertions, 0 deletions
diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIBaseResourceUri.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIBaseResourceUri.java new file mode 100644 index 0000000000..e933ffd339 --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIBaseResourceUri.java @@ -0,0 +1,29 @@ +/*- + * ============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.aaiclient.client.aai.entities.uri; + +import org.onap.aaiclient.client.aai.AAIObjectBase; +import org.onap.aaiclient.client.graphinventory.entities.uri.GraphInventoryResourceUri; + +public interface AAIBaseResourceUri<T extends AAIBaseResourceUri<?, ?>, OT extends AAIObjectBase> + extends AAIUri<T, OT>, GraphInventoryResourceUri<T, OT> { + +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIFluentPluralType.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIFluentPluralType.java new file mode 100644 index 0000000000..ea81e7b95c --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIFluentPluralType.java @@ -0,0 +1,12 @@ +package org.onap.aaiclient.client.aai.entities.uri; + +import org.onap.aaiclient.client.aai.AAIObjectPlurals; +import org.onap.aaiclient.client.graphinventory.GraphInventoryFluentType; + +public interface AAIFluentPluralType extends GraphInventoryFluentType<AAIObjectPlurals> { + + public interface Info extends GraphInventoryFluentType.Info { + + + } +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIFluentSingleType.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIFluentSingleType.java new file mode 100644 index 0000000000..9310933499 --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIFluentSingleType.java @@ -0,0 +1,12 @@ +package org.onap.aaiclient.client.aai.entities.uri; + +import org.onap.aaiclient.client.aai.AAIObjectType; +import org.onap.aaiclient.client.graphinventory.GraphInventoryFluentType; + +public interface AAIFluentSingleType extends GraphInventoryFluentType<AAIObjectType> { + + public interface Info extends GraphInventoryFluentType.Info { + + + } +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIFluentTopLevelType.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIFluentTopLevelType.java new file mode 100644 index 0000000000..46a521d82e --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIFluentTopLevelType.java @@ -0,0 +1,11 @@ +package org.onap.aaiclient.client.aai.entities.uri; + +import org.onap.aaiclient.client.graphinventory.GraphInventoryFluentTopLevelType; + +public interface AAIFluentTopLevelType extends GraphInventoryFluentTopLevelType { + + public interface Info extends GraphInventoryFluentTopLevelType.Info { + + + } +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIFluentTypeReverseLookup.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIFluentTypeReverseLookup.java new file mode 100644 index 0000000000..bd9f4c713f --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIFluentTypeReverseLookup.java @@ -0,0 +1,51 @@ +package org.onap.aaiclient.client.aai.entities.uri; + +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; +import org.onap.aaiclient.client.aai.AAIObjectType; +import org.onap.aaiclient.client.graphinventory.GraphInventoryFluentType; +import org.onap.aaiclient.client.graphinventory.entities.uri.parsers.UriParserSpringImpl; +import com.google.common.base.CaseFormat; + +public class AAIFluentTypeReverseLookup { + + public AAIObjectType fromName(String name, String uri) { + + String className = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, name); + + uri = uri.replaceFirst(".*?/v\\d+", ""); + try { + Class<? extends GraphInventoryFluentType.Info> clazz = + (Class<? extends GraphInventoryFluentType.Info>) Class + .forName("org.onap.aaiclient.client.generated.fluentbuilders." + className + "$Info"); + + GraphInventoryFluentType.Info type = clazz.newInstance(); + + Optional<String> parentTemplate = findParentPath(type, uri); + if (parentTemplate.isPresent()) { + return new AAIObjectType(parentTemplate.get(), type.getPartialUri(), type.getName(), false); + } else { + // fallback to enum lookup + return AAIObjectType.fromTypeName(name); + } + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { + } + return AAIObjectType.UNKNOWN; + } + + protected Optional<String> findParentPath(GraphInventoryFluentType.Info type, String uri) { + + List<UriParserSpringImpl> parsers = + type.getPaths().stream().map(path -> new UriParserSpringImpl(path)).collect(Collectors.toList()); + + for (UriParserSpringImpl parser : parsers) { + if (parser.isMatch(uri)) { + String partialUriReplacer = type.getPartialUri().replaceAll("\\{[^}]+\\}", "[^/]+"); + return Optional.of(parser.getTemplate().replaceFirst(partialUriReplacer + "$", "")); + } + } + + return Optional.empty(); + } +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIPluralResourceUri.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIPluralResourceUri.java new file mode 100644 index 0000000000..0d41e1df02 --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIPluralResourceUri.java @@ -0,0 +1,9 @@ +package org.onap.aaiclient.client.aai.entities.uri; + +import org.onap.aaiclient.client.aai.AAIObjectPlurals; +import org.onap.aaiclient.client.graphinventory.entities.uri.GraphInventoryPluralResourceUri; + +public interface AAIPluralResourceUri extends AAIBaseResourceUri<AAIPluralResourceUri, AAIObjectPlurals>, + GraphInventoryPluralResourceUri<AAIPluralResourceUri, AAIObjectPlurals> { + +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIResourceUri.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIResourceUri.java new file mode 100644 index 0000000000..3d01258f71 --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIResourceUri.java @@ -0,0 +1,10 @@ +package org.onap.aaiclient.client.aai.entities.uri; + +import org.onap.aaiclient.client.aai.AAIObjectPlurals; +import org.onap.aaiclient.client.aai.AAIObjectType; +import org.onap.aaiclient.client.graphinventory.entities.uri.GraphInventorySingleResourceUri; + +public interface AAIResourceUri extends AAIBaseResourceUri<AAIResourceUri, AAIObjectType>, + GraphInventorySingleResourceUri<AAIResourceUri, AAIPluralResourceUri, AAIObjectType, AAIObjectPlurals> { + +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAISimplePluralUri.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAISimplePluralUri.java new file mode 100644 index 0000000000..67ce0962dc --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAISimplePluralUri.java @@ -0,0 +1,68 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 - 2018 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.aaiclient.client.aai.entities.uri; + +import javax.ws.rs.core.UriBuilder; +import org.onap.aaiclient.client.aai.AAIObjectPlurals; +import org.onap.aaiclient.client.aai.AAIObjectType; +import org.onap.aaiclient.client.graphinventory.entities.uri.SimplePluralUri; + +public class AAISimplePluralUri + extends SimplePluralUri<AAIPluralResourceUri, AAIResourceUri, AAIObjectPlurals, AAIObjectType> + implements AAIPluralResourceUri { + + private static final long serialVersionUID = -6397024057188453229L; + + protected AAISimplePluralUri(AAIObjectPlurals type, UriBuilder builder, Object... values) { + super(type, builder, values); + } + + protected AAISimplePluralUri(AAIObjectPlurals type) { + super(type); + } + + protected AAISimplePluralUri(AAIObjectPlurals type, Object... values) { + super(type, values); + } + + protected AAISimplePluralUri(AAIObjectPlurals type, AAIResourceUri uri) { + super(type, uri); + } + + protected AAISimplePluralUri(AAIResourceUri parentUri, AAIObjectPlurals childType) { + super(parentUri, childType); + } + + protected AAISimplePluralUri(AAISimplePluralUri copy) { + super(copy); + } + + @Override + public AAISimplePluralUri clone() { + return new AAISimplePluralUri(this); + } + + @Override + public AAIObjectPlurals getObjectType() { + return this.pluralType; + } + +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAISimpleUri.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAISimpleUri.java new file mode 100644 index 0000000000..40184b028b --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAISimpleUri.java @@ -0,0 +1,73 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 - 2018 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.aaiclient.client.aai.entities.uri; + +import java.net.URI; +import javax.ws.rs.core.UriBuilder; +import org.onap.aaiclient.client.aai.AAIObjectPlurals; +import org.onap.aaiclient.client.aai.AAIObjectType; +import org.onap.aaiclient.client.graphinventory.entities.uri.SimpleUri; + +public class AAISimpleUri extends SimpleUri<AAIResourceUri, AAIPluralResourceUri, AAIObjectType, AAIObjectPlurals> + implements AAIResourceUri { + + private static final long serialVersionUID = -6397024057188453229L; + + protected AAISimpleUri(AAIObjectType type, Object... values) { + super(type, values); + + } + + protected AAISimpleUri(AAIObjectType type, URI uri) { + super(type, uri); + } + + protected AAISimpleUri(AAIObjectType type, UriBuilder builder, Object... values) { + super(type, builder, values); + } + + protected AAISimpleUri(AAIResourceUri parentUri, AAIObjectType childType, Object... childValues) { + super(parentUri, childType, childValues); + } + + // copy constructor + protected AAISimpleUri(AAISimpleUri copy) { + super(copy); + } + + @Override + public AAISimpleUri clone() { + return new AAISimpleUri(this); + } + + @Override + public AAISimpleUri relatedTo(AAIObjectType type, String... values) { + this.internalURI = internalURI.path(relatedTo); + return new AAISimpleUri(this, type, values); + } + + @Override + public AAISimplePluralUri relatedTo(AAIObjectPlurals plural) { + this.internalURI.path(relatedTo); + return new AAISimplePluralUri(this, plural); + } + +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIUri.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIUri.java new file mode 100644 index 0000000000..662a1ab37b --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIUri.java @@ -0,0 +1,32 @@ +/*- + * ============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.aaiclient.client.aai.entities.uri; + +import java.util.regex.Pattern; +import org.onap.aaiclient.client.aai.AAIObjectBase; +import org.onap.aaiclient.client.graphinventory.entities.uri.GraphInventoryUri; + +public interface AAIUri<T extends AAIUri<?, ?>, OP extends AAIObjectBase> extends GraphInventoryUri<T, OP> { + + public default Pattern getPrefixPattern() { + return Pattern.compile("/aai/v\\d+"); + } +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIUriFactory.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIUriFactory.java new file mode 100644 index 0000000000..6bb2cbd471 --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIUriFactory.java @@ -0,0 +1,124 @@ +/*- + * ============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.aaiclient.client.aai.entities.uri; + +import java.net.URI; +import org.onap.aaiclient.client.aai.AAIObjectPlurals; +import org.onap.aaiclient.client.aai.AAIObjectType; + + +public class AAIUriFactory { + + /** + * values are filled into the URI template specified in {@link AAIObjectType} in order <br> + * There are two special lookups performed on certain types when a single value is specified: <br> + * Service Instance and AllottedResources <br> + * These can be retrieved without all their required keys but an HTTP call is required to do so + * + * @param type + * @param values + * @return + */ + 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); + } + } + + public static AAIResourceUri createResourceUri(AAIFluentSingleType uri) { + return new AAISimpleUri(uri.build(), uri.values()); + } + + public static NodesSingleUri createNodesUri(AAIObjectType type, Object... values) { + return new NodesSingleUri(type, values); + + } + + public static NodesPluralUri createNodesUri(AAIObjectPlurals type) { + return new NodesPluralUri(type); + + } + + /** + * This method should only be used to wrap a URI retrieved from A&AI contained within an object response + * + * @param type + * @param uri + * @return + */ + public static AAISimpleUri createResourceFromExistingURI(AAIObjectType type, URI uri) { + return new AAISimpleUri(type, uri); + } + + + /** + * creates an AAIResourceUri from a parentUri + * + * @param parentUri + * @param childType + * @param childValues + * @return + */ + public static AAISimpleUri createResourceFromParentURI(AAIResourceUri parentUri, AAIObjectType childType, + Object... childValues) { + + return new AAISimpleUri(parentUri, childType, childValues); + } + + public static AAISimplePluralUri createResourceFromParentURI(AAIResourceUri parentUri, AAIObjectPlurals childType) { + + return new AAISimplePluralUri(parentUri, childType); + } + + public static AAISimplePluralUri createResourceUri(AAIFluentPluralType uri) { + + return new AAISimplePluralUri(uri.build()); + + } + + /** + * Creates a uri for a plural type e.g. /cloud-infrastructure/pservers + * + * @param type + * @return + */ + public static AAISimplePluralUri createResourceUri(AAIObjectPlurals type) { + + return new AAISimplePluralUri(type); + + } + + /** + * Creates a uri for a plural type with values e.g. /cloud-infrastructure/pservers + * + * @param type + * @return + */ + public static AAISimplePluralUri createResourceUri(AAIObjectPlurals type, Object... values) { + + return new AAISimplePluralUri(type, values); + + } +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AllottedResourceLookupUri.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AllottedResourceLookupUri.java new file mode 100644 index 0000000000..7f51835580 --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AllottedResourceLookupUri.java @@ -0,0 +1,54 @@ +/*- + * ============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.aaiclient.client.aai.entities.uri; + +import java.net.URI; +import java.util.Optional; +import javax.ws.rs.core.UriBuilder; +import org.onap.aaiclient.client.aai.AAIObjectType; +import org.onap.aaiclient.client.aai.AAIResourcesClient; + +public class AllottedResourceLookupUri extends HttpLookupUri { + + private static final long serialVersionUID = -9212594383876793188L; + + protected AllottedResourceLookupUri(Object... values) { + super(AAIObjectType.ALLOTTED_RESOURCE, values); + } + + protected AllottedResourceLookupUri(UriBuilder builder, Optional<String> cachedValue, Object... values) { + super(AAIObjectType.ALLOTTED_RESOURCE, builder, cachedValue, values); + } + + @Override + public AllottedResourceLookupUri clone() { + return new AllottedResourceLookupUri(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/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/HttpLookupUri.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/HttpLookupUri.java new file mode 100644 index 0000000000..c39eb50dc2 --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/HttpLookupUri.java @@ -0,0 +1,174 @@ +/*- + * ============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.aaiclient.client.aai.entities.uri; + +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.net.URI; +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.aaiclient.client.aai.AAIObjectType; +import org.onap.aaiclient.client.aai.AAIResourcesClient; +import org.onap.aaiclient.client.aai.entities.Results; +import org.onap.aaiclient.client.graphinventory.Format; +import org.onap.aaiclient.client.graphinventory.entities.Pathed; +import org.onap.aaiclient.client.graphinventory.entities.uri.HttpAwareUri; +import org.onap.aaiclient.client.graphinventory.exceptions.GraphInventoryPayloadException; +import org.onap.aaiclient.client.graphinventory.exceptions.GraphInventoryUriComputationException; +import org.onap.aaiclient.client.graphinventory.exceptions.GraphInventoryUriNotFoundException; +import org.onap.aaiclient.client.graphinventory.exceptions.IncorrectNumberOfUriKeys; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; + +public abstract class HttpLookupUri extends AAISimpleUri implements HttpAwareUri { + + private transient Optional<String> 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<String> 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); + } + } + return cachedValue.get(); + } + + protected Optional<String> extractRelatedLink(String jsonString) throws IOException { + Optional<String> result; + ObjectMapper mapper = new ObjectMapper(); + + Results<Pathed> results = mapper.readValue(jsonString, new TypeReference<Results<Pathed>>() {}); + if (results.getResult().size() == 1) { + String uriString = results.getResult().get(0).getResourceLink(); + 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 = getObjectById(this.values[0]); + Map<String, String> map = super.getURIKeys(uri); + this.values = map.values().toArray(values); + return super.build(values); + } + } catch (GraphInventoryUriNotFoundException | GraphInventoryPayloadException e) { + throw new GraphInventoryUriComputationException(e); + } + return super.build(); + } + + @Override + public URI locateAndBuild() { + try { + if (this.values.length == 1) { + String uri = getObjectById(this.values[0]); + Map<String, String> map = super.getURIKeys(uri); + this.values = map.values().toArray(values); + return super.build(values); + } + } catch (GraphInventoryUriNotFoundException | GraphInventoryPayloadException e) { + throw new GraphInventoryUriComputationException(e); + } + return super.build(); + } + + @Override + public abstract HttpLookupUri clone(); + + @Override + public void validateValuesSize(String template, Object... values) { + try { + super.validateValuesSize(template, values); + } catch (IncorrectNumberOfUriKeys e) { + if (values.length == 1) { + // Special case where we perform an http look up + } else { + throw e; + } + } + } + + public AAIResourcesClient getResourcesClient() { + return new AAIResourcesClient(); + } + + private void writeObject(ObjectOutputStream oos) throws IOException { + + oos.writeUTF(this.cachedValue.orElse("")); + } + + private void readObject(ObjectInputStream ois) throws IOException { + + String value = ois.readUTF(); + if ("".equals(value)) { + this.cachedValue = Optional.empty(); + } else { + this.cachedValue = Optional.ofNullable(value); + } + + } +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/NodesPluralUri.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/NodesPluralUri.java new file mode 100644 index 0000000000..80032fe9fd --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/NodesPluralUri.java @@ -0,0 +1,19 @@ +package org.onap.aaiclient.client.aai.entities.uri; + +import javax.ws.rs.core.UriBuilder; +import org.onap.aaiclient.client.aai.AAIObjectPlurals; +import org.onap.aaiclient.client.graphinventory.GraphInventoryObjectBase; + +public class NodesPluralUri extends AAISimplePluralUri implements NodesUri { + + private static final long serialVersionUID = -6743170679667245998L; + + protected NodesPluralUri(AAIObjectPlurals type) { + super(type); + } + + @Override + public String getTemplate(GraphInventoryObjectBase type) { + return UriBuilder.fromUri("/nodes").path(type.partialUri()).toTemplate(); + } +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/NodesSingleUri.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/NodesSingleUri.java new file mode 100644 index 0000000000..8ec4df5848 --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/NodesSingleUri.java @@ -0,0 +1,21 @@ +package org.onap.aaiclient.client.aai.entities.uri; + +import javax.ws.rs.core.UriBuilder; +import org.onap.aaiclient.client.aai.AAIObjectType; +import org.onap.aaiclient.client.graphinventory.GraphInventoryObjectBase; + +public class NodesSingleUri extends AAISimpleUri implements NodesUri { + + private static final long serialVersionUID = 2721165364903444248L; + + protected NodesSingleUri(AAIObjectType type, Object... values) { + super(type, values); + } + + + @Override + public String getTemplate(GraphInventoryObjectBase type) { + return UriBuilder.fromUri("/nodes").path(type.partialUri()).toTemplate(); + } + +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/NodesUri.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/NodesUri.java new file mode 100644 index 0000000000..03f1a62462 --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/NodesUri.java @@ -0,0 +1,26 @@ +/*- + * ============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.aaiclient.client.aai.entities.uri; + +public interface NodesUri { + + +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/ServiceInstanceUri.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/ServiceInstanceUri.java new file mode 100644 index 0000000000..96eaac5815 --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/ServiceInstanceUri.java @@ -0,0 +1,54 @@ +/*- + * ============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.aaiclient.client.aai.entities.uri; + +import java.net.URI; +import java.util.Optional; +import javax.ws.rs.core.UriBuilder; +import org.onap.aaiclient.client.aai.AAIObjectType; +import org.onap.aaiclient.client.aai.AAIResourcesClient; + +public class ServiceInstanceUri extends HttpLookupUri { + + private static final long serialVersionUID = 2248914170527514548L; + + protected ServiceInstanceUri(Object... values) { + super(AAIObjectType.SERVICE_INSTANCE, values); + } + + protected ServiceInstanceUri(UriBuilder builder, Optional<String> cachedValue, Object... values) { + super(AAIObjectType.SERVICE_INSTANCE, 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", (String) this.values[0]}); + } +} |