Extend probe mechanism
[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 - 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
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.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;
38
39 import javax.ws.rs.core.MediaType;
40 import java.nio.charset.StandardCharsets;
41 import java.util.Base64;
42 import java.util.Collections;
43 import java.util.Map;
44
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;
50
51 public class AaiOverTLSClient implements AaiOverTLSClientInterface {
52
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;
58
59     public AaiOverTLSClient(SyncRestClientInterface syncRestClient, AaiOverTLSPropertySupplier propertySupplier) {
60         this(syncRestClient, propertySupplier, SystemProperties.getProperty(AAIProperties.AAI_SERVER_URL));
61     }
62
63     AaiOverTLSClient(SyncRestClientInterface syncRestClient, AaiOverTLSPropertySupplier propertySupplier, String baseUrl) {
64         this.syncRestClient = syncRestClient;
65         this.propertySupplier = propertySupplier;
66         this.urlBase = baseUrl;
67     }
68
69     @Override
70     public void setUseClientCert(boolean useClientCert) {
71         this.useClientCert = useClientCert;
72     }
73
74     @Override
75     public boolean isNodeTypeExistsByName(String name, ResourceType type) {
76
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");
79         }
80
81         String path = String.format( // e.g. GET /aai/v$/nodes/vf-modules?vf-module-name={vf-module-name}
82                 "nodes/%s?%s=%s",
83                 type.getAaiFormat(),
84                 type.getNameFilter(),
85                 name
86         );
87
88         String uri = urlBase + path;
89         final HttpResponse<JsonNode> response = syncRestClient.get(uri, getRequestHeaders(), Collections.emptyMap());
90
91         return response.isSuccessful();
92     }
93
94     @Override
95     public HttpResponse<SubscriberList> getAllSubscribers() {
96         String uri = urlBase + String.format(URIS.SUBSCRIBERS, 0);
97         return syncRestClient.get(uri, getRequestHeaders(), Collections.emptyMap(), SubscriberList.class);
98     }
99
100     @Override
101     public ExternalComponentStatus probeGetAllSubscribers() {
102         String url = urlBase + String.format(URIS.SUBSCRIBERS, 0);
103         long startTime = System.currentTimeMillis();
104         ExternalComponentStatus externalComponentStatus;
105
106         try {
107             HttpResponse<SubscriberList> allSubscribers = getAllSubscribers();
108
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);
112
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);
117         }
118
119         return externalComponentStatus;
120     }
121
122
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)
130                 .toJavaMap();
131         result.putAll(getAuthorizationHeader());
132         return result;
133     }
134
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();
142         }
143         return HashMap.<String, String>empty().toJavaMap();
144     }
145
146 }