136d2a12b19ff48c43d7b6e1cf9274b758be58f8
[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  * Copyright (C) 2021 Samsung Electronics Intellectual Property. All rights reserved.
7  * =================================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software distributed under the License
14  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
15  * or implied. See the License for the specific language governing permissions and limitations under
16  * the License.
17  * ============LICENSE_END==========================================================================
18  */
19
20 package org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.impl;
21
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
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, MessageConfig> 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         ProvisioningConfig provisioningConfig = new ProvisioningConfig(configFileRepresentation);
59
60         configMap.put("pnfRegistration", pnfRegConfig);
61         configMap.put("fault", faultConfig);
62         configMap.put("provisioning", provisioningConfig);
63
64         dmaapEnabled = generalConfig.getEnabled();
65         if (dmaapEnabled) { // start dmaap consumer thread only if dmaapEnabled=true
66             LOG.info("DMaaP seems to be enabled, starting consumer(s)");
67             dmaapConsumerMain = new DMaaPVESMsgConsumerMain(configMap, generalConfig);
68             dmaapVESMsgConsumerMain = new Thread(dmaapConsumerMain);
69             dmaapVESMsgConsumerMain.start();
70         } else {
71             LOG.info("DMaaP seems to be disabled, not starting any consumer(s)");
72         }
73     }
74
75     /**
76      * Reflect status for Unit Tests
77      *
78      * @return Text with status
79      */
80     public String isInitializationOk() {
81         return "No implemented";
82     }
83
84     @Override
85     public void onConfigChanged() {
86         if (generalConfig == null) { // Included as NullPointerException observed once in docker logs
87                 LOG.warn("onConfigChange cannot be handled. Unexpected Null");
88                 return;
89         }
90         LOG.info("Service configuration state changed. Enabled: {}", generalConfig.getEnabled());
91         boolean dmaapEnabledNewVal = generalConfig.getEnabled();
92         if (!dmaapEnabled && dmaapEnabledNewVal) { // Dmaap disabled earlier (or during bundle startup) but enabled later, start Consumer(s)
93             LOG.info("DMaaP is enabled, starting consumer(s)");
94             dmaapConsumerMain = new DMaaPVESMsgConsumerMain(configMap, generalConfig);
95             dmaapVESMsgConsumerMain = new Thread(dmaapConsumerMain);
96             dmaapVESMsgConsumerMain.start();
97         } else if (dmaapEnabled && !dmaapEnabledNewVal) { // Dmaap enabled earlier (or during bundle startup) but disabled later, stop consumer(s)
98             LOG.info("DMaaP is disabled, stopping consumer(s)");
99             List<DMaaPVESMsgConsumer> consumers = dmaapConsumerMain.getConsumers();
100             for (DMaaPVESMsgConsumer consumer : consumers) {
101                 // stop all consumers
102                 consumer.stopConsumer();
103             }
104         }
105         dmaapEnabled = dmaapEnabledNewVal;
106     }
107
108     @Override
109     public void close() throws Exception {
110         LOG.info("{} closing ...", this.getClass().getName());
111         LOG.info("{} closing done", APPLICATION_NAME);
112     }
113
114     /**
115      * Used to close all Services, that should support AutoCloseable Pattern
116      *
117      * @param toClose
118      * @throws Exception
119      */
120     @SuppressWarnings("unused")
121     private void close(AutoCloseable... toCloseList) throws Exception {
122         for (AutoCloseable element : toCloseList) {
123             if (element != null) {
124                 element.close();
125             }
126         }
127     }
128
129
130 }