aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/main/java/org/onap/policy/pap/main/comm/PdpRequests.java
blob: 6a539a46cf055a00a186a5ae1a28c2e540150d16 (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
/*
 * ============LICENSE_START=======================================================
 * ONAP PAP
 * ================================================================================
 * Copyright (C) 2019 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.pap.main.comm;

import java.util.ArrayDeque;
import java.util.Queue;
import lombok.Getter;
import org.onap.policy.models.pdp.concepts.PdpMessage;
import org.onap.policy.pap.main.comm.msgdata.Request;
import org.onap.policy.pap.main.notification.PolicyNotifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Tracks requests associated with a particular PDP. Requests may be broadcast requests or
 * singleton requests (i.e., destined for a single PDP).
 */
public class PdpRequests {
    private static final Logger logger = LoggerFactory.getLogger(PdpRequests.class);

    /**
     * Name of the PDP with which the requests are associated.
     */
    @Getter
    private final String pdpName;

    /**
     * Notifier for policy update completions.
     */
    @Getter
    private final PolicyNotifier notifier;

    /**
     * Queue of requests to be published. The first item in the queue is currently being
     * published. Currently, there will be at most three messages in the queue: PASSIVE,
     * ACTIVE, and UPDATE.
     */
    private final Queue<Request> requests = new ArrayDeque<>(3);


    /**
     * Constructs the object.
     *
     * @param pdpName name of the PDP with which the requests are associated
     */
    public PdpRequests(String pdpName, PolicyNotifier notifier) {
        this.pdpName = pdpName;
        this.notifier = notifier;
    }

    /**
     * Adds a singleton request.
     *
     * @param request the request to be added
     */
    public void addSingleton(Request request) {

        request.setNotifier(notifier);

        if (request.getMessage().getName() == null) {
            throw new IllegalArgumentException("unexpected broadcast for " + pdpName);
        }

        // try to reconfigure an existing request with the new message
        PdpMessage newMessage = request.getMessage();
        for (Request req : requests) {
            if (req.reconfigure(newMessage)) {
                return;
            }
        }

        // couldn't reconfigure an existing request - must add the new one

        requests.add(request);

        if (requests.peek() == request) {
            // this is the first request in the queue - publish it
            request.startPublishing();
        }
    }

    /**
     * Stops all publishing and removes this PDP from any broadcast messages.
     */
    public void stopPublishing() {
        Request request = requests.peek();
        if (request != null) {
            request.stopPublishing();
        }
    }

    /**
     * Determines if a request is the first request in the queue.
     *
     * @param request request of interest
     * @return {@code true} if the request is the first in the queue, {@code false}
     *         otherwise
     */
    public boolean isFirstInQueue(Request request) {
        return (requests.peek() == request);
    }

    /**
     * Starts publishing the next request in the queue.
     *
     * @param request the request that just completed
     * @return {@code true} if there is another request in the queue, {@code false} if all
     *         requests for this PDP have been processed
     */
    public boolean startNextRequest(Request request) {
        if (request != requests.peek()) {
            // not the request we're looking for
            return !requests.isEmpty();
        }

        // remove the completed request
        requests.remove();

        // start publishing next request, but don't remove it from the queue
        Request nextRequest = requests.peek();
        if (nextRequest == null) {
            logger.info("{} has no more requests", pdpName);
            return false;
        }

        logger.info("{} start publishing next request", pdpName);

        nextRequest.startPublishing();
        return true;
    }
}