Merge "Merge from ECOMP's repository"
[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 io.joshworks.restclient.http.HttpResponse;
24 import io.joshworks.restclient.http.JsonNode;
25 import io.vavr.collection.HashMap;
26 import org.apache.commons.lang3.StringUtils;
27 import org.onap.portalsdk.core.util.SystemProperties;
28 import org.onap.vid.aai.model.ResourceType;
29 import org.onap.vid.aai.util.AAIProperties;
30 import org.onap.vid.client.SyncRestClientInterface;
31 import org.onap.vid.exceptions.GenericUncheckedException;
32 import org.onap.vid.model.SubscriberList;
33
34 import javax.ws.rs.core.MediaType;
35 import java.nio.charset.StandardCharsets;
36 import java.util.Base64;
37 import java.util.Collections;
38 import java.util.Map;
39
40 import static org.onap.vid.aai.AaiOverTLSClientInterface.HEADERS.*;
41
42 public class AaiOverTLSClient implements AaiOverTLSClientInterface {
43
44     private final AaiOverTLSPropertySupplier propertySupplier;
45     private SyncRestClientInterface syncRestClient;
46     private boolean useClientCert;
47     private static final String CALLER_APP_ID = "VidAaiController";
48     private String urlBase;
49
50     public AaiOverTLSClient(SyncRestClientInterface syncRestClient, AaiOverTLSPropertySupplier propertySupplier) {
51         this(syncRestClient, propertySupplier, SystemProperties.getProperty(AAIProperties.AAI_SERVER_URL));
52     }
53
54     AaiOverTLSClient(SyncRestClientInterface syncRestClient, AaiOverTLSPropertySupplier propertySupplier, String baseUrl) {
55         this.syncRestClient = syncRestClient;
56         this.propertySupplier = propertySupplier;
57         this.urlBase = baseUrl;
58     }
59
60     @Override
61     public void setUseClientCert(boolean useClientCert) {
62         this.useClientCert = useClientCert;
63     }
64
65     @Override
66     public boolean isNodeTypeExistsByName(String name, ResourceType type) {
67
68         if (StringUtils.isEmpty(name)) {
69             throw new GenericUncheckedException("Empty resource-name provided to isNodeTypeExistsByName; request is rejected as this will cause full resources listing");
70         }
71
72         String path = String.format( // e.g. GET /aai/v$/nodes/vf-modules?vf-module-name={vf-module-name}
73                 "nodes/%s?%s=%s",
74                 type.getAaiFormat(),
75                 type.getNameFilter(),
76                 name
77         );
78
79         String uri = urlBase + path;
80         final HttpResponse<JsonNode> response = syncRestClient.get(uri, getRequestHeaders(), Collections.emptyMap());
81
82         return response.isSuccessful();
83     }
84
85     @Override
86     public HttpResponse<SubscriberList> getAllSubscribers() {
87         String uri = urlBase + String.format(URIS.SUBSCRIBERS, 0);
88         return syncRestClient.get(uri, getRequestHeaders(), Collections.emptyMap(), SubscriberList.class);
89     }
90
91     private Map<String, String> getRequestHeaders() {
92         Map<String, String> result = HashMap.of(
93                 TRANSACTION_ID_HEADER, propertySupplier.getRandomUUID(),
94                 FROM_APP_ID_HEADER, CALLER_APP_ID,
95                 CONTENT_TYPE, MediaType.APPLICATION_JSON,
96                 REQUEST_ID, propertySupplier.getRequestId(),
97                 ACCEPT, MediaType.APPLICATION_JSON)
98                 .toJavaMap();
99         result.putAll(getAuthorizationHeader());
100         return result;
101     }
102
103     private Map<String, String> getAuthorizationHeader() {
104         if (!useClientCert) {
105             String vidUsername = propertySupplier.getUsername();
106             String vidPassword = propertySupplier.getPassword();
107             String encoded = Base64.getEncoder()
108                     .encodeToString((vidUsername + ":" + vidPassword).getBytes(StandardCharsets.UTF_8));
109             return HashMap.of("Authorization", "Basic " + encoded).toJavaMap();
110         }
111         return HashMap.<String, String>empty().toJavaMap();
112     }
113
114 }