Replaced all tabs with spaces in java and pom.xml
[so.git] / common / src / main / java / org / onap / so / client / aai / AAISingleTransactionClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.client.aai;
22
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;
42
43 public class AAISingleTransactionClient
44         extends GraphInventoryTransactionClient<AAISingleTransactionClient, AAIResourceUri, AAIEdgeLabel> {
45
46     private final SingleTransactionRequest request;
47     private AAIResourcesClient resourcesClient;
48     private AAIClient aaiClient;
49
50     protected AAISingleTransactionClient(AAIResourcesClient resourcesClient, AAIClient aaiClient) {
51         super();
52         this.resourcesClient = resourcesClient;
53         this.aaiClient = aaiClient;
54         this.request = new SingleTransactionRequest();
55     }
56
57     /*
58      * (non-Javadoc)
59      * 
60      * @see org.onap.so.client.aai.GraphInventoryTransactionClient#execute()
61      */
62     @Override
63     public void execute() throws BulkProcessFailed {
64         try {
65             if (!this.request.getOperations().isEmpty()) {
66                 RestClient client =
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());
75                     }
76                 } else {
77                     throw new BulkProcessFailed(
78                             "Transactions acccepted by A&AI, but there was no response. Unsure of result.");
79                 }
80             }
81         } finally {
82             this.request.getOperations().clear();
83             this.actionCount = 0;
84         }
85     }
86
87     protected Optional<String> locateErrorMessages(SingleTransactionResponse response) {
88         final List<String> errorMessages = new ArrayList<>();
89         final ObjectMapper mapper = new ObjectMapper();
90
91         for (OperationBodyResponse body : response.getOperationResponses()) {
92             if (Optional.ofNullable(body.getResponseStatusCode()).orElse(400) > 300) {
93                 AAIError error;
94                 try {
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();
99                 }
100                 AAIErrorFormatter formatter = new AAIErrorFormatter(error);
101                 String outputMessage = formatter.getMessage();
102                 errorMessages.add(outputMessage);
103             }
104         }
105
106         if (!errorMessages.isEmpty()) {
107             return Optional.of(Joiner.on("\n").join(errorMessages));
108         } else {
109             return Optional.empty();
110         }
111     }
112
113
114     protected SingleTransactionRequest getRequest() {
115         return this.request;
116     }
117
118     @Override
119     public void put(String uri, Object body) {
120         request.getOperations().add(new OperationBodyRequest().withAction("put").withUri(uri).withBody(body));
121     }
122
123     @Override
124     public void delete(String uri, Object body) {
125         request.getOperations().add(new OperationBodyRequest().withAction("delete").withUri(uri).withBody(body));
126     }
127
128     @Override
129     public void patch(String uri, Object body) {
130         request.getOperations().add(new OperationBodyRequest().withAction("patch").withUri(uri).withBody(body));
131     }
132
133     @Override
134     protected <T> Optional<T> get(GenericType<T> genericType, AAIResourceUri clone) {
135         return resourcesClient.get(genericType, clone);
136     }
137
138     @Override
139     protected boolean exists(AAIResourceUri uri) {
140         return resourcesClient.exists(uri);
141     }
142
143     @Override
144     protected String getGraphDBName() {
145         return aaiClient.getGraphDBName();
146     }
147
148     @Override
149     protected GraphInventoryPatchConverter getPatchConverter() {
150         return this.patchConverter;
151     }
152 }