2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017, 2019 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.onap.policyengine;
23 import java.util.Collection;
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;
38 public class Handler implements NotificationHandler {
40 private static final Logger LOGGER = FlexLogger.getLogger(Handler.class);
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());
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.
62 PolicyEngine policyEngine = new PolicyEngine("config.properties");
63 @SuppressWarnings("deprecation")
64 Collection<PolicyConfig> policyConfigs =
65 policyEngine.getConfigByPolicyName(updatedPolicy.getPolicyName());
66 for (PolicyConfig policyConfig : policyConfigs) {
67 if (policyConfig.getPolicyConfigStatus().equals(PolicyConfigStatus.CONFIG_RETRIEVED)) {
68 System.out.println("Policy Retrieved with this Name notified. ");
70 System.err.println("\n\n Fail to retrieve policy !!!!\n\n");
72 // Also Test this case.
73 if (policyConfig.getPolicyName().equals(updatedPolicy.getPolicyName())) {
74 System.out.println("Policy Name is good. ");
76 System.err.println("\n\n Fail to check Name \n\n");
79 } catch (PolicyEngineException e) {
80 LOGGER.error("Exception Occured" + e);
81 } catch (PolicyConfigException e) {
82 LOGGER.error("Exception Occured" + e);
86 } else if (notification.getNotificationType().equals(NotificationType.BOTH)) {
87 System.out.println("Both updated and Removed Notification: \n");
88 System.out.println("Removed Policies: \n");
89 for (RemovedPolicy removedPolicy : notification.getRemovedPolicies()) {
90 System.out.println(removedPolicy.getPolicyName());
91 System.out.println(removedPolicy.getVersionNo());
93 System.out.println("Updated Policies: \n");
94 for (LoadedPolicy updatedPolicy : notification.getLoadedPolicies()) {
95 System.out.println("policyName : " + updatedPolicy.getPolicyName());
96 System.out.println("policyVersion :" + updatedPolicy.getVersionNo());
97 System.out.println("Matches: " + updatedPolicy.getMatches());
98 System.out.println("UpdateType: " + updatedPolicy.getUpdateType());
99 // Checking the Name is correct or not.
101 PolicyEngine policyEngine = new PolicyEngine("config.properties");
102 @SuppressWarnings("deprecation")
103 Collection<PolicyConfig> policyConfigs =
104 policyEngine.getConfigByPolicyName(updatedPolicy.getPolicyName());
105 for (PolicyConfig policyConfig : policyConfigs) {
106 if (policyConfig.getPolicyConfigStatus().equals(PolicyConfigStatus.CONFIG_RETRIEVED)) {
107 System.out.println("Policy Retrieved with this Name notified. ");
109 System.err.println("\n\n Fail to retrieve policy !!!!\n\n");
111 // Also Test this case.
112 if (policyConfig.getPolicyName().equals(updatedPolicy.getPolicyName())) {
113 System.out.println("Policy Name is good. ");
115 System.err.println("\n\n Fail to check Name \n\n");
118 } catch (PolicyEngineException e) {
119 LOGGER.error("Exception Occured" + e);
120 } catch (PolicyConfigException e) {
121 LOGGER.error("Exception Occured" + e);