Get subscribers rewritten to new 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 import org.onap.vid.model.SubscriberList;
43
44 public class AaiOverTLSClient implements AaiOverTLSClientInterface {
45
46     private final AaiOverTLSPropertySupplier propertySupplier;
47     private SyncRestClientInterface syncRestClient;
48     private boolean useClientCert;
49     private static String CALLER_APP_ID = "VidAaiController";
50     private String urlBase;
51
52     public AaiOverTLSClient(SyncRestClientInterface syncRestClient, AaiOverTLSPropertySupplier propertySupplier) {
53         this(syncRestClient, propertySupplier, SystemProperties.getProperty(AAIProperties.AAI_SERVER_URL));
54     }
55
56     AaiOverTLSClient(SyncRestClientInterface syncRestClient, AaiOverTLSPropertySupplier propertySupplier, String baseUrl) {
57         this.syncRestClient = syncRestClient;
58         this.propertySupplier = propertySupplier;
59         this.urlBase = baseUrl;
60     }
61
62     @Override
63     public void setUseClientCert(boolean useClientCert) {
64         this.useClientCert = useClientCert;
65     }
66
67     @Override
68     public HttpResponse<AaiNodeQueryResponse> searchNodeTypeByName(String name, ResourceType type) {
69         val uri = urlBase + String.format(URIS.NODE_TYPE_BY_NAME, type.getAaiFormat(), type.getNameFilter(), name);
70         return syncRestClient.get(uri, getRequestHeaders(), Collections.emptyMap(), AaiNodeQueryResponse.class);
71     }
72
73     @Override
74     public HttpResponse<SubscriberList> getAllSubscribers() {
75         val uri = urlBase + String.format(URIS.SUBSCRIBERS, 0);
76         return syncRestClient.get(uri, getRequestHeaders(), Collections.emptyMap(), SubscriberList.class);
77     }
78
79     private Map<String, String> getRequestHeaders() {
80         val result = HashMap.of(
81             TRANSACTION_ID_HEADER, propertySupplier.getRandomUUID(),
82             FROM_APP_ID_HEADER, CALLER_APP_ID,
83             CONTENT_TYPE, MediaType.APPLICATION_JSON,
84             REQUEST_ID, propertySupplier.getRequestId(),
85             ACCEPT, MediaType.APPLICATION_JSON)
86             .toJavaMap();
87         result.putAll(getAuthorizationHeader());
88         return result;
89     }
90
91     private Map<String, String> getAuthorizationHeader() {
92         if (!useClientCert) {
93             val vidUsername = propertySupplier.getUsername();
94             val vidPassword = propertySupplier.getPassword();
95             val encoded = Base64.getEncoder()
96                 .encodeToString((vidUsername + ":" + vidPassword).getBytes(StandardCharsets.UTF_8));
97             return HashMap.of("Authorization", "Basic " + encoded).toJavaMap();
98         }
99         return HashMap.<String, String>empty().toJavaMap();
100     }
101
102 }