diff options
Diffstat (limited to 'common/src/main/java/org/openecomp/mso/client/aai/entities/uri')
9 files changed, 669 insertions, 0 deletions
diff --git a/common/src/main/java/org/openecomp/mso/client/aai/entities/uri/AAIResourceUri.java b/common/src/main/java/org/openecomp/mso/client/aai/entities/uri/AAIResourceUri.java new file mode 100644 index 0000000000..36e3d008ae --- /dev/null +++ b/common/src/main/java/org/openecomp/mso/client/aai/entities/uri/AAIResourceUri.java @@ -0,0 +1,36 @@ +/*- + * ============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 org.openecomp.mso.client.aai.AAIObjectPlurals; +import org.openecomp.mso.client.aai.AAIObjectType; + +public interface AAIResourceUri extends AAIUri { + + public AAIResourceUri relationshipAPI(); + public AAIResourceUri relatedTo(AAIObjectPlurals plural); + public AAIResourceUri relatedTo(AAIObjectType type, String... values); + public AAIResourceUri resourceVersion(String version); + public AAIResourceUri depth(Depth depth); + public AAIResourceUri nodesOnly(boolean nodesOnly); + public AAIResourceUri queryParam(String name, String... values); + public AAIResourceUri clone(); +} diff --git a/common/src/main/java/org/openecomp/mso/client/aai/entities/uri/AAIUri.java b/common/src/main/java/org/openecomp/mso/client/aai/entities/uri/AAIUri.java new file mode 100644 index 0000000000..1617bcddea --- /dev/null +++ b/common/src/main/java/org/openecomp/mso/client/aai/entities/uri/AAIUri.java @@ -0,0 +1,58 @@ +/*- + * ============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.net.URI; +import java.util.Map; + +import org.openecomp.mso.client.aai.AAIObjectPlurals; +import org.openecomp.mso.client.aai.AAIObjectType; + +public interface AAIUri { + + public URI build(); + /** + * By default A&AI enforces a depth of 1. Some objects can be told to retrieve objects + * nested beneath them by increasing this number. + * + * You can use 0 to restrict the returned information to only the object you requested + * You can use all to retrieve all nested objects (this should only be used if you really need a massive amount of information and are caching the retrieval) + * @param depth + * @return + */ + public AAIUri depth(Depth depth); + /** + * Makes client only return object fields, no relationships + * + * @return + */ + public AAIUri nodesOnly(boolean nodesOnly); + public AAIUri queryParam(String name, String... values); + public AAIUri clone(); + + /** + * returns all key values of the URI as a map. Key names can be found in {@link AAIObjectType} + * @return + */ + public Map<String, String> getURIKeys(); + public AAIObjectType getObjectType(); + public boolean equals(Object o); +}
\ No newline at end of file diff --git a/common/src/main/java/org/openecomp/mso/client/aai/entities/uri/AAIUriFactory.java b/common/src/main/java/org/openecomp/mso/client/aai/entities/uri/AAIUriFactory.java new file mode 100644 index 0000000000..cb33435c69 --- /dev/null +++ b/common/src/main/java/org/openecomp/mso/client/aai/entities/uri/AAIUriFactory.java @@ -0,0 +1,73 @@ +/*- + * ============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.net.URI; + +import org.openecomp.mso.client.aai.AAIObjectPlurals; +import org.openecomp.mso.client.aai.AAIObjectType; + + +public class AAIUriFactory { + + /** + * values are filled into the URI template specified in {@link AAIObjectType} in order + * + * @param type + * @param values + * @return + */ + public static AAIResourceUri createResourceUri(AAIObjectType type, Object... values) { + if (AAIObjectType.SERVICE_INSTANCE.equals(type)) { + return new ServiceInstanceUri(values); + } else { + return new SimpleUri(type, values); + } + } + + public static AAIResourceUri createNodesUri(AAIObjectType type, Object... values) { + return new NodesUri(type, values); + + } + + /** + * 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 AAIResourceUri createResourceFromExistingURI(AAIObjectType type, URI uri) { + return new SimpleUri(type, uri); + } + + /** + * Creates a uri for a plural type e.g. /cloud-infrastructure/pservers + * + * @param type + * @return + */ + public static AAIResourceUri createResourceUri(AAIObjectPlurals type) { + + return new SimpleUri(type); + + } +} diff --git a/common/src/main/java/org/openecomp/mso/client/aai/entities/uri/Depth.java b/common/src/main/java/org/openecomp/mso/client/aai/entities/uri/Depth.java new file mode 100644 index 0000000000..7522ea33d1 --- /dev/null +++ b/common/src/main/java/org/openecomp/mso/client/aai/entities/uri/Depth.java @@ -0,0 +1,45 @@ +/*- + * ============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; + +public enum Depth { + ZERO("0"), + ONE("1"), + TWO("2"), + THREE("3"), + FOUR("4"), + FIVE("5"), + SIX("6"), + ALL("all"); + + private final String depth; + private Depth(String s) { + + this.depth = s; + } + + + @Override + public String toString() { + return this.depth; + } + +} diff --git a/common/src/main/java/org/openecomp/mso/client/aai/entities/uri/NodesUri.java b/common/src/main/java/org/openecomp/mso/client/aai/entities/uri/NodesUri.java new file mode 100644 index 0000000000..407e0ecc9e --- /dev/null +++ b/common/src/main/java/org/openecomp/mso/client/aai/entities/uri/NodesUri.java @@ -0,0 +1,44 @@ +/*- + * ============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 javax.ws.rs.core.UriBuilder; + +import org.openecomp.mso.client.aai.AAIObjectPlurals; +import org.openecomp.mso.client.aai.AAIObjectType; + +public class NodesUri extends SimpleUri { + + protected NodesUri(AAIObjectType type, Object... values) { + super(type, values); + } + + + @Override + protected String getTemplate(AAIObjectType type) { + return "/nodes" + type.partialUri(); + } + + @Override + protected String getTemplate(AAIObjectPlurals type) { + return "/nodes" + type.partialUri(); + } +} 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 diff --git a/common/src/main/java/org/openecomp/mso/client/aai/entities/uri/SimpleUri.java b/common/src/main/java/org/openecomp/mso/client/aai/entities/uri/SimpleUri.java new file mode 100644 index 0000000000..f3e6fad48b --- /dev/null +++ b/common/src/main/java/org/openecomp/mso/client/aai/entities/uri/SimpleUri.java @@ -0,0 +1,187 @@ +/*- + * ============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.UnsupportedEncodingException; +import java.net.URI; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; + +import javax.ws.rs.core.UriBuilder; + +import org.openecomp.mso.client.aai.AAIObjectPlurals; +import org.openecomp.mso.client.aai.AAIObjectType; +import org.openecomp.mso.client.aai.entities.uri.parsers.UriParser; +import org.openecomp.mso.client.aai.entities.uri.parsers.UriParserSpringImpl; +import org.springframework.web.util.UriUtils; + +public class SimpleUri implements AAIResourceUri { + + protected UriBuilder internalURI; + protected final static String relationshipAPI = "/relationship-list/relationship"; + protected final static String relatedTo = "/related-to"; + protected final Object[] values; + protected final AAIObjectType type; + protected final AAIObjectPlurals pluralType; + protected SimpleUri(AAIObjectType type, Object... values) { + this.type = type; + this.pluralType = null; + this.internalURI = UriBuilder.fromPath(this.getTemplate(type)); + this.values = values; + } + protected SimpleUri(AAIObjectType type, URI uri) { + this.type = type; + this.pluralType = null; + this.internalURI = UriBuilder.fromPath(uri.getRawPath().replaceAll("/aai/v\\d+", "")); + this.values = new Object[0]; + } + protected SimpleUri(AAIObjectType type, UriBuilder builder, Object... values) { + this.internalURI = builder; + this.values = values; + this.type = type; + this.pluralType = null; + } + protected SimpleUri(AAIObjectPlurals type, UriBuilder builder, Object... values) { + this.internalURI = builder; + this.values = values; + this.type = null; + this.pluralType = type; + } + protected SimpleUri(AAIObjectPlurals type) { + this.type = null; + this.pluralType = type; + this.internalURI = UriBuilder.fromPath(this.getTemplate(type)); + this.values = new Object[0]; + } + + @Override + public SimpleUri relationshipAPI() { + this.internalURI = internalURI.path(relationshipAPI); + return this; + } + + @Override + public SimpleUri relatedTo(AAIObjectPlurals plural) { + + this.internalURI = internalURI.path(relatedTo).path(plural.partialUri()); + return this; + } + @Override + public SimpleUri relatedTo(AAIObjectType type, String... values) { + this.internalURI = internalURI.path(relatedTo).path(UriBuilder.fromPath(type.partialUri()).build(values).toString()); + return this; + } + + @Override + public SimpleUri resourceVersion(String version) { + this.internalURI = internalURI.queryParam("resource-version", version); + return this; + } + + @Override + public SimpleUri queryParam(String name, String... values) { + this.internalURI = internalURI.queryParam(name, values); + return this; + } + + @Override + public URI build() { + return build(this.values); + } + + protected URI build(Object... values) { + //This is a workaround because resteasy does not encode URIs correctly + final String[] encoded = new String[values.length]; + for (int i = 0; i < values.length; i++) { + try { + encoded[i] = UriUtils.encode(values[i].toString(), StandardCharsets.UTF_8.toString()); + } catch (UnsupportedEncodingException e) { + encoded[i] = values[i].toString(); + } + } + return internalURI.buildFromEncoded(encoded); + } + + @Override + public Map<String, String> getURIKeys() { + return this.getURIKeys(this.build().toString()); + } + + protected Map<String, String> getURIKeys(String uri) { + UriParser parser; + if (this.type != null) { + if (!("".equals(this.getTemplate(type)))) { + parser = new UriParserSpringImpl(this.getTemplate(type)); + } else { + return new HashMap<>(); + } + } else { + parser = new UriParserSpringImpl(this.getTemplate(pluralType)); + } + + + return parser.parse(uri); + } + + @Override + public SimpleUri clone() { + if (this.type != null) { + return new SimpleUri(this.type, this.internalURI.clone(), values); + } else { + return new SimpleUri(this.pluralType, this.internalURI.clone(), values); + } + } + + @Override + public AAIObjectType getObjectType() { + return this.type; + } + + @Override + public boolean equals(Object o) { + if (o instanceof AAIUri) { + return this.build().equals(((AAIUri)o).build()); + } + return false; + } + @Override + public SimpleUri depth(Depth depth) { + this.internalURI.queryParam("depth", depth.toString()); + return this; + } + @Override + public SimpleUri nodesOnly(boolean nodesOnly) { + if (nodesOnly) { + this.internalURI.queryParam("nodes-only", ""); + } + return this; + } + + protected String getTemplate(AAIObjectType type) { + return type.uriTemplate(); + } + + protected String getTemplate(AAIObjectPlurals type) { + return type.uriTemplate(); + } + +} diff --git a/common/src/main/java/org/openecomp/mso/client/aai/entities/uri/parsers/UriParser.java b/common/src/main/java/org/openecomp/mso/client/aai/entities/uri/parsers/UriParser.java new file mode 100644 index 0000000000..8e37d595b0 --- /dev/null +++ b/common/src/main/java/org/openecomp/mso/client/aai/entities/uri/parsers/UriParser.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.openecomp.mso.client.aai.entities.uri.parsers; + +import java.util.Map; +import java.util.Set; + +public interface UriParser { + public Set<String> getVariables(); + public Map<String, String> parse(final String uri); +}
\ No newline at end of file diff --git a/common/src/main/java/org/openecomp/mso/client/aai/entities/uri/parsers/UriParserSpringImpl.java b/common/src/main/java/org/openecomp/mso/client/aai/entities/uri/parsers/UriParserSpringImpl.java new file mode 100644 index 0000000000..75f9005125 --- /dev/null +++ b/common/src/main/java/org/openecomp/mso/client/aai/entities/uri/parsers/UriParserSpringImpl.java @@ -0,0 +1,68 @@ +/*- + * ============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.parsers; +import java.io.UnsupportedEncodingException; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import org.springframework.web.util.UriTemplate; +import org.springframework.web.util.UriUtils; + +public class UriParserSpringImpl implements UriParser { + + private final UriTemplate uriTemplate; + + public UriParserSpringImpl(final String template) { + this.uriTemplate = new UriTemplate(template); + } + + @Override + public Map<String, String> parse(final String uri) { + final boolean match = this.uriTemplate.matches(uri); + if (!match) { + return new LinkedHashMap<>(); + } + return Collections.unmodifiableMap(decodeParams(this.uriTemplate.match(uri))); + } + + @Override + public Set<String> getVariables() { + return Collections.unmodifiableSet(new LinkedHashSet<String>(this.uriTemplate.getVariableNames())); + } + + protected Map<String, String> decodeParams(Map<String, String> map) { + final Map<String, String> result = new LinkedHashMap<>(); + + for (Entry<String, String> entry : map.entrySet()) { + try { + result.put(entry.getKey(), UriUtils.decode(entry.getValue(), "UTF-8")); + } catch (UnsupportedEncodingException e) { + result.put(entry.getKey(), ""); + } + } + + return result; + } +}
\ No newline at end of file |