Development of NETCONF RPCs for tr-069 adapter to
[oam/tr069-adapter.git] / netconf-server / src / main / java / org / commscope / tr069adapter / netconf / boot / NetConfServiceBooter.java
1 /*
2  * ============LICENSE_START========================================================================
3  * ONAP : tr-069-adapter
4  * =================================================================================================
5  * Copyright (C) 2020 CommScope Inc Intellectual Property.
6  * =================================================================================================
7  * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You
9  * may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14  * either express or implied. See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ===============LICENSE_END=======================================================================
17  */
18
19 package org.commscope.tr069adapter.netconf.boot;
20
21 import java.util.Arrays;
22 import javax.jms.ConnectionFactory;
23 import org.apache.activemq.ActiveMQConnectionFactory;
24 import org.apache.activemq.RedeliveryPolicy;
25 import org.apache.activemq.broker.BrokerService;
26 import org.apache.activemq.broker.region.policy.RedeliveryPolicyMap;
27 import org.apache.activemq.command.ActiveMQQueue;
28 import org.commscope.tr069adapter.netconf.server.NetConfServerManagerImpl;
29 import org.commscope.tr069adapter.netconf.server.utils.NetConfServerConstants;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.boot.SpringApplication;
33 import org.springframework.boot.autoconfigure.SpringBootApplication;
34 import org.springframework.boot.autoconfigure.domain.EntityScan;
35 import org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer;
36 import org.springframework.context.ApplicationContext;
37 import org.springframework.context.annotation.Bean;
38 import org.springframework.context.annotation.ComponentScan;
39 import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
40 import org.springframework.jms.annotation.EnableJms;
41 import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
42 import org.springframework.jms.config.JmsListenerContainerFactory;
43 import org.springframework.jms.listener.MessageListenerContainer;
44 import org.springframework.jms.support.converter.MappingJackson2MessageConverter;
45 import org.springframework.jms.support.converter.MessageConverter;
46 import org.springframework.jms.support.converter.MessageType;
47 import org.springframework.retry.annotation.EnableRetry;
48
49 @EnableJms
50 @SpringBootApplication
51 @ComponentScan({"org.commscope.tr069adapter.netconf", "org.opendaylight.netconf.test",
52     "org.commscope.tr069adapter.common"})
53 @EnableJpaRepositories("org.commscope.tr069adapter.netconf.dao")
54 @EntityScan("org.commscope.tr069adapter.netconf.entity")
55 @EnableRetry
56 public class NetConfServiceBooter {
57
58   private static final Logger LOG = LoggerFactory.getLogger(NetConfServiceBooter.class);
59
60   private static ApplicationContext appContext;
61
62   public static void main(String[] args) {
63     if (args != null)
64       appContext = SpringApplication.run(NetConfServiceBooter.class, args);
65     NetConfServerManagerImpl serverManager =
66         NetConfServiceBooter.getApplicationContext().getBean(NetConfServerManagerImpl.class);
67     boolean isSchemaLoaded = serverManager.loadSchemas();
68     if (!isSchemaLoaded) {
69       LOG.error("Loading the schema failed while starting the container");
70       System.exit(1);
71     }
72     serverManager.restartServers();
73   }
74
75   public static ApplicationContext getApplicationContext() {
76     return appContext;
77   }
78
79   /*
80    * JMS Configuration Defining the connection factories used in the application Setting the
81    * Re-delivery configuration goes here
82    */
83   @Bean
84   public BrokerService broker() throws Exception {
85     final BrokerService broker = new BrokerService();
86     broker.addConnector("tcp://localhost:61616");
87     broker.addConnector("vm://localhost");
88     broker.setPersistent(false);
89     return broker;
90   }
91
92   @Bean
93   public ConnectionFactory jmsConnectionFactory() {
94     ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
95     connectionFactory
96         .setTrustedPackages(Arrays.asList("org.commscope", "org.commscope.tr069adapter"));
97     connectionFactory.setMaxThreadPoolSize(7);
98
99     ActiveMQQueue notificationQueue = new ActiveMQQueue(NetConfServerConstants.NETCONF_NOTIFICATION_Q);
100     RedeliveryPolicy notificationQueuePolicy = new RedeliveryPolicy();
101     notificationQueuePolicy.setInitialRedeliveryDelay(2* 60 * 1000L);
102     notificationQueuePolicy.setUseCollisionAvoidance(true);
103     notificationQueuePolicy.setRedeliveryDelay(2* 60 * 1000L);
104     notificationQueuePolicy.setUseExponentialBackOff(false);
105     notificationQueuePolicy.setMaximumRedeliveries(3);
106     notificationQueuePolicy.setDestination(notificationQueue);
107
108     RedeliveryPolicyMap rdMap = connectionFactory.getRedeliveryPolicyMap();
109     rdMap.put(notificationQueue, notificationQueuePolicy);
110     return connectionFactory;
111   }
112
113   @Bean
114   public JmsListenerContainerFactory<MessageListenerContainer> netConfNotificationCF(
115       ConnectionFactory connectionFactory,
116       DefaultJmsListenerContainerFactoryConfigurer configurer) {
117     return handleJMSCommonConfiguration(connectionFactory, configurer);
118   }
119
120   public JmsListenerContainerFactory handleJMSCommonConfiguration(
121       ConnectionFactory connectionFactory,
122       DefaultJmsListenerContainerFactoryConfigurer configurer) {
123     DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
124     configurer.configure(factory, connectionFactory);
125     return factory;
126   }
127
128   @Bean
129   public MessageConverter jacksonJmsMessageConverter() {
130     MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
131     converter.setTargetType(MessageType.TEXT);
132     converter.setTypeIdPropertyName("_type");
133     return converter;
134   }
135 }