f10cb1af7c561c9bae8bcca3cf4ff82a7af57033
[ccsdk/features.git] /
1 /*******************************************************************************
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18
19 package org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.impl;
20
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 import org.onap.ccsdk.features.sdnr.wt.common.configuration.Configuration;
26 import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation;
27 import org.onap.ccsdk.features.sdnr.wt.common.configuration.filechange.IConfigChangedListener;
28 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
29 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
30 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
31 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
32 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 @SuppressWarnings("deprecation")
37 public class MountpointRegistrarImpl implements AutoCloseable, IConfigChangedListener {
38
39         private static final Logger LOG = LoggerFactory.getLogger(MountpointRegistrarImpl.class);
40         private static final String APPLICATION_NAME = "mountpoint-registrar";
41         private static final String CONFIGURATIONFILE = "etc/mountpoint-registrar.properties";
42
43         private Thread dmaapVESMsgConsumerMain = null;
44         @SuppressWarnings("unused")
45         private DataBroker dataBroker = null;
46         @SuppressWarnings("unused")
47         private MountPointService mountPointService = null;
48         @SuppressWarnings("unused")
49         private RpcProviderRegistry rpcProviderRegistry = null;
50         @SuppressWarnings("unused")
51         private NotificationPublishService notificationPublishService = null;
52         @SuppressWarnings("unused")
53         private ClusterSingletonServiceProvider clusterSingletonServiceProvider;
54
55         private GeneralConfig generalConfig;
56         private boolean dmaapEnabled = false;
57         Map<String, Configuration> configMap = new HashMap<>();
58
59         // Blueprint 1
60         public MountpointRegistrarImpl() {
61                 LOG.info("Creating provider class for {}", APPLICATION_NAME);
62         }
63
64         public void setDataBroker(DataBroker dataBroker) {
65                 this.dataBroker = dataBroker;
66         }
67
68         public void setRpcProviderRegistry(RpcProviderRegistry rpcProviderRegistry) {
69                 this.rpcProviderRegistry = rpcProviderRegistry;
70         }
71
72         public void setNotificationPublishService(NotificationPublishService notificationPublishService) {
73                 this.notificationPublishService = notificationPublishService;
74         }
75
76         public void setMountPointService(MountPointService mountPointService) {
77                 this.mountPointService = mountPointService;
78         }
79
80         public void setClusterSingletonService(ClusterSingletonServiceProvider clusterSingletonService) {
81                 this.clusterSingletonServiceProvider = clusterSingletonService;
82         }
83
84         public void init() {
85                 LOG.info("Init call for {}", APPLICATION_NAME);
86
87                 ConfigurationFileRepresentation configFileRepresentation = new ConfigurationFileRepresentation(CONFIGURATIONFILE);
88                 configFileRepresentation.registerConfigChangedListener(this);
89
90                 generalConfig = new GeneralConfig(configFileRepresentation);
91                 PNFRegistrationConfig pnfRegConfig = new PNFRegistrationConfig(configFileRepresentation);
92                 FaultConfig faultConfig = new FaultConfig(configFileRepresentation);
93
94                 configMap.put("pnfRegistration", pnfRegConfig);
95                 configMap.put("fault", faultConfig);
96
97                 dmaapEnabled = generalConfig.getEnabled();
98                 if (dmaapEnabled) { // start dmaap consumer thread only if dmaapEnabled=true
99                         LOG.info("DMaaP seems to be enabled, starting consumer(s)");
100                         dmaapVESMsgConsumerMain = new Thread(new DMaaPVESMsgConsumerMain(configMap));
101                         dmaapVESMsgConsumerMain.start();
102                 } else {
103                         LOG.info("DMaaP seems to be disabled, not starting any consumer(s)");
104                 }
105         }
106
107         /**
108          * Reflect status for Unit Tests
109          * @return Text with status
110          */
111         public String isInitializationOk() {
112                 return "No implemented";
113         }
114
115         @Override
116         public void onConfigChanged() {
117                 LOG.info("Service configuration state changed. Enabled: {}", generalConfig.getEnabled());
118                 boolean dmaapEnabledNewVal = generalConfig.getEnabled();
119                 if (!dmaapEnabled && dmaapEnabledNewVal) { // Dmaap disabled earlier (or during bundle startup) but enabled later, start Consumer(s)
120                         LOG.info("DMaaP is enabled, starting consumer(s)");
121                         dmaapVESMsgConsumerMain = new Thread(new DMaaPVESMsgConsumerMain(configMap));
122                         dmaapVESMsgConsumerMain.start();
123                 } else if (dmaapEnabled && !dmaapEnabledNewVal) { // Dmaap enabled earlier (or during bundle startup) but disabled later, stop consumer(s)
124                         LOG.info("DMaaP is disabled, stopping consumer(s)");
125                         List<DMaaPVESMsgConsumer> consumers = DMaaPVESMsgConsumerMain.getConsumers();
126                         for (DMaaPVESMsgConsumer consumer : consumers) {
127                                 // stop all consumers
128                                 consumer.stopConsumer();
129                         }
130                 }
131                 dmaapEnabled = dmaapEnabledNewVal;
132         }
133
134         @Override
135         public void close() throws Exception {
136                 LOG.info("{} closing ...", this.getClass().getName());
137                 //close(updateService, configService, mwtnService); issue#1
138                 //close(updateService, mwtnService);
139                 LOG.info("{} closing done",APPLICATION_NAME);
140         }
141
142         /**
143          * Used to close all Services, that should support AutoCloseable Pattern
144          *
145          * @param toClose
146          * @throws Exception
147          */
148         @SuppressWarnings("unused")
149         private void close(AutoCloseable... toCloseList) throws Exception {
150                 for (AutoCloseable element : toCloseList) {
151                         if (element != null) {
152                                 element.close();
153                         }
154                 }
155         }
156
157
158 }