2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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;
24 import org.onap.policy.models.pdp.concepts.PdpMessage;
25 import org.onap.policy.pap.main.comm.msgdata.Request;
26 import org.onap.policy.pap.main.notification.PolicyNotifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Tracks requests associated with a particular PDP. Requests may be broadcast requests or
32 * singleton requests (i.e., destined for a single PDP).
34 public class PdpRequests {
35 private static final Logger logger = LoggerFactory.getLogger(PdpRequests.class);
38 * The maximum request priority + 1.
40 private static final int MAX_PRIORITY = 2;
43 * Name of the PDP with which the requests are associated.
46 private final String pdpName;
49 * Notifier for policy update completions.
52 private final PolicyNotifier notifier;
55 * Index of request currently being published.
57 private int curIndex = 0;
60 * Singleton requests. Items may be {@code null}.
62 private Request[] singletons = new Request[MAX_PRIORITY];
66 * Constructs the object.
68 * @param pdpName name of the PDP with which the requests are associated
70 public PdpRequests(String pdpName, PolicyNotifier notifier) {
71 this.pdpName = pdpName;
72 this.notifier = notifier;
76 * Adds a singleton request.
78 * @param request the request to be added
80 public void addSingleton(Request request) {
82 request.setNotifier(notifier);
84 if (request.getMessage().getName() == null) {
85 throw new IllegalArgumentException("unexpected broadcast for " + pdpName);
88 if (checkExisting(request)) {
89 // have an existing request that's similar - discard this request
93 // no existing request of this type
95 int priority = request.getPriority();
96 singletons[priority] = request;
98 // stop publishing anything of a lower priority
99 final QueueToken<PdpMessage> token = stopPublishingLowerPriority(priority);
101 // start publishing if nothing of higher priority
102 if (higherPrioritySingleton(priority)) {
103 logger.info("{} not publishing due to priority higher than {}", pdpName, priority);
108 request.startPublishing(token);
112 * Checks for an existing request.
114 * @param request the request of interest
115 * @return {@code true} if a similar request already exists, {@code false} otherwise
117 private boolean checkExisting(Request request) {
119 return checkExistingSingleton(request);
123 * Checks for an existing singleton request.
125 * @param request the request of interest
126 * @return {@code true} if a similar singleton request already exists, {@code false}
129 private boolean checkExistingSingleton(Request request) {
131 Request exsingle = singletons[request.getPriority()];
133 if (exsingle == null) {
137 if (exsingle.isSameContent(request)) {
138 // unchanged from existing request
139 logger.info("{} message content unchanged for {}", pdpName, exsingle.getClass().getSimpleName());
143 // reconfigure the existing request
144 PdpMessage message = request.getMessage();
145 exsingle.reconfigure(message, null);
147 // still have a singleton in the queue for this request
152 * Stops all publishing and removes this PDP from any broadcast messages.
154 public void stopPublishing() {
156 for (int x = 0; x < MAX_PRIORITY; ++x) {
157 Request single = singletons[x];
159 if (single != null) {
160 singletons[x] = null;
161 single.stopPublishing();
167 * Stops publishing requests of a lower priority than the specified priority.
169 * @param priority priority of interest
170 * @return the token that was being used to publish a lower priority request
172 private QueueToken<PdpMessage> stopPublishingLowerPriority(int priority) {
175 for (int x = 0; x < priority; ++x) {
176 Request single = singletons[x];
178 if (single != null) {
179 logger.info("{} stop publishing priority {}", pdpName, single.getPriority());
181 QueueToken<PdpMessage> token = single.stopPublishing(false);
183 // found one that was publishing
193 * Starts publishing the next request in the queue.
195 * @param request the request that just completed
196 * @return {@code true} if there is another request in the queue, {@code false} if all
197 * requests for this PDP have been processed
199 public boolean startNextRequest(Request request) {
200 if (!zapRequest(curIndex, request)) {
201 // not at curIndex - look for it in other indices
202 for (int x = 0; x < MAX_PRIORITY; ++x) {
203 if (zapRequest(x, request)) {
209 // find/start the highest priority request
210 for (curIndex = MAX_PRIORITY - 1; curIndex >= 0; --curIndex) {
212 if (singletons[curIndex] != null) {
213 logger.info("{} start publishing priority {}", pdpName, curIndex);
215 singletons[curIndex].startPublishing();
220 logger.info("{} has no more requests", pdpName);
227 * Zaps request pointers, if the request appears at the given index.
229 * @param index index to examine
230 * @param request request of interest
231 * @return {@code true} if a request pointer was zapped, {@code false} if the request
232 * did not appear at the given index
234 private boolean zapRequest(int index, Request request) {
235 if (singletons[index] == request) {
236 singletons[index] = null;
244 * Determines if any singleton request, with a higher priority, is associated with the
247 * @param priority priority of interest
249 * @return {@code true} if the PDP has a singleton, {@code false} otherwise
251 private boolean higherPrioritySingleton(int priority) {
252 for (int x = priority + 1; x < MAX_PRIORITY; ++x) {
253 if (singletons[x] != null) {