Drools support for kafka topics
[policy/drools-pdp.git] / feature-pooling-dmaap / src / main / java / org / onap / policy / drools / pooling / state / StartState.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2018, 2020-2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.drools.pooling.state;
22
23 import lombok.Getter;
24 import org.onap.policy.drools.pooling.PoolingManager;
25 import org.onap.policy.drools.pooling.message.Heartbeat;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * The start state. Upon entry, a heart beat is generated and the event filter is changed
31  * to look for just that particular message. Once the message is seen, it goes into the
32  * {@link QueryState}.
33  */
34 @Getter
35 public class StartState extends State {
36
37     private static final Logger logger = LoggerFactory.getLogger(StartState.class);
38
39     /**
40      * Time stamp inserted into the heart beat message.
41      */
42     private long hbTimestampMs = System.currentTimeMillis();
43
44     /**
45      * Constructor.
46      *
47      * @param mgr pooling manager
48      */
49     public StartState(PoolingManager mgr) {
50         super(mgr);
51     }
52
53     @Override
54     public void start() {
55
56         super.start();
57
58         var hb = makeHeartbeat(hbTimestampMs);
59         publish(getHost(), hb);
60
61         /*
62          * heart beat generator
63          */
64         long genMs = getProperties().getInterHeartbeatMs();
65
66         scheduleWithFixedDelay(genMs, genMs, () -> {
67             publish(getHost(), hb);
68             return null;
69         });
70
71         /*
72          * my heart beat checker
73          */
74         schedule(getProperties().getStartHeartbeatMs(), () -> {
75             logger.error("missed heartbeat on topic {}", getTopic());
76             return internalTopicFailed();
77         });
78     }
79
80     /**
81      * Transitions to the query state if the heart beat originated from this host and its
82      * time stamp matches.
83      */
84     @Override
85     public State process(Heartbeat msg) {
86         if (msg.getTimestampMs() == hbTimestampMs && getHost().equals(msg.getSource())) {
87             // saw our own heart beat - transition to query state
88             logger.info("saw our own heartbeat on topic {}", getTopic());
89             publish(makeQuery());
90             return goQuery();
91
92         } else {
93             logger.info("ignored old heartbeat message from {} on topic {}", msg.getSource(), getTopic());
94         }
95
96         return null;
97     }
98 }