Fix sonars in policy-pap
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / comm / PdpRequests.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.pap.main.comm;
22
23 import java.util.ArrayDeque;
24 import java.util.Queue;
25 import lombok.Getter;
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;
31
32 /**
33  * Tracks requests associated with a particular PDP. Requests may be broadcast requests or
34  * singleton requests (i.e., destined for a single PDP).
35  */
36 public class PdpRequests {
37     private static final Logger logger = LoggerFactory.getLogger(PdpRequests.class);
38
39     /**
40      * Name of the PDP with which the requests are associated.
41      */
42     @Getter
43     private final String pdpName;
44
45     /**
46      * Notifier for policy update completions.
47      */
48     @Getter
49     private final PolicyNotifier notifier;
50
51     /**
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.
55      */
56     private final Queue<Request> requests = new ArrayDeque<>(3);
57
58
59     /**
60      * Constructs the object.
61      *
62      * @param pdpName name of the PDP with which the requests are associated
63      */
64     public PdpRequests(String pdpName, PolicyNotifier notifier) {
65         this.pdpName = pdpName;
66         this.notifier = notifier;
67     }
68
69     /**
70      * Adds a singleton request.
71      *
72      * @param request the request to be added
73      */
74     public void addSingleton(Request request) {
75
76         request.setNotifier(notifier);
77
78         if (request.getMessage().getName() == null) {
79             throw new IllegalArgumentException("unexpected broadcast for " + pdpName);
80         }
81
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)) {
86                 return;
87             }
88         }
89
90         // couldn't reconfigure an existing request - must add the new one
91
92         requests.add(request);
93
94         if (requests.peek() == request) {
95             // this is the first request in the queue - publish it
96             request.startPublishing();
97         }
98     }
99
100     /**
101      * Stops all publishing and removes this PDP from any broadcast messages.
102      */
103     public void stopPublishing() {
104         var request = requests.peek();
105         if (request != null) {
106             request.stopPublishing();
107         }
108     }
109
110     /**
111      * Determines if a request is the first request in the queue.
112      *
113      * @param request request of interest
114      * @return {@code true} if the request is the first in the queue, {@code false}
115      *         otherwise
116      */
117     public boolean isFirstInQueue(Request request) {
118         return (requests.peek() == request);
119     }
120
121     /**
122      * Starts publishing the next request in the queue.
123      *
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
127      */
128     public boolean startNextRequest(Request request) {
129         if (request != requests.peek()) {
130             // not the request we're looking for
131             return !requests.isEmpty();
132         }
133
134         // remove the completed request
135         requests.remove();
136
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);
141             return false;
142         }
143
144         logger.info("{} start publishing next request", pdpName);
145
146         nextRequest.startPublishing();
147         return true;
148     }
149 }