Nodes query rewritten to generic rest client
[vid.git] / vid-app-common / src / main / java / org / onap / vid / aai / AaiOverTLSClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2018 Nokia 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.vid.aai;
22
23 import static org.onap.vid.aai.AaiOverTLSClientInterface.HEADERS.ACCEPT;
24 import static org.onap.vid.aai.AaiOverTLSClientInterface.HEADERS.CONTENT_TYPE;
25 import static org.onap.vid.aai.AaiOverTLSClientInterface.HEADERS.FROM_APP_ID_HEADER;
26 import static org.onap.vid.aai.AaiOverTLSClientInterface.HEADERS.REQUEST_ID;
27 import static org.onap.vid.aai.AaiOverTLSClientInterface.HEADERS.TRANSACTION_ID_HEADER;
28
29 import io.joshworks.restclient.http.HttpResponse;
30 import io.vavr.collection.HashMap;
31 import java.nio.charset.StandardCharsets;
32 import java.util.Base64;
33 import java.util.Collections;
34 import java.util.Map;
35 import javax.ws.rs.core.MediaType;
36 import lombok.val;
37 import org.onap.portalsdk.core.util.SystemProperties;
38 import org.onap.vid.aai.model.AaiNodeQueryResponse;
39 import org.onap.vid.aai.model.ResourceType;
40 import org.onap.vid.aai.util.AAIProperties;
41 import org.onap.vid.client.SyncRestClientInterface;
42
43 public class AaiOverTLSClient implements AaiOverTLSClientInterface {
44
45     private final AaiOverTLSPropertySupplier propertySupplier;
46     private SyncRestClientInterface syncRestClient;
47     private boolean useClientCert;
48     private static String CALLER_APP_ID = "VidAaiController";
49     private String urlBase;
50
51     public AaiOverTLSClient(SyncRestClientInterface syncRestClient, AaiOverTLSPropertySupplier propertySupplier) {
52         this(syncRestClient, propertySupplier, SystemProperties.getProperty(AAIProperties.AAI_SERVER_URL));
53     }
54
55     AaiOverTLSClient(SyncRestClientInterface syncRestClient, AaiOverTLSPropertySupplier propertySupplier, String baseUrl) {
56         this.syncRestClient = syncRestClient;
57         this.propertySupplier = propertySupplier;
58         this.urlBase = baseUrl;
59     }
60
61     @Override
62     public void setUseClientCert(boolean useClientCert) {
63         this.useClientCert = useClientCert;
64     }
65
66     @Override
67     public HttpResponse<AaiNodeQueryResponse> searchNodeTypeByName(String name, ResourceType type) {
68         val uri = urlBase + String.format(URIS.NODE_TYPE_BY_NAME, type.getAaiFormat(), type.getNameFilter(), name);
69         return syncRestClient.get(uri, getRequestHeaders(), Collections.emptyMap(), AaiNodeQueryResponse.class);
70     }
71
72     private Map<String, String> getRequestHeaders() {
73         val result = HashMap.of(
74             TRANSACTION_ID_HEADER, propertySupplier.getRandomUUID(),
75             FROM_APP_ID_HEADER, CALLER_APP_ID,
76             CONTENT_TYPE, MediaType.APPLICATION_JSON,
77             REQUEST_ID, propertySupplier.getRequestId(),
78             ACCEPT, MediaType.APPLICATION_JSON)
79             .toJavaMap();
80         result.putAll(getAuthorizationHeader());
81         return result;
82     }
83
84     private Map<String, String> getAuthorizationHeader() {
85         if (!useClientCert) {
86             val vidUsername = propertySupplier.getUsername();
87             val vidPassword = propertySupplier.getPassword();
88             val encoded = Base64.getEncoder()
89                 .encodeToString((vidUsername + ":" + vidPassword).getBytes(StandardCharsets.UTF_8));
90             return HashMap.of("Authorization", "Basic " + encoded).toJavaMap();
91         }
92         return HashMap.<String, String>empty().toJavaMap();
93     }
94
95 }