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 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;
34 import javax.ws.rs.core.MediaType;
35 import java.nio.charset.StandardCharsets;
36 import java.util.Base64;
37 import java.util.Collections;
40 import static org.onap.vid.aai.AaiOverTLSClientInterface.HEADERS.*;
42 public class AaiOverTLSClient implements AaiOverTLSClientInterface {
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;
50 public AaiOverTLSClient(SyncRestClientInterface syncRestClient, AaiOverTLSPropertySupplier propertySupplier) {
51 this(syncRestClient, propertySupplier, SystemProperties.getProperty(AAIProperties.AAI_SERVER_URL));
54 AaiOverTLSClient(SyncRestClientInterface syncRestClient, AaiOverTLSPropertySupplier propertySupplier, String baseUrl) {
55 this.syncRestClient = syncRestClient;
56 this.propertySupplier = propertySupplier;
57 this.urlBase = baseUrl;
61 public void setUseClientCert(boolean useClientCert) {
62 this.useClientCert = useClientCert;
66 public boolean isNodeTypeExistsByName(String name, ResourceType type) {
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");
72 String path = String.format( // e.g. GET /aai/v$/nodes/vf-modules?vf-module-name={vf-module-name}
79 String uri = urlBase + path;
80 final HttpResponse<JsonNode> response = syncRestClient.get(uri, getRequestHeaders(), Collections.emptyMap());
82 return response.isSuccessful();
86 public HttpResponse<SubscriberList> getAllSubscribers() {
87 String uri = urlBase + String.format(URIS.SUBSCRIBERS, 0);
88 return syncRestClient.get(uri, getRequestHeaders(), Collections.emptyMap(), SubscriberList.class);
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)
99 result.putAll(getAuthorizationHeader());
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();
111 return HashMap.<String, String>empty().toJavaMap();