Add SvcLogicContext interaction with netbox-client
[ccsdk/sli/adaptors.git] / netbox-client / provider / src / main / java / org / onap / ccsdk / sli / adaptors / netbox / impl / NetboxHttpClient.java
1 /*
2  * Copyright (C) 2018 Bell Canada.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.ccsdk.sli.adaptors.netbox.impl;
17
18 import static org.apache.http.HttpHeaders.ACCEPT;
19 import static org.apache.http.HttpHeaders.AUTHORIZATION;
20 import static org.apache.http.HttpHeaders.CONTENT_TYPE;
21
22 import java.io.IOException;
23 import java.nio.charset.Charset;
24 import java.security.KeyManagementException;
25 import java.security.KeyStoreException;
26 import java.security.NoSuchAlgorithmException;
27 import javax.net.ssl.SSLContext;
28 import org.apache.http.HttpResponse;
29 import org.apache.http.client.methods.HttpDelete;
30 import org.apache.http.client.methods.HttpPost;
31 import org.apache.http.client.methods.HttpRequestBase;
32 import org.apache.http.conn.ssl.NoopHostnameVerifier;
33 import org.apache.http.entity.StringEntity;
34 import org.apache.http.impl.client.CloseableHttpClient;
35 import org.apache.http.impl.client.HttpClientBuilder;
36 import org.apache.http.ssl.SSLContexts;
37 import org.apache.http.ssl.TrustStrategy;
38 import org.onap.ccsdk.sli.adaptors.netbox.property.NetboxProperties;
39
40 public class NetboxHttpClient implements AutoCloseable {
41
42     private static final String APPLICATION_JSON = "application/json";
43
44     private final CloseableHttpClient client;
45     private final String url;
46     private final String token;
47
48     // Used by the blueprint container
49     public NetboxHttpClient(NetboxProperties properties) {
50         this(properties.getHost(), properties.getApiKey());
51     }
52
53     NetboxHttpClient(final String url, final String token) {
54         this.url = url;
55         this.token = token;
56
57         final TrustStrategy acceptingTrustStrategy = (certificate, authType) -> true;
58         final SSLContext sslContext;
59         try {
60             sslContext = SSLContexts.custom()
61                 .loadTrustMaterial(null, acceptingTrustStrategy).build();
62         } catch (final NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
63             throw new IllegalStateException("Can't create http client", e);
64         }
65         client = HttpClientBuilder.create()
66             .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
67             .setSSLContext(sslContext)
68             .build();
69     }
70
71     @Override
72     public void close() throws IOException {
73         client.close();
74     }
75
76     HttpResponse post(final String uri, final String requestBody) throws IOException {
77         final HttpPost request = new HttpPost(url + uri);
78         setHeaders(request);
79         request.setEntity(new StringEntity(requestBody, Charset.forName("UTF-8")));
80         return client.execute(request);
81     }
82
83     HttpResponse delete(final String uri) throws IOException {
84         final HttpDelete request = new HttpDelete(url + uri);
85         setHeaders(request);
86         return client.execute(request);
87     }
88
89     private void setHeaders(final HttpRequestBase request) {
90         request.addHeader(ACCEPT, APPLICATION_JSON);
91         request.addHeader(CONTENT_TYPE, APPLICATION_JSON);
92         request.addHeader(AUTHORIZATION, "Token " + token);
93     }
94 }