2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2018 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.onap.so.client.aai;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Optional;
27 import javax.ws.rs.core.GenericType;
28 import org.onap.so.client.RestClient;
29 import org.onap.so.client.aai.entities.AAIEdgeLabel;
30 import org.onap.so.client.aai.entities.AAIError;
31 import org.onap.so.client.aai.entities.singletransaction.OperationBodyRequest;
32 import org.onap.so.client.aai.entities.singletransaction.OperationBodyResponse;
33 import org.onap.so.client.aai.entities.singletransaction.SingleTransactionRequest;
34 import org.onap.so.client.aai.entities.singletransaction.SingleTransactionResponse;
35 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
36 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
37 import org.onap.so.client.graphinventory.GraphInventoryPatchConverter;
38 import org.onap.so.client.graphinventory.GraphInventoryTransactionClient;
39 import org.onap.so.client.graphinventory.exceptions.BulkProcessFailed;
40 import com.fasterxml.jackson.databind.ObjectMapper;
41 import com.google.common.base.Joiner;
43 public class AAISingleTransactionClient
44 extends GraphInventoryTransactionClient<AAISingleTransactionClient, AAIResourceUri, AAIEdgeLabel> {
46 private final SingleTransactionRequest request;
47 private AAIResourcesClient resourcesClient;
48 private AAIClient aaiClient;
50 protected AAISingleTransactionClient(AAIResourcesClient resourcesClient, AAIClient aaiClient) {
52 this.resourcesClient = resourcesClient;
53 this.aaiClient = aaiClient;
54 this.request = new SingleTransactionRequest();
60 * @see org.onap.so.client.aai.GraphInventoryTransactionClient#execute()
63 public void execute() throws BulkProcessFailed {
65 if (!this.request.getOperations().isEmpty()) {
67 aaiClient.createClient(AAIUriFactory.createResourceUri(AAIObjectType.SINGLE_TRANSACTION));
68 SingleTransactionResponse response = client.post(this.request, SingleTransactionResponse.class);
69 if (response != null) {
70 final Optional<String> errorMessage = this.locateErrorMessages(response);
71 if (errorMessage.isPresent()) {
72 throw new BulkProcessFailed(
73 "One or more transactions failed in A&AI. Check logs for payloads.\nMessages:\n"
74 + errorMessage.get());
77 throw new BulkProcessFailed(
78 "Transactions acccepted by A&AI, but there was no response. Unsure of result.");
82 this.request.getOperations().clear();
87 protected Optional<String> locateErrorMessages(SingleTransactionResponse response) {
88 final List<String> errorMessages = new ArrayList<>();
89 final ObjectMapper mapper = new ObjectMapper();
91 for (OperationBodyResponse body : response.getOperationResponses()) {
92 if (Optional.ofNullable(body.getResponseStatusCode()).orElse(400) > 300) {
95 error = mapper.readValue(mapper.writeValueAsString(body.getResponseBody()), AAIError.class);
96 } catch (IOException e) {
97 logger.error("could not parse error object from A&AI", e);
98 error = new AAIError();
100 AAIErrorFormatter formatter = new AAIErrorFormatter(error);
101 String outputMessage = formatter.getMessage();
102 errorMessages.add(outputMessage);
106 if (!errorMessages.isEmpty()) {
107 return Optional.of(Joiner.on("\n").join(errorMessages));
109 return Optional.empty();
114 protected SingleTransactionRequest getRequest() {
119 public void put(String uri, Object body) {
120 request.getOperations().add(new OperationBodyRequest().withAction("put").withUri(uri).withBody(body));
124 public void delete(String uri, Object body) {
125 request.getOperations().add(new OperationBodyRequest().withAction("delete").withUri(uri).withBody(body));
129 public void patch(String uri, Object body) {
130 request.getOperations().add(new OperationBodyRequest().withAction("patch").withUri(uri).withBody(body));
134 protected <T> Optional<T> get(GenericType<T> genericType, AAIResourceUri clone) {
135 return resourcesClient.get(genericType, clone);
139 protected boolean exists(AAIResourceUri uri) {
140 return resourcesClient.exists(uri);
144 protected String getGraphDBName() {
145 return aaiClient.getGraphDBName();
149 protected GraphInventoryPatchConverter getPatchConverter() {
150 return this.patchConverter;