522ec0b02c0d8fdc96904791d4f0648bde6c2d28
[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 com.fasterxml.jackson.core.JsonProcessingException;
19 import lombok.extern.slf4j.Slf4j;
20 import org.onap.holmes.common.dcae.DcaeConfigurationQuery;
21 import org.onap.holmes.common.dcae.DcaeConfigurationsCache;
22 import org.onap.holmes.common.dcae.entity.DcaeConfigurations;
23 import org.onap.holmes.common.dropwizard.ioc.utils.ServiceLocatorHolder;
24 import org.onap.holmes.common.exception.CorrelationException;
25 import org.onap.holmes.common.utils.Md5Util;
26 import org.onap.holmes.dsa.dmaappolling.Subscriber;
27 import org.onap.holmes.engine.dmaap.SubscriberAction;
28
29 @Slf4j
30 public class DcaeConfigurationPolling implements Runnable {
31
32     private String hostname;
33
34     public static final long POLLING_PERIOD = 30 * 1000L;
35
36     private String prevConfigMd5 = Md5Util.md5(null);
37
38     public DcaeConfigurationPolling(String hostname) {
39         this.hostname = hostname;
40     }
41
42     @Override
43     public void run() {
44         DcaeConfigurations dcaeConfigurations = null;
45         try {
46             dcaeConfigurations = DcaeConfigurationQuery.getDcaeConfigurations(hostname);
47             String md5 = Md5Util.md5(dcaeConfigurations);
48             if (prevConfigMd5.equals(md5)){
49                 log.info("Operation aborted due to identical Configurations.");
50                 return;
51             }
52             prevConfigMd5 = md5;
53         } catch (CorrelationException e) {
54             log.error("Failed to poll the DCAE configurations. " + e.getMessage(), e);
55         } catch (Exception e) {
56             log.info("Failed to generate the MD5 information for new configurations.", e);
57         }
58         if (dcaeConfigurations != null) {
59             DcaeConfigurationsCache.setDcaeConfigurations(dcaeConfigurations);
60             addSubscribers(dcaeConfigurations);
61         }
62     }
63
64     private void addSubscribers(DcaeConfigurations dcaeConfigurations) {
65         SubscriberAction subscriberAction = ServiceLocatorHolder.getLocator()
66                 .getService(SubscriberAction.class);
67         for (String key : dcaeConfigurations.getSubKeys()) {
68             Subscriber subscriber = new Subscriber();
69             subscriber.setTopic(key);
70             subscriber.setUrl(dcaeConfigurations.getSubSecInfo(key).getDmaapInfo()
71                     .getTopicUrl());
72             subscriberAction.addSubscriber(subscriber);
73         }
74     }
75 }