Refactor of an AAIRestInterface
[vid.git] / vid-app-common / src / main / java / org / onap / vid / controllers / WebConfig.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2018 Nokia. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.vid.controllers;
23
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import org.onap.vid.aai.*;
26 import org.onap.vid.aai.model.PortDetailsTranslator;
27 import org.onap.vid.aai.util.*;
28 import org.onap.vid.asdc.AsdcClient;
29 import org.onap.vid.asdc.parser.ToscaParserImpl2;
30 import org.onap.vid.asdc.rest.RestfulAsdcClient;
31 import org.onap.vid.exceptions.GenericUncheckedException;
32 import org.onap.vid.properties.AsdcClientConfiguration;
33 import org.onap.vid.services.*;
34 import org.springframework.beans.factory.annotation.Qualifier;
35 import org.springframework.context.annotation.Bean;
36 import org.springframework.context.annotation.Configuration;
37 import org.togglz.core.manager.FeatureManager;
38
39 import javax.net.ssl.SSLContext;
40 import javax.servlet.ServletContext;
41 import javax.ws.rs.client.Client;
42 import javax.ws.rs.client.ClientBuilder;
43 import java.io.File;
44 import java.net.URI;
45 import java.net.URISyntaxException;
46 import java.security.KeyManagementException;
47 import java.security.NoSuchAlgorithmException;
48
49 @Configuration
50 public class WebConfig {
51
52     /**
53      * Gets the object mapper.
54      *
55      * @return the object mapper
56      */
57     @Bean
58     public ObjectMapper getObjectMapper() {
59         return new ObjectMapper();
60     }
61
62
63     @Bean
64     public VidService vidService(AsdcClient asdcClient, FeatureManager featureManager) {
65         return new VidServiceImpl(asdcClient, featureManager);
66     }
67
68     @Bean
69     public AaiService getAaiService() {
70         return new AaiServiceImpl();
71     }
72
73     @Bean
74     public AaiResponseTranslator aaiResponseTranslator() {
75         return new AaiResponseTranslator();
76     }
77
78     @Bean
79     public PortDetailsTranslator portDetailsTranslator() {
80         return new PortDetailsTranslator();
81     }
82
83     @Bean
84     public AaiClientInterface getAaiRestInterface(@Qualifier("aaiRestInterface") AAIRestInterface restController, PortDetailsTranslator portsDetailsTranslator) {
85         return new AaiClient(restController, portsDetailsTranslator);
86     }
87
88     @Bean(name = "aaiRestInterface")
89     public AAIRestInterface aaiRestInterface(HttpsAuthClient httpsAuthClientFactory, ServletRequestHelper servletRequestHelper, SystemPropertyHelper systemPropertyHelper) {
90         return new AAIRestInterface(httpsAuthClientFactory, servletRequestHelper, systemPropertyHelper);
91     }
92
93     @Bean
94     public PombaRestInterface getPombaRestInterface(HttpsAuthClient httpsAuthClientFactory, ServletRequestHelper servletRequestHelper, SystemPropertyHelper systemPropertyHelper) {
95         return new PombaRestInterface(httpsAuthClientFactory, servletRequestHelper, systemPropertyHelper);
96     }
97
98     @Bean
99     public SSLContextProvider sslContextProvider() {
100         return new SSLContextProvider();
101     }
102
103     @Bean
104     public SystemPropertyHelper systemPropertyHelper() {
105         return new SystemPropertyHelper();
106     }
107
108     @Bean
109     public ServletRequestHelper servletRequestHelper() {
110         return new ServletRequestHelper();
111     }
112
113     @Bean
114     public HttpsAuthClient httpsAuthClientFactory(ServletContext servletContext, SystemPropertyHelper systemPropertyHelper, SSLContextProvider sslContextProvider) {
115         final String certFilePath = new File(servletContext.getRealPath("/WEB-INF/cert/")).getAbsolutePath();
116         return new HttpsAuthClient(certFilePath, systemPropertyHelper, sslContextProvider);
117     }
118
119     @Bean
120     public AsdcClient asdcClient(AsdcClientConfiguration asdcClientConfig) {
121
122         final String protocol = asdcClientConfig.getAsdcClientProtocol();
123         final String host = asdcClientConfig.getAsdcClientHost();
124         final int port = asdcClientConfig.getAsdcClientPort();
125         final String auth = asdcClientConfig.getAsdcClientAuth();
126         Client cl = null;
127         if (protocol.equalsIgnoreCase("https")) {
128             try {
129                 SSLContext ctx = SSLContext.getInstance("TLSv1.2");
130                 ctx.init(null, null, null);
131                 cl = ClientBuilder.newBuilder().sslContext(ctx).build();
132             } catch (NoSuchAlgorithmException n) {
133                 throw new GenericUncheckedException("SDC Client could not be instantiated due to unsupported protocol TLSv1.2", n);
134             } catch (KeyManagementException k) {
135                 throw new GenericUncheckedException("SDC Client could not be instantiated due to a key management exception", k);
136             }
137         } else {
138             cl = ClientBuilder.newBuilder().build();
139         }
140
141         try {
142             final URI uri = new URI(protocol + "://" + host + ":" + port + "/");
143             return new RestfulAsdcClient.Builder(cl, uri)
144                     .auth(auth)
145                     .build();
146         } catch (URISyntaxException e) {
147             throw new GenericUncheckedException("SDC Client could not be instantiated due to a syntax error in the URI", e);
148         }
149     }
150
151     @Bean
152     public ToscaParserImpl2 getToscaParser() {
153         return new ToscaParserImpl2();
154     }
155
156     @Bean
157     public PombaService getVerifyServiceInstanceService() {
158         return new PombaServiceImpl();
159     }
160
161     @Bean
162     public PombaClientInterface getVerifyServiceInstanceClientInterface() {
163         return new PombaClientImpl();
164     }
165 }