2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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.brms.api;
23 import java.util.ArrayList;
24 import java.util.Collection;
26 import org.onap.policy.api.ConfigRequestParameters;
27 import org.onap.policy.api.LoadedPolicy;
28 import org.onap.policy.api.NotificationType;
29 import org.onap.policy.api.PDPNotification;
30 import org.onap.policy.api.PolicyConfig;
31 import org.onap.policy.api.PolicyConfigStatus;
32 import org.onap.policy.api.PolicyEngine;
33 import org.onap.policy.api.PolicyException;
34 import org.onap.policy.api.RemovedPolicy;
35 import org.onap.policy.common.logging.flexlogger.FlexLogger;
36 import org.onap.policy.common.logging.flexlogger.Logger;
37 import org.onap.policy.utils.BackUpHandler;
38 import org.onap.policy.xacml.api.XACMLErrorConstants;
41 * BRMSHandler: Notification Handler which listens for PDP Notifications. Take action only for BRMS
46 public class BrmsHandler implements BackUpHandler {
48 private static final Logger logger = FlexLogger.getLogger(BrmsHandler.class.getName());
50 private BrmsPush brmsPush = null;
52 public BrmsHandler(final String propertiesFile) throws PolicyException {
53 setBrmsPush(new BrmsPush(propertiesFile, this));
56 public void setBrmsPush(final BrmsPush brmsPush) {
57 this.brmsPush = brmsPush;
61 * This Method is executed upon notification by the Policy Engine API Notification.
65 * org.onap.policy.utils.BackUpHandler#notificationReceived(org.onap.policy.api.PDPNotification)
68 public void notificationReceived(final PDPNotification notification) {
69 logger.info("Notification Recieved");
70 logger.info(notification.getNotificationType().toString());
71 final Boolean flag = BrmsPush.getBackUpMonitor().getFlag();
72 brmsPush.initiate(flag);
74 logger.info("Master Application performing on Notification ");
75 runOnNotification(notification);
77 logger.info("Slave application Skipping Notification.. ");
82 * Executed when a policy is removed from PDP.
84 private void removedPolicies(final Collection<RemovedPolicy> removedPolicies) {
85 Boolean removed = false;
86 logger.info("Removed Policies");
87 for (final RemovedPolicy removedPolicy : removedPolicies) {
88 logger.info(removedPolicy.getPolicyName());
89 logger.info(removedPolicy.getVersionNo());
90 if (removedPolicy.getPolicyName().contains("_BRMS_")) {
92 logger.info("Policy Removed with this policy Name : " + removedPolicy.getPolicyName());
93 brmsPush.removeRule(removedPolicy.getPolicyName());
95 } catch (final Exception e) {
96 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Rertriving policy failed " + e.getMessage(),
107 brmsPush.pushRules();
108 } catch (final PolicyException e) {
109 // Upon Notification failure
111 brmsPush.rotateUrls();
112 logger.error("Failure during Push Operation ", e);
117 while (failureFlag && index < brmsPush.urlListSize());
121 * This method is executed if BRMSGW is "MASTER" (non-Javadoc)
124 * org.onap.policy.utils.BackUpHandler#runOnNotification(org.onap.policy.api.PDPNotification)
127 public void runOnNotification(final PDPNotification notification) {
128 // reset the BRMSPush data structures
130 if (notification.getNotificationType().equals(NotificationType.REMOVE)) {
131 removedPolicies(notification.getRemovedPolicies());
132 } else if (notification.getNotificationType().equals(NotificationType.UPDATE)
133 || notification.getNotificationType().equals(NotificationType.BOTH)) {
134 logger.info("Updated Policies: \n");
135 final ArrayList<PolicyConfig> brmsPolicies = addedPolicies(notification);
136 Boolean successFlag = false;
137 for (int i = 0; !successFlag && i < brmsPush.urlListSize(); i++) {
139 for (final PolicyConfig policyConfig : brmsPolicies) {
140 logger.info("Policy Retry with this Name notified: " + policyConfig.getPolicyName());
141 brmsPush.addRule(policyConfig.getPolicyName(), policyConfig.toOther(),
142 policyConfig.getResponseAttributes());
146 brmsPush.pushRules();
148 } catch (final PolicyException e) {
149 // Upon Notification failure
151 brmsPush.rotateUrls();
152 logger.error("Failure during Push Operation ", e);
159 * Executed when a policy is added to PDP.
161 private ArrayList<PolicyConfig> addedPolicies(final PDPNotification notification) {
162 final ArrayList<PolicyConfig> result = new ArrayList<>();
163 for (final LoadedPolicy updatedPolicy : notification.getLoadedPolicies()) {
164 logger.info("policyName : " + updatedPolicy.getPolicyName());
165 logger.info("policyVersion :" + updatedPolicy.getVersionNo());
166 logger.info("Matches: " + updatedPolicy.getMatches());
167 // Checking the Name is correct or not.
168 if (updatedPolicy.getPolicyName().contains("_BRMS_")) {
170 final PolicyEngine policyEngine = getPolicyEngine();
171 if (policyEngine != null) {
172 final ConfigRequestParameters configRequestParameters = new ConfigRequestParameters();
173 configRequestParameters.setPolicyName(updatedPolicy.getPolicyName());
174 final Collection<PolicyConfig> policyConfigs = policyEngine.getConfig(configRequestParameters);
175 for (final PolicyConfig policyConfig : policyConfigs) {
176 if (policyConfig.getPolicyConfigStatus().equals(PolicyConfigStatus.CONFIG_RETRIEVED)) {
178 "Policy Retrieved with this Name notified: " + policyConfig.getPolicyName());
179 result.add(policyConfig);
180 brmsPush.addRule(policyConfig.getPolicyName(), policyConfig.toOther(),
181 policyConfig.getResponseAttributes());
183 logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR
184 + "Fail to retrieve policy so rule will not be pushed to PolicyRepo !!!!\n\n");
188 } catch (final Exception e) {
189 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Rertriving policy failed " + e.getMessage(),
197 public PolicyEngine getPolicyEngine() {
198 return BrmsGateway.getPolicyEngine();