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