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