From 20040441d76b9523832a20d9308e7345c0eb9061 Mon Sep 17 00:00:00 2001 From: "RamaPrasad Amaranarayana (ra5425)" Date: Wed, 19 Sep 2018 18:42:40 -0400 Subject: Change Management Schedule Optimization Adding CMSO Service Code for Change Management Schedule Optimization Change-Id: Idac29c1f315c957851c1b0369fd7fb44804c9255 Issue-ID: OPTFRA-353 Signed-off-by: RamaPrasad Amaranarayana (ra5425) --- .../main/java/org/onap/optf/cmso/Application.java | 109 +++++++++++++++++ .../optf/cmso/AutowiringSpringBeanJobFactory.java | 67 +++++++++++ .../optf/cmso/CMSEnvironmentPostProcessor.java | 58 +++++++++ .../org/onap/optf/cmso/CMSQuartzConfiguration.java | 129 +++++++++++++++++++++ .../org/onap/optf/cmso/JerseyConfiguration.java | 106 +++++++++++++++++ 5 files changed, 469 insertions(+) create mode 100644 cmso-service/src/main/java/org/onap/optf/cmso/Application.java create mode 100644 cmso-service/src/main/java/org/onap/optf/cmso/AutowiringSpringBeanJobFactory.java create mode 100644 cmso-service/src/main/java/org/onap/optf/cmso/CMSEnvironmentPostProcessor.java create mode 100644 cmso-service/src/main/java/org/onap/optf/cmso/CMSQuartzConfiguration.java create mode 100644 cmso-service/src/main/java/org/onap/optf/cmso/JerseyConfiguration.java (limited to 'cmso-service/src/main/java') diff --git a/cmso-service/src/main/java/org/onap/optf/cmso/Application.java b/cmso-service/src/main/java/org/onap/optf/cmso/Application.java new file mode 100644 index 0000000..7327124 --- /dev/null +++ b/cmso-service/src/main/java/org/onap/optf/cmso/Application.java @@ -0,0 +1,109 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * Modifications Copyright © 2018 IBM. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. +*/ + +package org.onap.optf.cmso; + +import java.net.InetAddress; +import java.util.TimeZone; +import javax.annotation.PostConstruct; +import org.apache.catalina.connector.Connector; +import org.onap.optf.cmso.common.LogMessages; +import org.slf4j.MDC; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; +import org.springframework.boot.web.servlet.server.ServletWebServerFactory; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.scheduling.annotation.EnableAsync; +import com.att.eelf.configuration.Configuration; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; + +@SpringBootApplication +@ComponentScan(basePackages = {"org.onap.optf.cmso"}) +@EnableAsync +@EnableAutoConfiguration(exclude = { /*DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class*/ }) +public class Application extends SpringBootServletInitializer { + + private static EELFLogger log = EELFManager.getInstance().getLogger(Application.class); + + @Override + protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { + return application.sources(Application.class); + } + + @Value("${server.dispatchPort:8089}") + private String dispatchPort; + + @PostConstruct + void started() { + // Make sure all datetimes are stored in UTC format. + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + } + + public static void main(String[] args) { + initMDCData(); + SpringApplication.run(Application.class, args); + } + + protected static void initMDCData() { + MDC.clear(); + try { + MDC.put(Configuration.MDC_SERVER_FQDN, InetAddress.getLocalHost().getHostName()); + MDC.put("hostname", InetAddress.getLocalHost().getCanonicalHostName()); + MDC.put("serviceName", System.getProperty("info.build.artifact")); + MDC.put("version", System.getProperty("info.build.version")); + MDC.put(Configuration.MDC_SERVER_IP_ADDRESS, InetAddress.getLocalHost().getHostAddress()); + } catch (Exception e) { + log.error(LogMessages.UNEXPECTED_EXCEPTION, e, e.getMessage()); + } + } + + @Bean + public ServletWebServerFactory servletContainer() { + TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory(); + int port = Integer.parseInt(dispatchPort); + tomcat.addAdditionalTomcatConnectors(createStandardConnector(port)); + return tomcat; + } + + private Connector createStandardConnector(int port) { + Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); + connector.setScheme("http"); + connector.setPort(port); + return connector; + } +} diff --git a/cmso-service/src/main/java/org/onap/optf/cmso/AutowiringSpringBeanJobFactory.java b/cmso-service/src/main/java/org/onap/optf/cmso/AutowiringSpringBeanJobFactory.java new file mode 100644 index 0000000..aad3104 --- /dev/null +++ b/cmso-service/src/main/java/org/onap/optf/cmso/AutowiringSpringBeanJobFactory.java @@ -0,0 +1,67 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * Modifications Copyright © 2018 IBM. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. +*/ + +package org.onap.optf.cmso; + +import org.quartz.spi.TriggerFiredBundle; +import org.springframework.beans.factory.config.AutowireCapableBeanFactory; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.scheduling.quartz.SpringBeanJobFactory; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; + +/** + * This class makes it possible to use @Autowired references in QuartzJobBeans. + * + * Also enables @Autowire of the SchedulerFactoryBean the add Triggers + * for @Autowired QuartzJobBeans for ChangeManagement events. Making a big + * investment in SpringBoot Quartz. It had better work ;-) + * + */ +public final class AutowiringSpringBeanJobFactory extends SpringBeanJobFactory implements ApplicationContextAware { + private static EELFLogger log = EELFManager.getInstance().getLogger(AutowiringSpringBeanJobFactory.class); + + private transient AutowireCapableBeanFactory beanFactory; + + @Override + public void setApplicationContext(final ApplicationContext context) { + beanFactory = context.getAutowireCapableBeanFactory(); + } + + @Override + protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception { + final Object job = super.createJobInstance(bundle); + log.info("create job instance"); + beanFactory.autowireBean(job); + return job; + } +} diff --git a/cmso-service/src/main/java/org/onap/optf/cmso/CMSEnvironmentPostProcessor.java b/cmso-service/src/main/java/org/onap/optf/cmso/CMSEnvironmentPostProcessor.java new file mode 100644 index 0000000..d1ccb7e --- /dev/null +++ b/cmso-service/src/main/java/org/onap/optf/cmso/CMSEnvironmentPostProcessor.java @@ -0,0 +1,58 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * Modifications Copyright © 2018 IBM. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. +*/ + +package org.onap.optf.cmso; + +import java.util.HashMap; +import java.util.Map; +import org.onap.optf.cmso.common.PropertiesManagement; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.env.EnvironmentPostProcessor; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.MapPropertySource; +import org.springframework.core.env.MutablePropertySources; + +public class CMSEnvironmentPostProcessor implements EnvironmentPostProcessor { + + @Override + public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { + String pwd = environment.getProperty("cmso.database.password"); + if (pwd != null) { + pwd = PropertiesManagement.getDecryptedValue(pwd); + Map map = new HashMap(); + map.put("spring.datasource.password", pwd); + MapPropertySource propertySource = new MapPropertySource("abc", map); + MutablePropertySources proeprtySources = environment.getPropertySources(); + proeprtySources.addLast(propertySource); + } + } + +} diff --git a/cmso-service/src/main/java/org/onap/optf/cmso/CMSQuartzConfiguration.java b/cmso-service/src/main/java/org/onap/optf/cmso/CMSQuartzConfiguration.java new file mode 100644 index 0000000..5ae3f05 --- /dev/null +++ b/cmso-service/src/main/java/org/onap/optf/cmso/CMSQuartzConfiguration.java @@ -0,0 +1,129 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * Modifications Copyright © 2018 IBM. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. +*/ + +package org.onap.optf.cmso; + +import org.onap.optf.cmso.eventq.CmQuartzJob; +import org.onap.optf.cmso.optimizer.OptimizerQuartzJob; +import org.onap.optf.cmso.sostatus.ScheduleStatusJob; +import org.quartz.spi.JobFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; +import org.springframework.scheduling.quartz.JobDetailFactoryBean; +import org.springframework.scheduling.quartz.SchedulerFactoryBean; +import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +@Configuration +@ComponentScan({"org.onap.optf.cmso"}) +@EnableTransactionManagement +public class CMSQuartzConfiguration { + + @Autowired + Environment env; + + @Bean + public SimpleTriggerFactoryBean eventqTriggerFactoryBean() { + + Integer interval = env.getProperty("cmso.cm.polling.job.interval.ms", Integer.class, 60000); + SimpleTriggerFactoryBean stFactory = new SimpleTriggerFactoryBean(); + stFactory.setJobDetail(eventqDetailFactoryBean().getObject()); + stFactory.setStartDelay(3000); + stFactory.setRepeatInterval(interval); + // Indefinitely + return stFactory; + } + + @Bean + public JobDetailFactoryBean eventqDetailFactoryBean() { + JobDetailFactoryBean factory = new JobDetailFactoryBean(); + factory.setJobClass(CmQuartzJob.class); + return factory; + } + + @Bean + public SimpleTriggerFactoryBean statusTriggerFactoryBean() { + + Integer interval = env.getProperty("cmso.status.job.interval.ms", Integer.class, 60000); + SimpleTriggerFactoryBean stFactory = new SimpleTriggerFactoryBean(); + stFactory.setJobDetail(statusDetailFactoryBean().getObject()); + stFactory.setStartDelay(3000); + stFactory.setRepeatInterval(interval); + // Indefinitely + return stFactory; + } + + @Bean + public JobDetailFactoryBean statusDetailFactoryBean() { + JobDetailFactoryBean factory = new JobDetailFactoryBean(); + factory.setJobClass(ScheduleStatusJob.class); + return factory; + } + + @Bean + public SimpleTriggerFactoryBean optimizerTriggerFactoryBean() { + + Integer interval = env.getProperty("cmso.optimizer.job.interval.ms", Integer.class, 60000); + SimpleTriggerFactoryBean stFactory = new SimpleTriggerFactoryBean(); + stFactory.setJobDetail(optimizerDetailFactoryBean().getObject()); + stFactory.setStartDelay(3000); + stFactory.setRepeatInterval(interval); + // Indefinitely + return stFactory; + } + + @Bean + public JobDetailFactoryBean optimizerDetailFactoryBean() { + JobDetailFactoryBean factory = new JobDetailFactoryBean(); + factory.setJobClass(OptimizerQuartzJob.class); + return factory; + } + + @Bean + public JobFactory jobFactory(ApplicationContext applicationContext) { + AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory(); + jobFactory.setApplicationContext(applicationContext); + return jobFactory; + } + + @Bean + public SchedulerFactoryBean cmsoFactoryBean(JobFactory jobFactory) { + SchedulerFactoryBean cmso = new SchedulerFactoryBean(); + cmso.setJobFactory(jobFactory); + cmso.setTriggers(eventqTriggerFactoryBean().getObject(), optimizerTriggerFactoryBean().getObject(), + statusTriggerFactoryBean().getObject()); + return cmso; + } +} diff --git a/cmso-service/src/main/java/org/onap/optf/cmso/JerseyConfiguration.java b/cmso-service/src/main/java/org/onap/optf/cmso/JerseyConfiguration.java new file mode 100644 index 0000000..8d9cb60 --- /dev/null +++ b/cmso-service/src/main/java/org/onap/optf/cmso/JerseyConfiguration.java @@ -0,0 +1,106 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * Modifications Copyright © 2018 IBM. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. +*/ + +package org.onap.optf.cmso; + +import java.util.logging.Logger; +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import org.glassfish.jersey.client.ClientConfig; +import org.glassfish.jersey.logging.LoggingFeature; +import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.servlet.ServletProperties; +import org.onap.optf.cmso.dispatcher.rs.DispatcherServiceImpl; +import org.onap.optf.cmso.filters.CMSOContainerFilters; +import org.onap.optf.cmso.service.rs.AdminToolImpl; +import org.onap.optf.cmso.service.rs.CMSCallbackImpl; +import org.onap.optf.cmso.service.rs.CMSOServiceImpl; +import org.onap.optf.cmso.service.rs.HealthCheckImpl; +import org.onap.optf.cmso.test.loopback.SchedulerTestLoopbackServiceImpl; +import org.onap.optf.cmso.test.loopback.TicketMgtLoopbackServiceImpl; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Primary; +import org.springframework.stereotype.Component; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.MapperFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; + +@Component +@ApplicationPath("/") +public class JerseyConfiguration extends ResourceConfig { + private static final Logger log = Logger.getLogger(JerseyConfiguration.class.getName()); + + @Bean + @Primary + public ObjectMapper objectMapper() { + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + objectMapper.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES); + objectMapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING); + return objectMapper; + } + + @Autowired + public JerseyConfiguration( /* LogRequestFilter lrf */ ) { + register(CMSOServiceImpl.class); + register(CMSCallbackImpl.class); + register(SchedulerTestLoopbackServiceImpl.class); + register(TicketMgtLoopbackServiceImpl.class); + register(HealthCheckImpl.class); + register(AdminToolImpl.class); + register(DispatcherServiceImpl.class); + property(ServletProperties.FILTER_FORWARD_ON_404, true); + // TODO: ONAP Conversion identify appropriate ONAP logging filters if any + // register(lrf, 6001); + // register(LogResponseFilter.class, 6004); + + // TODO: Examine which logging features to enable + register(new LoggingFeature(log)); + register(CMSOContainerFilters.class); + } + + @Bean + public Client jerseyClient() { + ClientConfig client = new ClientConfig(); + + // TODO: ONAP Conversion identify appropriate ONAP logging filters if any + // client.register(TransactionIdRequestFilter.class); + // client.register(TransactionIdResponseFilter.class); + // client.register(DateTimeParamConverterProvider.class); + + return ClientBuilder.newClient(client); + } +} -- cgit 1.2.3-korg