2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2021 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pap.main.comm;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.Iterator;
29 import java.util.List;
32 import org.onap.policy.models.base.PfModelException;
33 import org.onap.policy.models.pdp.concepts.Pdp;
34 import org.onap.policy.models.pdp.concepts.PdpGroup;
35 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
36 import org.onap.policy.models.pdp.concepts.PdpMessage;
37 import org.onap.policy.models.pdp.concepts.PdpStateChange;
38 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
39 import org.onap.policy.models.pdp.concepts.PdpUpdate;
40 import org.onap.policy.models.pdp.enums.PdpState;
41 import org.onap.policy.models.provider.PolicyModelsProvider;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
43 import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper;
44 import org.onap.policy.pap.main.comm.msgdata.Request;
45 import org.onap.policy.pap.main.comm.msgdata.RequestListener;
46 import org.onap.policy.pap.main.comm.msgdata.StateChangeReq;
47 import org.onap.policy.pap.main.comm.msgdata.UpdateReq;
48 import org.onap.policy.pap.main.notification.PolicyNotifier;
49 import org.onap.policy.pap.main.parameters.PdpModifyRequestMapParams;
50 import org.onap.policy.pap.main.parameters.RequestParams;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
55 * Maps a PDP name to requests that modify PDPs.
57 public class PdpModifyRequestMap {
58 private static final Logger logger = LoggerFactory.getLogger(PdpModifyRequestMap.class);
60 private static final String UNEXPECTED_BROADCAST = "unexpected broadcast message: ";
63 * Maps a PDP name to its outstanding requests.
65 private final Map<String, PdpRequests> pdp2requests = new HashMap<>();
68 * PDP modification lock.
70 private final Object modifyLock;
73 * The configuration parameters.
75 private final PdpModifyRequestMapParams params;
78 * Factory for PAP DAO.
80 private final PolicyModelsProviderFactoryWrapper daoFactory;
83 * Used to notify when policy updates completes.
85 private final PolicyNotifier policyNotifier;
88 * Used to undeploy policies from the system, when they cannot be deployed to a PDP.
91 * Note: there's a "catch-22" here. The request map needs an undeployer, but the
92 * undeployer needs the request map. Consequently, the request map is created first,
93 * then the undeployer, and finally, this field is set.
96 private PolicyUndeployer policyUndeployer;
100 * Constructs the object.
102 * @param params configuration parameters
104 * @throws IllegalArgumentException if a required parameter is not set
106 public PdpModifyRequestMap(PdpModifyRequestMapParams params) {
109 this.params = params;
110 this.modifyLock = params.getModifyLock();
111 this.daoFactory = params.getDaoFactory();
112 this.policyNotifier = params.getPolicyNotifier();
116 * Determines if the map contains any requests.
118 * @return {@code true} if the map is empty, {@code false} otherwise
120 public boolean isEmpty() {
121 return pdp2requests.isEmpty();
125 * Stops publishing requests to the given PDP.
127 * @param pdpName PDP name
129 public void stopPublishing(String pdpName) {
130 synchronized (modifyLock) {
131 PdpRequests requests = pdp2requests.remove(pdpName);
132 if (requests != null) {
133 requests.stopPublishing();
139 * Adds a pair of requests to the map.
141 * @param update the UPDATE request or {@code null}
142 * @param stateChange the STATE-CHANGE request or {@code null}
144 public void addRequest(PdpUpdate update, PdpStateChange stateChange) {
145 if (update == null) {
146 addRequest(stateChange);
148 } else if (stateChange == null) {
151 } else if (stateChange.getState() == PdpState.ACTIVE) {
152 // publish update before activating
153 synchronized (modifyLock) {
155 addRequest(stateChange);
159 // deactivate before publishing update
160 synchronized (modifyLock) {
161 addRequest(stateChange);
168 * Adds an UPDATE request to the map.
170 * @param update the UPDATE request or {@code null}
172 public void addRequest(PdpUpdate update) {
173 if (update == null) {
177 if (isBroadcast(update)) {
178 throw new IllegalArgumentException(UNEXPECTED_BROADCAST + update);
182 RequestParams reqparams = new RequestParams()
183 .setMaxRetryCount(params.getParams().getUpdateParameters().getMaxRetryCount())
184 .setTimers(params.getUpdateTimers())
185 .setModifyLock(params.getModifyLock())
186 .setPdpPublisher(params.getPdpPublisher())
187 .setResponseDispatcher(params.getResponseDispatcher());
190 String name = update.getName() + " " + PdpUpdate.class.getSimpleName();
191 UpdateReq request = new UpdateReq(reqparams, name, update);
193 addSingleton(request);
197 * Adds a STATE-CHANGE request to the map.
199 * @param stateChange the STATE-CHANGE request or {@code null}
201 public void addRequest(PdpStateChange stateChange) {
202 if (stateChange == null) {
206 if (isBroadcast(stateChange)) {
207 throw new IllegalArgumentException(UNEXPECTED_BROADCAST + stateChange);
211 RequestParams reqparams = new RequestParams()
212 .setMaxRetryCount(params.getParams().getStateChangeParameters().getMaxRetryCount())
213 .setTimers(params.getStateChangeTimers())
214 .setModifyLock(params.getModifyLock())
215 .setPdpPublisher(params.getPdpPublisher())
216 .setResponseDispatcher(params.getResponseDispatcher());
219 String name = stateChange.getName() + " " + PdpStateChange.class.getSimpleName();
220 StateChangeReq request = new StateChangeReq(reqparams, name, stateChange);
222 addSingleton(request);
226 * Determines if a message is a broadcast message.
228 * @param message the message to examine
229 * @return {@code true} if the message is a broadcast message, {@code false} if
230 * destined for a single PDP
232 private boolean isBroadcast(PdpMessage message) {
233 return (message.getName() == null);
237 * Configures and adds a request to the map.
239 * @param request the request to be added
241 private void addSingleton(Request request) {
243 synchronized (modifyLock) {
244 PdpRequests requests = pdp2requests.computeIfAbsent(request.getMessage().getName(), this::makePdpRequests);
246 request.setListener(new SingletonListener(requests, request));
247 requests.addSingleton(request);
252 * Removes a PDP from all active groups.
254 * @param pdpName name of the PDP to be removed
255 * @return {@code true} if the PDP was removed from a group, {@code false} if it was
256 * not assigned to a group
257 * @throws PfModelException if an error occurred
259 public boolean removeFromGroups(String pdpName) throws PfModelException {
261 try (PolicyModelsProvider dao = daoFactory.create()) {
263 PdpGroupFilter filter = PdpGroupFilter.builder().groupState(PdpState.ACTIVE).build();
264 List<PdpGroup> groups = dao.getFilteredPdpGroups(filter);
265 List<PdpGroup> updates = new ArrayList<>(1);
267 for (PdpGroup group : groups) {
268 if (removeFromGroup(pdpName, group)) {
273 if (updates.isEmpty()) {
277 dao.updatePdpGroups(updates);
284 * Removes a PDP from a group.
286 * @param pdpName name of the PDP to be removed
287 * @param group group from which it should be removed
288 * @return {@code true} if the PDP was removed from the group, {@code false} if it was
289 * not assigned to the group
291 private boolean removeFromGroup(String pdpName, PdpGroup group) {
292 for (PdpSubGroup subgrp : group.getPdpSubgroups()) {
293 if (removeFromSubgroup(pdpName, group, subgrp)) {
302 * Removes a PDP from a subgroup.
304 * @param pdpName name of the PDP to be removed
305 * @param group group from which to attempt to remove the PDP
306 * @param subgrp subgroup from which to attempt to remove the PDP
307 * @return {@code true} if the PDP was removed, {@code false} if the PDP was not in
310 private boolean removeFromSubgroup(String pdpName, PdpGroup group, PdpSubGroup subgrp) {
312 Iterator<Pdp> iter = subgrp.getPdpInstances().iterator();
314 while (iter.hasNext()) {
315 Pdp instance = iter.next();
317 if (pdpName.equals(instance.getInstanceId())) {
318 logger.info("removed {} from group={} subgroup={}", pdpName, group.getName(), subgrp.getPdpType());
320 subgrp.setCurrentInstanceCount(subgrp.getPdpInstances().size());
329 * Creates a new set of requests for a PDP. May be overridden by junit tests.
331 * @param pdpName PDP name
332 * @return a new set of requests
334 protected PdpRequests makePdpRequests(String pdpName) {
335 return new PdpRequests(pdpName, policyNotifier);
339 * Listener for singleton request events.
341 private class SingletonListener implements RequestListener {
342 private final PdpRequests requests;
343 private final Request request;
344 private final String pdpName;
346 public SingletonListener(PdpRequests requests, Request request) {
347 this.requests = requests;
348 this.request = request;
349 this.pdpName = requests.getPdpName();
353 public void failure(String responsePdpName, String reason) {
354 Collection<ToscaConceptIdentifier> undeployPolicies = requestCompleted(responsePdpName);
355 if (undeployPolicies.isEmpty()) {
356 // nothing to undeploy
361 * Undeploy the extra policies. Note: this will likely cause a new message to
362 * be assigned to the request, thus we must re-start it after making the
365 PdpMessage oldmsg = request.getMessage();
368 logger.warn("undeploy policies from {}:{} that failed to deploy: {}", oldmsg.getPdpGroup(),
369 oldmsg.getPdpSubgroup(), undeployPolicies);
370 policyUndeployer.undeploy(oldmsg.getPdpGroup(), oldmsg.getPdpSubgroup(), undeployPolicies);
371 } catch (PfModelException | RuntimeException e) {
372 logger.error("cannot undeploy policies {}", undeployPolicies, e);
375 if (request.getMessage() == oldmsg) {
376 // message is unchanged - start the next request
377 startNextRequest(request);
379 // message changed - restart the request
380 request.startPublishing();
385 public void success(String responsePdpName) {
386 requestCompleted(responsePdpName);
390 * Handles a request completion, starting the next request, if there is one.
392 * @param responsePdpName name of the PDP provided in the response
393 * @return a list of policies to be undeployed
395 private Collection<ToscaConceptIdentifier> requestCompleted(String responsePdpName) {
396 if (!pdpName.equals(responsePdpName)) {
397 return Collections.emptyList();
400 if (pdp2requests.get(pdpName) != requests) {
401 logger.info("discard old requests for {}", responsePdpName);
402 requests.stopPublishing();
403 return Collections.emptyList();
406 if (!requests.isFirstInQueue(request)) {
407 logger.error("request is not first in the queue {}", request.getMessage());
408 return Collections.emptyList();
411 Collection<ToscaConceptIdentifier> undeployPolicies = request.getUndeployPolicies();
412 if (undeployPolicies.isEmpty()) {
413 // nothing to undeploy - just start the next request
414 startNextRequest(request);
417 return undeployPolicies;
421 public void retryCountExhausted() {
426 * Starts the next request associated with a PDP.
428 * @param request the request that just completed
430 private void startNextRequest(Request request) {
431 if (!requests.startNextRequest(request)) {
432 pdp2requests.remove(pdpName, requests);
437 * Removes a PDP from its subgroup.
439 private void removePdp() {
440 requests.stopPublishing();
442 // remove the requests from the map
443 if (!pdp2requests.remove(pdpName, requests)) {
444 // wasn't in the map - the requests must be old
445 logger.warn("discarding old requests for {}", pdpName);
449 logger.warn("removing {}", pdpName);
451 policyNotifier.removePdp(pdpName);
453 // remove the PDP from all groups
455 removeFromGroups(pdpName);
456 } catch (PfModelException e) {
457 logger.info("unable to remove PDP {} from subgroup", pdpName, e);