cf32e92e01c6ee0ca147f91b49ca2d359740f307
[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.scheduler.SchedulerRestInterface;
34 import org.onap.vid.scheduler.SchedulerRestInterfaceIfc;
35 import org.onap.vid.services.*;
36 import org.springframework.beans.factory.annotation.Qualifier;
37 import org.springframework.context.annotation.Bean;
38 import org.springframework.context.annotation.Configuration;
39 import org.togglz.core.manager.FeatureManager;
40
41 import javax.net.ssl.SSLContext;
42 import javax.servlet.ServletContext;
43 import javax.ws.rs.client.Client;
44 import javax.ws.rs.client.ClientBuilder;
45 import java.io.File;
46 import java.net.URI;
47 import java.net.URISyntaxException;
48 import java.security.KeyManagementException;
49 import java.security.NoSuchAlgorithmException;
50
51 @Configuration
52 public class WebConfig {
53
54     /**
55      * Gets the object mapper.
56      *
57      * @return the object mapper
58      */
59     @Bean
60     public ObjectMapper getObjectMapper() {
61         return new ObjectMapper();
62     }
63
64
65     @Bean
66     public VidService vidService(AsdcClient asdcClient, FeatureManager featureManager) {
67         return new VidServiceImpl(asdcClient, featureManager);
68     }
69
70     @Bean
71     public AaiService getAaiService() {
72         return new AaiServiceImpl();
73     }
74
75     @Bean
76     public AaiResponseTranslator aaiResponseTranslator() {
77         return new AaiResponseTranslator();
78     }
79
80     @Bean
81     public PortDetailsTranslator portDetailsTranslator() {
82         return new PortDetailsTranslator();
83     }
84
85     @Bean
86     public AaiClientInterface getAaiRestInterface(@Qualifier("aaiRestInterface") AAIRestInterface restController, PortDetailsTranslator portsDetailsTranslator) {
87         return new AaiClient(restController, portsDetailsTranslator);
88     }
89
90     @Bean(name = "aaiRestInterface")
91     public AAIRestInterface aaiRestInterface(HttpsAuthClient httpsAuthClientFactory, ServletRequestHelper servletRequestHelper, SystemPropertyHelper systemPropertyHelper) {
92         return new AAIRestInterface(httpsAuthClientFactory, servletRequestHelper, systemPropertyHelper);
93     }
94
95     @Bean
96     public PombaRestInterface getPombaRestInterface(HttpsAuthClient httpsAuthClientFactory, ServletRequestHelper servletRequestHelper, SystemPropertyHelper systemPropertyHelper) {
97         return new PombaRestInterface(httpsAuthClientFactory, servletRequestHelper, systemPropertyHelper);
98     }
99
100     @Bean
101     public SSLContextProvider sslContextProvider() {
102         return new SSLContextProvider();
103     }
104
105     @Bean
106     public SystemPropertyHelper systemPropertyHelper() {
107         return new SystemPropertyHelper();
108     }
109
110     @Bean
111     public ServletRequestHelper servletRequestHelper() {
112         return new ServletRequestHelper();
113     }
114
115     @Bean
116     public HttpsAuthClient httpsAuthClientFactory(ServletContext servletContext, SystemPropertyHelper systemPropertyHelper, SSLContextProvider sslContextProvider) {
117         final String certFilePath = new File(servletContext.getRealPath("/WEB-INF/cert/")).getAbsolutePath();
118         return new HttpsAuthClient(certFilePath, systemPropertyHelper, sslContextProvider);
119     }
120
121     @Bean
122     public AsdcClient asdcClient(AsdcClientConfiguration asdcClientConfig) {
123
124         final String protocol = asdcClientConfig.getAsdcClientProtocol();
125         final String host = asdcClientConfig.getAsdcClientHost();
126         final int port = asdcClientConfig.getAsdcClientPort();
127         final String auth = asdcClientConfig.getAsdcClientAuth();
128         Client cl = null;
129         if (protocol.equalsIgnoreCase("https")) {
130             try {
131                 SSLContext ctx = SSLContext.getInstance("TLSv1.2");
132                 ctx.init(null, null, null);
133                 cl = ClientBuilder.newBuilder().sslContext(ctx).build();
134             } catch (NoSuchAlgorithmException n) {
135                 throw new GenericUncheckedException("SDC Client could not be instantiated due to unsupported protocol TLSv1.2", n);
136             } catch (KeyManagementException k) {
137                 throw new GenericUncheckedException("SDC Client could not be instantiated due to a key management exception", k);
138             }
139         } else {
140             cl = ClientBuilder.newBuilder().build();
141         }
142
143         try {
144             final URI uri = new URI(protocol + "://" + host + ":" + port + "/");
145             return new RestfulAsdcClient.Builder(cl, uri)
146                     .auth(auth)
147                     .build();
148         } catch (URISyntaxException e) {
149             throw new GenericUncheckedException("SDC Client could not be instantiated due to a syntax error in the URI", e);
150         }
151     }
152
153     @Bean
154     public ToscaParserImpl2 getToscaParser() {
155         return new ToscaParserImpl2();
156     }
157
158     @Bean
159     public PombaService getVerifyServiceInstanceService() {
160         return new PombaServiceImpl();
161     }
162
163     @Bean
164     public PombaClientInterface getVerifyServiceInstanceClientInterface() {
165         return new PombaClientImpl();
166     }
167
168     @Bean
169     public SchedulerRestInterfaceIfc getSchedulerRestInterface(){
170         return new SchedulerRestInterface();
171     }
172 }