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;
24 import java.util.Optional;
25 import java.util.UUID;
27 import javax.ws.rs.NotFoundException;
28 import javax.ws.rs.client.ResponseProcessingException;
29 import javax.ws.rs.core.GenericType;
31 import javax.ws.rs.core.Response;
32 import org.onap.aai.domain.yang.Relationship;
33 import org.openecomp.mso.client.aai.entities.AAIResultWrapper;
34 import org.openecomp.mso.client.aai.entities.uri.AAIResourceUri;
35 import org.openecomp.mso.client.aai.entities.uri.AAIUri;
36 import org.openecomp.mso.client.aai.entities.uri.Depth;
37 import org.openecomp.mso.client.policy.RestClient;
39 public class AAIResourcesClient extends AAIClient {
41 private final AAIVersion version;
43 public AAIResourcesClient() {
44 super(UUID.randomUUID());
45 this.version = super.getVersion();
48 public AAIResourcesClient(AAIVersion version) {
49 super(UUID.randomUUID());
50 this.version = version;
53 public AAIResourcesClient(AAIVersion version, UUID requestId) {
55 this.version = version;
59 * creates a new object in A&AI
61 * @param obj - can be any object which will marshal into a valid A&AI payload
65 public void create(AAIResourceUri uri, Object obj) {
66 RestClient aaiRC = this.createClient(uri);
72 * creates a new object in A&AI with no payload body
77 public void createEmpty(AAIResourceUri uri) {
78 RestClient aaiRC = this.createClient(uri);
84 * returns false if the object does not exist in A&AI
89 public boolean exists(AAIResourceUri uri) {
90 AAIUri forceMinimal = this.addParams(Optional.of(Depth.ZERO), true, uri);
91 RestClient aaiRC = this.createClient(forceMinimal);
94 } catch(ResponseProcessingException e) {
95 if (e.getCause() instanceof NotFoundException) {
105 * Adds a relationship between two objects in A&AI
110 public void connect(AAIResourceUri uriA, AAIResourceUri uriB) {
111 AAIResourceUri uriAClone = uriA.clone();
112 RestClient aaiRC = this.createClient(uriAClone.relationshipAPI());
113 aaiRC.put(this.buildRelationship(uriB));
118 * Removes relationship from two objects in A&AI
124 public void disconnect(AAIResourceUri uriA, AAIResourceUri uriB) {
125 AAIResourceUri uriAClone = uriA.clone();
126 RestClient aaiRC = this.createClient(uriAClone.relationshipAPI());
127 aaiRC.delete(this.buildRelationship(uriB));
132 * Deletes object from A&AI. Automatically handles resource-version.
137 public void delete(AAIResourceUri uri) {
138 AAIResourceUri clone = uri.clone();
139 RestClient aaiRC = this.createClient(clone);
140 Map<String, Object> result = aaiRC.get(new GenericType<Map<String, Object>>(){});
141 String resourceVersion = (String) result.get("resource-version");
142 aaiRC = this.createClient(clone.resourceVersion(resourceVersion));
148 * @param obj - can be any object which will marshal into a valid A&AI payload
152 public void update(AAIResourceUri uri, Object obj) {
153 RestClient aaiRC = this.createClient(uri);
159 * Retrieves an object from A&AI and unmarshalls it into the Class specified
164 public <T> T get(Class<T> clazz, AAIResourceUri uri) {
165 return this.createClient(uri).get(clazz);
169 * Retrieves an object from A&AI and returns complete response
173 public Response getFullResponse(AAIResourceUri uri) {
174 return this.createClient(uri).get();
178 * Retrieves an object from A&AI and automatically unmarshalls it into a Map or List
183 public <T> T get(GenericType<T> resultClass, AAIResourceUri uri) {
184 return this.createClient(uri).get(resultClass);
188 * Retrieves an object from A&AI wrapped in a helper class which offer additional features
193 public AAIResultWrapper get(AAIResourceUri uri) {
194 String json = this.createClient(uri).get(String.class);
196 return new AAIResultWrapper(json);
199 private Relationship buildRelationship(AAIResourceUri uri) {
200 final Relationship result = new Relationship();
201 result.setRelatedLink(uri.build().toString());
206 * Will automatically create the object if it does not exist
208 * @param obj - Optional object which serializes to a valid A&AI payload
212 public AAIResourcesClient createIfNotExists(AAIResourceUri uri, Optional<Object> obj) {
213 if(!this.exists(uri)){
214 if (obj.isPresent()) {
215 this.create(uri, obj.get());
217 this.createEmpty(uri);
225 * Starts a transaction which encloses multiple A&AI mutations
229 public AAITransactionalClient beginTransaction() {
230 return new AAITransactionalClient(this.version, this.requestId);
234 protected AAIVersion getVersion() {
239 protected RestClient createClient(AAIUri uri) {
240 return super.createClient(uri);
243 private AAIUri addParams(Optional<Depth> depth, boolean nodesOnly, AAIUri uri) {
244 AAIUri clone = uri.clone();
245 if (depth.isPresent()) {
246 clone.depth(depth.get());
249 clone.nodesOnly(nodesOnly);