summaryrefslogtreecommitdiffstats
path: root/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/state/State.java
blob: 1e3a907e3aa9551bd9a0c124c0df86bf3b7b2352 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
 * ============LICENSE_START=======================================================
 * ONAP
 * ================================================================================
 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.drools.pooling.state;

import static org.onap.policy.drools.pooling.state.FilterUtils.MSG_CHANNEL;
import static org.onap.policy.drools.pooling.state.FilterUtils.makeEquals;
import static org.onap.policy.drools.pooling.state.FilterUtils.makeOr;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledFuture;
import org.onap.policy.drools.pooling.PoolingManager;
import org.onap.policy.drools.pooling.PoolingProperties;
import org.onap.policy.drools.pooling.message.BucketAssignments;
import org.onap.policy.drools.pooling.message.Forward;
import org.onap.policy.drools.pooling.message.Heartbeat;
import org.onap.policy.drools.pooling.message.Identification;
import org.onap.policy.drools.pooling.message.Leader;
import org.onap.policy.drools.pooling.message.Message;
import org.onap.policy.drools.pooling.message.Offline;
import org.onap.policy.drools.pooling.message.Query;

/**
 * A state in the finite state machine.
 * <p>
 * A state may have several timers associated with it, which must be cancelled
 * whenever the state is changed. Assumes that timers are not continuously added
 * to the same state.
 */
public abstract class State {

    /**
     * Host pool manager.
     */
    private final PoolingManager mgr;

    /**
     * Timers added by this state.
     */
    private final List<ScheduledFuture<?>> timers = new LinkedList<>();

    /**
     * 
     * @param mgr
     */
    public State(PoolingManager mgr) {
        this.mgr = mgr;
    }

    /**
     * Gets the server-side filter to use when polling the DMaaP internal topic.
     * The default method returns a filter that accepts messages on the admin
     * channel and on the host's own channel.
     * 
     * @return the server-side filter to use.
     */
    @SuppressWarnings("unchecked")
    public Map<String, Object> getFilter() {
        return makeOr(makeEquals(MSG_CHANNEL, Message.ADMIN), makeEquals(MSG_CHANNEL, getHost()));
    }

    /**
     * Cancels the timers added by this state.
     */
    public void cancelTimers() {
        for (ScheduledFuture<?> fut : timers) {
            fut.cancel(false);
        }
    }

    /**
     * Starts the state.
     */
    public void start() {

    }

    /**
     * Indicates that the finite state machine is stopping. Sends an "offline"
     * message to the other hosts.
     */
    public void stop() {
        publish(makeOffline());
    }

    /**
     * Transitions to the "start" state.
     * 
     * @return the new state
     */
    public State goStart() {
        return mgr.goStart();
    }

    /**
     * Transitions to the "query" state.
     * 
     * @return the new state
     */
    public State goQuery() {
        return mgr.goQuery();
    }

    /**
     * Transitions to the "active" state.
     * 
     * @return the new state
     */
    public State goActive() {
        return mgr.goActive();
    }

    /**
     * Transitions to the "inactive" state.
     * 
     * @return the new state
     */
    protected State goInactive() {
        return mgr.goInactive();
    }

    /**
     * Processes a message. The default method passes it to the manager to
     * handle and returns {@code null}.
     * 
     * @param msg message to be processed
     * @return the new state, or {@code null} if the state is unchanged
     */
    public State process(Forward msg) {
        mgr.handle(msg);
        return null;
    }

    /**
     * Processes a message. The default method just returns {@code null}.
     * 
     * @param msg message to be processed
     * @return the new state, or {@code null} if the state is unchanged
     */
    public State process(Heartbeat msg) {
        return null;
    }

    /**
     * Processes a message. The default method just returns {@code null}.
     * 
     * @param msg message to be processed
     * @return the new state, or {@code null} if the state is unchanged
     */
    public State process(Identification msg) {
        return null;
    }

