2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2018 - 2019 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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.vid.controller;
24 import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
26 import com.fasterxml.jackson.module.kotlin.KotlinModule;
27 import io.joshworks.restclient.http.mapper.ObjectMapper;
29 import java.util.concurrent.ExecutorService;
30 import java.util.concurrent.Executors;
31 import javax.annotation.PostConstruct;
32 import javax.servlet.ServletContext;
33 import org.apache.commons.lang3.StringUtils;
34 import org.onap.portalsdk.core.domain.App;
35 import org.onap.portalsdk.core.service.DataAccessService;
36 import org.onap.portalsdk.core.util.SystemProperties;
37 import org.onap.vid.aai.AaiClient;
38 import org.onap.vid.aai.AaiClientInterface;
39 import org.onap.vid.aai.AaiOverTLSClient;
40 import org.onap.vid.aai.AaiOverTLSClientInterface;
41 import org.onap.vid.aai.AaiOverTLSPropertySupplier;
42 import org.onap.vid.aai.AaiResponseTranslator;
43 import org.onap.vid.aai.PombaClientImpl;
44 import org.onap.vid.aai.PombaClientInterface;
45 import org.onap.vid.aai.PombaRestInterface;
46 import org.onap.vid.aai.model.PortDetailsTranslator;
47 import org.onap.vid.aai.util.AAIRestInterface;
48 import org.onap.vid.aai.util.CacheProvider;
49 import org.onap.vid.aai.util.HttpsAuthClient;
50 import org.onap.vid.aai.util.SSLContextProvider;
51 import org.onap.vid.aai.util.ServiceInstanceStandardQuery;
52 import org.onap.vid.aai.util.ServletRequestHelper;
53 import org.onap.vid.aai.util.SystemPropertyHelper;
54 import org.onap.vid.asdc.AsdcClient;
55 import org.onap.vid.asdc.parser.ToscaParserImpl2;
56 import org.onap.vid.asdc.parser.VidNotionsBuilder;
57 import org.onap.vid.asdc.rest.SdcRestClient;
58 import org.onap.vid.client.SyncRestClient;
59 import org.onap.vid.logging.VidLoggingInterceptor;
60 import org.onap.vid.properties.AsdcClientConfiguration;
61 import org.onap.vid.properties.Features;
62 import org.onap.vid.properties.VidProperties;
63 import org.onap.vid.scheduler.SchedulerService;
64 import org.onap.vid.scheduler.SchedulerServiceImpl;
65 import org.onap.vid.services.AAIServiceTree;
66 import org.onap.vid.services.AaiService;
67 import org.onap.vid.services.AaiServiceImpl;
68 import org.onap.vid.services.ChangeManagementService;
69 import org.onap.vid.services.PombaService;
70 import org.onap.vid.services.PombaServiceImpl;
71 import org.onap.vid.utils.JoshworksJacksonObjectMapper;
72 import org.onap.vid.utils.Logging;
73 import org.onap.vid.utils.SystemPropertiesWrapper;
74 import org.springframework.beans.factory.annotation.Autowired;
75 import org.springframework.beans.factory.annotation.Qualifier;
76 import org.springframework.context.annotation.Bean;
77 import org.springframework.context.annotation.Configuration;
78 import org.springframework.core.Ordered;
79 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
80 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
81 import org.togglz.core.manager.FeatureManager;
82 import springfox.documentation.builders.PathSelectors;
83 import springfox.documentation.builders.RequestHandlerSelectors;
84 import springfox.documentation.spi.DocumentationType;
85 import springfox.documentation.spring.web.plugins.Docket;
86 import springfox.documentation.swagger2.annotations.EnableSwagger2;
90 public class WebConfig implements WebMvcConfigurer {
92 @Autowired DataAccessService dataAccessService;
93 @Autowired FeatureManager featureManager;
94 String portalAppPassword = System.getenv(VidProperties.PORTAL_APP_PASSWORD_ENVIRONMENT_VARIABLE_NAME);
97 public void persistPortalAppPassword() {
98 if (featureManager.isActive(Features.FLAG_GUILIN_CONFIG_PORTAL_APP_PASSWORD)) {
99 if (StringUtils.isEmpty(portalAppPassword)) {
103 // Read self app object (i.e. where id=1) from database as AppServiceImpl.getDefaultApp() does
104 App defaultApp = (App) dataAccessService.getDomainObject(App.class, 1L, null);
106 if (defaultApp == null || StringUtils.equals(defaultApp.getAppPassword(), portalAppPassword)) {
110 defaultApp.setAppPassword(portalAppPassword);
111 dataAccessService.saveDomainObject(defaultApp, null);
117 public com.fasterxml.jackson.databind.ObjectMapper getObjectMapper() {
118 return new com.fasterxml.jackson.databind.ObjectMapper().registerModule(new KotlinModule());
123 public SchedulerService schedulerService(ChangeManagementService changeManagementService) {
124 return new SchedulerServiceImpl(changeManagementService);
128 public AaiService getAaiService(AaiClientInterface aaiClient, AaiResponseTranslator aaiResponseTranslator,
129 AAIServiceTree aaiServiceTree, Logging logging, ExecutorService executorService) {
130 return new AaiServiceImpl(aaiClient, aaiResponseTranslator, aaiServiceTree, executorService, logging);
134 public AaiResponseTranslator aaiResponseTranslator() {
135 return new AaiResponseTranslator();
139 public PortDetailsTranslator portDetailsTranslator() {
140 return new PortDetailsTranslator();
144 public AaiClientInterface getAaiRestInterface(@Qualifier("aaiRestInterface") AAIRestInterface restController, PortDetailsTranslator portsDetailsTranslator, CacheProvider cacheProvider) {
145 return new AaiClient(restController, portsDetailsTranslator, cacheProvider);
148 @Bean(name = "aaiRestInterface")
149 public AAIRestInterface aaiRestInterface(HttpsAuthClient httpsAuthClientFactory,
150 ServletRequestHelper servletRequestHelper,
151 SystemPropertyHelper systemPropertyHelper,
152 Logging loggingService) {
153 return new AAIRestInterface(httpsAuthClientFactory, servletRequestHelper, systemPropertyHelper, loggingService);
157 public PombaRestInterface getPombaRestInterface(HttpsAuthClient httpsAuthClientFactory,
158 ServletRequestHelper servletRequestHelper,
159 SystemPropertyHelper systemPropertyHelper,
160 Logging loggingService) {
161 return new PombaRestInterface(httpsAuthClientFactory, servletRequestHelper, systemPropertyHelper, loggingService);
165 public SSLContextProvider sslContextProvider() {
166 return new SSLContextProvider();
170 public SystemPropertyHelper systemPropertyHelper() {
171 return new SystemPropertyHelper();
175 public ServletRequestHelper servletRequestHelper() {
176 return new ServletRequestHelper();
180 public HttpsAuthClient httpsAuthClientFactory(ServletContext servletContext, SystemPropertyHelper systemPropertyHelper, SSLContextProvider sslContextProvider ,FeatureManager featureManager) {
181 final String certFilePath = new File(servletContext.getRealPath("/WEB-INF/cert/")).getAbsolutePath();
182 return new HttpsAuthClient(certFilePath, systemPropertyHelper, sslContextProvider, featureManager);
186 public AsdcClient sdcClient(AsdcClientConfiguration asdcClientConfiguration, Logging loggingService) {
187 String auth = asdcClientConfiguration.getAsdcClientAuth();
188 String host = asdcClientConfiguration.getAsdcClientHost();
189 String protocol = asdcClientConfiguration.getAsdcClientProtocol();
190 int port = asdcClientConfiguration.getAsdcClientPort();
192 return new SdcRestClient(protocol + "://" + host + ":" + port + "/", auth,
193 new SyncRestClient( loggingService, true),
198 public VidNotionsBuilder vidNotionsBuilder(FeatureManager featureManager) {
199 return new VidNotionsBuilder(featureManager);
203 public ToscaParserImpl2 getToscaParser(VidNotionsBuilder vidNotionsBuilder) {
204 return new ToscaParserImpl2(vidNotionsBuilder);
208 public PombaService getVerifyServiceInstanceService() {
209 return new PombaServiceImpl();
213 public PombaClientInterface getVerifyServiceInstanceClientInterface() {
214 return new PombaClientImpl();
218 public ServiceInstanceStandardQuery serviceInstanceStandardQuery(AaiClientInterface aaiClient) {
219 return new ServiceInstanceStandardQuery(aaiClient);
223 public AaiOverTLSClientInterface aaiOverTLSClient(ObjectMapper unirestObjectMapper, SystemProperties systemProperties, Logging loggingService){
224 return new AaiOverTLSClient(
225 new SyncRestClient(unirestObjectMapper, loggingService),
226 new AaiOverTLSPropertySupplier());
230 public ObjectMapper unirestFasterxmlObjectMapper() {
231 return new JoshworksJacksonObjectMapper();
236 return new Docket(DocumentationType.SWAGGER_2)
238 .apis(RequestHandlerSelectors.basePackage("org.onap.vid.controller.open"))
239 .paths(PathSelectors.any())
244 public ExecutorService executorService() {
245 int threadsCount = defaultIfNull(Integer.parseInt(SystemProperties.getProperty(VidProperties.VID_THREAD_COUNT)), 1);
246 return Executors.newFixedThreadPool(threadsCount);
250 public void addInterceptors(InterceptorRegistry registry) {
251 registry.addInterceptor(
252 new VidLoggingInterceptor(new ControllersUtils(new SystemPropertiesWrapper()))
253 ).order(Ordered.HIGHEST_PRECEDENCE);