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