Update css file name in conf.py
[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
21 package org.onap.policyengine;
22
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Map;
27 import java.util.UUID;
28
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;
39
40 /**
41  * Class contains static functions which make call to policy engine using API.
42  * This class is used by generalTestClient.java
43  *
44  *
45  * @version 1.0
46  *
47  */
48 public class PolicyEngineTestClient {
49     /**
50      * This function make API call to policy engine to get config. And returns
51      * policy name, policy version and policy configStatus
52      *
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
58      */
59
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<>();
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, configName, requestID);
72                 } else {
73
74                     policyConfigs = policyEngine.getConfig(onapComponentName, configName, configAttributes, requestID);
75                 }
76             }
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());
81                 }
82             }
83         } catch (PolicyConfigException e) {
84             // logger.error("Exception Occured"+e);
85             resultReceived.add("" + e);
86         }
87         return resultReceived;
88     }
89
90     /**
91      * This functions make API call to policy engine to get decision. And
92      * returns policy Decision
93      *
94      * @param policyEngine Policy Engine
95      * @param onapComponentName String
96      * @param decisionAttributes Map of String
97      * @return List of String
98      */
99     public static ArrayList<String> getDecision(PolicyEngine policyEngine, String onapComponentName,
100             Map<String, String> decisionAttributes) {
101         ArrayList<String> resultReceived = new ArrayList<>();
102         // Decision example
103         try {
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);
112         }
113         return resultReceived;
114     }
115
116     /**
117      * This function makes API call to policy engine to get action. And returns
118      * responseMessage and responseStatus
119      *
120      * @param policyEngine Policy Engine object
121      * @param eventAttributes Map of String
122      * @return List of String
123      */
124     public static ArrayList<String> getAction(PolicyEngine policyEngine, Map<String, String> eventAttributes) {
125         ArrayList<String> resultReceived = new ArrayList<>();
126         try {
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());
134                 }
135             }
136         } catch (PolicyEventException e) {
137             // logger.error("Exception Occured"+e);
138             resultReceived.add("" + e);
139         }
140         return resultReceived;
141     }
142
143     /**
144      * This function makes API call to policy engine to get manual
145      * notifications.
146      *
147      * @param policyEngine Policy Engine object
148      */
149
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());
157         }
158         for (RemovedPolicy removed : policyEngine.getNotification().getRemovedPolicies()) {
159             System.out.println(removed.getPolicyName());
160             System.out.println(removed.getVersionNo());
161         }
162     }
163
164     /**
165      * This function makes API call to policy engine to get automatic
166      * notifications.
167      *
168      * @param policyEngine Policy Engine object
169      */
170     public static void getAutoNotifications(PolicyEngine policyEngine) {
171         Handler handler = new Handler();
172         policyEngine.setNotification(NotificationScheme.AUTO_ALL_NOTIFICATIONS, handler);
173         //
174         System.out.println("Enter a any key to exit");
175         try {
176             System.in.read();
177         } catch (IOException e) {
178             //
179         }
180     }
181
182 }