Initial OpenECOMP policy/engine commit
[policy/engine.git] / PolicyEngineAPI / src / test / java / org / openecomp / policy / std / test / Handler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * PolicyEngineAPI
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.policy.std.test;
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                                                 Collection<PolicyConfig> policyConfigs = policyEngine.getConfigByPolicyName(updatedPolicy.getPolicyName());
60                                                 for(PolicyConfig policyConfig: policyConfigs){
61                                                         if(policyConfig.getPolicyConfigStatus().equals(PolicyConfigStatus.CONFIG_RETRIEVED)){
62                                                                 System.out.println("Policy Retrieved with this Name notified. ");
63                                                         }else{
64                                                                 System.err.println("\n\n Fail to retrieve policy !!!!\n\n");
65                                                         }
66                                                         // Also Test this case. 
67                                                         if(policyConfig.getPolicyName().equals(updatedPolicy.getPolicyName())){
68                                                                 System.out.println("Policy Name is good. ");
69                                                         }else{
70                                                                 System.err.println("\n\n Fail to check Name \n\n");
71                                                         }
72                                                 }
73                                         } catch (PolicyEngineException e) {
74                                                 e.printStackTrace();
75                                         } catch (PolicyConfigException e) {
76                                                 e.printStackTrace();
77                                         }
78                                 }
79                         }
80                 }else if(notification.getNotificationType().equals(NotificationType.BOTH)){
81                         System.out.println("Both updated and Removed Notification: \n");
82                         System.out.println("Removed Policies: \n");
83                         for(RemovedPolicy removedPolicy: notification.getRemovedPolicies()){
84                                 System.out.println(removedPolicy.getPolicyName());
85                                 System.out.println(removedPolicy.getVersionNo());
86                         }
87                         System.out.println("Updated Policies: \n");
88                         for(LoadedPolicy updatedPolicy: notification.getLoadedPolicies()){
89                                 System.out.println("policyName : " + updatedPolicy.getPolicyName());
90                                 System.out.println("policyVersion :" + updatedPolicy.getVersionNo());
91                                 System.out.println("Matches: " + updatedPolicy.getMatches());
92                                 System.out.println("UpdateType: "+ updatedPolicy.getUpdateType());
93                                 // Checking the Name is correct or not. 
94                                 try {
95                                         PolicyEngine policyEngine = new PolicyEngine("config.properties");
96                                         Collection<PolicyConfig> policyConfigs = policyEngine.getConfigByPolicyName(updatedPolicy.getPolicyName());
97                                         for(PolicyConfig policyConfig: policyConfigs){
98                                                 if(policyConfig.getPolicyConfigStatus().equals(PolicyConfigStatus.CONFIG_RETRIEVED)){
99                                                         System.out.println("Policy Retrieved with this Name notified. ");
100                                                 }else{
101                                                         System.err.println("\n\n Fail to retrieve policy !!!!\n\n");
102                                                 }
103                                                 // Also Test this case. 
104                                                 if(policyConfig.getPolicyName().equals(updatedPolicy.getPolicyName())){
105                                                         System.out.println("Policy Name is good. ");
106                                                 }else{
107                                                         System.err.println("\n\n Fail to check Name \n\n");
108                                                 }
109                                         }
110                                 } catch (PolicyEngineException e) {
111                                         e.printStackTrace();
112                                 } catch (PolicyConfigException e) {
113                                         e.printStackTrace();
114                                 }
115                         }
116                 }
117         }
118
119
120 }