Removed dependency from lombok
[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 org.onap.portalsdk.core.util.SystemProperties;
37 import org.onap.vid.aai.model.AaiNodeQueryResponse;
38 import org.onap.vid.aai.model.ResourceType;
39 import org.onap.vid.aai.util.AAIProperties;
40 import org.onap.vid.client.SyncRestClientInterface;
41 import org.onap.vid.model.SubscriberList;
42
43 public class AaiOverTLSClient implements AaiOverTLSClientInterface {
44
45     private final AaiOverTLSPropertySupplier propertySupplier;
46     private SyncRestClientInterface syncRestClient;
47     private boolean useClientCert;
48     private static final 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         String 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     @Override
73     public HttpResponse<SubscriberList> getAllSubscribers() {
74         String uri = urlBase + String.format(URIS.SUBSCRIBERS, 0);
75         return syncRestClient.get(uri, getRequestHeaders(), Collections.emptyMap(), SubscriberList.class);
76     }
77
78     private Map<String, String> getRequestHeaders() {
79         Map<String, String> result = HashMap.of(
80                 TRANSACTION_ID_HEADER, propertySupplier.getRandomUUID(),
81                 FROM_APP_ID_HEADER, CALLER_APP_ID,
82                 CONTENT_TYPE, MediaType.APPLICATION_JSON,
83                 REQUEST_ID, propertySupplier.getRequestId(),
84                 ACCEPT, MediaType.APPLICATION_JSON)
85                 .toJavaMap();
86         result.putAll(getAuthorizationHeader());
87         return result;
88     }
89
90     private Map<String, String> getAuthorizationHeader() {
91         if (!useClientCert) {
92             String vidUsername = propertySupplier.getUsername();
93             String vidPassword = propertySupplier.getPassword();
94             String encoded = Base64.getEncoder()
95                     .encodeToString((vidUsername + ":" + vidPassword).getBytes(StandardCharsets.UTF_8));
96             return HashMap.of("Authorization", "Basic " + encoded).toJavaMap();
97         }
98         return HashMap.<String, String>empty().toJavaMap();
99     }
100
101 }