53454ac03c669d5dc277fb2e18350298e4da420e
[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 import org.onap.ccsdk.features.sdnr.wt.common.configuration.Configuration;
25 import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation;
26 import org.onap.ccsdk.features.sdnr.wt.common.configuration.filechange.IConfigChangedListener;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class MountpointRegistrarImpl implements AutoCloseable, IConfigChangedListener {
31
32     private static final Logger LOG = LoggerFactory.getLogger(MountpointRegistrarImpl.class);
33     private static final String APPLICATION_NAME = "mountpoint-registrar";
34     private static final String CONFIGURATIONFILE = "etc/mountpoint-registrar.properties";
35
36     private Thread dmaapVESMsgConsumerMain = null;
37
38     private GeneralConfig generalConfig;
39     private boolean dmaapEnabled = false;
40     private Map<String, Configuration> configMap = new HashMap<>();
41     private DMaaPVESMsgConsumerMain dmaapConsumerMain = null;
42
43     // Blueprint 1
44     public MountpointRegistrarImpl() {
45         LOG.info("Creating provider class for {}", APPLICATION_NAME);
46     }
47
48     public void init() {
49         LOG.info("Init call for {}", APPLICATION_NAME);
50
51         ConfigurationFileRepresentation configFileRepresentation =
52                 new ConfigurationFileRepresentation(CONFIGURATIONFILE);
53         configFileRepresentation.registerConfigChangedListener(this);
54
55         generalConfig = new GeneralConfig(configFileRepresentation);
56         PNFRegistrationConfig pnfRegConfig = new PNFRegistrationConfig(configFileRepresentation);
57         FaultConfig faultConfig = new FaultConfig(configFileRepresentation);
58
59         configMap.put("pnfRegistration", pnfRegConfig);
60         configMap.put("fault", faultConfig);
61
62         dmaapEnabled = generalConfig.getEnabled();
63         if (dmaapEnabled) { // start dmaap consumer thread only if dmaapEnabled=true
64             LOG.info("DMaaP seems to be enabled, starting consumer(s)");
65             dmaapConsumerMain = new DMaaPVESMsgConsumerMain(configMap, generalConfig);
66             dmaapVESMsgConsumerMain = new Thread(dmaapConsumerMain);
67             dmaapVESMsgConsumerMain.start();
68         } else {
69             LOG.info("DMaaP seems to be disabled, not starting any consumer(s)");
70         }
71     }
72
73     /**
74      * Reflect status for Unit Tests
75      *
76      * @return Text with status
77      */
78     public String isInitializationOk() {
79         return "No implemented";
80     }
81
82     @Override
83     public void onConfigChanged() {
84         LOG.info("Service configuration state changed. Enabled: {}", generalConfig.getEnabled());
85         boolean dmaapEnabledNewVal = generalConfig.getEnabled();
86         if (!dmaapEnabled && dmaapEnabledNewVal) { // Dmaap disabled earlier (or during bundle startup) but enabled later, start Consumer(s)
87             LOG.info("DMaaP is enabled, starting consumer(s)");
88             dmaapConsumerMain = new DMaaPVESMsgConsumerMain(configMap, generalConfig);
89             dmaapVESMsgConsumerMain = new Thread(dmaapConsumerMain);
90             dmaapVESMsgConsumerMain.start();
91         } else if (dmaapEnabled && !dmaapEnabledNewVal) { // Dmaap enabled earlier (or during bundle startup) but disabled later, stop consumer(s)
92             LOG.info("DMaaP is disabled, stopping consumer(s)");
93             List<DMaaPVESMsgConsumer> consumers = dmaapConsumerMain.getConsumers();
94             for (DMaaPVESMsgConsumer consumer : consumers) {
95                 // stop all consumers
96                 consumer.stopConsumer();
97             }
98         }
99         dmaapEnabled = dmaapEnabledNewVal;
100     }
101
102     @Override
103     public void close() throws Exception {
104         LOG.info("{} closing ...", this.getClass().getName());
105         LOG.info("{} closing done", APPLICATION_NAME);
106     }
107
108     /**
109      * Used to close all Services, that should support AutoCloseable Pattern
110      *
111      * @param toClose
112      * @throws Exception
113      */
114     @SuppressWarnings("unused")
115     private void close(AutoCloseable... toCloseList) throws Exception {
116         for (AutoCloseable element : toCloseList) {
117             if (element != null) {
118                 element.close();
119             }
120         }
121     }
122
123
124 }