SDN Controller Blueprints Rest Adaptor 79/64479/1
authorSingal, Kapil (ks220y) <ks220y@att.com>
Tue, 4 Sep 2018 17:22:52 +0000 (13:22 -0400)
committerSingal, Kapil (ks220y) <ks220y@att.com>
Tue, 4 Sep 2018 17:22:52 +0000 (13:22 -0400)
Creating SDN Controller Blueprints Rest Adaptor Component

Change-Id: Ic53f91f0ea47808b486e64a5d7f2c763b67dbf31
Issue-ID: CCSDK-496
Signed-off-by: Singal, Kapil (ks220y) <ks220y@att.com>
12 files changed:
blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/ConfigRestAdaptorConstants.java [new file with mode: 0644]
blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/ConfigRestAdaptorException.java [new file with mode: 0644]
blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/data/RestResponse.java [new file with mode: 0644]
blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/service/AbstractConfigRestClientAdapter.java [new file with mode: 0644]
blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/service/ConfigRestAdaptorService.java [new file with mode: 0644]
blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/service/ConfigRestAdaptorServiceImpl.java [new file with mode: 0644]
blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/service/ConfigRestClientServiceAdapter.java [new file with mode: 0644]
blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/service/GenericRestClientAdapterImpl.java [new file with mode: 0644]
blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/service/SSLRestClientAdapterImpl.java [new file with mode: 0644]
blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/utils/BasicAuthorizationInterceptor.java [new file with mode: 0644]
blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/utils/LoggingRequestInterceptor.java [new file with mode: 0644]
blueprints-processor/adaptors/rest-adaptor-provider/src/main/resources/org/opendaylight/blueprint/impl-blueprint.xml [new file with mode: 0644]

