2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 - 2019 Nokia. 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.io.IOUtils;
27 import org.apache.commons.lang3.StringUtils;
28 import org.onap.portalsdk.core.util.SystemProperties;
29 import org.onap.vid.aai.model.ResourceType;
30 import org.onap.vid.aai.util.AAIProperties;
31 import org.onap.vid.client.SyncRestClientInterface;
32 import org.onap.vid.exceptions.GenericUncheckedException;
33 import org.onap.vid.model.SubscriberList;
34 import org.onap.vid.model.probes.ExternalComponentStatus;
35 import org.onap.vid.model.probes.HttpRequestMetadata;
36 import org.onap.vid.utils.Logging;
37 import org.springframework.http.HttpMethod;
39 import javax.ws.rs.core.MediaType;
40 import java.nio.charset.StandardCharsets;
41 import java.util.Base64;
42 import java.util.Collections;
45 import static org.onap.vid.aai.AaiOverTLSClientInterface.HEADERS.ACCEPT;
46 import static org.onap.vid.aai.AaiOverTLSClientInterface.HEADERS.CONTENT_TYPE;
47 import static org.onap.vid.aai.AaiOverTLSClientInterface.HEADERS.FROM_APP_ID_HEADER;
48 import static org.onap.vid.aai.AaiOverTLSClientInterface.HEADERS.REQUEST_ID;
49 import static org.onap.vid.aai.AaiOverTLSClientInterface.HEADERS.TRANSACTION_ID_HEADER;
51 public class AaiOverTLSClient implements AaiOverTLSClientInterface {
53 private final AaiOverTLSPropertySupplier propertySupplier;
54 private SyncRestClientInterface syncRestClient;
55 private boolean useClientCert;
56 private static final String CALLER_APP_ID = "VidAaiController";
57 private String urlBase;
59 public AaiOverTLSClient(SyncRestClientInterface syncRestClient, AaiOverTLSPropertySupplier propertySupplier) {
60 this(syncRestClient, propertySupplier, SystemProperties.getProperty(AAIProperties.AAI_SERVER_URL));
63 AaiOverTLSClient(SyncRestClientInterface syncRestClient, AaiOverTLSPropertySupplier propertySupplier, String baseUrl) {
64 this.syncRestClient = syncRestClient;
65 this.propertySupplier = propertySupplier;
66 this.urlBase = baseUrl;
70 public void setUseClientCert(boolean useClientCert) {
71 this.useClientCert = useClientCert;
75 public boolean isNodeTypeExistsByName(String name, ResourceType type) {
77 if (StringUtils.isEmpty(name)) {
78 throw new GenericUncheckedException("Empty resource-name provided to isNodeTypeExistsByName; request is rejected as this will cause full resources listing");
81 String path = String.format( // e.g. GET /aai/v$/nodes/vf-modules?vf-module-name={vf-module-name}
88 String uri = urlBase + path;
89 final HttpResponse<JsonNode> response = syncRestClient.get(uri, getRequestHeaders(), Collections.emptyMap());
91 return response.isSuccessful();
95 public HttpResponse<SubscriberList> getAllSubscribers() {
96 String uri = urlBase + String.format(URIS.SUBSCRIBERS, 0);
97 return syncRestClient.get(uri, getRequestHeaders(), Collections.emptyMap(), SubscriberList.class);
101 public ExternalComponentStatus probeGetAllSubscribers() {
102 String url = urlBase + String.format(URIS.SUBSCRIBERS, 0);
103 long startTime = System.currentTimeMillis();
104 ExternalComponentStatus externalComponentStatus;
107 HttpResponse<SubscriberList> allSubscribers = getAllSubscribers();
109 HttpRequestMetadata httpRequestMetadata = new HttpRequestMetadata(HttpMethod.GET, allSubscribers.getStatus(), url,
110 IOUtils.toString(allSubscribers.getRawBody()), "VID-AAI connection using new client works", System.currentTimeMillis() - startTime);
111 externalComponentStatus = new ExternalComponentStatus(ExternalComponentStatus.Component.AAI, allSubscribers.isSuccessful(), httpRequestMetadata);
113 } catch (Exception e) {
114 HttpRequestMetadata httpRequestMetadata = new HttpRequestMetadata(HttpMethod.GET, 0,
115 url, "", Logging.exceptionToDescription(e), System.currentTimeMillis() - startTime);
116 externalComponentStatus = new ExternalComponentStatus(ExternalComponentStatus.Component.AAI, false, httpRequestMetadata);
119 return externalComponentStatus;
123 private Map<String, String> getRequestHeaders() {
124 Map<String, String> result = HashMap.of(
125 TRANSACTION_ID_HEADER, propertySupplier.getRandomUUID(),
126 FROM_APP_ID_HEADER, CALLER_APP_ID,
127 CONTENT_TYPE, MediaType.APPLICATION_JSON,
128 REQUEST_ID, propertySupplier.getRequestId(),
129 ACCEPT, MediaType.APPLICATION_JSON)
131 result.putAll(getAuthorizationHeader());
135 private Map<String, String> getAuthorizationHeader() {
136 if (!useClientCert) {
137 String vidUsername = propertySupplier.getUsername();
138 String vidPassword = propertySupplier.getPassword();
139 String encoded = Base64.getEncoder()
140 .encodeToString((vidUsername + ":" + vidPassword).getBytes(StandardCharsets.UTF_8));
141 return HashMap.of("Authorization", "Basic " + encoded).toJavaMap();
143 return HashMap.<String, String>empty().toJavaMap();