2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2020 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 java.util.stream.Collectors;
27 import javax.ws.rs.core.Response.Status;
28 import org.onap.policy.common.utils.services.Registry;
29 import org.onap.policy.models.base.PfModelException;
30 import org.onap.policy.models.base.PfModelRuntimeException;
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.ToscaPolicy;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifierOptVersion;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
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;
78 * Constructs the object.
80 protected ProviderBase() {
81 this.updateLock = Registry.get(PapConstants.REG_PDP_MODIFY_LOCK, Object.class);
82 this.requestMap = Registry.get(PapConstants.REG_PDP_MODIFY_MAP, PdpModifyRequestMap.class);
83 this.daoFactory = Registry.get(PapConstants.REG_PAP_DAO_FACTORY, PolicyModelsProviderFactoryWrapper.class);
84 this.notifier = Registry.get(PapConstants.REG_POLICY_NOTIFIER, PolicyNotifier.class);
88 * Processes a policy request.
90 * @param request PDP policy request
91 * @param processor function that processes the request
92 * @throws PfModelException if an error occurred
94 protected <T> void process(T request, BiConsumerWithEx<SessionData, T> processor) throws PfModelException {
96 synchronized (updateLock) {
99 try (PolicyModelsProvider dao = daoFactory.create()) {
101 data = new SessionData(dao);
102 processor.accept(data, request);
104 // make all of the DB updates
107 } catch (PfModelException | PfModelRuntimeException e) {
110 } catch (RuntimeException e) {
111 throw new PfModelException(Status.INTERNAL_SERVER_ERROR, "request failed", e);
114 // track responses for notification purposes
115 data.getDeployData().forEach(notifier::addDeploymentData);
116 data.getUndeployData().forEach(notifier::addUndeploymentData);
118 // publish the requests
119 data.getPdpRequests().forEach(pair -> requestMap.addRequest(pair.getLeft(), pair.getRight()));
124 * Process a single policy from the request.
126 * @param data session data
127 * @param desiredPolicy request policy
128 * @throws PfModelException if an error occurred
130 protected void processPolicy(SessionData data, ToscaPolicyIdentifierOptVersion desiredPolicy)
131 throws PfModelException {
133 ToscaPolicy policy = getPolicy(data, desiredPolicy);
135 Collection<PdpGroup> groups = getGroups(data, policy.getTypeIdentifier());
136 if (groups.isEmpty()) {
137 throw new PfModelException(Status.BAD_REQUEST, "policy not supported by any PDP group: "
138 + desiredPolicy.getName() + " " + desiredPolicy.getVersion());
141 Updater updater = makeUpdater(data, policy, desiredPolicy);
143 for (PdpGroup group : groups) {
144 upgradeGroup(data, group, updater);
149 * Makes a function to update a subgroup. The function is expected to return
150 * {@code true} if the subgroup was updated, {@code false} if no update was
151 * necessary/appropriate.
153 * @param data session data
154 * @param policy policy to be added to or removed from each subgroup
155 * @param desiredPolicy request policy
156 * @return a function to update a subgroup
158 protected abstract Updater makeUpdater(SessionData data, ToscaPolicy policy,
159 ToscaPolicyIdentifierOptVersion desiredPolicy);
162 * Finds the active PDP group(s) that supports the given policy type.
164 * @param data session data
165 * @param policyType the policy type of interest
166 * @return the matching PDP group, or {@code null} if no active group supports the
168 * @throws PfModelException if an error occurred
170 private Collection<PdpGroup> getGroups(SessionData data, ToscaPolicyTypeIdentifier policyType)
171 throws PfModelException {
173 return data.getActivePdpGroupsByPolicyType(policyType);
177 * Updates a group, assigning a new version number, if it actually changes.
179 * @param data session data
180 * @param group the original group, to be updated
181 * @param updater function to update a group
182 * @throws PfModelException if an error occurred
184 private void upgradeGroup(SessionData data, PdpGroup group, Updater updater) throws PfModelException {
186 boolean updated = false;
188 for (PdpSubGroup subgroup : group.getPdpSubgroups()) {
190 if (!updater.apply(group, subgroup)) {
196 makeUpdates(data, group, subgroup);
207 * Makes UPDATE messages for each PDP in a subgroup.
209 * @param data session data
210 * @param group group containing the subgroup
211 * @param subgroup subgroup whose PDPs should receive messages
213 protected void makeUpdates(SessionData data, PdpGroup group, PdpSubGroup subgroup) {
214 for (Pdp pdp : subgroup.getPdpInstances()) {
215 data.addUpdate(makeUpdate(data, group, subgroup, pdp));
220 * Makes an UPDATE message for a particular PDP.
222 * @param data session data
223 * @param group group to which the PDP should belong
224 * @param subgroup subgroup to which the PDP should belong
225 * @param pdp the PDP of interest
226 * @return a new UPDATE message
228 private PdpUpdate makeUpdate(SessionData data, PdpGroup group, PdpSubGroup subgroup, Pdp pdp) {
230 PdpUpdate update = new PdpUpdate();
232 update.setName(pdp.getInstanceId());
233 update.setDescription(group.getDescription());
234 update.setPdpGroup(group.getName());
235 update.setPdpSubgroup(subgroup.getPdpType());
236 update.setPolicies(subgroup.getPolicies().stream().map(ToscaPolicyIdentifierOptVersion::new)
237 .map(ident -> getPolicy(data, ident)).collect(Collectors.toList()));
243 * Gets the specified policy.
245 * @param data session data
246 * @param ident policy identifier, with an optional version
247 * @return the policy of interest
248 * @throws PfModelRuntimeException if an error occurred or the policy was not found
250 private ToscaPolicy getPolicy(SessionData data, ToscaPolicyIdentifierOptVersion ident) {
252 ToscaPolicy policy = data.getPolicy(ident);
253 if (policy == null) {
254 throw new PfModelRuntimeException(Status.NOT_FOUND,
255 "cannot find policy: " + ident.getName() + " " + ident.getVersion());
260 } catch (PfModelException e) {
261 throw new PfModelRuntimeException(e.getErrorResponse().getResponseCode(),
262 e.getErrorResponse().getErrorMessage(), e);
267 public static interface BiConsumerWithEx<F, S> {
269 * Performs this operation on the given arguments.
271 * @param firstArg the first input argument
272 * @param secondArg the second input argument
273 * @throws PfModelException if an error occurred
275 void accept(F firstArg, S secondArg) throws PfModelException;
279 public static interface Updater {
280 boolean apply(PdpGroup group, PdpSubGroup subgroup) throws PfModelException;