2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.brmsInterface;
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.
42 * Take action only for BRMS policies.
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(String propertiesFile) throws PolicyException{
53 setBRMSPush(new BRMSPush(propertiesFile, this));
56 public void setBRMSPush(BRMSPush brmsPush) {
57 this.bRMSPush = brmsPush;
61 * This Method is executed upon notification by the Policy Engine API Notification.
63 * @see org.onap.policy.utils.BackUpHandler#notificationReceived(org.onap.policy.api.PDPNotification)
66 public void notificationReceived(PDPNotification notification) {
67 logger.info("Notification Recieved");
68 logger.info(notification.getNotificationType().toString());
69 Boolean flag = BRMSPush.getBackUpMonitor().getFlag();
70 bRMSPush.initiate(flag);
72 logger.info("Master Application performing on Notification ");
73 runOnNotification(notification);
75 logger.info("Slave application Skipping Notification.. ");
80 * Executed when a policy is removed from PDP.
82 private void removedPolicies(Collection<RemovedPolicy> removedPolicies){
83 Boolean removed = false;
84 logger.info("Removed Policies");
85 for(RemovedPolicy removedPolicy: removedPolicies){
86 logger.info(removedPolicy.getPolicyName());
87 logger.info(removedPolicy.getVersionNo());
88 if(removedPolicy.getPolicyName().contains("_BRMS_")){
90 logger.info("Policy Removed with this policy Name : " + removedPolicy.getPolicyName());
91 bRMSPush.removeRule(removedPolicy.getPolicyName());
94 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Rertriving policy failed " + e.getMessage(), e);
104 bRMSPush.pushRules();
105 }catch(PolicyException e){
106 //Upon Notification failure
108 bRMSPush.rotateURLs();
109 logger.error("Failure during Push Operation " , e);
113 }while(failureFlag && i< bRMSPush.urlListSize());
117 * This method is executed if BRMSGW is "MASTER"
119 * @see org.onap.policy.utils.BackUpHandler#runOnNotification(org.onap.policy.api.PDPNotification)
122 public void runOnNotification(PDPNotification notification){
123 if(notification.getNotificationType().equals(NotificationType.REMOVE)){
124 removedPolicies(notification.getRemovedPolicies());
125 }else if(notification.getNotificationType().equals(NotificationType.UPDATE)|| notification.getNotificationType().equals(NotificationType.BOTH)){
126 logger.info("Updated Policies: \n");
127 ArrayList<PolicyConfig> brmsPolicies = addedPolicies(notification);
128 Boolean successFlag = false;
129 for(int i=0; !successFlag && i< bRMSPush.urlListSize(); i++){
131 for(PolicyConfig policyConfig: brmsPolicies){
132 logger.info("Policy Retry with this Name notified: " + policyConfig.getPolicyName());
133 bRMSPush.addRule(policyConfig.getPolicyName(),policyConfig.toOther(),policyConfig.getResponseAttributes());
137 bRMSPush.pushRules();
139 }catch(PolicyException e){
140 //Upon Notification failure
142 bRMSPush.rotateURLs();
143 logger.error("Failure during Push Operation " , e);
150 * Executed when a policy is added to PDP.
152 private ArrayList<PolicyConfig> addedPolicies(PDPNotification notification) {
153 ArrayList<PolicyConfig> result = new ArrayList<>();
154 for(LoadedPolicy updatedPolicy: notification.getLoadedPolicies()){
155 logger.info("policyName : " + updatedPolicy.getPolicyName());
156 logger.info("policyVersion :" + updatedPolicy.getVersionNo());
157 logger.info("Matches: " + updatedPolicy.getMatches());
158 // Checking the Name is correct or not.
159 if(updatedPolicy.getPolicyName().contains("_BRMS_")){
161 PolicyEngine policyEngine = getPolicyEngine();
162 if(policyEngine!=null){
163 ConfigRequestParameters configRequestParameters = new ConfigRequestParameters();
164 configRequestParameters.setPolicyName(updatedPolicy.getPolicyName());
165 Collection<PolicyConfig> policyConfigs = policyEngine.getConfig(configRequestParameters);
166 for(PolicyConfig policyConfig: policyConfigs){
167 if(policyConfig.getPolicyConfigStatus().equals(PolicyConfigStatus.CONFIG_RETRIEVED)){
168 logger.info("Policy Retrieved with this Name notified: " + policyConfig.getPolicyName());
169 result.add(policyConfig);
170 bRMSPush.addRule(policyConfig.getPolicyName(),policyConfig.toOther(),policyConfig.getResponseAttributes());
172 logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR +"Fail to retrieve policy so rule will not be pushed to PolicyRepo !!!!\n\n");
177 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Rertriving policy failed " + e.getMessage(), e);
184 public PolicyEngine getPolicyEngine() {
185 return BRMSGateway.getPolicyEngine();