2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.modelloader.restclient;
23 import com.sun.jersey.core.util.MultivaluedMapImpl; // NOSONAR
24 import java.io.IOException;
25 import java.io.StringReader;
27 import java.util.Collections;
28 import java.util.List;
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.w3c.dom.Document;
46 import org.w3c.dom.Node;
47 import org.w3c.dom.NodeList;
48 import org.xml.sax.InputSource;
49 import org.xml.sax.SAXException;
52 * Wrapper around the standard A&AI Rest Client interface. This currently uses Jersey client 1.x
55 public class AaiRestClient {
57 public static final String HEADER_TRANS_ID = "X-TransactionId";
58 public static final String HEADER_FROM_APP_ID = "X-FromAppId";
59 public static final String ML_APP_NAME = "ModelLoader";
60 private static final String RESOURCE_VERSION_PARAM = "resource-version";
62 private static Logger logger = LoggerFactory.getInstance().getLogger(AaiRestClient.class.getName());
64 private ModelLoaderConfig config = null;
66 public AaiRestClient(ModelLoaderConfig config) {
72 * Send a GET request to the A&AI for a resource.
79 public OperationResult getResource(String url, String transId, MediaType mediaType) {
80 return setupClient().get(url, buildHeaders(transId), mediaType);
84 * Send a PUT request to the A&AI.
86 * @param url - the url
87 * @param payload - the XML or JSON payload for the request
88 * @param transId - transaction ID
89 * @param mediaType - the content type (XML or JSON)
90 * @return operation result
92 public OperationResult putResource(String url, String payload, String transId, MediaType mediaType) {
93 logger.info(ModelLoaderMsgs.AAI_REST_REQUEST_PAYLOAD, payload);
94 return setupClient().put(url, payload, buildHeaders(transId), mediaType, mediaType);
99 * Send a POST request to the A&AI.
101 * @param url - the url
102 * @param transId - transaction ID
103 * @param payload - the XML or JSON payload for the request
104 * @param mimeType - the content type (XML or JSON)
105 * @return ClientResponse
107 public OperationResult postResource(String url, String payload, String transId, MediaType mediaType) {
108 logger.info(ModelLoaderMsgs.AAI_REST_REQUEST_PAYLOAD, payload);
109 return setupClient().post(url, payload, buildHeaders(transId), mediaType, mediaType);
114 * Send a DELETE request to the A&AI.
116 * @param url - the url
117 * @param resourceVersion - the resource-version of the model to delete
118 * @param transId - transaction ID
119 * @return ClientResponse
121 public OperationResult deleteResource(String url, String resourceVersion, String transId) {
122 URI uri = UriBuilder.fromUri(url).queryParam(RESOURCE_VERSION_PARAM, resourceVersion).build();
123 return setupClient().delete(uri.toString(), buildHeaders(transId), null);
127 * Does a GET on a resource to retrieve the resource version, and then DELETE that version.
129 * @param url - the url
130 * @param transId - transaction ID
131 * @return ClientResponse
133 public OperationResult getAndDeleteResource(String url, String transId) {
134 // First, GET the model
135 OperationResult getResponse = getResource(url, transId, MediaType.APPLICATION_XML_TYPE);
136 if (getResponse == null || getResponse.getResultCode() != Response.Status.OK.getStatusCode()) {
140 // Delete the model using the resource version in the response
141 String resVersion = null;
143 resVersion = getResourceVersion(getResponse);
144 } catch (Exception e) {
145 logger.error(ModelLoaderMsgs.AAI_REST_REQUEST_ERROR, "GET", url, e.getLocalizedMessage());
149 return deleteResource(url, resVersion, transId);
153 public boolean useBasicAuth() {
154 return config.getAaiAuthenticationUser() != null && config.getAaiAuthenticationPassword() != null;
157 private RestClient setupClient() {
158 RestClient restClient = new RestClient();
161 restClient.validateServerHostname(false)
162 .validateServerCertChain(false)
163 .clientCertFile(config.getAaiKeyStorePath())
164 .clientCertPassword(config.getAaiKeyStorePassword())
165 .connectTimeoutMs(120000)
166 .readTimeoutMs(120000);
169 if (useBasicAuth()) {
170 restClient.authenticationMode(RestAuthenticationMode.SSL_BASIC);
171 restClient.basicAuthUsername(config.getAaiAuthenticationUser());
172 restClient.basicAuthPassword(config.getAaiAuthenticationPassword());
179 * Create the HTTP headers required for an A&AI operation (GET/POST/PUT/DELETE)
182 * @return map of headers
184 private Map<String, List<String>> buildHeaders(String transId) {
185 MultivaluedMap<String, String> headers = new MultivaluedMapImpl();
186 headers.put(HEADER_TRANS_ID, Collections.singletonList(transId));
187 headers.put(HEADER_FROM_APP_ID, Collections.singletonList(ML_APP_NAME));
191 private String getResourceVersion(OperationResult getResponse)
192 throws ParserConfigurationException, SAXException, IOException {
193 String respData = getResponse.getResult();
195 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
196 factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
197 DocumentBuilder builder = factory.newDocumentBuilder();
198 InputSource is = new InputSource(new StringReader(respData));
199 Document doc = builder.parse(is);
201 NodeList nodesList = doc.getDocumentElement().getChildNodes();
204 return IntStream.range(0, nodesList.getLength()).mapToObj(nodesList::item)
205 .filter(childNode -> childNode.getNodeName().equals(RESOURCE_VERSION_PARAM))
207 .map(Node::getTextContent)