Fixes for sonar critical issues
[policy/engine.git] / PolicyEngineClient / src / test / java / org / onap / policyengine / Handler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * PolicyEngineClient
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.policyengine;
22
23 import java.util.Collection;
24
25 import org.onap.policy.api.LoadedPolicy;
26 import org.onap.policy.api.NotificationHandler;
27 import org.onap.policy.api.NotificationType;
28 import org.onap.policy.api.PDPNotification;
29 import org.onap.policy.api.PolicyConfig;
30 import org.onap.policy.api.PolicyConfigException;
31 import org.onap.policy.api.PolicyConfigStatus;
32 import org.onap.policy.api.PolicyEngine;
33 import org.onap.policy.api.PolicyEngineException;
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
38 public class Handler implements NotificationHandler{
39
40         private static final Logger LOGGER      = FlexLogger.getLogger(Handler.class);
41         
42         @Override
43         public void notificationReceived(PDPNotification notification) {
44                 System.out.println("Notification Received...");
45                 System.out.println(notification.getNotificationType());
46                 if(notification.getNotificationType().equals(NotificationType.REMOVE)){                                                                                             
47                         System.out.println("Removed Policies: \n");
48                         for(RemovedPolicy removedPolicy: notification.getRemovedPolicies()){
49                                 System.out.println(removedPolicy.getPolicyName());
50                                 System.out.println(removedPolicy.getVersionNo());
51                         }
52                 }else if(notification.getNotificationType().equals(NotificationType.UPDATE)){
53                         System.out.println("Updated Policies: \n");
54                         for(LoadedPolicy updatedPolicy: notification.getLoadedPolicies()){
55                                 System.out.println("policyName : " + updatedPolicy.getPolicyName());
56                                 System.out.println("policyVersion :" + updatedPolicy.getVersionNo());
57                                 if(updatedPolicy.getPolicyName().contains(".Config_")){
58                                         System.out.println("Matches: " + updatedPolicy.getMatches());
59                                         System.out.println("UpdateType: "+ updatedPolicy.getUpdateType());
60                                         // Checking the Name is correct or not. 
61                                         try {
62                                                 PolicyEngine policyEngine = new PolicyEngine("config.properties");
63                                                 @SuppressWarnings("deprecation")
64                                                 Collection<PolicyConfig> policyConfigs = policyEngine.getConfigByPolicyName(updatedPolicy.getPolicyName());
65                                                 for(PolicyConfig policyConfig: policyConfigs){
66                                                         if(policyConfig.getPolicyConfigStatus().equals(PolicyConfigStatus.CONFIG_RETRIEVED)){
67                                                                 System.out.println("Policy Retrieved with this Name notified. ");
68                                                         }else{
69                                                                 System.err.println("\n\n Fail to retrieve policy !!!!\n\n");
70                                                         }
71                                                         // Also Test this case. 
72                                                         if(policyConfig.getPolicyName().equals(updatedPolicy.getPolicyName())){
73                                                                 System.out.println("Policy Name is good. ");
74                                                         }else{
75                                                                 System.err.println("\n\n Fail to check Name \n\n");
76                                                         }
77                                                 }
78                                         } catch (PolicyEngineException e) {
79                                                 LOGGER.error("Exception Occured"+e);
80                                         } catch (PolicyConfigException e) {
81                                                 LOGGER.error("Exception Occured"+e);
82                                         }
83                                 }
84                         }
85                 }else if(notification.getNotificationType().equals(NotificationType.BOTH)){
86                         System.out.println("Both updated and Removed Notification: \n");
87                         System.out.println("Removed Policies: \n");
88                         for(RemovedPolicy removedPolicy: notification.getRemovedPolicies()){
89                                 System.out.println(removedPolicy.getPolicyName());
90                                 System.out.println(removedPolicy.getVersionNo());
91                         }
92                         System.out.println("Updated Policies: \n");
93                         for(LoadedPolicy updatedPolicy: notification.getLoadedPolicies()){
94                                 System.out.println("policyName : " + updatedPolicy.getPolicyName());
95                                 System.out.println("policyVersion :" + updatedPolicy.getVersionNo());
96                                 System.out.println("Matches: " + updatedPolicy.getMatches());
97                                 System.out.println("UpdateType: "+ updatedPolicy.getUpdateType());
98                                 // Checking the Name is correct or not. 
99                                 try {
100                                         PolicyEngine policyEngine = new PolicyEngine("config.properties");
101                                         @SuppressWarnings("deprecation")
102                                         Collection<PolicyConfig> policyConfigs = policyEngine.getConfigByPolicyName(updatedPolicy.getPolicyName());
103                                         for(PolicyConfig policyConfig: policyConfigs){
104                                                 if(policyConfig.getPolicyConfigStatus().equals(PolicyConfigStatus.CONFIG_RETRIEVED)){
105                                                         System.out.println("Policy Retrieved with this Name notified. ");
106                                                 }else{
107                                                         System.err.println("\n\n Fail to retrieve policy !!!!\n\n");
108                                                 }
109                                                 // Also Test this case. 
110                                                 if(policyConfig.getPolicyName().equals(updatedPolicy.getPolicyName())){
111                                                         System.out.println("Policy Name is good. ");
112                                                 }else{
113                                                         System.err.println("\n\n Fail to check Name \n\n");
114                                                 }
115                                         }
116                                 } catch (PolicyEngineException e) {
117                                         LOGGER.error("Exception Occured"+e);
118                                 } catch (PolicyConfigException e) {
119                                         LOGGER.error("Exception Occured"+e);
120                                 }
121                         }
122                 }
123         }
124
125         
126         
127 }