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;
43 public class AaiOverTLSClient implements AaiOverTLSClientInterface {
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;
51 public AaiOverTLSClient(SyncRestClientInterface syncRestClient, AaiOverTLSPropertySupplier propertySupplier) {
52 this(syncRestClient, propertySupplier, SystemProperties.getProperty(AAIProperties.AAI_SERVER_URL));
55 AaiOverTLSClient(SyncRestClientInterface syncRestClient, AaiOverTLSPropertySupplier propertySupplier, String baseUrl) {
56 this.syncRestClient = syncRestClient;
57 this.propertySupplier = propertySupplier;
58 this.urlBase = baseUrl;
62 public void setUseClientCert(boolean useClientCert) {
63 this.useClientCert = useClientCert;
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);
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)
80 result.putAll(getAuthorizationHeader());
84 private Map<String, String> getAuthorizationHeader() {
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();
92 return HashMap.<String, String>empty().toJavaMap();