2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.ccsdk.apps.ms.neng.service.extinf.impl;
25 import com.fasterxml.jackson.databind.ObjectMapper;
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;
47 * Implements interface with A&AI.
50 public class AaiServiceImpl {
52 private static final Logger log = Logger.getLogger(AaiServiceImpl.class.getName());
56 RestTemplate restTemplate;
58 AaiAuthorizationInterceptor authInt;
61 @Qualifier("aaiRestTempBuilder")
62 RestTemplateBuilder aaiRestTempBuilder;
65 * Validates the given network element name against A&AI, using the given URL.
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
71 public boolean validate(String url, String name) throws Exception {
72 AaiResponse resp = makeOutboundCall(url, name);
73 return !resp.isRecFound();
77 public void setAaiRestTempBuilder(RestTemplateBuilder aaiRestTempBuilder) {
78 this.aaiRestTempBuilder = aaiRestTempBuilder;
81 public void setRestTemplate(RestTemplate restTemplate) {
82 this.restTemplate = restTemplate;
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;
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);
100 log.warning(resp.toString());
101 throw new NengException("Error while validating name with A&AI");
103 } catch (HttpClientErrorException e) {
104 log.warning(e.getStatusCode().name() + " -- " + e.getResponseBodyAsString());
105 if (HttpStatus.NOT_FOUND.equals(e.getStatusCode())) {
106 return buildResponse(false);
108 throw new NengException("Error while validating name with AAI");
112 AaiResponse buildResponse(boolean found) {
113 AaiResponse aaiResp = new AaiResponse();
114 aaiResp.setRecFound(found);
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)
126 //this.restTemplate = restBld.requestFactory(new HttpComponentsClientHttpRequestFactory(client)).build();
128 return this.restTemplate;
131 public void setAaiProps(AaiProps aaiProps) {
132 this.aaiProps = aaiProps;