2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2020-2021 Nordix Foundation.
7 * Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.pap.main.rest;
25 import java.util.Collection;
26 import javax.ws.rs.core.Response.Status;
27 import org.onap.policy.common.utils.services.Registry;
28 import org.onap.policy.models.base.PfModelException;
29 import org.onap.policy.models.base.PfModelRuntimeException;
30 import org.onap.policy.models.pap.concepts.PolicyNotification;
31 import org.onap.policy.models.pdp.concepts.Pdp;
32 import org.onap.policy.models.pdp.concepts.PdpGroup;
33 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
34 import org.onap.policy.models.pdp.concepts.PdpUpdate;
35 import org.onap.policy.models.provider.PolicyModelsProvider;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
39 import org.onap.policy.pap.main.PapConstants;
40 import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper;
41 import org.onap.policy.pap.main.comm.PdpModifyRequestMap;
42 import org.onap.policy.pap.main.notification.PolicyNotifier;
43 import org.springframework.boot.context.event.ApplicationReadyEvent;
44 import org.springframework.context.event.EventListener;
47 * Super class of providers that deploy and undeploy PDP groups. The following items must
48 * be in the {@link Registry}:
50 * <li>PDP Modification Lock</li>
51 * <li>PDP Modify Request Map</li>
52 * <li>PAP DAO Factory</li>
55 public abstract class ProviderBase {
56 public static final String DB_ERROR_MSG = "DB error";
57 public static final String DEFAULT_USER = "PAP";
60 * Lock used when updating PDPs.
62 private Object updateLock;
65 * Used to send UPDATE and STATE-CHANGE requests to the PDPs.
67 private PdpModifyRequestMap requestMap;
70 * Generates policy notifications based on responses from PDPs.
72 private PolicyNotifier notifier;
75 * Factory for PAP DAO.
77 private PolicyModelsProviderFactoryWrapper daoFactory;
80 * Initializes the parameters..
82 @EventListener(ApplicationReadyEvent.class)
83 public void initialize() {
84 this.updateLock = Registry.get(PapConstants.REG_PDP_MODIFY_LOCK, Object.class);
85 this.requestMap = Registry.get(PapConstants.REG_PDP_MODIFY_MAP, PdpModifyRequestMap.class);
86 this.daoFactory = Registry.get(PapConstants.REG_PAP_DAO_FACTORY, PolicyModelsProviderFactoryWrapper.class);
87 this.notifier = Registry.get(PapConstants.REG_POLICY_NOTIFIER, PolicyNotifier.class);
91 * Processes a policy request.
93 * @param user user triggering request
94 * @param request PDP policy request
95 * @param processor function that processes the request
96 * @throws PfModelException if an error occurred
98 protected <T> void process(String user, T request, BiConsumerWithEx<SessionData, T> processor)
99 throws PfModelException {
101 synchronized (updateLock) {
103 var notif = new PolicyNotification();
105 try (PolicyModelsProvider dao = daoFactory.create()) {
107 data = new SessionData(dao, user);
108 processor.accept(data, request);
110 // make all of the DB updates
111 data.updateDb(notif);
113 } catch (PfModelException | PfModelRuntimeException e) {
116 } catch (RuntimeException e) {
117 throw new PfModelException(Status.INTERNAL_SERVER_ERROR, "request failed", e);
120 // publish the requests
121 data.getPdpRequests().forEach(pair -> requestMap.addRequest(pair.getLeft(), pair.getRight()));
123 // publish the notifications
124 notifier.publish(notif);
129 * Processes a policy request.
131 * @param request PDP policy request
132 * @param processor function that processes the request
133 * @throws PfModelException if an error occurred
135 protected <T> void process(T request, BiConsumerWithEx<SessionData, T> processor) throws PfModelException {
136 this.process(DEFAULT_USER, request, processor);
140 * Process a single policy from the request.
142 * @param data session data
143 * @param desiredPolicy request policy
144 * @throws PfModelException if an error occurred
146 protected void processPolicy(SessionData data, ToscaConceptIdentifierOptVersion desiredPolicy)
147 throws PfModelException {
149 ToscaPolicy policy = getPolicy(data, desiredPolicy);
151 Collection<PdpGroup> groups = getGroups(data, policy.getTypeIdentifier());
152 if (groups.isEmpty()) {
153 throw new PfModelException(Status.BAD_REQUEST, "policy not supported by any PDP group: "
154 + desiredPolicy.getName() + " " + desiredPolicy.getVersion());
157 var updater = makeUpdater(data, policy, desiredPolicy);
159 for (PdpGroup group : groups) {
160 upgradeGroup(data, group, updater);
165 * Makes a function to update a subgroup. The function is expected to return
166 * {@code true} if the subgroup was updated, {@code false} if no update was
167 * necessary/appropriate.
169 * @param data session data
170 * @param policy policy to be added to or removed from each subgroup
171 * @param desiredPolicy request policy
172 * @return a function to update a subgroup
174 protected abstract Updater makeUpdater(SessionData data, ToscaPolicy policy,
175 ToscaConceptIdentifierOptVersion desiredPolicy);
178 * Finds the active PDP group(s) that supports the given policy type.
180 * @param data session data
181 * @param policyType the policy type of interest
182 * @return the matching PDP group, or {@code null} if no active group supports the
184 * @throws PfModelException if an error occurred
186 private Collection<PdpGroup> getGroups(SessionData data, ToscaConceptIdentifier policyType)
187 throws PfModelException {
189 return data.getActivePdpGroupsByPolicyType(policyType);
193 * Updates a group, assigning a new version number, if it actually changes.
195 * @param data session data
196 * @param group the original group, to be updated
197 * @param updater function to update a group
198 * @throws PfModelException if an error occurred
200 private void upgradeGroup(SessionData data, PdpGroup group, Updater updater) throws PfModelException {
204 for (PdpSubGroup subgroup : group.getPdpSubgroups()) {
206 if (!updater.apply(group, subgroup)) {
212 makeUpdates(data, group, subgroup);
222 * Makes UPDATE messages for each PDP in a subgroup.
224 * @param data session data
225 * @param group group containing the subgroup
226 * @param subgroup subgroup whose PDPs should receive messages
228 protected void makeUpdates(SessionData data, PdpGroup group, PdpSubGroup subgroup) {
229 for (Pdp pdp : subgroup.getPdpInstances()) {
230 data.addUpdate(makeUpdate(data, group, subgroup, pdp));
235 * Makes an UPDATE message for a particular PDP.
237 * @param data session data
238 * @param group group to which the PDP should belong
239 * @param subgroup subgroup to which the PDP should belong
240 * @param pdp the PDP of interest
241 * @return a new UPDATE message
243 private PdpUpdate makeUpdate(SessionData data, PdpGroup group, PdpSubGroup subgroup, Pdp pdp) {
245 var update = new PdpUpdate();
247 update.setSource(PapConstants.PAP_NAME);
248 update.setName(pdp.getInstanceId());
249 update.setDescription(group.getDescription());
250 update.setPdpGroup(group.getName());
251 update.setPdpSubgroup(subgroup.getPdpType());
252 update.setPoliciesToBeDeployed(data.getPoliciesToBeDeployed());
253 update.setPoliciesToBeUndeployed(data.getPoliciesToBeUndeployed());
259 * Gets the specified policy.
261 * @param data session data
262 * @param ident policy identifier, with an optional version
263 * @return the policy of interest
264 * @throws PfModelRuntimeException if an error occurred or the policy was not found
266 private ToscaPolicy getPolicy(SessionData data, ToscaConceptIdentifierOptVersion ident) {
268 ToscaPolicy policy = data.getPolicy(ident);
269 if (policy == null) {
270 throw new PfModelRuntimeException(Status.NOT_FOUND,
271 "cannot find policy: " + ident.getName() + " " + ident.getVersion());
276 } catch (PfModelException e) {
277 throw new PfModelRuntimeException(e.getErrorResponse().getResponseCode(),
278 e.getErrorResponse().getErrorMessage(), e);
283 public static interface BiConsumerWithEx<F, S> {
285 * Performs this operation on the given arguments.
287 * @param firstArg the first input argument
288 * @param secondArg the second input argument
289 * @throws PfModelException if an error occurred
291 void accept(F firstArg, S secondArg) throws PfModelException;
295 public static interface Updater {
296 boolean apply(PdpGroup group, PdpSubGroup subgroup) throws PfModelException;