Resolve LF license header issue
[policy/engine.git] / PolicyEngineClient / src / test / java / org / onap / policyengine / PolicyEngineTestClient.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 package org.onap.policyengine;
21
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Map;
26 import java.util.UUID;
27
28 import org.onap.policy.api.DecisionResponse;
29 import org.onap.policy.api.LoadedPolicy;
30 import org.onap.policy.api.NotificationScheme;
31 import org.onap.policy.api.PolicyConfig;
32 import org.onap.policy.api.PolicyConfigException;
33 import org.onap.policy.api.PolicyDecisionException;
34 import org.onap.policy.api.PolicyEngine;
35 import org.onap.policy.api.PolicyEventException;
36 import org.onap.policy.api.PolicyResponse;
37 import org.onap.policy.api.RemovedPolicy;
38
39 /**
40  * Class contains static functions which make call to policy engine using API.
41  * This class is used by generalTestClient.java
42  * 
43  * 
44  * @version 1.0
45  *
46  */
47 public class PolicyEngineTestClient {
48         /**
49          * This fuction make API call to policy engine to get config. And returns
50          * policy name, policy version and policy configStatus
51          * 
52          * @param org.onap.policyEngine
53          * @param onapComponentName
54          * @param configName
55          * @param configAttributes
56          * @return
57          */
58
59         @SuppressWarnings("deprecation")
60         public static ArrayList<String> getConfig(PolicyEngine policyEngine,
61                         String onapComponentName, String configName,
62                         Map<String, String> configAttributes) {
63                 ArrayList<String> resultReceived = new ArrayList<>();
64                 try {
65                         UUID requestID = UUID.randomUUID();
66                         Collection<PolicyConfig> policyConfigs;
67                         if (configName == null) {
68                                 policyConfigs = policyEngine.getConfig(onapComponentName, requestID);
69                         } else {
70                                 if (configAttributes == null) {
71                                         policyConfigs = policyEngine.getConfig(onapComponentName,
72                                                         configName, requestID);
73                                 } else {
74                                         
75                                         policyConfigs = policyEngine.getConfig(onapComponentName,
76                                                         configName, configAttributes, requestID);
77                                 }
78                         }
79                         if (policyConfigs != null && !policyConfigs.isEmpty()) {
80                                 for (PolicyConfig policyConfig : policyConfigs) {
81                                         resultReceived.add("Policy Name: "
82                                                         + policyConfig.getPolicyName()
83                                                         + " Policy version: "
84                                                         + policyConfig.getPolicyVersion() + " - "
85                                                         + policyConfig.getPolicyConfigStatus());
86                                 }
87                         }
88                 } catch (PolicyConfigException e) {
89 //                      logger.error("Exception Occured"+e);
90                         resultReceived.add(""+e);
91                 }
92                 return resultReceived;
93         }
94
95         /**
96          * This functions make API call to policy engine to get decision. And
97          * returns policy Decision
98          * 
99          * @param org.onap.policyEngine
100          * @param onapComponentName
101          * @param decisionAttributes
102          * @return
103          */
104         public static ArrayList<String> getDecision(PolicyEngine policyEngine,
105                         String onapComponentName, Map<String, String> decisionAttributes) {
106                 ArrayList<String> resultReceived = new ArrayList<>();
107                 // Decision example
108                 try {
109                         UUID requestID = UUID.randomUUID();
110                         @SuppressWarnings("deprecation")
111                         DecisionResponse policyDecision = policyEngine.getDecision(
112                                         onapComponentName, decisionAttributes, requestID);
113                         resultReceived.add(policyDecision.getDecision().toString());
114                 } catch (PolicyDecisionException e) {
115 //                      logger.error("Exception Occured"+e);
116                         resultReceived.add(""+e);
117                 }
118                 return resultReceived;
119         }
120
121         /**
122          * This function makes API call to policy engine to get action. And returns
123          * responseMessage and responseStatus
124          * 
125          * @param org.onap.policyEngine
126          * @param eventAttributes
127          * @return
128          */
129         public static ArrayList<String> getAction(PolicyEngine policyEngine,
130                         Map<String, String> eventAttributes) {
131                 ArrayList<String> resultReceived = new ArrayList<>();
132                 try {
133                         UUID requestID = UUID.randomUUID();
134                         @SuppressWarnings("deprecation")
135                         Collection<PolicyResponse> policyResponses = policyEngine
136                                         .sendEvent(eventAttributes, requestID);
137                         if (policyResponses != null && !policyResponses.isEmpty()) {
138                                 for (PolicyResponse policyResponse : policyResponses) {
139                                         resultReceived.add(policyResponse
140                                                         .getPolicyResponseMessage()
141                                                         + " : "
142                                                         + policyResponse.getPolicyResponseStatus());
143                                 }
144                         }
145                 } catch (PolicyEventException e) {
146 //                      logger.error("Exception Occured"+e);
147                         resultReceived.add(""+e);
148                 }
149                 return resultReceived;
150         }
151
152         /**
153          * This function makes API call to policy engine to get manual
154          * notifications.
155          * 
156          * @param org.onap.policyEngine
157          */
158
159         public static void getManualNotifications(PolicyEngine policyEngine) {
160                 policyEngine.setScheme(NotificationScheme.MANUAL_ALL_NOTIFICATIONS);
161                 System.out.println(policyEngine.getNotification().getNotificationType());
162                 for (LoadedPolicy updated : policyEngine.getNotification().getLoadedPolicies()) {
163                         System.out.println(updated.getPolicyName());
164                         System.out.println(updated.getVersionNo());
165                         System.out.println(updated.getMatches());
166                 }
167                 for (RemovedPolicy removed : policyEngine.getNotification()
168                                 .getRemovedPolicies()) {
169                         System.out.println(removed.getPolicyName());
170                         System.out.println(removed.getVersionNo());
171                 }
172         }
173
174         /**
175          * This function makes API call to policy engine to get automatic
176          * notifications.
177          * 
178          * @param org.onap.policyEngine
179          */
180         public static void getAutoNotifications(PolicyEngine policyEngine) {
181                 Handler handler = new Handler();
182                 policyEngine.setNotification(NotificationScheme.AUTO_ALL_NOTIFICATIONS,
183                                 handler);
184                 //
185                 System.out.println("Enter a any key to exit");
186                 try {
187                         System.in.read();
188                 } catch (IOException e) {
189                         //
190                 }
191         }
192
193 }