2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pap.main.rest;
23 import java.util.Collection;
24 import java.util.stream.Collectors;
25 import javax.ws.rs.core.Response.Status;
26 import org.onap.policy.common.utils.services.Registry;
27 import org.onap.policy.models.base.PfModelException;
28 import org.onap.policy.models.base.PfModelRuntimeException;
29 import org.onap.policy.models.pdp.concepts.Pdp;
30 import org.onap.policy.models.pdp.concepts.PdpGroup;
31 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
32 import org.onap.policy.models.pdp.concepts.PdpUpdate;
33 import org.onap.policy.models.provider.PolicyModelsProvider;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifierOptVersion;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
37 import org.onap.policy.pap.main.PapConstants;
38 import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper;
39 import org.onap.policy.pap.main.comm.PdpModifyRequestMap;
40 import org.onap.policy.pap.main.notification.PolicyNotifier;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
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 private static final String DEPLOY_FAILED = "failed to deploy/undeploy policies";
55 public static final String DB_ERROR_MSG = "DB error";
57 private static final Logger logger = LoggerFactory.getLogger(ProviderBase.class);
60 * Lock used when updating PDPs.
62 private final Object updateLock;
65 * Used to send UPDATE and STATE-CHANGE requests to the PDPs.
67 private final PdpModifyRequestMap requestMap;
70 * Generates policy notifications based on responses from PDPs.
72 private final PolicyNotifier notifier;
75 * Factory for PAP DAO.
77 private final PolicyModelsProviderFactoryWrapper daoFactory;
81 * Constructs the object.
83 public ProviderBase() {
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 request PDP policy request
94 * @param processor function that processes the request
95 * @throws PfModelException if an error occurred
97 protected <T> void process(T request, BiConsumerWithEx<SessionData, T> processor) throws PfModelException {
99 synchronized (updateLock) {
102 try (PolicyModelsProvider dao = daoFactory.create()) {
104 data = new SessionData(dao);
105 processor.accept(data, request);
107 // make all of the DB updates
110 } catch (PfModelException | PfModelRuntimeException e) {
111 logger.warn(DEPLOY_FAILED, e);
114 } catch (RuntimeException e) {
115 logger.warn(DEPLOY_FAILED, e);
116 throw new PfModelException(Status.INTERNAL_SERVER_ERROR, "request failed", e);
119 // track responses for notification purposes
120 data.getDeployData().forEach(notifier::addDeploymentData);
121 data.getUndeployData().forEach(notifier::addUndeploymentData);
123 // publish the requests
124 data.getPdpRequests().forEach(pair -> requestMap.addRequest(pair.getLeft(), pair.getRight()));
129 * Process a single policy from the request.
131 * @param data session data
132 * @param desiredPolicy request policy
133 * @throws PfModelException if an error occurred
135 protected void processPolicy(SessionData data, ToscaPolicyIdentifierOptVersion desiredPolicy)
136 throws PfModelException {
138 ToscaPolicy policy = getPolicy(data, desiredPolicy);
140 Collection<PdpGroup> groups = getGroups(data, policy.getTypeIdentifier());
141 if (groups.isEmpty()) {
142 throw new PfModelException(Status.BAD_REQUEST, "policy not supported by any PDP group: "
143 + desiredPolicy.getName() + " " + desiredPolicy.getVersion());
146 Updater updater = makeUpdater(data, policy, desiredPolicy);
148 for (PdpGroup group : groups) {
149 upgradeGroup(data, group, updater);
154 * Makes a function to update a subgroup. The function is expected to return
155 * {@code true} if the subgroup was updated, {@code false} if no update was
156 * necessary/appropriate.
158 * @param data session data
159 * @param policy policy to be added to or removed from each subgroup
160 * @param desiredPolicy request policy
161 * @return a function to update a subgroup
163 protected abstract Updater makeUpdater(SessionData data, ToscaPolicy policy,
164 ToscaPolicyIdentifierOptVersion desiredPolicy);
167 * Finds the active PDP group(s) that supports the given policy type.
169 * @param data session data
170 * @param policyType the policy type of interest
171 * @return the matching PDP group, or {@code null} if no active group supports the
173 * @throws PfModelException if an error occurred
175 private Collection<PdpGroup> getGroups(SessionData data, ToscaPolicyTypeIdentifier policyType)
176 throws PfModelException {
178 return data.getActivePdpGroupsByPolicyType(policyType);
182 * Updates a group, assigning a new version number, if it actually changes.
184 * @param data session data
185 * @param group the original group, to be updated
186 * @param updater function to update a group
187 * @throws PfModelException if an error occurred
189 private void upgradeGroup(SessionData data, PdpGroup group, Updater updater) throws PfModelException {
191 boolean updated = false;
193 for (PdpSubGroup subgroup : group.getPdpSubgroups()) {
195 if (!updater.apply(group, subgroup)) {
201 makeUpdates(data, group, subgroup);
212 * Makes UPDATE messages for each PDP in a subgroup.
214 * @param data session data
215 * @param group group containing the subgroup
216 * @param subgroup subgroup whose PDPs should receive messages
218 protected void makeUpdates(SessionData data, PdpGroup group, PdpSubGroup subgroup) {
219 for (Pdp pdp : subgroup.getPdpInstances()) {
220 data.addUpdate(makeUpdate(data, group, subgroup, pdp));
225 * Makes an UPDATE message for a particular PDP.
227 * @param data session data
228 * @param group group to which the PDP should belong
229 * @param subgroup subgroup to which the PDP should belong
230 * @param pdp the PDP of interest
231 * @return a new UPDATE message
233 private PdpUpdate makeUpdate(SessionData data, PdpGroup group, PdpSubGroup subgroup, Pdp pdp) {
235 PdpUpdate update = new PdpUpdate();
237 update.setName(pdp.getInstanceId());
238 update.setDescription(group.getDescription());
239 update.setPdpGroup(group.getName());
240 update.setPdpSubgroup(subgroup.getPdpType());
241 update.setPolicies(subgroup.getPolicies().stream().map(ToscaPolicyIdentifierOptVersion::new)
242 .map(ident -> getPolicy(data, ident)).collect(Collectors.toList()));
248 * Gets the specified policy.
250 * @param data session data
251 * @param ident policy identifier, with an optional version
252 * @return the policy of interest
253 * @throws PfModelRuntimeException if an error occurred or the policy was not found
255 private ToscaPolicy getPolicy(SessionData data, ToscaPolicyIdentifierOptVersion ident) {
257 ToscaPolicy policy = data.getPolicy(ident);
258 if (policy == null) {
259 throw new PfModelRuntimeException(Status.NOT_FOUND,
260 "cannot find policy: " + ident.getName() + " " + ident.getVersion());
265 } catch (PfModelException e) {
266 throw new PfModelRuntimeException(e.getErrorResponse().getResponseCode(),
267 e.getErrorResponse().getErrorMessage(), e);
272 public static interface BiConsumerWithEx<F, S> {
274 * Performs this operation on the given arguments.
276 * @param firstArg the first input argument
277 * @param secondArg the second input argument
278 * @throws PfModelException if an error occurred
280 void accept(F firstArg, S secondArg) throws PfModelException;
284 public static interface Updater {
285 boolean apply(PdpGroup group, PdpSubGroup subgroup) throws PfModelException;