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.onap.policyengine;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Collection;
27 import java.util.UUID;
29 import org.onap.policy.api.DecisionResponse;
30 import org.onap.policy.api.LoadedPolicy;
31 import org.onap.policy.api.NotificationScheme;
32 import org.onap.policy.api.PolicyConfig;
33 import org.onap.policy.api.PolicyConfigException;
34 import org.onap.policy.api.PolicyDecisionException;
35 import org.onap.policy.api.PolicyEngine;
36 import org.onap.policy.api.PolicyEventException;
37 import org.onap.policy.api.PolicyResponse;
38 import org.onap.policy.api.RemovedPolicy;
41 * Class contains static functions which make call to policy engine using API.
42 * This class is used by generalTestClient.java
48 public class PolicyEngineTestClient {
50 * This function make API call to policy engine to get config. And returns
51 * policy name, policy version and policy configStatus
53 * @param policyEngine Policy Engine object
54 * @param onapComponentName String
55 * @param configName String
56 * @param configAttributes Map of String to String
57 * @return ArrayList of String
60 @SuppressWarnings("deprecation")
61 public static ArrayList<String> getConfig(PolicyEngine policyEngine, String onapComponentName, String configName,
62 Map<String, String> configAttributes) {
63 ArrayList<String> resultReceived = new ArrayList<>();
65 UUID requestID = UUID.randomUUID();
66 Collection<PolicyConfig> policyConfigs;
67 if (configName == null) {
68 policyConfigs = policyEngine.getConfig(onapComponentName, requestID);
70 if (configAttributes == null) {
71 policyConfigs = policyEngine.getConfig(onapComponentName, configName, requestID);
74 policyConfigs = policyEngine.getConfig(onapComponentName, configName, configAttributes, requestID);
77 if (policyConfigs != null && !policyConfigs.isEmpty()) {
78 for (PolicyConfig policyConfig : policyConfigs) {
79 resultReceived.add("Policy Name: " + policyConfig.getPolicyName() + " Policy version: "
80 + policyConfig.getPolicyVersion() + " - " + policyConfig.getPolicyConfigStatus());
83 } catch (PolicyConfigException e) {
84 // logger.error("Exception Occured"+e);
85 resultReceived.add("" + e);
87 return resultReceived;
91 * This functions make API call to policy engine to get decision. And
92 * returns policy Decision
94 * @param policyEngine Policy Engine
95 * @param onapComponentName String
96 * @param decisionAttributes Map of String
97 * @return List of String
99 public static ArrayList<String> getDecision(PolicyEngine policyEngine, String onapComponentName,
100 Map<String, String> decisionAttributes) {
101 ArrayList<String> resultReceived = new ArrayList<>();
104 UUID requestID = UUID.randomUUID();
105 @SuppressWarnings("deprecation")
106 DecisionResponse policyDecision =
107 policyEngine.getDecision(onapComponentName, decisionAttributes, requestID);
108 resultReceived.add(policyDecision.getDecision().toString());
109 } catch (PolicyDecisionException e) {
110 // logger.error("Exception Occured"+e);
111 resultReceived.add("" + e);
113 return resultReceived;
117 * This function makes API call to policy engine to get action. And returns
118 * responseMessage and responseStatus
120 * @param policyEngine Policy Engine object
121 * @param eventAttributes Map of String
122 * @return List of String
124 public static ArrayList<String> getAction(PolicyEngine policyEngine, Map<String, String> eventAttributes) {
125 ArrayList<String> resultReceived = new ArrayList<>();
127 UUID requestID = UUID.randomUUID();
128 @SuppressWarnings("deprecation")
129 Collection<PolicyResponse> policyResponses = policyEngine.sendEvent(eventAttributes, requestID);
130 if (policyResponses != null && !policyResponses.isEmpty()) {
131 for (PolicyResponse policyResponse : policyResponses) {
132 resultReceived.add(policyResponse.getPolicyResponseMessage() + " : "
133 + policyResponse.getPolicyResponseStatus());
136 } catch (PolicyEventException e) {
137 // logger.error("Exception Occured"+e);
138 resultReceived.add("" + e);
140 return resultReceived;
144 * This function makes API call to policy engine to get manual
147 * @param policyEngine Policy Engine object
150 public static void getManualNotifications(PolicyEngine policyEngine) {
151 policyEngine.setScheme(NotificationScheme.MANUAL_ALL_NOTIFICATIONS);
152 System.out.println(policyEngine.getNotification().getNotificationType());
153 for (LoadedPolicy updated : policyEngine.getNotification().getLoadedPolicies()) {
154 System.out.println(updated.getPolicyName());
155 System.out.println(updated.getVersionNo());
156 System.out.println(updated.getMatches());
158 for (RemovedPolicy removed : policyEngine.getNotification().getRemovedPolicies()) {
159 System.out.println(removed.getPolicyName());
160 System.out.println(removed.getVersionNo());
165 * This function makes API call to policy engine to get automatic
168 * @param policyEngine Policy Engine object
170 public static void getAutoNotifications(PolicyEngine policyEngine) {
171 Handler handler = new Handler();
172 policyEngine.setNotification(NotificationScheme.AUTO_ALL_NOTIFICATIONS, handler);
174 System.out.println("Enter a any key to exit");
177 } catch (IOException e) {