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.openecomp.policy.brmsInterface;
23 import java.util.Collection;
25 import org.openecomp.policy.api.ConfigRequestParameters;
26 import org.openecomp.policy.api.LoadedPolicy;
27 import org.openecomp.policy.api.NotificationType;
28 import org.openecomp.policy.api.PDPNotification;
29 import org.openecomp.policy.api.PolicyConfig;
30 import org.openecomp.policy.api.PolicyConfigStatus;
31 import org.openecomp.policy.api.PolicyEngine;
32 import org.openecomp.policy.api.PolicyException;
33 import org.openecomp.policy.api.RemovedPolicy;
34 import org.openecomp.policy.common.logging.flexlogger.Logger;
35 import org.openecomp.policy.utils.BackUpHandler;
36 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
37 //import org.apache.log4j.Logger;
38 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
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 public void notificationReceived(PDPNotification notification) {
62 logger.info("Notification Recieved");
63 logger.info(notification.getNotificationType().toString());
64 Boolean flag = BRMSPush.getBackUpMonitor().getFlag();
65 bRMSPush.initiate(flag);
67 logger.info("Master Application performing on Notification ");
68 runOnNotification(notification);
70 logger.info("Slave application Skipping Notification.. ");
74 private void removedPolicies(Collection<RemovedPolicy> removedPolicies){
75 Boolean removed = false;
76 logger.info("Removed Policies");
77 for(RemovedPolicy removedPolicy: removedPolicies){
78 logger.info(removedPolicy.getPolicyName());
79 logger.info(removedPolicy.getVersionNo());
80 if(removedPolicy.getPolicyName().contains("_BRMS_")){
82 logger.info("Policy Removed with this policy Name : " + removedPolicy.getPolicyName());
83 bRMSPush.removeRule(removedPolicy.getPolicyName());
86 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Rertriving policy failed " + e.getMessage());
95 public void runOnNotification(PDPNotification notification){
96 if(notification.getNotificationType().equals(NotificationType.REMOVE)){
97 removedPolicies(notification.getRemovedPolicies());
98 }else if(notification.getNotificationType().equals(NotificationType.UPDATE)|| notification.getNotificationType().equals(NotificationType.BOTH)){
99 logger.info("Updated Policies: \n");
100 for(LoadedPolicy updatedPolicy: notification.getLoadedPolicies()){
101 logger.info("policyName : " + updatedPolicy.getPolicyName());
102 logger.info("policyVersion :" + updatedPolicy.getVersionNo());
103 logger.info("Matches: " + updatedPolicy.getMatches());
104 // Checking the Name is correct or not.
105 if(updatedPolicy.getPolicyName().contains("_BRMS_")){
107 PolicyEngine policyEngine = getPolicyEngine();
108 if(policyEngine!=null){
109 ConfigRequestParameters configRequestParameters = new ConfigRequestParameters();
110 configRequestParameters.setPolicyName(updatedPolicy.getPolicyName());
111 Collection<PolicyConfig> policyConfigs = policyEngine.getConfig(configRequestParameters);
112 for(PolicyConfig policyConfig: policyConfigs){
113 if(policyConfig.getPolicyConfigStatus().equals(PolicyConfigStatus.CONFIG_RETRIEVED)){
114 logger.info("Policy Retrieved with this Name notified: " + policyConfig.getPolicyName());
115 bRMSPush.addRule(policyConfig.getPolicyName(),policyConfig.toOther(),policyConfig.getResponseAttributes());
117 logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR +"Fail to retrieve policy so rule will not be pushed to PolicyRepo !!!!\n\n");
122 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Rertriving policy failed " + e.getMessage());
126 bRMSPush.pushRules();
130 public PolicyEngine getPolicyEngine() {
131 return BRMSGateway.getPolicyEngine();