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.core.type.TypeReference;
26 import com.fasterxml.jackson.databind.ObjectMapper;
28 import java.security.cert.X509Certificate;
29 import java.util.List;
31 import java.util.logging.Logger;
32 import javax.net.ssl.HostnameVerifier;
33 import javax.net.ssl.SSLContext;
34 import javax.net.ssl.SSLSession;
35 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
36 import org.apache.http.conn.ssl.TrustStrategy;
37 import org.apache.http.impl.client.CloseableHttpClient;
38 import org.apache.http.impl.client.HttpClients;
39 import org.onap.ccsdk.apps.ms.neng.core.exceptions.NengException;
40 import org.onap.ccsdk.apps.ms.neng.core.policy.PolicyFinder;
41 import org.onap.ccsdk.apps.ms.neng.core.resource.model.GetConfigRequest;
42 import org.onap.ccsdk.apps.ms.neng.core.resource.model.GetConfigResponse;
43 import org.onap.ccsdk.apps.ms.neng.core.rs.interceptors.PolicyManagerAuthorizationInterceptor;
44 import org.onap.ccsdk.apps.ms.neng.extinf.props.PolicyManagerProps;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.beans.factory.annotation.Qualifier;
47 import org.springframework.boot.web.client.RestTemplateBuilder;
48 import org.springframework.http.HttpStatus;
49 import org.springframework.http.MediaType;
50 import org.springframework.http.RequestEntity;
51 import org.springframework.http.ResponseEntity;
52 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
53 import org.springframework.stereotype.Component;
54 import org.springframework.web.client.HttpStatusCodeException;
55 import org.springframework.web.client.RestTemplate;
58 * Finds policies from policy manager.
61 @Qualifier("PolicyFinderServiceImpl")
62 public class PolicyFinderServiceImpl implements PolicyFinder {
63 private static Logger log = Logger.getLogger(PolicyFinderServiceImpl.class.getName());
65 @Autowired PolicyManagerProps policManProps;
66 @Autowired @Qualifier("policyMgrRestTempBuilder") RestTemplateBuilder policyMgrRestTempBuilder;
67 @Autowired PolicyManagerAuthorizationInterceptor authInt;
68 RestTemplate restTemplate;
71 * Find policy with the given name from policy manager.
74 public Map<String, Object> findPolicy(String policyName) throws Exception {
75 Object response = getConfig(policyName).getResponse();
76 if (response instanceof List) {
77 @SuppressWarnings("unchecked")
78 List<Map<String, Object>> policyList = (List<Map<String, Object>>) response;
79 return ((!policyList.isEmpty()) ? policyList.get(0) : null);
85 GetConfigResponse getConfig(String policyName) throws Exception {
86 GetConfigRequest getConfigRequest = new GetConfigRequest();
87 getConfigRequest.setPolicyName(policyName);
88 return (makeOutboundCall(getConfigRequest, GetConfigResponse.class));
91 <T, R> GetConfigResponse makeOutboundCall(T request, Class<R> response) throws Exception {
92 log.info("Policy Manager - " + policManProps.getUrl());
93 RequestEntity<T> re = RequestEntity.post(new URI(policManProps.getUrl()))
94 .accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON).body(request);
96 ResponseEntity<Object> resp = getRestTemplate().exchange(re, Object.class);
97 if (HttpStatus.OK.equals(resp.getStatusCode())) {
98 ObjectMapper objectmapper = new ObjectMapper();
99 log.info(objectmapper.writeValueAsString(resp.getBody()));
100 //System.out.println(objectmapper.writeValueAsString(resp.getBody()));
101 List<Map<Object, Object>> respObj = objectmapper.readValue(
102 objectmapper.writeValueAsString(resp.getBody()),
103 new TypeReference<List<Map<Object, Object>>>() {});
104 transformConfigObject(objectmapper, respObj);
105 GetConfigResponse getConfigResp = new GetConfigResponse();
106 getConfigResp.setResponse(respObj);
107 return getConfigResp;
109 } catch (HttpStatusCodeException e) {
112 throw new NengException("Error while retrieving policy from policy manager.");
115 void handleError(HttpStatusCodeException e) throws Exception {
116 String respString = e.getResponseBodyAsString();
117 log.info(respString);
118 if (e.getStatusText() != null) {
119 log.info(e.getStatusText());
121 if (e.getResponseHeaders() != null && e.getResponseHeaders().toSingleValueMap() != null) {
122 log.info(e.getResponseHeaders().toSingleValueMap().toString());
124 if (HttpStatus.NOT_FOUND.equals(e.getStatusCode()) && (respString != null && respString.contains(""))) {
125 throw new NengException("Policy not found in policy manager.");
127 throw new NengException("Error while retrieving policy from policy manager.");
131 * Transforms the 'config' element (which is received as a JSON string) to a map like a JSON object.
133 void transformConfigObject(ObjectMapper objectmapper, List<Map<Object, Object>> respObj) throws Exception {
134 Object configElement = respObj.get(0).get("config");
135 if (configElement instanceof String) {
136 Map<Object, Object> obj = objectmapper.readValue(configElement.toString(),
137 new TypeReference<Map<Object, Object>>() {});
138 respObj.get(0).put("config", obj);
142 RestTemplate getRestTemplate() throws Exception {
143 if (restTemplate != null) {
146 TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
147 SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
148 .loadTrustMaterial(null, acceptingTrustStrategy).build();
149 HostnameVerifier verifier = (String arg0, SSLSession arg1) -> true;
150 SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext, verifier);
151 CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(csf).build();
152 HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
153 requestFactory.setHttpClient(httpClient);
154 restTemplate = new RestTemplate(requestFactory);
155 restTemplate.getInterceptors().add(getAuthInt());
159 RestTemplateBuilder getPolicyMgrRestTempBuilder() {
160 return policyMgrRestTempBuilder;
163 void setPolicyMgrRestTempBuilder(RestTemplateBuilder policyMgrRestTempBuilder) {
164 this.policyMgrRestTempBuilder = policyMgrRestTempBuilder;
167 PolicyManagerAuthorizationInterceptor getAuthInt() {
171 void setAuthInt(PolicyManagerAuthorizationInterceptor authInt) {
172 this.authInt = authInt;