2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.aai;
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;
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;
35 import javax.ws.rs.core.MediaType;
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;
44 public class AaiOverTLSClient implements AaiOverTLSClientInterface {
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;
52 public AaiOverTLSClient(SyncRestClientInterface syncRestClient, AaiOverTLSPropertySupplier propertySupplier) {
53 this(syncRestClient, propertySupplier, SystemProperties.getProperty(AAIProperties.AAI_SERVER_URL));
56 AaiOverTLSClient(SyncRestClientInterface syncRestClient, AaiOverTLSPropertySupplier propertySupplier, String baseUrl) {
57 this.syncRestClient = syncRestClient;
58 this.propertySupplier = propertySupplier;
59 this.urlBase = baseUrl;
63 public void setUseClientCert(boolean useClientCert) {
64 this.useClientCert = useClientCert;
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);
74 public HttpResponse<SubscriberList> getAllSubscribers() {
75 val uri = urlBase + String.format(URIS.SUBSCRIBERS, 0);
76 return syncRestClient.get(uri, getRequestHeaders(), Collections.emptyMap(), SubscriberList.class);
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)
87 result.putAll(getAuthorizationHeader());
91 private Map<String, String> getAuthorizationHeader() {
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();
99 return HashMap.<String, String>empty().toJavaMap();