2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.pap.main.comm;
23 import java.util.ArrayDeque;
24 import java.util.Queue;
26 import org.onap.policy.models.pdp.concepts.PdpMessage;
27 import org.onap.policy.pap.main.comm.msgdata.Request;
28 import org.onap.policy.pap.main.notification.PolicyNotifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Tracks requests associated with a particular PDP. Requests may be broadcast requests or
34 * singleton requests (i.e., destined for a single PDP).
36 public class PdpRequests {
37 private static final Logger logger = LoggerFactory.getLogger(PdpRequests.class);
40 * Name of the PDP with which the requests are associated.
43 private final String pdpName;
46 * Notifier for policy update completions.
49 private final PolicyNotifier notifier;
52 * Queue of requests to be published. The first item in the queue is currently being
53 * published. Currently, there will be at most four messages in the queue: the request
54 * being worked, one PASSIVE request, one ACTIVE, and one UPDATE.
56 private final Queue<Request> requests = new ArrayDeque<>(3);
60 * Constructs the object.
62 * @param pdpName name of the PDP with which the requests are associated
64 public PdpRequests(String pdpName, PolicyNotifier notifier) {
65 this.pdpName = pdpName;
66 this.notifier = notifier;
70 * Adds a singleton request.
72 * @param request the request to be added
74 public void addSingleton(Request request) {
76 request.setNotifier(notifier);
78 if (request.getMessage().getName() == null) {
79 throw new IllegalArgumentException("unexpected broadcast for " + pdpName);
82 // try to reconfigure an existing request with the new message
83 PdpMessage newMessage = request.getMessage();
84 for (Request req : requests) {
85 if (req.reconfigure(newMessage)) {
90 // couldn't reconfigure an existing request - must add the new one
92 requests.add(request);
94 if (requests.peek() == request) {
95 // this is the first request in the queue - publish it
96 request.startPublishing();
101 * Stops all publishing and removes this PDP from any broadcast messages.
103 public void stopPublishing() {
104 var request = requests.peek();
105 if (request != null) {
106 request.stopPublishing();
111 * Determines if a request is the first request in the queue.
113 * @param request request of interest
114 * @return {@code true} if the request is the first in the queue, {@code false}
117 public boolean isFirstInQueue(Request request) {
118 return (requests.peek() == request);
122 * Starts publishing the next request in the queue.
124 * @param request the request that just completed
125 * @return {@code true} if there is another request in the queue, {@code false} if all
126 * requests for this PDP have been processed
128 public boolean startNextRequest(Request request) {
129 if (request != requests.peek()) {
130 // not the request we're looking for
131 return !requests.isEmpty();
134 // remove the completed request
137 // start publishing next request, but don't remove it from the queue
138 var nextRequest = requests.peek();
139 if (nextRequest == null) {
140 logger.info("{} has no more requests", pdpName);
144 logger.info("{} start publishing next request", pdpName);
146 nextRequest.startPublishing();