Added UT Codes
[holmes/rule-management.git] / rulemgt / src / main / java / org / onap / holmes / rulemgt / msb / MsbQuery.java
1 /**
2  * Copyright 2017 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 lombok.extern.slf4j.Slf4j;
19 import org.glassfish.hk2.api.ServiceLocator;
20 import org.onap.holmes.common.dropwizard.ioc.utils.ServiceLocatorHolder;
21 import org.onap.holmes.rulemgt.send.RuleAllocator;
22 import org.onap.holmes.rulemgt.send.Ip4AddingRule;
23 import org.onap.holmes.rulemgt.wrapper.RuleMgtWrapper;
24
25 import java.util.List;
26 import java.util.Timer;
27 import java.util.TimerTask;
28
29
30 @Slf4j
31 public class MsbQuery {
32
33     private RuleAllocator ruleAllocator;
34
35     private Ip4AddingRule ip4AddingRule;
36
37     private EngineInsQueryTool engineInsQueryTool;
38
39     private RuleMgtWrapper ruleMgtWrapper;
40
41     private List<String> timerIpList;
42
43     public MsbQuery() {
44         ruleAllocator = new RuleAllocator();
45
46         ServiceLocator locator = ServiceLocatorHolder.getLocator();
47         ip4AddingRule = locator.getService(Ip4AddingRule.class);
48         engineInsQueryTool = locator.getService(EngineInsQueryTool.class);
49         ruleMgtWrapper = locator.getService(RuleMgtWrapper.class);
50     }
51
52     public void startTimer() {
53         try {
54             timer();
55         } catch (Exception e) {
56             log.error("MSBQuery startTimer timer task failed !" + e.getMessage(), e);
57             try {
58                 Thread.sleep(30000);
59             } catch (InterruptedException e1) {
60                 Thread.currentThread().interrupt();
61             }
62         }
63
64
65     }
66
67     public void timer() throws Exception {
68         Timer timer = new Timer();
69         timer.schedule(new TimerTask() {
70
71             public void run() {
72                 try {
73                     timerIpList = engineInsQueryTool.getInstanceList();
74                     log.info(String.format("There are %d engine instance(s) running currently.", timerIpList.size()));
75
76                     ip4AddingRule.setIpList(timerIpList);
77                     ruleAllocator.allocateRules(timerIpList);
78                 } catch (Exception e) {
79                     log.error("The timing query engine instance failed ", e);
80                 }
81             }
82
83         }, 10000, 30000);
84
85     }
86
87 }