Fix the DCAE integration Bugs
[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.dsa.dmaappolling.Subscriber;
25 import org.onap.holmes.engine.dmaap.SubscriberAction;
26
27 @Slf4j
28 public class DcaeConfigurationPolling implements Runnable {
29
30     private String hostname;
31
32     public static long POLLING_PERIOD = 30 * 1000L;
33
34     public DcaeConfigurationPolling(String hostname) {
35         this.hostname = hostname;
36     }
37
38     @Override
39     public void run() {
40         DcaeConfigurations dcaeConfigurations = null;
41         try {
42             dcaeConfigurations = DcaeConfigurationQuery
43                     .getDcaeConfigurations(hostname);
44         } catch (CorrelationException e) {
45             log.error("Failed to poll the DCAE configurations. " + e.getMessage());
46         }
47         if (dcaeConfigurations != null) {
48             DcaeConfigurationsCache.setDcaeConfigurations(dcaeConfigurations);
49             addSubscribers(dcaeConfigurations);
50         }
51     }
52
53     private void addSubscribers(DcaeConfigurations dcaeConfigurations) {
54         SubscriberAction subscriberAction = ServiceLocatorHolder.getLocator()
55                 .getService(SubscriberAction.class);
56         for (String key : dcaeConfigurations.getSubKeys()) {
57             Subscriber subscriber = new Subscriber();
58             subscriber.setTopic(key);
59             subscriber.setUrl(dcaeConfigurations.getSubSecInfo(key).getDmaapInfo()
60                     .getTopicUrl());
61             subscriberAction.addSubscriber(subscriber);
62         }
63     }
64 }