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 org.onap.aai.domain.yang.Relationship;
32 import org.openecomp.mso.client.aai.entities.AAIResultWrapper;
33 import org.openecomp.mso.client.aai.entities.uri.AAIResourceUri;
34 import org.openecomp.mso.client.aai.entities.uri.AAIUri;
35 import org.openecomp.mso.client.aai.entities.uri.Depth;
36 import org.openecomp.mso.client.policy.RestClient;
38 public class AAIResourcesClient extends AAIClient {
40 private final AAIVersion version;
42 public AAIResourcesClient() {
43 super(UUID.randomUUID());
44 this.version = super.getVersion();
47 public AAIResourcesClient(AAIVersion version) {
48 super(UUID.randomUUID());
49 this.version = version;
52 public AAIResourcesClient(AAIVersion version, UUID requestId) {
54 this.version = version;
58 * creates a new object in A&AI
60 * @param obj - can be any object which will marshal into a valid A&AI payload
64 public void create(AAIResourceUri uri, Object obj) {
65 RestClient aaiRC = this.createClient(uri);
71 * creates a new object in A&AI with no payload body
76 public void createEmpty(AAIResourceUri uri) {
77 RestClient aaiRC = this.createClient(uri);
83 * returns false if the object does not exist in A&AI
88 public boolean exists(AAIResourceUri uri) {
89 AAIUri forceMinimal = this.addParams(Optional.of(Depth.ZERO), true, uri);
90 RestClient aaiRC = this.createClient(forceMinimal);
93 } catch(ResponseProcessingException e) {
94 if (e.getCause() instanceof NotFoundException) {
104 * Adds a relationship between two objects in A&AI
109 public void connect(AAIResourceUri uriA, AAIResourceUri uriB) {
110 AAIResourceUri uriAClone = uriA.clone();
111 RestClient aaiRC = this.createClient(uriAClone.relationshipAPI());
112 aaiRC.put(this.buildRelationship(uriB));
117 * Removes relationship from two objects in A&AI
123 public void disconnect(AAIResourceUri uriA, AAIResourceUri uriB) {
124 AAIResourceUri uriAClone = uriA.clone();
125 RestClient aaiRC = this.createClient(uriAClone.relationshipAPI());
126 aaiRC.delete(this.buildRelationship(uriB));
131 * Deletes object from A&AI. Automatically handles resource-version.
136 public void delete(AAIResourceUri uri) {
137 AAIResourceUri clone = uri.clone();
138 RestClient aaiRC = this.createClient(clone);
139 Map<String, Object> result = aaiRC.get(new GenericType<Map<String, Object>>(){});
140 String resourceVersion = (String) result.get("resource-version");
141 aaiRC = this.createClient(clone.resourceVersion(resourceVersion));
147 * @param obj - can be any object which will marshal into a valid A&AI payload
151 public void update(AAIResourceUri uri, Object obj) {
152 RestClient aaiRC = this.createClient(uri);
158 * Retrieves an object from A&AI and unmarshalls it into the Class specified
163 public <T> T get(Class<T> clazz, AAIResourceUri uri) {
164 return this.createClient(uri).get(clazz);
168 * Retrieves an object from A&AI and automatically unmarshalls it into a Map or List
173 public <T> T get(GenericType<T> resultClass, AAIResourceUri uri) {
174 return this.createClient(uri).get(resultClass);
178 * Retrieves an object from A&AI wrapped in a helper class which offer additional features
183 public AAIResultWrapper get(AAIResourceUri uri) {
184 String json = this.createClient(uri).get(String.class);
186 return new AAIResultWrapper(json);
189 private Relationship buildRelationship(AAIResourceUri uri) {
190 final Relationship result = new Relationship();
191 result.setRelatedLink(uri.build().toString());
196 * Will automatically create the object if it does not exist
198 * @param obj - Optional object which serializes to a valid A&AI payload
202 public AAIResourcesClient createIfNotExists(AAIResourceUri uri, Optional<Object> obj) {
203 if(!this.exists(uri)){
204 if (obj.isPresent()) {
205 this.create(uri, obj.get());
207 this.createEmpty(uri);
215 * Starts a transaction which encloses multiple A&AI mutations
219 public AAITransactionalClient beginTransaction() {
220 return new AAITransactionalClient(this.version, this.requestId);
224 protected AAIVersion getVersion() {
229 protected RestClient createClient(AAIUri uri) {
230 return super.createClient(uri);
233 private AAIUri addParams(Optional<Depth> depth, boolean nodesOnly, AAIUri uri) {
234 AAIUri clone = uri.clone();
235 if (depth.isPresent()) {
236 clone.depth(depth.get());
239 clone.nodesOnly(nodesOnly);