Fix the Sonar Findings
[holmes/engine-management.git] / engine-d / src / main / java / org / onap / holmes / engine / dcae / DcaeConfigurationPolling.java
1 /**
2  * Copyright 2017 ZTE Corporation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.holmes.engine.dcae;
17
18 import lombok.extern.slf4j.Slf4j;
19 import org.onap.holmes.common.dcae.DcaeConfigurationQuery;
20 import org.onap.holmes.common.dcae.DcaeConfigurationsCache;
21 import org.onap.holmes.common.dcae.entity.DcaeConfigurations;
22 import org.onap.holmes.common.dropwizard.ioc.utils.ServiceLocatorHolder;
23 import org.onap.holmes.common.exception.CorrelationException;
24 import org.onap.holmes.common.utils.Md5Util;
25 import org.onap.holmes.dsa.dmaappolling.Subscriber;
26 import org.onap.holmes.engine.dmaap.SubscriberAction;
27
28 @Slf4j
29 public class DcaeConfigurationPolling implements Runnable {
30
31     private String hostname;
32
33     public static final long POLLING_PERIOD = 30 * 1000L;
34
35     private String prevConfigMd5 = Md5Util.md5(null);
36
37     public DcaeConfigurationPolling(String hostname) {
38         this.hostname = hostname;
39     }
40
41     @Override
42     public void run() {
43         DcaeConfigurations dcaeConfigurations = null;
44         try {
45             dcaeConfigurations = DcaeConfigurationQuery.getDcaeConfigurations(hostname);
46             String md5 = Md5Util.md5(dcaeConfigurations);
47             if (prevConfigMd5.equals(md5)){
48                 log.info("Operation aborted due to identical Configurations.");
49                 return;
50             }
51             prevConfigMd5 = md5;
52         } catch (CorrelationException e) {
53             log.error("Failed to poll the DCAE configurations. " + e.getMessage(), e);
54         } catch (Exception e) {
55             log.info("Failed to generate the MD5 information for new configurations.", e);
56         }
57         if (dcaeConfigurations != null) {
58             DcaeConfigurationsCache.setDcaeConfigurations(dcaeConfigurations);
59             addSubscribers(dcaeConfigurations);
60         }
61     }
62
63     private void addSubscribers(DcaeConfigurations dcaeConfigurations) {
64         SubscriberAction subscriberAction = ServiceLocatorHolder.getLocator()
65                 .getService(SubscriberAction.class);
66         for (String key : dcaeConfigurations.getSubKeys()) {
67             Subscriber subscriber = new Subscriber();
68             subscriber.setTopic(key);
69             subscriber.setUrl(dcaeConfigurations.getSubSecInfo(key).getDmaapInfo()
70                     .getTopicUrl());
71             subscriberAction.addSubscriber(subscriber);
72         }
73     }
74 }