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;
45 * Super class of providers that deploy and undeploy PDP groups. The following items must
46 * be in the {@link Registry}:
48 * <li>PDP Modification Lock</li>
49 * <li>PDP Modify Request Map</li>
50 * <li>PAP DAO Factory</li>
53 public abstract class ProviderBase {
54 public static final String DB_ERROR_MSG = "DB error";
57 * Lock used when updating PDPs.
59 private final Object updateLock;
62 * Used to send UPDATE and STATE-CHANGE requests to the PDPs.
64 private final PdpModifyRequestMap requestMap;
67 * Generates policy notifications based on responses from PDPs.
69 private final PolicyNotifier notifier;
72 * Factory for PAP DAO.
74 private final PolicyModelsProviderFactoryWrapper daoFactory;
77 * Constructs the object.
79 protected ProviderBase() {
80 this.updateLock = Registry.get(PapConstants.REG_PDP_MODIFY_LOCK, Object.class);
81 this.requestMap = Registry.get(PapConstants.REG_PDP_MODIFY_MAP, PdpModifyRequestMap.class);
82 this.daoFactory = Registry.get(PapConstants.REG_PAP_DAO_FACTORY, PolicyModelsProviderFactoryWrapper.class);
83 this.notifier = Registry.get(PapConstants.REG_POLICY_NOTIFIER, PolicyNotifier.class);
87 * Processes a policy request.
89 * @param request PDP policy request
90 * @param processor function that processes the request
91 * @throws PfModelException if an error occurred
93 protected <T> void process(T request, BiConsumerWithEx<SessionData, T> processor) throws PfModelException {
95 synchronized (updateLock) {
97 var notif = new PolicyNotification();
99 try (PolicyModelsProvider dao = daoFactory.create()) {
101 data = new SessionData(dao);
102 processor.accept(data, request);
104 // make all of the DB updates
105 data.updateDb(notif);
107 } catch (PfModelException | PfModelRuntimeException e) {
110 } catch (RuntimeException e) {
111 throw new PfModelException(Status.INTERNAL_SERVER_ERROR, "request failed", e);
114 // publish the requests
115 data.getPdpRequests().forEach(pair -> requestMap.addRequest(pair.getLeft(), pair.getRight()));
117 // publish the notifications
118 notifier.publish(notif);
123 * Process a single policy from the request.
125 * @param data session data
126 * @param desiredPolicy request policy
127 * @throws PfModelException if an error occurred
129 protected void processPolicy(SessionData data, ToscaConceptIdentifierOptVersion desiredPolicy)
130 throws PfModelException {
132 ToscaPolicy policy = getPolicy(data, desiredPolicy);
134 Collection<PdpGroup> groups = getGroups(data, policy.getTypeIdentifier());
135 if (groups.isEmpty()) {
136 throw new PfModelException(Status.BAD_REQUEST, "policy not supported by any PDP group: "
137 + desiredPolicy.getName() + " " + desiredPolicy.getVersion());
140 var updater = makeUpdater(data, policy, desiredPolicy);
142 for (PdpGroup group : groups) {
143 upgradeGroup(data, group, updater);
148 * Makes a function to update a subgroup. The function is expected to return
149 * {@code true} if the subgroup was updated, {@code false} if no update was
150 * necessary/appropriate.
152 * @param data session data
153 * @param policy policy to be added to or removed from each subgroup
154 * @param desiredPolicy request policy
155 * @return a function to update a subgroup
157 protected abstract Updater makeUpdater(SessionData data, ToscaPolicy policy,
158 ToscaConceptIdentifierOptVersion desiredPolicy);
161 * Finds the active PDP group(s) that supports the given policy type.
163 * @param data session data
164 * @param policyType the policy type of interest
165 * @return the matching PDP group, or {@code null} if no active group supports the
167 * @throws PfModelException if an error occurred
169 private Collection<PdpGroup> getGroups(SessionData data, ToscaConceptIdentifier policyType)
170 throws PfModelException {
172 return data.getActivePdpGroupsByPolicyType(policyType);
176 * Updates a group, assigning a new version number, if it actually changes.
178 * @param data session data
179 * @param group the original group, to be updated
180 * @param updater function to update a group
181 * @throws PfModelException if an error occurred
183 private void upgradeGroup(SessionData data, PdpGroup group, Updater updater)
184 throws PfModelException {
188 for (PdpSubGroup subgroup : group.getPdpSubgroups()) {
190 if (!updater.apply(group, subgroup)) {
196 makeUpdates(data, group, subgroup);
206 * Makes UPDATE messages for each PDP in a subgroup.
208 * @param data session data
209 * @param group group containing the subgroup
210 * @param subgroup subgroup whose PDPs should receive messages
212 protected void makeUpdates(SessionData data, PdpGroup group, PdpSubGroup subgroup) {
213 for (Pdp pdp : subgroup.getPdpInstances()) {
214 data.addUpdate(makeUpdate(data, group, subgroup, pdp));
219 * Makes an UPDATE message for a particular PDP.
221 * @param data session data
222 * @param group group to which the PDP should belong
223 * @param subgroup subgroup to which the PDP should belong
224 * @param pdp the PDP of interest
225 * @return a new UPDATE message
227 private PdpUpdate makeUpdate(SessionData data, PdpGroup group, PdpSubGroup subgroup, Pdp pdp) {
229 var update = new PdpUpdate();
231 update.setName(pdp.getInstanceId());
232 update.setDescription(group.getDescription());
233 update.setPdpGroup(group.getName());
234 update.setPdpSubgroup(subgroup.getPdpType());
235 update.setPoliciesToBeDeployed(data.getPoliciesToBeDeployed());
236 update.setPoliciesToBeUndeployed(data.getPoliciesToBeUndeployed());
242 * Gets the specified policy.
244 * @param data session data
245 * @param ident policy identifier, with an optional version
246 * @return the policy of interest
247 * @throws PfModelRuntimeException if an error occurred or the policy was not found
249 private ToscaPolicy getPolicy(SessionData data, ToscaConceptIdentifierOptVersion ident) {
251 ToscaPolicy policy = data.getPolicy(ident);
252 if (policy == null) {
253 throw new PfModelRuntimeException(Status.NOT_FOUND,
254 "cannot find policy: " + ident.getName() + " " + ident.getVersion());
259 } catch (PfModelException e) {
260 throw new PfModelRuntimeException(e.getErrorResponse().getResponseCode(),
261 e.getErrorResponse().getErrorMessage(), e);
266 public static interface BiConsumerWithEx<F, S> {
268 * Performs this operation on the given arguments.
270 * @param firstArg the first input argument
271 * @param secondArg the second input argument
272 * @throws PfModelException if an error occurred
274 void accept(F firstArg, S secondArg) throws PfModelException;
278 public static interface Updater {
279 boolean apply(PdpGroup group, PdpSubGroup subgroup) throws PfModelException;