fecbf59c36e27feb83171fffb02d3f46fd50244b
[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
28 import javax.ws.rs.core.GenericType;
29
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;
42
43 import com.fasterxml.jackson.databind.ObjectMapper;
44 import com.google.common.base.Joiner;
45
46 public class AAISingleTransactionClient extends GraphInventoryTransactionClient<AAISingleTransactionClient, AAIResourceUri, AAIEdgeLabel> {
47
48         private final SingleTransactionRequest request;
49         private AAIResourcesClient resourcesClient;
50         private AAIClient aaiClient;
51         protected AAISingleTransactionClient(AAIResourcesClient resourcesClient, AAIClient aaiClient) {
52                 super();
53                 this.resourcesClient = resourcesClient;
54                 this.aaiClient = aaiClient;
55                 this.request = new SingleTransactionRequest();
56         }
57         
58         /* (non-Javadoc)
59          * @see org.onap.so.client.aai.GraphInventoryTransactionClient#execute()
60          */
61         @Override
62         public void execute() throws BulkProcessFailed {
63                 try {
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());
71                                         }
72                                 } else {
73                                         throw new BulkProcessFailed("Transactions acccepted by A&AI, but there was no response. Unsure of result.");
74                                 }
75                         }
76                 } finally {
77                         this.request.getOperations().clear();
78                         this.actionCount = 0;
79                 }
80         }
81
82         protected Optional<String> locateErrorMessages(SingleTransactionResponse response) {
83                 final List<String> errorMessages = new ArrayList<>();
84                 final ObjectMapper mapper = new ObjectMapper();
85                 
86                 for (OperationBodyResponse body : response.getOperationResponses()) {
87                         if (Optional.ofNullable(body.getResponseStatusCode()).orElse(400) > 300) {
88                                 AAIError error;
89                                 try {
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();
94                                 }
95                                 AAIErrorFormatter formatter = new AAIErrorFormatter(error);
96                                 String outputMessage = formatter.getMessage();
97                                 errorMessages.add(outputMessage);
98                         }
99                 }
100                 
101                 if (!errorMessages.isEmpty()) {
102                         return Optional.of(Joiner.on("\n").join(errorMessages));
103                 } else {
104                         return Optional.empty();
105                 }
106         }
107
108         
109         protected SingleTransactionRequest getRequest() {
110                 return this.request;
111         }
112
113         @Override
114         public void put(String uri, Object body) {
115                 request.getOperations().add(new OperationBodyRequest().withAction("put").withUri(uri).withBody(body));
116         }
117
118         @Override
119         public void delete(String uri, Object body) {
120                 request.getOperations().add(new OperationBodyRequest().withAction("delete").withUri(uri).withBody(body));
121         }
122
123         @Override
124         public void patch(String uri, Object body) {
125                 request.getOperations().add(new OperationBodyRequest().withAction("patch").withUri(uri).withBody(body));
126         }
127
128         @Override
129         protected <T> Optional<T> get(GenericType<T> genericType, AAIResourceUri clone) {
130                 return resourcesClient.get(genericType, clone);
131         }
132         
133         @Override
134         protected boolean exists(AAIResourceUri uri) {
135                 return resourcesClient.exists(uri);
136         }
137         
138         @Override
139         protected String getGraphDBName() {
140                 return aaiClient.getGraphDBName();
141         }
142         
143         @Override
144         protected GraphInventoryPatchConverter getPatchConverter() {
145                 return this.patchConverter;
146         }
147 }