Port to java 17
[ccsdk/apps.git] / ms / neng / src / main / java / org / onap / ccsdk / apps / ms / neng / service / extinf / impl / AaiServiceImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK.apps
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2018 IBM.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.ccsdk.apps.ms.neng.service.extinf.impl;
24
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import java.net.URI;
27 import java.util.logging.Logger;
28 import org.apache.http.client.HttpClient;
29 import org.apache.http.conn.ssl.NoopHostnameVerifier;
30 import org.apache.http.impl.client.HttpClientBuilder;
31 import org.onap.ccsdk.apps.ms.neng.core.exceptions.NengException;
32 import org.onap.ccsdk.apps.ms.neng.core.resource.model.AaiResponse;
33 import org.onap.ccsdk.apps.ms.neng.core.rs.interceptors.AaiAuthorizationInterceptor;
34 import org.onap.ccsdk.apps.ms.neng.extinf.props.AaiProps;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.beans.factory.annotation.Qualifier;
37 import org.springframework.boot.web.client.RestTemplateBuilder;
38 import org.springframework.http.HttpStatus;
39 import org.springframework.http.RequestEntity;
40 import org.springframework.http.ResponseEntity;
41 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
42 import org.springframework.stereotype.Service;
43 import org.springframework.web.client.HttpClientErrorException;
44 import org.springframework.web.client.RestTemplate;
45
46 /**
47  * Implements interface with A&AI.
48  */
49 @Service
50 public class AaiServiceImpl {
51
52     private static final Logger log = Logger.getLogger(AaiServiceImpl.class.getName());
53
54     @Autowired
55     AaiProps aaiProps;
56     RestTemplate restTemplate;
57     @Autowired
58     AaiAuthorizationInterceptor authInt;
59
60     @Autowired
61     @Qualifier("aaiRestTempBuilder")
62     RestTemplateBuilder aaiRestTempBuilder;
63
64     /**
65      * Validates the given network element name against A&AI, using the given URL.
66      *
67      * @param url the URL for A&AI
68      * @param name a generated network element name
69      * @return true if the element name is valid
70      */
71     public boolean validate(String url, String name) throws Exception {
72         AaiResponse resp = makeOutboundCall(url, name);
73         return !resp.isRecFound();
74     }
75
76
77     public void setAaiRestTempBuilder(RestTemplateBuilder aaiRestTempBuilder) {
78         this.aaiRestTempBuilder = aaiRestTempBuilder;
79     }
80
81     public void setRestTemplate(RestTemplate restTemplate) {
82         this.restTemplate = restTemplate;
83     }
84
85     AaiResponse makeOutboundCall(String url, String name) throws Exception {
86         String uri = aaiProps.getUriBase() + url + name;
87         log.info("AAI URI - " + uri);
88         ResponseEntity<Object> resp = null;
89         try {
90             RequestEntity<Void> re = RequestEntity.get(new URI(uri)).build();
91             resp = getRestTemplate().exchange(re, Object.class);
92             if (HttpStatus.OK.equals(resp.getStatusCode())) {
93                 ObjectMapper objectmapper = new ObjectMapper();
94                 log.info(objectmapper.writeValueAsString(resp.getBody()));
95                 return buildResponse(true);
96             } else if (HttpStatus.NOT_FOUND.equals(resp.getStatusCode())) {
97                 log.warning(resp.toString());
98                 return buildResponse(false);
99             } else {
100                 log.warning(resp.toString());
101                 throw new NengException("Error while validating name with A&AI");
102             }
103         } catch (HttpClientErrorException e) {
104             log.warning(e.getStatusCode() + " -- " + e.getResponseBodyAsString());
105             if (HttpStatus.NOT_FOUND.equals(e.getStatusCode())) {
106                 return buildResponse(false);
107             }
108             throw new NengException("Error while validating name with AAI");
109         }
110     }
111
112     AaiResponse buildResponse(boolean found) {
113         AaiResponse aaiResp = new AaiResponse();
114         aaiResp.setRecFound(found);
115         return aaiResp;
116     }
117
118     RestTemplate getRestTemplate() {
119         if (this.restTemplate == null) {
120             System.setProperty("javax.net.ssl.trustStore", aaiProps.getCert());
121             System.setProperty("javax.net.ssl.trustStorePassword", aaiProps.getCertPassword());
122             RestTemplateBuilder restBld = aaiRestTempBuilder.additionalInterceptors(authInt);
123             HttpClient client = HttpClientBuilder.create()
124                 .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
125                 .build();
126             //this.restTemplate = restBld.requestFactory(new HttpComponentsClientHttpRequestFactory(client)).build();
127         }
128         return this.restTemplate;
129     }
130
131     public void setAaiProps(AaiProps aaiProps) {
132         this.aaiProps = aaiProps;
133     }
134 }