    /**
     * Processes a message. If this host has a new assignment, then it
     * transitions to the active state. Otherwise, it transitions to the
     * inactive state.
     * 
     * @param msg message to be processed
     * @return the new state, or {@code null} if the state is unchanged
     */
    public State process(Leader msg) {
        BucketAssignments asgn = msg.getAssignments();
        if (asgn == null) {
            return null;
        }

        String source = msg.getSource();
        if (source == null) {
            return null;
        }

        // the new leader must equal the source
        if (source.equals(asgn.getLeader())) {
            startDistributing(asgn);

            if (asgn.hasAssignment(getHost())) {
                return goActive();

            } else {
                return goInactive();
            }
        }

        return null;
    }

    /**
     * Processes a message. The default method just returns {@code null}.
     * 
     * @param msg message to be processed
     * @return the new state, or {@code null} if the state is unchanged
     */
    public State process(Offline msg) {
        return null;
    }

    /**
     * Processes a message. The default method just returns {@code null}.
     * 
     * @param msg message to be processed
     * @return the new state, or {@code null} if the state is unchanged
     */
    public State process(Query msg) {
        return null;
    }

    /**
     * Publishes a message.
     * 
     * @param msg message to be published
     */
    protected void publish(Identification msg) {
        mgr.publishAdmin(msg);
    }

    /**
     * Publishes a message.
     * 
     * @param msg message to be published
     */
    protected void publish(Leader msg) {
        mgr.publishAdmin(msg);
    }

    /**
     * Publishes a message.
     * 
     * @param msg message to be published
     */
    protected void publish(Offline msg) {
        mgr.publishAdmin(msg);
    }

    /**
     * Publishes a message.
     * 
     * @param msg message to be published
     */
    protected void publish(Query msg) {
        mgr.publishAdmin(msg);
    }

    /**
     * Publishes a message on the specified channel.
     * 
     * @param channel
     * @param msg message to be published
     */
    protected void publish(String channel, Forward msg) {
        mgr.publish(channel, msg);
    }

    /**
     * Publishes a message on the specified channel.
     * 
     * @param channel
     * @param msg message to be published
     */
    protected void publish(String channel, Heartbeat msg) {
        mgr.publish(channel, msg);
    }

    /**
     * Starts distributing messages using the specified bucket assignments.
     * 
     * @param assignments
     */
    protected void startDistributing(BucketAssignments assignments) {
        if (assignments != null) {
            mgr.startDistributing(assignments);
        }
    }

    /**
     * Schedules a timer to fire after a delay.
     * 
     * @param delayMs
     * @param task
     */
    protected void schedule(long delayMs, StateTimerTask task) {
        timers.add(mgr.schedule(delayMs, task));
    }

    /**
     * Schedules a timer to fire repeatedly.
     * 
     * @param initialDelayMs
     * @param delayMs
     * @param task
     */
    protected void scheduleWithFixedDelay(long initialDelayMs, long delayMs, StateTimerTask task) {
        timers.add(mgr.scheduleWithFixedDelay(initialDelayMs, delayMs, task));
    }

    /**
     * Indicates that the internal topic failed.
     * 
     * @return a new {@link InactiveState}
     */
    protected State internalTopicFailed() {
        publish(makeOffline());
        mgr.internalTopicFailed();

        return mgr.goInactive();
    }

    /**
     * Makes a heart beat message.
     * 
     * @param timestampMs time, in milliseconds, associated with the message
     * 
     * @return a new message
     */
    protected Heartbeat makeHeartbeat(long timestampMs) {
        return new Heartbeat(getHost(), timestampMs);
    }

    /**
     * Makes an "offline" message.
     * 
     * @return a new message
     */
    protected Offline makeOffline() {
        return new Offline(getHost());
    }

    /**
     * Makes a query message.
     * 
     * @return a new message
     */
    protected Query makeQuery() {
        return new Query(getHost());
    }

    public final BucketAssignments getAssignments() {
        return mgr.getAssignments();
    }

    public final String getHost() {
        return mgr.getHost();
    }

    public final String getTopic() {
        return mgr.getTopic();
    }

    public final PoolingProperties getProperties() {
        return mgr.getProperties();
    }
}