2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019, 2021 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.time.Instant;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.HashSet;
30 import java.util.Iterator;
31 import java.util.List;
35 import org.onap.policy.models.base.PfModelException;
36 import org.onap.policy.models.pap.concepts.PolicyNotification;
37 import org.onap.policy.models.pdp.concepts.Pdp;
38 import org.onap.policy.models.pdp.concepts.PdpGroup;
39 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
40 import org.onap.policy.models.pdp.concepts.PdpMessage;
41 import org.onap.policy.models.pdp.concepts.PdpStateChange;
42 import org.onap.policy.models.pdp.concepts.PdpStatus;
43 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
44 import org.onap.policy.models.pdp.concepts.PdpUpdate;
45 import org.onap.policy.models.pdp.enums.PdpState;
46 import org.onap.policy.models.provider.PolicyModelsProvider;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
48 import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper;
49 import org.onap.policy.pap.main.comm.msgdata.Request;
50 import org.onap.policy.pap.main.comm.msgdata.RequestListener;
51 import org.onap.policy.pap.main.comm.msgdata.StateChangeReq;
52 import org.onap.policy.pap.main.comm.msgdata.UpdateReq;
53 import org.onap.policy.pap.main.notification.DeploymentStatus;
54 import org.onap.policy.pap.main.notification.PolicyNotifier;
55 import org.onap.policy.pap.main.parameters.PdpModifyRequestMapParams;
56 import org.onap.policy.pap.main.parameters.RequestParams;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
61 * Maps a PDP name to requests that modify PDPs.
63 public class PdpModifyRequestMap {
64 private static final Logger logger = LoggerFactory.getLogger(PdpModifyRequestMap.class);
66 private static final String UNEXPECTED_BROADCAST = "unexpected broadcast message: ";
69 * Maps a PDP name to its outstanding requests.
71 private final Map<String, PdpRequests> pdp2requests = new HashMap<>();
74 * PDP modification lock.
76 private final Object modifyLock;
79 * The configuration parameters.
81 private final PdpModifyRequestMapParams params;
84 * Factory for PAP DAO.
86 private final PolicyModelsProviderFactoryWrapper daoFactory;
89 * Used to notify when policy updates completes.
91 private final PolicyNotifier policyNotifier;
94 * Used to undeploy policies from the system, when they cannot be deployed to a PDP.
97 * Note: there's a "catch-22" here. The request map needs an undeployer, but the
98 * undeployer needs the request map. Consequently, the request map is created first,
99 * then the undeployer, and finally, this field is set.
102 private PolicyUndeployer policyUndeployer;
106 * Constructs the object.
108 * @param params configuration parameters
110 * @throws IllegalArgumentException if a required parameter is not set
112 public PdpModifyRequestMap(PdpModifyRequestMapParams params) {
115 this.params = params;
116 this.modifyLock = params.getModifyLock();
117 this.daoFactory = params.getDaoFactory();
118 this.policyNotifier = params.getPolicyNotifier();
122 * Determines if the map contains any requests.
124 * @return {@code true} if the map is empty, {@code false} otherwise
126 public boolean isEmpty() {
127 return pdp2requests.isEmpty();
131 * Stops publishing requests to the given PDP.
133 * @param pdpName PDP name
135 public void stopPublishing(String pdpName) {
136 synchronized (modifyLock) {
137 PdpRequests requests = pdp2requests.remove(pdpName);
138 if (requests != null) {
139 requests.stopPublishing();
145 * Adds a pair of requests to the map.
147 * @param update the UPDATE request or {@code null}
148 * @param stateChange the STATE-CHANGE request or {@code null}
150 public void addRequest(PdpUpdate update, PdpStateChange stateChange) {
151 if (update == null) {
152 addRequest(stateChange);
154 } else if (stateChange == null) {
157 } else if (stateChange.getState() == PdpState.ACTIVE) {
158 // publish update before activating
159 synchronized (modifyLock) {
161 addRequest(stateChange);
165 // deactivate before publishing update
166 synchronized (modifyLock) {
167 addRequest(stateChange);
174 * Adds an UPDATE request to the map.
176 * @param update the UPDATE request or {@code null}
177 * @return the new request (this should only be used by junit tests)
179 public Request addRequest(PdpUpdate update) {
180 if (update == null) {
184 if (isBroadcast(update)) {
185 throw new IllegalArgumentException(UNEXPECTED_BROADCAST + update);
189 RequestParams reqparams = new RequestParams()
190 .setMaxRetryCount(params.getParams().getUpdateParameters().getMaxRetryCount())
191 .setTimers(params.getUpdateTimers())
192 .setModifyLock(params.getModifyLock())
193 .setPdpPublisher(params.getPdpPublisher())
194 .setResponseDispatcher(params.getResponseDispatcher());
197 String name = update.getName() + " " + PdpUpdate.class.getSimpleName();
198 var request = new UpdateReq(reqparams, name, update);
200 addSingleton(request);
205 * Adds a STATE-CHANGE request to the map.
207 * @param stateChange the STATE-CHANGE request or {@code null}
208 * @return the new request (this should only be used by junit tests)
210 public Request addRequest(PdpStateChange stateChange) {
211 if (stateChange == null) {
215 if (isBroadcast(stateChange)) {
216 throw new IllegalArgumentException(UNEXPECTED_BROADCAST + stateChange);
220 RequestParams reqparams = new RequestParams()
221 .setMaxRetryCount(params.getParams().getStateChangeParameters().getMaxRetryCount())
222 .setTimers(params.getStateChangeTimers())
223 .setModifyLock(params.getModifyLock())
224 .setPdpPublisher(params.getPdpPublisher())
225 .setResponseDispatcher(params.getResponseDispatcher());
228 String name = stateChange.getName() + " " + PdpStateChange.class.getSimpleName();
229 var request = new StateChangeReq(reqparams, name, stateChange);
231 addSingleton(request);
236 * Determines if a message is a broadcast message.
238 * @param message the message to examine
239 * @return {@code true} if the message is a broadcast message, {@code false} if
240 * destined for a single PDP
242 private boolean isBroadcast(PdpMessage message) {
243 return (message.getName() == null);
247 * Configures and adds a request to the map.
249 * @param request the request to be added
251 private void addSingleton(Request request) {
253 synchronized (modifyLock) {
254 PdpRequests requests = pdp2requests.computeIfAbsent(request.getMessage().getName(), this::makePdpRequests);
256 request.setListener(new SingletonListener(requests, request));
257 requests.addSingleton(request);
262 * Removes expired PDPs from all active groups.
264 public void removeExpiredPdps() {
266 synchronized (modifyLock) {
267 logger.info("check for PDP records older than {}ms", params.getMaxPdpAgeMs());
269 try (PolicyModelsProvider dao = daoFactory.create()) {
271 PdpGroupFilter filter = PdpGroupFilter.builder().groupState(PdpState.ACTIVE).build();
272 List<PdpGroup> groups = dao.getFilteredPdpGroups(filter);
273 List<PdpGroup> updates = new ArrayList<>(1);
275 var status = new DeploymentStatus(dao);
277 Instant minAge = Instant.now().minusMillis(params.getMaxPdpAgeMs());
279 for (PdpGroup group : groups) {
280 Set<String> pdps = removeFromGroup(minAge, group);
281 if (!pdps.isEmpty()) {
283 status.loadByGroup(group.getName());
284 pdps.forEach(status::deleteDeployment);
288 if (!updates.isEmpty()) {
289 dao.updatePdpGroups(updates);
291 var notification = new PolicyNotification();
292 status.flush(notification);
294 policyNotifier.publish(notification);
297 } catch (PfModelException e) {
298 logger.warn("failed to remove expired PDPs", e);
304 * Removes expired PDPs from a group.
306 * @param minAge minimum age for active PDPs
307 * @param group group from which expired PDPs should be removed
308 * @return the expired PDPs
310 private Set<String> removeFromGroup(Instant minAge, PdpGroup group) {
311 Set<String> pdps = new HashSet<>();
312 for (PdpSubGroup subgrp : group.getPdpSubgroups()) {
313 removeFromSubgroup(minAge, group, subgrp, pdps);
320 * Removes expired PDPs from a subgroup.
322 * @param minAge minimum age for active PDPs
323 * @param group group from which to attempt to remove the PDP
324 * @param subgrp subgroup from which to attempt to remove the PDP
325 * @param pdps where to place the expired PDPs
327 private void removeFromSubgroup(Instant minAge, PdpGroup group, PdpSubGroup subgrp, Set<String> pdps) {
329 Iterator<Pdp> iter = subgrp.getPdpInstances().iterator();
331 while (iter.hasNext()) {
332 Pdp instance = iter.next();
334 if (instance.getLastUpdate().isBefore(minAge)) {
335 String pdpName = instance.getInstanceId();
336 logger.info("removed {} from group={} subgroup={}", pdpName, group.getName(), subgrp.getPdpType());
338 subgrp.setCurrentInstanceCount(subgrp.getPdpInstances().size());
345 * Creates a new set of requests for a PDP. May be overridden by junit tests.
347 * @param pdpName PDP name
348 * @return a new set of requests
350 protected PdpRequests makePdpRequests(String pdpName) {
351 return new PdpRequests(pdpName, policyNotifier);
355 * Makes a handler for PDP responses.
356 * @return a response handler
358 protected PdpStatusMessageHandler makePdpResponseHandler() {
359 return new PdpStatusMessageHandler(params.getParams());
363 * Listener for singleton request events.
365 private class SingletonListener implements RequestListener {
366 private final PdpRequests requests;
367 private final Request request;
368 private final String pdpName;
370 public SingletonListener(PdpRequests requests, Request request) {
371 this.requests = requests;
372 this.request = request;
373 this.pdpName = requests.getPdpName();
377 public void failure(String responsePdpName, String reason) {
378 Collection<ToscaConceptIdentifier> undeployPolicies = requestCompleted(responsePdpName);
379 if (undeployPolicies.isEmpty()) {
380 // nothing to undeploy
385 * Undeploy the extra policies. Note: this will likely cause a new message to
386 * be assigned to the request, thus we must re-start it after making the
389 PdpMessage oldmsg = request.getMessage();
392 logger.warn("undeploy policies from {}:{} that failed to deploy: {}", oldmsg.getPdpGroup(),
393 oldmsg.getPdpSubgroup(), undeployPolicies);
394 policyUndeployer.undeploy(oldmsg.getPdpGroup(), oldmsg.getPdpSubgroup(), undeployPolicies);
395 } catch (PfModelException | RuntimeException e) {
396 logger.error("cannot undeploy policies {}", undeployPolicies, e);
399 if (request.getMessage() == oldmsg) {
400 // message is unchanged - start the next request
401 startNextRequest(request);
403 // message changed - restart the request
404 request.startPublishing();
409 public void success(String responsePdpName, PdpStatus response) {
410 requestCompleted(responsePdpName);
412 if (!(request instanceof UpdateReq)) {
413 // other response types may not include the list of policies
418 * Update PDP time stamps. Also send pdp-update and pdp-state-change, as
419 * necessary, if the response does not reflect what's in the DB.
421 var handler = makePdpResponseHandler();
422 handler.handlePdpStatus(response);
426 * Handles a request completion, starting the next request, if there is one.
428 * @param responsePdpName name of the PDP provided in the response
429 * @return a list of policies to be undeployed
431 private Collection<ToscaConceptIdentifier> requestCompleted(String responsePdpName) {
432 if (!pdpName.equals(responsePdpName)) {
433 return Collections.emptyList();
436 if (pdp2requests.get(pdpName) != requests) {
437 logger.info("discard old requests for {}", responsePdpName);
438 requests.stopPublishing();
439 return Collections.emptyList();
442 if (!requests.isFirstInQueue(request)) {
443 logger.error("request is not first in the queue {}", request.getMessage());
444 return Collections.emptyList();
447 Collection<ToscaConceptIdentifier> undeployPolicies = request.getUndeployPolicies();
448 if (undeployPolicies.isEmpty()) {
449 // nothing to undeploy - just start the next request
450 startNextRequest(request);
453 return undeployPolicies;
457 public void retryCountExhausted(Request request) {
458 if (pdp2requests.get(pdpName) == requests) {
459 requests.stopPublishing();
460 startNextRequest(request);
465 * Starts the next request associated with a PDP.
467 * @param request the request that just completed
469 private void startNextRequest(Request request) {
470 if (!requests.startNextRequest(request)) {
471 pdp2requests.remove(pdpName, requests);