diff --git a/blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/ConfigRestAdaptorConstants.java b/blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/ConfigRestAdaptorConstants.java
new file mode 100644 (file)
index 0000000..4b09416
--- /dev/null
@@ -0,0 +1,66 @@
+/*\r
+ * Copyright © 2017-2018 AT&T Intellectual Property.\r
+ * \r
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except\r
+ * in compliance with the License. You may obtain a copy of the License at\r
+ * \r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ * \r
+ * Unless required by applicable law or agreed to in writing, software distributed under the License\r
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express\r
+ * or implied. See the License for the specific language governing permissions and limitations under\r
+ * the License.\r
+ */\r
+\r
+package org.onap.ccsdk.config.rest.adaptor;\r
+\r
+public class ConfigRestAdaptorConstants {\r
+    private ConfigRestAdaptorConstants() {\r
+        \r
+    }\r
+    \r
+    public static final String SDNC_ROOT_DIR_ENV_VAR_KEY = "SDNC_CONFIG_DIR";\r
+    public static final String REST_ADAPTOR_PROPERTIES_FILE_NAME = "config-rest-adaptor.properties";\r
+    public static final String PROXY_URL_KEY = "proxyUrl";\r
+    public static final String PROXY_URLS_VALUE_SEPARATOR = ",";\r
+    public static final String AAF_USERNAME_KEY = "aafUserName";\r
+    public static final String AAF_PSSWD_KEY = "aafPassword";\r
+    public static final String COMMON_SERVICE_VERSION_KEY = "commonServiceVersion";\r
+    \r
+    public static final String PROPERTY_ENV_PROD = "field";\r
+    public static final String PROPERTY_ENV_SOLO = "solo";\r
+    \r
+    public static final String REST_ADAPTOR_BASE_PROPERTY = "org.onap.ccsdk.config.rest.adaptors.";\r
+    public static final String REST_ADAPTOR_ENV_TYPE = "envtype";\r
+    public static final String REST_ADAPTOR_TYPE_GENERIC = "generic";\r
+    public static final String REST_ADAPTOR_TYPE_SSL = "ssl";\r
+    \r
+    public static final String SSL_SERVICE_BASEURL = ".url";\r
+    public static final String SSL_SERVICE_APP = ".application";\r
+    public static final String SSL_SERVICE_TRUST = ".ssl.trust";\r
+    public static final String SSL_SERVICE_TRUST_PSSWD = ".ssl.trust.psswd";\r
+    public static final String SSL_SERVICE_KEY = ".ssl.key";\r
+    public static final String SSL_SERVICE_KEY_PSSWD = ".ssl.key.psswd";\r
+    \r
+    public static final String SERVICE_TYPE_PROPERTY = ".type";\r
+    public static final String SERVICE_EANABLED_PROPERTY = ".enable";\r
+    public static final String SERVICE_ENV_PROPERTY = ".env";\r
+    public static final String SERVICE_BASEURL_PROPERTY = ".url";\r
+    public static final String SERVICE_PROPERTYFILE = ".propertyfile";\r
+    public static final String SERVICE_USER_PROPERTY = ".user";\r
+    public static final String SERVICE_APPID_PROPERTY = ".appId";\r
+    public static final String SERVICE_PSSWD_PROPERTY = ".passwd";\r
+    public static final String SERVICE_CLIENTAUTH_PROPERTY = ".clientAuth";\r
+    public static final String SERVICE_AUTHORIZATION_PROPERTY = ".authorization";\r
+    \r
+    public static final String SELECTOR_AAI = "aai";\r
+    public static final String SELECTOR_ALTS = "alts";\r
+    public static final String SELECTOR_EIPAM = "eipam";\r
+    public static final String SELECTOR_COSMS = "cosms";\r
+    public static final String SELECTOR_RESTCONF = "restconf";\r
+    public static final String SELECTOR_MODEL_SERVICE = "modelservice";\r
+    public static final String SELECTOR_POLICY_MANAGER = "policymanager";\r
+    public static final String SELECTOR_NRD = "networkresourcediscovery";\r
+    public static final String SELECTOR_NSM = "nsm";\r
+    \r
+}\r
diff --git a/blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/ConfigRestAdaptorException.java b/blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/ConfigRestAdaptorException.java
new file mode 100644 (file)
index 0000000..3bb04a2
--- /dev/null
@@ -0,0 +1,47 @@
+/*\r
+ * Copyright © 2017-2018 AT&T Intellectual Property.\r
+ * \r
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except\r
+ * in compliance with the License. You may obtain a copy of the License at\r
+ * \r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ * \r
+ * Unless required by applicable law or agreed to in writing, software distributed under the License\r
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express\r
+ * or implied. See the License for the specific language governing permissions and limitations under\r
+ * the License.\r
+ */\r
+\r
+package org.onap.ccsdk.config.rest.adaptor;\r
+\r
+/**\r
+ * ConfigRestAdaptorException.java Purpose: Provide Configuration Rest Adaptor Exception\r
+ *\r
+ * @version 1.0\r
+ */\r
+public class ConfigRestAdaptorException extends Exception {\r
+    \r
+    /**\r
+     *\r
+     */\r
+    private static final long serialVersionUID = 1L;\r
+    \r
+    /**\r
+     * This is a ConfigRestAdaptorException constructor\r
+     *\r
+     * @param message\r
+     */\r
+    public ConfigRestAdaptorException(String message) {\r
+        super(message);\r
+    }\r
+    \r
+    /**\r
+     * This is a ConfigRestAdaptorException constructor\r
+     *\r
+     * @param message\r
+     */\r
+    public ConfigRestAdaptorException(String message, Throwable cause) {\r
+        super(message, cause);\r
+    }\r
+    \r
+}\r
diff --git a/blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/data/RestResponse.java b/blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/data/RestResponse.java
new file mode 100644 (file)
index 0000000..ed39ea9
--- /dev/null
@@ -0,0 +1,73 @@
+/*\r
+ * Copyright © 2017-2018 AT&T Intellectual Property.\r
+ * \r
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except\r
+ * in compliance with the License. You may obtain a copy of the License at\r
+ * \r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ * \r
+ * Unless required by applicable law or agreed to in writing, software distributed under the License\r
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express\r
+ * or implied. See the License for the specific language governing permissions and limitations under\r
+ * the License.\r
+ */\r
+\r
+package org.onap.ccsdk.config.rest.adaptor.data;\r
+\r
+import java.util.Map;\r
+\r
+public class RestResponse {\r
+    \r
+    private String statusCode;\r
+    private String body;\r
+    private Map<String, String> parameters;\r
+    private Map<String, String> responseHeaders;\r
+    private Map<String, String> requestHeaders;\r
+    \r
+    public String getStatusCode() {\r
+        return statusCode;\r
+    }\r
+    \r
+    public void setStatusCode(String statusCode) {\r
+        this.statusCode = statusCode;\r
+    }\r
+    \r
+    public String getBody() {\r
+        return body;\r
+    }\r
+    \r
+    public void setBody(String body) {\r
+        this.body = body;\r
+    }\r
+    \r
+    public Map<String, String> getParameters() {\r
+        return parameters;\r
+    }\r
+    \r
+    public void setParameters(Map<String, String> parameters) {\r
+        this.parameters = parameters;\r
+    }\r
+    \r
+    public Map<String, String> getResponseHeaders() {\r
+        return responseHeaders;\r
+    }\r
+    \r
+    public void setResponseHeaders(Map<String, String> responseHeaders) {\r
+        this.responseHeaders = responseHeaders;\r
+    }\r
+    \r
+    public Map<String, String> getRequestHeaders() {\r
+        return requestHeaders;\r
+    }\r
+    \r
+    public void setRequestHeaders(Map<String, String> requestHeaders) {\r
+        this.requestHeaders = requestHeaders;\r
+    }\r
+    \r
+    @Override\r
+    public String toString() {\r
+        return "RestResponse [statusCode=" + statusCode + ", body=" + body + ", parameters=" + parameters\r
+                + ", responseHeaders=" + responseHeaders + ", requestHeaders=" + requestHeaders + "]";\r
+    }\r
+    \r
+}\r
diff --git a/blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/service/AbstractConfigRestClientAdapter.java b/blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/service/AbstractConfigRestClientAdapter.java
new file mode 100644 (file)
index 0000000..7ccb627
--- /dev/null
@@ -0,0 +1,274 @@
+/*\r
+ * Copyright © 2017-2018 AT&T Intellectual Property.\r
+ * \r
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except\r
+ * in compliance with the License. You may obtain a copy of the License at\r
+ * \r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ * \r
+ * Unless required by applicable law or agreed to in writing, software distributed under the License\r
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express\r
+ * or implied. See the License for the specific language governing permissions and limitations under\r
+ * the License.\r
+ */\r
+\r
+package org.onap.ccsdk.config.rest.adaptor.service;\r
+\r
+import java.io.FileInputStream;\r
+import java.io.InputStream;\r
+import java.security.KeyStore;\r
+import java.security.cert.X509Certificate;\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+import java.util.Map;\r
+import java.util.concurrent.ConcurrentHashMap;\r
+import javax.net.ssl.SSLContext;\r
+import org.apache.commons.lang3.StringUtils;\r
+import org.apache.http.conn.ssl.NoopHostnameVerifier;\r
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;\r
+import org.apache.http.impl.client.CloseableHttpClient;\r
+import org.apache.http.impl.client.HttpClients;\r
+import org.apache.http.ssl.SSLContextBuilder;\r
+import org.apache.http.ssl.TrustStrategy;\r
+import org.onap.ccsdk.config.rest.adaptor.ConfigRestAdaptorConstants;\r
+import org.onap.ccsdk.config.rest.adaptor.ConfigRestAdaptorException;\r
+import org.onap.ccsdk.config.rest.adaptor.data.RestResponse;\r
+import org.onap.ccsdk.config.rest.adaptor.utils.BasicAuthorizationInterceptor;\r
+import org.springframework.http.HttpEntity;\r
+import org.springframework.http.HttpHeaders;\r
+import org.springframework.http.HttpMethod;\r
+import org.springframework.http.HttpStatus;\r
+import org.springframework.http.ResponseEntity;\r
+import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;\r
+import org.springframework.http.converter.ByteArrayHttpMessageConverter;\r
+import org.springframework.http.converter.HttpMessageConverter;\r
+import org.springframework.http.converter.ResourceHttpMessageConverter;\r
+import org.springframework.http.converter.StringHttpMessageConverter;\r
+import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;\r
+import org.springframework.http.converter.xml.SourceHttpMessageConverter;\r
+import org.springframework.util.ResourceUtils;\r
+import org.springframework.web.client.HttpClientErrorException;\r
+import org.springframework.web.client.RestTemplate;\r
+import com.att.eelf.configuration.EELFLogger;\r
+import com.att.eelf.configuration.EELFManager;\r
+\r
+abstract class AbstractConfigRestClientAdapter implements ConfigRestClientServiceAdapter {\r
+    \r
+    private static EELFLogger logger = EELFManager.getInstance().getLogger(AbstractConfigRestClientAdapter.class);\r
+    private static final String MS_INIT_FAIL = "Failed to initialise microservice client restTemplate.";\r
+    \r
+    protected boolean isRestClientServiceAdapaterEnabled = false;\r
+    protected boolean isSSLServiceAdapaterEnabled = true;\r
+    \r
+    protected Map<String, String> properties = new ConcurrentHashMap<>();\r
+    protected String serviceSelector;\r
+    \r
+    protected RestTemplate restTemplate;\r
+    \r
+    protected AbstractConfigRestClientAdapter(Map<String, String> properties, String serviceSelector) {\r
+        this.properties = properties;\r
+        this.serviceSelector = serviceSelector;\r
+        setRestClientServiceAdapaterEnabled();\r
+    }\r
+    \r
+    private void setRestClientServiceAdapaterEnabled() {\r
+        String isEnabledProperty = ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY + serviceSelector\r
+                + ConfigRestAdaptorConstants.SERVICE_EANABLED_PROPERTY;\r
+        String isRestClientServiceAdapaterEnabledStr = properties.get(isEnabledProperty);\r
+        logger.info("Service selector ({}) enable status ({}) ", serviceSelector,\r
+                isRestClientServiceAdapaterEnabledStr);\r
+        if (StringUtils.isNotBlank(isRestClientServiceAdapaterEnabledStr)\r
+                && Boolean.parseBoolean(isRestClientServiceAdapaterEnabledStr)) {\r
+            isRestClientServiceAdapaterEnabled = true;\r
+        }\r
+    }\r
+    \r
+    private List<HttpMessageConverter<?>> getMessageConverters() {\r
+        List<HttpMessageConverter<?>> converters = new ArrayList<>();\r
+        converters.add(new ByteArrayHttpMessageConverter());\r
+        converters.add(new StringHttpMessageConverter());\r
+        converters.add(new ResourceHttpMessageConverter());\r
+        converters.add(new SourceHttpMessageConverter());\r
+        converters.add(new MappingJackson2HttpMessageConverter());\r
+        return converters;\r
+    }\r
+    \r
+    public void initialise(String user, String pass) {\r
+        logger.trace("Config rest template factory user ({}) ", user);\r
+        \r
+        CloseableHttpClient httpClient =\r
+                HttpClients.custom().setSSLHostnameVerifier(new NoopHostnameVerifier()).build();\r
+        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();\r
+        requestFactory.setHttpClient(httpClient);\r
+        \r
+        restTemplate = new RestTemplate(getMessageConverters());\r
+        restTemplate.setRequestFactory(requestFactory);\r
+        if (StringUtils.isNotBlank(user) && StringUtils.isNotBlank(pass)) {\r
+            restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor(user, pass));\r
+        }\r
+    }\r
+    \r
+    public void initialiseSSL(String keyStorePath, String trustStorePath, String keyPass, String trustPass)\r
+            throws ConfigRestAdaptorException {\r
+        logger.trace("SSL rest template factory");\r
+        \r
+        TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;\r
+        SSLContext sslContext = null;\r
+        \r
+        try (InputStream keyInput = new FileInputStream(keyStorePath)) {\r
+            KeyStore keyStore = KeyStore.getInstance("PKCS12");\r
+            keyStore.load(keyInput, keyPass.toCharArray());\r
+            \r
+            logger.info("key loaded successfully");\r
+            sslContext = SSLContextBuilder.create().loadKeyMaterial(keyStore, keyPass.toCharArray()).loadTrustMaterial(\r
+                    ResourceUtils.getFile(trustStorePath), trustPass.toCharArray(), acceptingTrustStrategy).build();\r
+        } catch (Exception e) {\r
+            throw new ConfigRestAdaptorException(e.getMessage());\r
+        }\r
+        \r
+        SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);\r
+        CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(csf).build();\r
+        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);\r
+        \r
+        restTemplate = new RestTemplate(getMessageConverters());\r
+        restTemplate.setRequestFactory(requestFactory);\r
+    }\r
+    \r
+    public <T> T getResource(HttpHeaders headers, String url, Class<T> responseType) throws ConfigRestAdaptorException {\r
+        ResponseEntity<T> response = exchangeForEntity(headers, url, HttpMethod.GET, null, responseType);\r
+        return processResponse(response, url, HttpMethod.GET);\r
+    }\r
+    \r
+    public <T> T postResource(HttpHeaders headers, String url, Object request, Class<T> responseType)\r
+            throws ConfigRestAdaptorException {\r
+        ResponseEntity<T> response = exchangeForEntity(headers, url, HttpMethod.POST, request, responseType);\r
+        return processResponse(response, url, HttpMethod.POST);\r
+    }\r
+    \r
+    public <T> T exchangeResource(HttpHeaders headers, String url, Object request, Class<T> responseType, String method)\r
+            throws ConfigRestAdaptorException {\r
+        ResponseEntity<T> response = exchangeForEntity(headers, url, HttpMethod.resolve(method), request, responseType);\r
+        return processResponse(response, url, HttpMethod.resolve(method));\r
+    }\r
+    \r
+    public RestResponse getResource(HttpHeaders headers, String url) throws ConfigRestAdaptorException {\r
+        return exchangeForEntity(headers, url, HttpMethod.GET, null);\r
+    }\r
+    \r
+    public RestResponse postResource(HttpHeaders headers, String url, Object request)\r
+            throws ConfigRestAdaptorException {\r
+        return exchangeForEntity(headers, url, HttpMethod.POST, request);\r
+    }\r
+    \r
+    public RestResponse exchangeResource(HttpHeaders headers, String url, Object request, String method)\r
+            throws ConfigRestAdaptorException {\r
+        return exchangeForEntity(headers, url, HttpMethod.resolve(method), request);\r
+    }\r
+    \r
+    private RestResponse exchangeForEntity(HttpHeaders headers, String url, HttpMethod httpMethod, Object request)\r
+            throws ConfigRestAdaptorException {\r
+        RestResponse restResponse = new RestResponse();\r
+        restResponse.setRequestHeaders(headers.toSingleValueMap());\r
+        ResponseEntity<String> response = null;\r
+        \r
+        try {\r
+            if (restTemplate == null) {\r
+                logger.error(MS_INIT_FAIL);\r
+            } else {\r
+                logger.debug("Rest Operation: {}", httpMethod);\r
+                logger.debug("url    : ({})", url);\r
+                logger.debug("headers: ({})", headers);\r
+                logger.debug("request: ({})", request);\r
+                \r
+                if (HttpMethod.GET == httpMethod) {\r
+                    HttpEntity<String> entity = new HttpEntity<>("parameters", headers);\r
+                    response = restTemplate.exchange(url, httpMethod, entity, String.class);\r
+                } else {\r
+                    HttpEntity<?> entity = new HttpEntity<>(request, headers);\r
+                    response = restTemplate.exchange(url, httpMethod, entity, String.class);\r
+                }\r
+                logger.debug("response: ({})", response);\r
+                \r
+                if (response != null) {\r
+                    logger.debug("response status code: ({})", response.getStatusCode());\r
+                    restResponse.setBody(response.getBody());\r
+                    restResponse.setStatusCode(response.getStatusCode().toString());\r
+                    restResponse.setResponseHeaders(\r
+                            response.getHeaders() != null ? response.getHeaders().toSingleValueMap() : null);\r
+                    return restResponse;\r
+                }\r
+                throw new ConfigRestAdaptorException("Rest exchangeForEntity failed to perform ");\r
+            }\r
+        } catch (HttpClientErrorException clientError) {\r
+            logger.debug("clientError: ({})", clientError);\r
+            restResponse.setBody(StringUtils.isBlank(clientError.getResponseBodyAsString()) ? clientError.getMessage()\r
+                    : clientError.getResponseBodyAsString());\r
+            restResponse.setStatusCode(clientError.getStatusCode().toString());\r
+        } catch (Exception e) {\r
+            throw new ConfigRestAdaptorException(\r
+                    String.format("httpMethod (%s) for url (%s) resulted in Exception (%s)", httpMethod, url, e));\r
+        }\r
+        return restResponse;\r
+    }\r
+    \r
+    private <T> ResponseEntity<T> exchangeForEntity(HttpHeaders headers, String url, HttpMethod httpMethod,\r
+            Object request, Class<T> responseType) throws ConfigRestAdaptorException {\r
+        ResponseEntity<T> response = null;\r
+        \r
+        try {\r
+            if (restTemplate == null) {\r
+                logger.error(MS_INIT_FAIL);\r
+            } else {\r
+                logger.debug("Rest Operation: {}", httpMethod);\r
+                logger.debug("url    : ({})", url);\r
+                logger.debug("headers: ({})", headers);\r
+                logger.debug("request: ({})", request);\r
+                \r
+                if (HttpMethod.GET == httpMethod) {\r
+                    HttpEntity<String> entity = new HttpEntity<>("parameters", headers);\r
+                    response = restTemplate.exchange(url, httpMethod, entity, responseType);\r
+                } else {\r
+                    HttpEntity<?> entity = new HttpEntity<>(request, headers);\r
+                    response = restTemplate.exchange(url, httpMethod, entity, responseType);\r
+                }\r
+                logger.debug("response: ({})", response);\r
+                \r
+                if (response != null) {\r
+                    logger.debug("response status code: ({})", response.getStatusCode());\r
+                } else {\r
+                    throw new ConfigRestAdaptorException("exchangeForEntity failed to perform ");\r
+                }\r
+            }\r
+        } catch (Exception e) {\r
+            throw new ConfigRestAdaptorException(\r
+                    String.format("httpMethod (%s) for url (%s) resulted in Exception (%s)", httpMethod, url, e));\r
+        }\r
+        return response;\r
+    }\r
+    \r
+    protected synchronized <T> T processResponse(ResponseEntity<T> response, String url, HttpMethod httpMethod)\r
+            throws ConfigRestAdaptorException {\r
+        if (response != null) {\r
+            if ((HttpMethod.DELETE == httpMethod && (response.getStatusCode() == HttpStatus.NO_CONTENT\r
+                    || response.getStatusCode() == HttpStatus.NOT_FOUND))\r
+                    || ((HttpMethod.GET == httpMethod || HttpMethod.PUT == httpMethod || HttpMethod.POST == httpMethod)\r
+                            && (response.getStatusCode() == HttpStatus.OK\r
+                                    || response.getStatusCode() == HttpStatus.CREATED))) {\r
+                return response.getBody();\r
+            }\r
+            throw new ConfigRestAdaptorException(\r
+                    String.format("Rest Operation is failed with response-code (%s) for the URL (%s)",\r
+                            response.getStatusCode(), url));\r
+        }\r
+        throw new ConfigRestAdaptorException(String.format("Rest Operation is failed for the URL (%s)", url));\r
+    }\r
+    \r
+    protected synchronized String constructUrl(String baseUrl, String path) {\r
+        if (StringUtils.isNotBlank(path)) {\r
+            return baseUrl + path;\r
+        } else {\r
+            return baseUrl;\r
+        }\r
+    }\r
+    \r
+}\r
diff --git a/blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/service/ConfigRestAdaptorService.java b/blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/service/ConfigRestAdaptorService.java
new file mode 100644 (file)
index 0000000..e15f4ae
--- /dev/null
@@ -0,0 +1,89 @@
+/*\r
+ * Copyright © 2017-2018 AT&T Intellectual Property.\r
+ * \r
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except\r
+ * in compliance with the License. You may obtain a copy of the License at\r
+ * \r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ * \r
+ * Unless required by applicable law or agreed to in writing, software distributed under the License\r
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express\r
+ * or implied. See the License for the specific language governing permissions and limitations under\r
+ * the License.\r
+ */\r
+\r
+package org.onap.ccsdk.config.rest.adaptor.service;\r
+\r
+import org.onap.ccsdk.config.rest.adaptor.ConfigRestAdaptorException;\r
+import org.onap.ccsdk.config.rest.adaptor.data.RestResponse;\r
+\r
+public interface ConfigRestAdaptorService {\r
+    \r
+    /**\r
+     * Retrieve an entity by doing a GET on the specified URL. The response is converted and stored in\r
+     * defined responseType.\r
+     * \r
+     * @param selectorName the property selector\r
+     * @param path the URI path which will append in baseURL mentioned in selector property\r
+     * @param responseType the type of the return value\r
+     */\r
+    public <T> T getResource(String selectorName, String path, Class<T> responseType) throws ConfigRestAdaptorException;\r
+    \r
+    /**\r
+     * Create a new resource by POSTing the given object to the URI template, and returns the response\r
+     * as defined responseType\r
+     * \r
+     * @param selectorName the property selector\r
+     * @param path the URI path which will append in baseURL mentioned in selector property\r
+     * @param request the Object to be POSTed, may be {@code null}\r
+     * @param responseType the type of the return value\r
+     */\r
+    public <T> T postResource(String selectorName, String path, Object request, Class<T> responseType)\r
+            throws ConfigRestAdaptorException;\r
+    \r
+    /**\r
+     * Execute the HTTP method to the given URI template, writing the given request entity to the\r
+     * request, and returns the response as defined responseType\r
+     * \r
+     * @param selectorName the property selector\r
+     * @param path the URI path which will append in baseURL mentioned in selector property\r
+     * @param request the Object to be POSTed, may be {@code null}\r
+     * @param responseType the type of the return value\r
+     * @param method the HTTP method (GET, POST, etc)\r
+     */\r
+    public <T> T exchangeResource(String selectorName, String path, Object request, Class<T> responseType,\r
+            String method) throws ConfigRestAdaptorException;\r
+    \r
+    /**\r
+     * Retrieve an entity by doing a GET on the specified URL. The response is converted and stored in\r
+     * defined responseType.\r
+     * \r
+     * @param selectorName the property selector\r
+     * @param path the URI path which will append in baseURL mentioned in selector property\r
+     */\r
+    public RestResponse getResource(String selectorName, String path) throws ConfigRestAdaptorException;\r
+    \r
+    /**\r
+     * Create a new resource by POSTing the given object to the URI template, and returns the response\r
+     * as defined responseType\r
+     * \r
+     * @param selectorName the property selector\r
+     * @param path the URI path which will append in baseURL mentioned in selector property\r
+     * @param request the Object to be POSTed, may be {@code null}\r
+     */\r
+    public RestResponse postResource(String selectorName, String path, Object request)\r
+            throws ConfigRestAdaptorException;\r
+    \r
+    /**\r
+     * Execute the HTTP method to the given URI template, writing the given request entity to the\r
+     * request, and returns the response as defined responseType\r
+     * \r
+     * @param selectorName the property selector\r
+     * @param path the URI path which will append in baseURL mentioned in selector property\r
+     * @param request the Object to be POSTed, may be {@code null}\r
+     * @param method the HTTP method (GET, POST, etc)\r
+     */\r
+    public RestResponse exchangeResource(String selectorName, String path, Object request, String method)\r
+            throws ConfigRestAdaptorException;\r
+    \r
+}\r
diff --git a/blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/service/ConfigRestAdaptorServiceImpl.java b/blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/service/ConfigRestAdaptorServiceImpl.java
new file mode 100644 (file)
index 0000000..b17ed22
--- /dev/null
@@ -0,0 +1,142 @@
+/*\r
+ * Copyright © 2017-2018 AT&T Intellectual Property.\r
+ * \r
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except\r
+ * in compliance with the License. You may obtain a copy of the License at\r
+ * \r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ * \r
+ * Unless required by applicable law or agreed to in writing, software distributed under the License\r
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express\r
+ * or implied. See the License for the specific language governing permissions and limitations under\r
+ * the License.\r
+ */\r
+\r
+package org.onap.ccsdk.config.rest.adaptor.service;\r
+\r
+import java.io.File;\r
+import java.io.FileInputStream;\r
+import java.util.Map;\r
+import java.util.Properties;\r
+import java.util.concurrent.ConcurrentHashMap;\r
+import java.util.concurrent.Executors;\r
+import java.util.concurrent.ScheduledExecutorService;\r
+import java.util.concurrent.TimeUnit;\r
+import java.util.stream.Collectors;\r
+import org.apache.commons.lang3.StringUtils;\r
+import org.onap.ccsdk.config.rest.adaptor.ConfigRestAdaptorConstants;\r
+import org.onap.ccsdk.config.rest.adaptor.ConfigRestAdaptorException;\r
+import org.onap.ccsdk.config.rest.adaptor.data.RestResponse;\r
+import com.att.eelf.configuration.EELFLogger;\r
+import com.att.eelf.configuration.EELFManager;\r
+\r
+public class ConfigRestAdaptorServiceImpl implements ConfigRestAdaptorService {\r
+    \r
+    private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigRestAdaptorServiceImpl.class);\r
+    private Map<String, String> restProperties = new ConcurrentHashMap<>();\r
+    \r
+    public ConfigRestAdaptorServiceImpl(String propertyPath) {\r
+        initializeProperties(propertyPath);\r
+        try {\r
+            String envType = restProperties.get(ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY\r
+                    + ConfigRestAdaptorConstants.REST_ADAPTOR_ENV_TYPE);\r
+            \r
+            if (!(ConfigRestAdaptorConstants.PROPERTY_ENV_PROD.equalsIgnoreCase(envType)\r
+                    || ConfigRestAdaptorConstants.PROPERTY_ENV_SOLO.equalsIgnoreCase(envType))) {\r
+                ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);\r
+                Runnable task = () -> {\r
+                    initializeProperties(propertyPath);\r
+                };\r
+                executor.scheduleWithFixedDelay(task, 60, 15, TimeUnit.MINUTES);\r
+            }\r
+        } catch (Exception e) {\r
+            logger.error(e.getMessage(), e);\r
+        }\r
+    }\r
+    \r
+    private void initializeProperties(String propertyPath) {\r
+        logger.trace("Initialising Config rest adaptor Service with property directory ({})", propertyPath);\r
+        try {\r
+            if (StringUtils.isBlank(propertyPath)) {\r
+                propertyPath = System.getProperty(ConfigRestAdaptorConstants.SDNC_ROOT_DIR_ENV_VAR_KEY);\r
+            }\r
+            \r
+            if (StringUtils.isBlank(propertyPath)) {\r
+                throw new ConfigRestAdaptorException(\r
+                        String.format("Failed to get the property directory (%s)", propertyPath));\r
+            }\r
+            \r
+            // Loading Default config-rest-adaptor.properties\r
+            String propertyFile =\r
+                    propertyPath + File.separator + ConfigRestAdaptorConstants.REST_ADAPTOR_PROPERTIES_FILE_NAME;\r
+            \r
+            Properties properties = new Properties();\r
+            properties.load(new FileInputStream(propertyFile));\r
+            \r
+            logger.trace("Initializing properties details for property file ({}) properties ({})", propertyFile,\r
+                    properties);\r
+            restProperties.putAll(properties.entrySet().stream()\r
+                    .collect(Collectors.toMap(e -> e.getKey().toString(), e -> e.getValue().toString())));\r
+            \r
+        } catch (Exception e) {\r
+            logger.error(e.getMessage(), e);\r
+        }\r
+    }\r
+    \r
+    @Override\r
+    public <T> T getResource(String serviceSelector, String path, Class<T> responseType)\r
+            throws ConfigRestAdaptorException {\r
+        return getRestClientAdapterBySelectorName(serviceSelector).getResource(path, responseType);\r
+    }\r
+    \r
+    @Override\r
+    public <T> T postResource(String serviceSelector, String path, Object request, Class<T> responseType)\r
+            throws ConfigRestAdaptorException {\r
+        return getRestClientAdapterBySelectorName(serviceSelector).postResource(path, request, responseType);\r
+    }\r
+    \r
+    @Override\r
+    public <T> T exchangeResource(String serviceSelector, String path, Object request, Class<T> responseType,\r
+            String method) throws ConfigRestAdaptorException {\r
+        return getRestClientAdapterBySelectorName(serviceSelector).exchangeResource(path, request, responseType,\r
+                method);\r
+    }\r
+    \r
+    @Override\r
+    public RestResponse getResource(String serviceSelector, String path) throws ConfigRestAdaptorException {\r
+        return getRestClientAdapterBySelectorName(serviceSelector).getResource(path);\r
+    }\r
+    \r
+    @Override\r
+    public RestResponse postResource(String serviceSelector, String path, Object request)\r
+            throws ConfigRestAdaptorException {\r
+        return getRestClientAdapterBySelectorName(serviceSelector).postResource(path, request);\r
+    }\r
+    \r
+    @Override\r
+    public RestResponse exchangeResource(String serviceSelector, String path, Object request, String method)\r
+            throws ConfigRestAdaptorException {\r
+        return getRestClientAdapterBySelectorName(serviceSelector).exchangeResource(path, request, method);\r
+    }\r
+    \r
+    private ConfigRestClientServiceAdapter getRestClientAdapterBySelectorName(String serviceSelector)\r
+            throws ConfigRestAdaptorException {\r
+        String adoptorType = restProperties.get(ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY + serviceSelector\r
+                + ConfigRestAdaptorConstants.SERVICE_TYPE_PROPERTY);\r
+        if (StringUtils.isNotBlank(adoptorType)) {\r
+            if (ConfigRestAdaptorConstants.REST_ADAPTOR_TYPE_GENERIC.equalsIgnoreCase(adoptorType)) {\r
+                return new GenericRestClientAdapterImpl(restProperties, serviceSelector);\r
+            } else if (ConfigRestAdaptorConstants.REST_ADAPTOR_TYPE_SSL.equalsIgnoreCase(adoptorType)) {\r
+                return new SSLRestClientAdapterImpl(restProperties, serviceSelector);\r
+            } else {\r
+                throw new ConfigRestAdaptorException(\r
+                        String.format("no implementation for rest adoptor type (%s) for the selector (%s).",\r
+                                adoptorType, serviceSelector));\r
+            }\r
+        } else {\r
+            throw new ConfigRestAdaptorException(\r
+                    String.format("couldn't get rest adoptor type for the selector (%s)", serviceSelector));\r
+        }\r
+    }\r
+    \r
+}\r
diff --git a/blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/service/ConfigRestClientServiceAdapter.java b/blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/service/ConfigRestClientServiceAdapter.java
new file mode 100644 (file)
index 0000000..e1adb38
--- /dev/null
@@ -0,0 +1,35 @@
+/*\r
+ * Copyright © 2017-2018 AT&T Intellectual Property.\r
+ * \r
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except\r
+ * in compliance with the License. You may obtain a copy of the License at\r
+ * \r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ * \r
+ * Unless required by applicable law or agreed to in writing, software distributed under the License\r
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express\r
+ * or implied. See the License for the specific language governing permissions and limitations under\r
+ * the License.\r
+ */\r
+\r
+package org.onap.ccsdk.config.rest.adaptor.service;\r
+\r
+import org.onap.ccsdk.config.rest.adaptor.ConfigRestAdaptorException;\r
+import org.onap.ccsdk.config.rest.adaptor.data.RestResponse;\r
+\r
+interface ConfigRestClientServiceAdapter {\r
+    \r
+    public <T> T getResource(String path, Class<T> responseType) throws ConfigRestAdaptorException;\r
+    \r
+    public <T> T postResource(String path, Object request, Class<T> responseType) throws ConfigRestAdaptorException;\r
+    \r
+    public <T> T exchangeResource(String path, Object request, Class<T> responseType, String method)\r
+            throws ConfigRestAdaptorException;\r
+    \r
+    public RestResponse getResource(String path) throws ConfigRestAdaptorException;\r
+    \r
+    public RestResponse postResource(String path, Object request) throws ConfigRestAdaptorException;\r
+    \r
+    public RestResponse exchangeResource(String path, Object request, String method) throws ConfigRestAdaptorException;\r
+    \r
+}\r
diff --git a/blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/service/GenericRestClientAdapterImpl.java b/blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/service/GenericRestClientAdapterImpl.java
new file mode 100644 (file)
index 0000000..55cdca4
--- /dev/null
@@ -0,0 +1,136 @@
+/*\r
+ * Copyright © 2017-2018 AT&T Intellectual Property.\r
+ * \r
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except\r
+ * in compliance with the License. You may obtain a copy of the License at\r
+ * \r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ * \r
+ * Unless required by applicable law or agreed to in writing, software distributed under the License\r
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express\r
+ * or implied. See the License for the specific language governing permissions and limitations under\r
+ * the License.\r
+ */\r
+\r
+package org.onap.ccsdk.config.rest.adaptor.service;\r
+\r
+import java.util.Map;\r
+import java.util.UUID;\r
+import org.apache.commons.lang3.StringUtils;\r
+import org.onap.ccsdk.config.rest.adaptor.ConfigRestAdaptorConstants;\r
+import org.onap.ccsdk.config.rest.adaptor.ConfigRestAdaptorException;\r
+import org.onap.ccsdk.config.rest.adaptor.data.RestResponse;\r
+import org.springframework.http.HttpHeaders;\r
+import org.springframework.http.MediaType;\r
+import com.att.eelf.configuration.EELFLogger;\r
+import com.att.eelf.configuration.EELFManager;\r
+\r
+public class GenericRestClientAdapterImpl extends AbstractConfigRestClientAdapter {\r
+    \r
+    private static EELFLogger logger = EELFManager.getInstance().getLogger(GenericRestClientAdapterImpl.class);\r
+    private String baseUrl = "";\r
+    \r
+    public GenericRestClientAdapterImpl(Map<String, String> properties, String serviceSelector)\r
+            throws ConfigRestAdaptorException {\r
+        super(properties, serviceSelector);\r
+        init(serviceSelector);\r
+    }\r
+    \r
+    private void init(String serviceSelector) throws ConfigRestAdaptorException {\r
+        try {\r
+            if (isRestClientServiceAdapaterEnabled) {\r
+                String baseUrlProperty = ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY + serviceSelector\r
+                        + ConfigRestAdaptorConstants.SERVICE_BASEURL_PROPERTY;\r
+                String userProperty = ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY + serviceSelector\r
+                        + ConfigRestAdaptorConstants.SERVICE_USER_PROPERTY;\r
+                String passProperty = ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY + serviceSelector\r
+                        + ConfigRestAdaptorConstants.SERVICE_PSSWD_PROPERTY;\r
+                \r
+                baseUrl = properties.get(baseUrlProperty);\r
+                String userId = properties.get(userProperty);\r
+                String pass = properties.get(passProperty);\r
+                \r
+                initialise(userId, pass);\r
+                logger.info("Initialised restconf adaptor service for selector ({})", serviceSelector);\r
+                if (restTemplate == null) {\r
+                    throw new ConfigRestAdaptorException("couldn't initialise rest selector (" + serviceSelector + ")");\r
+                }\r
+            } else {\r
+                throw new ConfigRestAdaptorException("rest selector (" + serviceSelector + ") is not enabled");\r
+            }\r
+            \r
+        } catch (Exception e) {\r
+            throw new ConfigRestAdaptorException("GenericRestClientServiceAdapter : " + e.getMessage(), e);\r
+        }\r
+    }\r
+    \r
+    @Override\r
+    public <T> T getResource(String path, Class<T> responseType) throws ConfigRestAdaptorException {\r
+        return super.getResource(formHttpHeaders(), constructUrl(baseUrl, path), responseType);\r
+    }\r
+    \r
+    @Override\r
+    public <T> T postResource(String path, Object request, Class<T> responseType) throws ConfigRestAdaptorException {\r
+        return super.postResource(formHttpHeaders(), constructUrl(baseUrl, path), request, responseType);\r
+    }\r
+    \r
+    @Override\r
+    public <T> T exchangeResource(String path, Object request, Class<T> responseType, String method)\r
+            throws ConfigRestAdaptorException {\r
+        return super.exchangeResource(formHttpHeaders(), constructUrl(baseUrl, path), request, responseType, method);\r
+    }\r
+    \r
+    @Override\r
+    public RestResponse getResource(String path) throws ConfigRestAdaptorException {\r
+        return super.getResource(formHttpHeaders(), constructUrl(baseUrl, path));\r
+    }\r
+    \r
+    @Override\r
+    public RestResponse postResource(String path, Object request) throws ConfigRestAdaptorException {\r
+        return super.postResource(formHttpHeaders(), constructUrl(baseUrl, path), request);\r
+    }\r
+    \r
+    @Override\r
+    public RestResponse exchangeResource(String path, Object request, String method) throws ConfigRestAdaptorException {\r
+        return super.exchangeResource(formHttpHeaders(), constructUrl(baseUrl, path), request, method);\r
+    }\r
+    \r
+    private HttpHeaders formHttpHeaders() {\r
+        \r
+        HttpHeaders headers = new HttpHeaders();\r
+        \r
+        headers.setContentType(MediaType.APPLICATION_JSON);\r
+        headers.add("Accept", MediaType.APPLICATION_JSON_VALUE);\r
+        headers.add("X-TransactionId", generateUUID());\r
+        headers.add("X-ECOMP-RequestID", headers.getFirst("X-TransactionId"));\r
+        \r
+        String appIDPath = ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY + serviceSelector\r
+                + ConfigRestAdaptorConstants.SERVICE_APPID_PROPERTY;\r
+        String environmentPath = ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY + serviceSelector\r
+                + ConfigRestAdaptorConstants.SERVICE_ENV_PROPERTY;\r
+        String clientAuthPath = ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY + serviceSelector\r
+                + ConfigRestAdaptorConstants.SERVICE_CLIENTAUTH_PROPERTY;\r
+        String authorizationPath = ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY + serviceSelector\r
+                + ConfigRestAdaptorConstants.SERVICE_AUTHORIZATION_PROPERTY;\r
+        \r
+        if (StringUtils.isNotBlank(properties.get(appIDPath))) {\r
+            headers.add("X-FromAppId", properties.get(appIDPath));\r
+        }\r
+        if (StringUtils.isNotBlank(properties.get(clientAuthPath))) {\r
+            headers.add("ClientAuth", properties.get(clientAuthPath));\r
+        }\r
+        if (StringUtils.isNotBlank(properties.get(authorizationPath))) {\r
+            headers.add("Authorization", properties.get(authorizationPath));\r
+        }\r
+        if (StringUtils.isNotBlank(properties.get(environmentPath))) {\r
+            headers.add("Environment", properties.get(environmentPath));\r
+        }\r
+        \r
+        return headers;\r
+    }\r
+    \r
+    private synchronized String generateUUID() {\r
+        return UUID.randomUUID().toString();\r
+    }\r
+    \r
+}\r
diff --git a/blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/service/SSLRestClientAdapterImpl.java b/blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/service/SSLRestClientAdapterImpl.java
new file mode 100644 (file)
index 0000000..7d31b81
--- /dev/null
@@ -0,0 +1,127 @@
+/*\r
+ * Copyright © 2017-2018 AT&T Intellectual Property.\r
+ * \r
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except\r
+ * in compliance with the License. You may obtain a copy of the License at\r
+ * \r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ * \r
+ * Unless required by applicable law or agreed to in writing, software distributed under the License\r
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express\r
+ * or implied. See the License for the specific language governing permissions and limitations under\r
+ * the License.\r
+ */\r
+\r
+package org.onap.ccsdk.config.rest.adaptor.service;\r
+\r
+import java.util.Map;\r
+import java.util.UUID;\r
+import org.onap.ccsdk.config.rest.adaptor.ConfigRestAdaptorConstants;\r
+import org.onap.ccsdk.config.rest.adaptor.ConfigRestAdaptorException;\r
+import org.onap.ccsdk.config.rest.adaptor.data.RestResponse;\r
+import org.springframework.http.HttpHeaders;\r
+import org.springframework.http.MediaType;\r
+import com.att.eelf.configuration.EELFLogger;\r
+import com.att.eelf.configuration.EELFManager;\r
+\r
+public class SSLRestClientAdapterImpl extends AbstractConfigRestClientAdapter {\r
+    \r
+    private static EELFLogger logger = EELFManager.getInstance().getLogger(SSLRestClientAdapterImpl.class);\r
+    private String baseUrl = "";\r
+    private String application = "";\r
+    \r
+    public SSLRestClientAdapterImpl(Map<String, String> properties, String serviceSelector)\r
+            throws ConfigRestAdaptorException {\r
+        super(properties, serviceSelector);\r
+        init(serviceSelector);\r
+    }\r
+    \r
+    private void init(String serviceSelector) throws ConfigRestAdaptorException {\r
+        try {\r
+            if (isSSLServiceAdapaterEnabled) {\r
+                \r
+                logger.info("Initializing SSL client for selector ({}), properties ({})", serviceSelector, properties);\r
+                \r
+                String baseUrlProp = ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY + serviceSelector\r
+                        + ConfigRestAdaptorConstants.SSL_SERVICE_BASEURL;\r
+                String applicationProp = ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY + serviceSelector\r
+                        + ConfigRestAdaptorConstants.SSL_SERVICE_APP;\r
+                String keyStorePathProp = ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY + serviceSelector\r
+                        + ConfigRestAdaptorConstants.SSL_SERVICE_KEY;\r
+                String keyStorePassProp = ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY + serviceSelector\r
+                        + ConfigRestAdaptorConstants.SSL_SERVICE_KEY_PSSWD;\r
+                String trustStorePathProp = ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY + serviceSelector\r
+                        + ConfigRestAdaptorConstants.SSL_SERVICE_TRUST;\r
+                String trustStorePassProp = ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY + serviceSelector\r
+                        + ConfigRestAdaptorConstants.SSL_SERVICE_TRUST_PSSWD;\r
+                \r
+                baseUrl = properties.get(baseUrlProp);\r
+                application = properties.get(applicationProp);\r
+                \r
+                String keyStorePath = properties.get(keyStorePathProp);\r
+                String trustStorePath = properties.get(trustStorePathProp);\r
+                String keyStorePass = properties.get(keyStorePassProp);\r
+                String trustStorePass = properties.get(trustStorePassProp);\r
+                \r
+                initialiseSSL(keyStorePath, trustStorePath, keyStorePass, trustStorePass);\r
+                logger.info("Initialised SSL Client Service adaptor service for selector ({})", serviceSelector);\r
+                if (restTemplate == null) {\r
+                    throw new ConfigRestAdaptorException(\r
+                            "couldn't initialise SSL Client selector (" + serviceSelector + ")");\r
+                }\r
+            } else {\r
+                throw new ConfigRestAdaptorException("SSL Client selector (" + serviceSelector + ") is not enabled");\r
+            }\r
+            \r
+        } catch (Exception e) {\r
+            throw new ConfigRestAdaptorException("SSLRestClientAdapterImpl : " + e.getMessage(), e);\r
+        }\r
+    }\r
+    \r
+    @Override\r
+    public <T> T getResource(String path, Class<T> responseType) throws ConfigRestAdaptorException {\r
+        return super.getResource(formHttpHeaders(), constructUrl(baseUrl, path), responseType);\r
+    }\r
+    \r
+    @Override\r
+    public <T> T postResource(String path, Object request, Class<T> responseType) throws ConfigRestAdaptorException {\r
+        return super.postResource(formHttpHeaders(), constructUrl(baseUrl, path), request, responseType);\r
+    }\r
+    \r
+    @Override\r
+    public <T> T exchangeResource(String path, Object request, Class<T> responseType, String method)\r
+            throws ConfigRestAdaptorException {\r
+        return super.exchangeResource(formHttpHeaders(), constructUrl(baseUrl, path), request, responseType, method);\r
+    }\r
+    \r
+    @Override\r
+    public RestResponse getResource(String path) throws ConfigRestAdaptorException {\r
+        return super.getResource(formHttpHeaders(), constructUrl(baseUrl, path));\r
+    }\r
+    \r
+    @Override\r
+    public RestResponse postResource(String path, Object request) throws ConfigRestAdaptorException {\r
+        return super.postResource(formHttpHeaders(), constructUrl(baseUrl, path), request);\r
+    }\r
+    \r
+    @Override\r
+    public RestResponse exchangeResource(String path, Object request, String method) throws ConfigRestAdaptorException {\r
+        return super.exchangeResource(formHttpHeaders(), constructUrl(baseUrl, path), request, method);\r
+    }\r
+    \r
+    private HttpHeaders formHttpHeaders() {\r
+        \r
+        HttpHeaders headers = new HttpHeaders();\r
+        \r
+        headers.setContentType(MediaType.APPLICATION_JSON);\r
+        headers.add("Accept", MediaType.APPLICATION_JSON_VALUE);\r
+        headers.add("X-FromAppId", application);\r
+        headers.add("X-TransactionId", generateUUID());\r
+        \r
+        return headers;\r
+    }\r
+    \r
+    private synchronized String generateUUID() {\r
+        return UUID.randomUUID().toString();\r
+    }\r
+}\r
diff --git a/blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/utils/BasicAuthorizationInterceptor.java b/blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/utils/BasicAuthorizationInterceptor.java
new file mode 100644 (file)
index 0000000..bc486f4
--- /dev/null
@@ -0,0 +1,53 @@
+/*\r
+ * Copyright © 2017-2018 AT&T Intellectual Property.\r
+ * \r
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except\r
+ * in compliance with the License. You may obtain a copy of the License at\r
+ * \r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ * \r
+ * Unless required by applicable law or agreed to in writing, software distributed under the License\r
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express\r
+ * or implied. See the License for the specific language governing permissions and limitations under\r
+ * the License.\r
+ */\r
+\r
+package org.onap.ccsdk.config.rest.adaptor.utils;\r
+\r
+import java.io.IOException;\r
+import java.nio.charset.Charset;\r
+import org.springframework.http.HttpRequest;\r
+import org.springframework.http.client.ClientHttpRequestExecution;\r
+import org.springframework.http.client.ClientHttpRequestInterceptor;\r
+import org.springframework.http.client.ClientHttpResponse;\r
+import org.springframework.util.Assert;\r
+import org.springframework.util.Base64Utils;\r
+\r
+public class BasicAuthorizationInterceptor implements ClientHttpRequestInterceptor {\r
+    private static final Charset UTF_8 = Charset.forName("UTF-8");\r
+    \r
+    private final String username;\r
+    \r
+    private final String pass;\r
+    \r
+    /**\r
+     * Create a new interceptor which adds a BASIC authorization header for the given username and pass.\r
+     *\r
+     * @param username the username to use\r
+     * @param pass the password to use\r
+     */\r
+    public BasicAuthorizationInterceptor(String username, String pass) {\r
+        Assert.hasLength(username, "Username must not be empty");\r
+        this.username = username;\r
+        this.pass = (pass != null ? pass : "");\r
+    }\r
+    \r
+    @Override\r
+    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)\r
+            throws IOException {\r
+        \r
+        String token = Base64Utils.encodeToString((this.username + ":" + this.pass).getBytes(UTF_8));\r
+        request.getHeaders().add("Authorization", "Basic " + token);\r
+        return execution.execute(request, body);\r
+    }\r
+}\r
diff --git a/blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/utils/LoggingRequestInterceptor.java b/blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/utils/LoggingRequestInterceptor.java
new file mode 100644 (file)
index 0000000..0fcc465
--- /dev/null
@@ -0,0 +1,68 @@
+/*\r
+ * Copyright © 2017-2018 AT&T Intellectual Property.\r
+ * \r
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except\r
+ * in compliance with the License. You may obtain a copy of the License at\r
+ * \r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ * \r
+ * Unless required by applicable law or agreed to in writing, software distributed under the License\r
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express\r
+ * or implied. See the License for the specific language governing permissions and limitations under\r
+ * the License.\r
+ */\r
+\r
+package org.onap.ccsdk.config.rest.adaptor.utils;\r
+\r
+import java.io.BufferedReader;\r
+import java.io.IOException;\r
+import java.io.InputStreamReader;\r
+import org.springframework.http.HttpRequest;\r
+import org.springframework.http.client.ClientHttpRequestExecution;\r
+import org.springframework.http.client.ClientHttpRequestInterceptor;\r
+import org.springframework.http.client.ClientHttpResponse;\r
+import com.att.eelf.configuration.EELFLogger;\r
+import com.att.eelf.configuration.EELFManager;\r
+\r
+public class LoggingRequestInterceptor implements ClientHttpRequestInterceptor {\r
+    \r
+    private static EELFLogger logger = EELFManager.getInstance().getLogger(LoggingRequestInterceptor.class);\r
+    \r
+    @Override\r
+    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)\r
+            throws IOException {\r
+        traceRequest(request, body);\r
+        ClientHttpResponse response = execution.execute(request, body);\r
+        traceResponse(response);\r
+        return response;\r
+    }\r
+    \r
+    @SuppressWarnings({"squid:S2629", "squid:S3457"})\r
+    private void traceRequest(HttpRequest request, byte[] body) throws IOException {\r
+        logger.info("===========================request begin================================================");\r
+        logger.info("URI         : {}", request.getURI());\r
+        logger.info("Method      : {}", request.getMethod());\r
+        logger.info("Headers     : {}", request.getHeaders());\r
+        logger.info("Request body: {}", new String(body, "UTF-8"));\r
+        logger.debug("==========================request end================================================");\r
+    }\r
+    \r
+    @SuppressWarnings({"squid:S2629", "squid:S3457"})\r
+    private void traceResponse(ClientHttpResponse response) throws IOException {\r
+        StringBuilder inputStringBuilder = new StringBuilder();\r
+        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getBody(), "UTF-8"));\r
+        String line = bufferedReader.readLine();\r
+        while (line != null) {\r
+            inputStringBuilder.append(line);\r
+            inputStringBuilder.append('\n');\r
+            line = bufferedReader.readLine();\r
+        }\r
+        logger.info("============================response begin==========================================");\r
+        logger.info("Status code  : {}", response.getStatusCode());\r
+        logger.info("Status text  : {}", response.getStatusText());\r
+        logger.info("Headers      : {}", response.getHeaders());\r
+        logger.debug("Response body: {}", inputStringBuilder.toString());\r
+        logger.debug("=======================response end=================================================");\r
+    }\r
+    \r
+}\r
diff --git a/blueprints-processor/adaptors/rest-adaptor-provider/src/main/resources/org/opendaylight/blueprint/impl-blueprint.xml b/blueprints-processor/adaptors/rest-adaptor-provider/src/main/resources/org/opendaylight/blueprint/impl-blueprint.xml
new file mode 100644 (file)
index 0000000..e6b2694
--- /dev/null
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8"?>\r
+<!--\r
+  Copyright © 2017-2018 AT&T Intellectual Property.\r
+  \r
+  Licensed under the Apache License, Version 2.0 (the "License");\r
+  you may not use this file except in compliance with the License.\r
+  You may obtain a copy of the License at\r
+  \r
+      http://www.apache.org/licenses/LICENSE-2.0\r
+  \r
+  Unless required by applicable law or agreed to in writing, software\r
+  distributed under the License is distributed on an "AS IS" BASIS,\r
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+  See the License for the specific language governing permissions and\r
+  limitations under the License.\r
+-->\r
+\r
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"\r
+       xmlns:odl="http://opendaylight.org/xmlns/blueprint/v1.0.0"\r
+       odl:use-default-for-reference-types="true">\r
+\r
+       <!-- Expose the Common Component Bean -->\r
+\r
+       <bean id="configRestAdaptorService"\r
+               class="org.onap.ccsdk.config.rest.adaptor.service.ConfigRestAdaptorServiceImpl">\r
+               <argument value="/opt/sdnc/data/properties" />\r
+       </bean>\r
+\r
+       <service ref="configRestAdaptorService"\r
+               interface="org.onap.ccsdk.config.rest.adaptor.service.ConfigRestAdaptorService" />\r
+\r
+</blueprint>\r