Refactor babel-related code to not update parameter values
[aai/model-loader.git] / src / main / java / org / onap / aai / modelloader / restclient / AaiRestClient.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 European Software Marketing Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.modelloader.restclient;
22
23 import com.sun.jersey.core.util.MultivaluedMapImpl; // NOSONAR
24 import java.io.IOException;
25 import java.io.StringReader;
26 import java.net.URI;
27 import java.util.Collections;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.stream.IntStream;
31 import javax.ws.rs.core.MediaType;
32 import javax.ws.rs.core.MultivaluedMap;
33 import javax.ws.rs.core.Response;
34 import javax.ws.rs.core.UriBuilder;
35 import javax.xml.parsers.DocumentBuilder;
36 import javax.xml.parsers.DocumentBuilderFactory;
37 import javax.xml.parsers.ParserConfigurationException;
38 import org.onap.aai.cl.api.Logger;
39 import org.onap.aai.cl.eelf.LoggerFactory;
40 import org.onap.aai.modelloader.config.ModelLoaderConfig;
41 import org.onap.aai.modelloader.service.ModelLoaderMsgs;
42 import org.onap.aai.restclient.client.OperationResult;
43 import org.onap.aai.restclient.client.RestClient;
44 import org.onap.aai.restclient.enums.RestAuthenticationMode;
45 import org.springframework.stereotype.Component;
46 import org.w3c.dom.Document;
47 import org.w3c.dom.Node;
48 import org.w3c.dom.NodeList;
49 import org.xml.sax.InputSource;
50 import org.xml.sax.SAXException;
51
52 /**
53  * Wrapper around the standard A&AI Rest Client interface. This currently uses Jersey client 1.x
54  *
55  */
56 @Component
57 public class AaiRestClient {
58
59     public static final String HEADER_TRANS_ID = "X-TransactionId";
60     public static final String HEADER_FROM_APP_ID = "X-FromAppId";
61     public static final String ML_APP_NAME = "ModelLoader";
62     private static final String RESOURCE_VERSION_PARAM = "resource-version";
63
64     private static Logger logger = LoggerFactory.getInstance().getLogger(AaiRestClient.class.getName());
65
66     private ModelLoaderConfig config = null;
67
68     public AaiRestClient(ModelLoaderConfig config) {
69         this.config = config;
70     }
71
72
73     /**
74      * Send a GET request to the A&AI for a resource.
75      *
76      * @param url
77      * @param transId
78      * @param mediaType
79      * @return
80      */
81     public OperationResult getResource(String url, String transId, MediaType mediaType) {
82         return setupClient().get(url, buildHeaders(transId), mediaType);
83     }
84
85     /**
86      * Send a PUT request to the A&AI.
87      *
88      * @param url - the url
89      * @param payload - the XML or JSON payload for the request
90      * @param transId - transaction ID
91      * @param mediaType - the content type (XML or JSON)
92      * @return operation result
93      */
94     public OperationResult putResource(String url, String payload, String transId, MediaType mediaType) {
95         logger.info(ModelLoaderMsgs.AAI_REST_REQUEST_PAYLOAD, payload);
96         return setupClient().put(url, payload, buildHeaders(transId), mediaType, mediaType);
97     }
98
99
100     /**
101      * Send a POST request to the A&AI.
102      *
103      * @param url - the url
104      * @param transId - transaction ID
105      * @param payload - the XML or JSON payload for the request
106      * @param mimeType - the content type (XML or JSON)
107      * @return ClientResponse
108      */
109     public OperationResult postResource(String url, String payload, String transId, MediaType mediaType) {
110         logger.info(ModelLoaderMsgs.AAI_REST_REQUEST_PAYLOAD, payload);
111         return setupClient().post(url, payload, buildHeaders(transId), mediaType, mediaType);
112     }
113
114
115     /**
116      * Send a DELETE request to the A&AI.
117      *
118      * @param url - the url
119      * @param resourceVersion - the resource-version of the model to delete
120      * @param transId - transaction ID
121      * @return ClientResponse
122      */
123     public OperationResult deleteResource(String url, String resourceVersion, String transId) {
124         URI uri = UriBuilder.fromUri(url).queryParam(RESOURCE_VERSION_PARAM, resourceVersion).build();
125         return setupClient().delete(uri.toString(), buildHeaders(transId), null);
126     }
127
128     /**
129      * Does a GET on a resource to retrieve the resource version, and then DELETE that version.
130      *
131      * @param url - the url
132      * @param transId - transaction ID
133      * @return ClientResponse
134      */
135     public OperationResult getAndDeleteResource(String url, String transId) {
136         // First, GET the model
137         OperationResult getResponse = getResource(url, transId, MediaType.APPLICATION_XML_TYPE);
138         if (getResponse == null || getResponse.getResultCode() != Response.Status.OK.getStatusCode()) {
139             return getResponse;
140         }
141
142         // Delete the model using the resource version in the response
143         String resVersion = null;
144         try {
145             resVersion = getResourceVersion(getResponse);
146         } catch (Exception e) {
147             logger.error(ModelLoaderMsgs.AAI_REST_REQUEST_ERROR, "GET", url, e.getLocalizedMessage());
148             return null;
149         }
150
151         return deleteResource(url, resVersion, transId);
152     }
153
154
155     public boolean useBasicAuth() {
156         return config.getAaiAuthenticationUser() != null && config.getAaiAuthenticationPassword() != null;
157     }
158
159     private RestClient setupClient() {
160         RestClient restClient = new RestClient();
161         restClient.validateServerHostname(false)
162                 .validateServerCertChain(false)
163                 .connectTimeoutMs(config.getClientConnectTimeoutMs())
164                 .readTimeoutMs(config.getClientReadTimeoutMs());
165
166         //Use certs only if SSL is enabled
167         if (config.useHttpsWithAAI())
168         {// @formatter:off
169             restClient
170                 .clientCertFile(config.getAaiKeyStorePath())
171                 .clientCertPassword(config.getAaiKeyStorePassword());
172             // @formatter:on
173         }
174
175         if (useBasicAuth()) {
176             restClient.authenticationMode(RestAuthenticationMode.SSL_BASIC);
177             restClient.basicAuthUsername(config.getAaiAuthenticationUser());
178             restClient.basicAuthPassword(config.getAaiAuthenticationPassword());
179         }
180
181         return restClient;
182     }
183
184     /**
185      * Create the HTTP headers required for an A&AI operation (GET/POST/PUT/DELETE)
186      * 
187      * @param transId
188      * @return map of headers
189      */
190     private Map<String, List<String>> buildHeaders(String transId) {
191         MultivaluedMap<String, String> headers = new MultivaluedMapImpl();
192         headers.put(HEADER_TRANS_ID, Collections.singletonList(transId));
193         headers.put(HEADER_FROM_APP_ID, Collections.singletonList(ML_APP_NAME));
194         return headers;
195     }
196
197     private String getResourceVersion(OperationResult getResponse)
198             throws ParserConfigurationException, SAXException, IOException {
199         String respData = getResponse.getResult();
200
201         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
202         factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
203         DocumentBuilder builder = factory.newDocumentBuilder();
204         InputSource is = new InputSource(new StringReader(respData));
205         Document doc = builder.parse(is);
206
207         NodeList nodesList = doc.getDocumentElement().getChildNodes();
208
209         // @formatter:off
210         return IntStream.range(0, nodesList.getLength()).mapToObj(nodesList::item)
211                 .filter(childNode -> childNode.getNodeName().equals(RESOURCE_VERSION_PARAM))
212                 .findFirst()
213                 .map(Node::getTextContent)
214                 .orElse(null);
215         // @formatter:on
216     }
217 }