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.RemovedPolicy;
33 import org.openecomp.policy.utils.BackUpHandler;
35 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
37 //import org.apache.log4j.Logger;
39 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
40 import org.openecomp.policy.common.logging.flexlogger.Logger;
43 * BRMSHandler: Notification Handler which listens for PDP Notifications.
44 * Take action only for BRMS policies.
48 public class BRMSHandler implements BackUpHandler{
50 // private static final Logger logger = Logger.getLogger(BRMSHandler.class.getName());
51 private static final Logger logger = FlexLogger.getLogger(BRMSHandler.class.getName());
52 // This Values are fixed for BRMS based rules. Don't change until they are changed during creation in PAP-ADMIN.
53 //private static final String ecompName = "DROOLS";
54 //private static final String configName = "BRMS_RAW_RULE";
56 private BRMSPush bRMSPush = null;
58 public BRMSHandler(String propertiesFile) throws Exception{
59 bRMSPush = new BRMSPush(propertiesFile, this);
63 public void notificationReceived(PDPNotification notification) {
64 logger.info("Notification Recieved");
65 logger.info(notification.getNotificationType().toString());
67 if(BRMSPush.getBackUpMonitor().getFlag()){
68 logger.info("Master Application performing on Notification ");
69 runOnNotification(notification);
71 logger.info("Slave application Skipping Notification.. ");
75 private void removedPolicies(Collection<RemovedPolicy> removedPolicies){
76 Boolean removed = false;
77 logger.info("Removed Policies");
78 for(RemovedPolicy removedPolicy: removedPolicies){
79 logger.info(removedPolicy.getPolicyName());
80 logger.info(removedPolicy.getVersionNo());
81 if(removedPolicy.getPolicyName().contains("_BRMS_")){
83 String name = removedPolicy.getPolicyName().substring(removedPolicy.getPolicyName().indexOf("_BRMS_")+6, removedPolicy.getPolicyName().length());
84 name= name.substring(name.indexOf("_")+1,name.length());
85 logger.info("Policy Removed with this policy Name : " + name);
86 bRMSPush.removeRule(name);
89 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Rertriving policy failed " + e.getMessage());
98 public void runOnNotification(PDPNotification notification){
99 if(notification.getNotificationType().equals(NotificationType.REMOVE)){
100 removedPolicies(notification.getRemovedPolicies());
101 }else if(notification.getNotificationType().equals(NotificationType.UPDATE)|| notification.getNotificationType().equals(NotificationType.BOTH)){
102 logger.info("Updated Policies: \n");
103 for(LoadedPolicy updatedPolicy: notification.getLoadedPolicies()){
104 logger.info("policyName : " + updatedPolicy.getPolicyName());
105 logger.info("policyVersion :" + updatedPolicy.getVersionNo());
106 logger.info("Matches: " + updatedPolicy.getMatches());
107 // Checking the Name is correct or not.
108 if(updatedPolicy.getPolicyName().contains("_BRMS_")){
110 PolicyEngine policyEngine = BRMSGateway.getPolicyEngine();
111 if(policyEngine!=null){
112 ConfigRequestParameters configRequestParameters = new ConfigRequestParameters();
113 configRequestParameters.setPolicyName(updatedPolicy.getPolicyName());
114 Collection<PolicyConfig> policyConfigs = policyEngine.getConfig(configRequestParameters);
115 for(PolicyConfig policyConfig: policyConfigs){
116 if(policyConfig.getPolicyConfigStatus().equals(PolicyConfigStatus.CONFIG_RETRIEVED)){
117 String name = policyConfig.getPolicyName().substring(policyConfig.getPolicyName().indexOf("_BRMS_")+6, policyConfig.getPolicyName().length());
118 name= name.substring(name.indexOf("_")+1,name.length());
119 logger.info("Policy Retrieved with this Name notified: " + name);
120 bRMSPush.addRule(name,policyConfig.toOther(),policyConfig.getResponseAttributes());
122 logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR +"Fail to retrieve policy so rule will not be pushed to PolicyRepo !!!!\n\n");
127 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Rertriving policy failed " + e.getMessage());
131 bRMSPush.pushRules();