Fixed an Issue on API Calling via MSB
[holmes/rule-management.git] / rulemgt / src / main / java / org / onap / holmes / rulemgt / msb / MsbQuery.java
1 /**
2  * Copyright 2017-2020 ZTE Corporation.
3  * <p>
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  * <p>
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * <p>
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.rulemgt.msb;
17
18 import org.onap.holmes.common.dropwizard.ioc.utils.ServiceLocatorHolder;
19 import org.onap.holmes.rulemgt.send.Ip4AddingRule;
20 import org.onap.holmes.rulemgt.send.RuleAllocator;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import java.util.List;
25 import java.util.Timer;
26 import java.util.TimerTask;
27
28 import static java.util.concurrent.TimeUnit.SECONDS;
29
30 public class MsbQuery {
31
32     static final private Logger log = LoggerFactory.getLogger(MsbQuery.class);
33     final private RuleAllocator ruleAllocator;
34     private Ip4AddingRule ip4AddingRule;
35     private EngineInsQueryTool engineInsQueryTool;
36
37     public MsbQuery() {
38         ruleAllocator = new RuleAllocator();
39         ip4AddingRule = ServiceLocatorHolder.getLocator().getService(Ip4AddingRule.class);
40         engineInsQueryTool = ServiceLocatorHolder.getLocator().getService(EngineInsQueryTool.class);
41     }
42
43     public void startTimer() {
44         try {
45             new Timer().schedule(new TimerTask() {
46
47                 public void run() {
48                     try {
49                         List<String> timerIpList = engineInsQueryTool.getInstanceList();
50                         log.info(String.format("There are %d engine instance(s) running currently.", timerIpList.size()));
51
52                         ip4AddingRule.setIpList(timerIpList);
53                         ruleAllocator.allocateRules(timerIpList);
54                     } catch (Exception e) {
55                         log.error("The timing query engine instance failed ", e);
56                     }
57                 }
58
59             }, SECONDS.toMillis(10), SECONDS.toMillis(30));
60         } catch (Exception e) {
61             log.error("MSBQuery startTimer timer task failed !" + e.getMessage(), e);
62             try {
63                 SECONDS.sleep(30);
64             } catch (InterruptedException e1) {
65                 Thread.currentThread().interrupt();
66             }
67         }
68     }
69 }