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