2 * Copyright © 2017-2018 AT&T Intellectual Property.
\r
3 * Modifications Copyright © 2018 IBM.
\r
5 * Licensed under the Apache License, Version 2.0 (the "License");
\r
6 * you may not use this file except in compliance with the License.
\r
7 * You may obtain a copy of the License at
\r
9 * http://www.apache.org/licenses/LICENSE-2.0
\r
11 * Unless required by applicable law or agreed to in writing, software
\r
12 * distributed under the License is distributed on an "AS IS" BASIS,
\r
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
14 * See the License for the specific language governing permissions and
\r
15 * limitations under the License.
\r
18 package org.onap.ccsdk.features.rest.adaptor.service;
\r
20 import java.util.Map;
\r
21 import java.util.UUID;
\r
22 import org.apache.commons.lang3.StringUtils;
\r
23 import org.onap.ccsdk.features.rest.adaptor.ConfigRestAdaptorConstants;
\r
24 import org.onap.ccsdk.features.rest.adaptor.ConfigRestAdaptorException;
\r
25 import org.onap.ccsdk.features.rest.adaptor.data.RestResponse;
\r
26 import org.springframework.http.HttpHeaders;
\r
27 import org.springframework.http.MediaType;
\r
28 import com.att.eelf.configuration.EELFLogger;
\r
29 import com.att.eelf.configuration.EELFManager;
\r
31 public class GenericRestClientAdapterImpl extends AbstractConfigRestClientAdapter {
\r
33 private static EELFLogger logger = EELFManager.getInstance().getLogger(GenericRestClientAdapterImpl.class);
\r
34 private String baseUrl = "";
\r
36 public GenericRestClientAdapterImpl(Map<String, String> properties, String serviceSelector)
\r
37 throws ConfigRestAdaptorException {
\r
38 super(properties, serviceSelector);
\r
39 init(serviceSelector);
\r
42 private void init(String serviceSelector) throws ConfigRestAdaptorException {
\r
44 if (isRestClientServiceAdapaterEnabled) {
\r
45 String baseUrlProperty = ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY + serviceSelector
\r
46 + ConfigRestAdaptorConstants.SERVICE_BASEURL_PROPERTY;
\r
47 String userProperty = ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY + serviceSelector
\r
48 + ConfigRestAdaptorConstants.SERVICE_USER_PROPERTY;
\r
49 String passProperty = ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY + serviceSelector
\r
50 + ConfigRestAdaptorConstants.SERVICE_PSSWD_PROPERTY;
\r
52 baseUrl = properties.get(baseUrlProperty);
\r
53 String userId = properties.get(userProperty);
\r
54 String pass = properties.get(passProperty);
\r
56 initialise(userId, pass);
\r
57 logger.info("Initialised restconf adaptor service for selector ({})", serviceSelector);
\r
58 if (restTemplate == null) {
\r
59 throw new ConfigRestAdaptorException("couldn't initialise rest selector (" + serviceSelector + ")");
\r
62 throw new ConfigRestAdaptorException("rest selector (" + serviceSelector + ") is not enabled");
\r
65 } catch (Exception e) {
\r
66 throw new ConfigRestAdaptorException("GenericRestClientServiceAdapter : " + e.getMessage(), e);
\r
71 public <T> T getResource(String path, Class<T> responseType) throws ConfigRestAdaptorException {
\r
72 return super.getResource(formHttpHeaders(), constructUrl(baseUrl, path), responseType);
\r
76 public <T> T postResource(String path, Object request, Class<T> responseType) throws ConfigRestAdaptorException {
\r
77 return super.postResource(formHttpHeaders(), constructUrl(baseUrl, path), request, responseType);
\r
81 public <T> T exchangeResource(String path, Object request, Class<T> responseType, String method)
\r
82 throws ConfigRestAdaptorException {
\r
83 return super.exchangeResource(formHttpHeaders(), constructUrl(baseUrl, path), request, responseType, method);
\r
87 public RestResponse getResource(String path) throws ConfigRestAdaptorException {
\r
88 return super.getResource(formHttpHeaders(), constructUrl(baseUrl, path));
\r
92 public RestResponse postResource(String path, Object request) throws ConfigRestAdaptorException {
\r
93 return super.postResource(formHttpHeaders(), constructUrl(baseUrl, path), request);
\r
97 public RestResponse exchangeResource(String path, Object request, String method) throws ConfigRestAdaptorException {
\r
98 return super.exchangeResource(formHttpHeaders(), constructUrl(baseUrl, path), request, method);
\r
101 private HttpHeaders formHttpHeaders() {
\r
103 HttpHeaders headers = new HttpHeaders();
\r
105 headers.setContentType(MediaType.APPLICATION_JSON);
\r
106 headers.add("Accept", MediaType.APPLICATION_JSON_VALUE);
\r
107 headers.add("X-TransactionId", generateUUID());
\r
108 headers.add("X-ECOMP-RequestID", headers.getFirst("X-TransactionId"));
\r
110 String appIDPath = ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY + serviceSelector
\r
111 + ConfigRestAdaptorConstants.SERVICE_APPID_PROPERTY;
\r
112 String environmentPath = ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY + serviceSelector
\r
113 + ConfigRestAdaptorConstants.SERVICE_ENV_PROPERTY;
\r
114 String clientAuthPath = ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY + serviceSelector
\r
115 + ConfigRestAdaptorConstants.SERVICE_CLIENTAUTH_PROPERTY;
\r
116 String authorizationPath = ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY + serviceSelector
\r
117 + ConfigRestAdaptorConstants.SERVICE_AUTHORIZATION_PROPERTY;
\r
119 if (StringUtils.isNotBlank(properties.get(appIDPath))) {
\r
120 headers.add("X-FromAppId", properties.get(appIDPath));
\r
122 if (StringUtils.isNotBlank(properties.get(clientAuthPath))) {
\r
123 headers.add("ClientAuth", properties.get(clientAuthPath));
\r
125 if (StringUtils.isNotBlank(properties.get(authorizationPath))) {
\r
126 headers.add("Authorization", properties.get(authorizationPath));
\r
128 if (StringUtils.isNotBlank(properties.get(environmentPath))) {
\r
129 headers.add("Environment", properties.get(environmentPath));
\r
135 private synchronized String generateUUID() {
\r
136 return UUID.randomUUID().toString();
\r