Initial OpenECOMP policy/engine commit
[policy/engine.git] / PolicyEngineClient / src / main / java / org / openecomp / 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.openecomp.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.openecomp.policy.api.DecisionResponse;
40 import org.openecomp.policy.api.LoadedPolicy;
41 import org.openecomp.policy.api.NotificationScheme;
42 import org.openecomp.policy.api.PolicyConfig;
43 import org.openecomp.policy.api.PolicyConfigException;
44 import org.openecomp.policy.api.PolicyDecisionException;
45 import org.openecomp.policy.api.PolicyEngine;
46 import org.openecomp.policy.api.PolicyEventException;
47 import org.openecomp.policy.api.PolicyResponse;
48 import org.openecomp.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.openecomp.policyEngine
64          * @param eCOMPComponentName
65          * @param configName
66          * @param configAttributes
67          * @return
68          */
69
70         public static ArrayList<String> getConfig(PolicyEngine policyEngine,
71                         String eCOMPComponentName, String configName,
72                         Map<String, String> configAttributes) {
73                 ArrayList<String> resultReceived = new ArrayList<String>();
74                 try {
75                         UUID requestID = UUID.randomUUID();
76                         Collection<PolicyConfig> policyConfigs;
77                         if (configName == null) {
78                                 policyConfigs = policyEngine.getConfig(eCOMPComponentName, requestID);
79                         } else {
80                                 if (configAttributes == null) {
81                                         policyConfigs = policyEngine.getConfig(eCOMPComponentName,
82                                                         configName, requestID);
83                                 } else {
84                                         
85                                         policyConfigs = policyEngine.getConfig(eCOMPComponentName,
86                                                         configName, configAttributes, requestID);
87                                 }
88                         }
89                         if (policyConfigs != null && !policyConfigs.isEmpty()) {
90                                 for (PolicyConfig policyConfig : policyConfigs) {
91                                         resultReceived.add("Policy Name: "
92                                                         + policyConfig.getPolicyName()
93                                                         + " Policy version: "
94                                                         + policyConfig.getPolicyVersion() + " - "
95                                                         + policyConfig.getPolicyConfigStatus());
96                                 }
97                         }
98                 } catch (PolicyConfigException e) {
99 //                      e.printStackTrace();
100                         resultReceived.add(""+e);
101                 }
102                 return resultReceived;
103         }
104
105         /**
106          * This functions make API call to policy engine to get decision. And
107          * returns policy Decision
108          * 
109          * @param org.openecomp.policyEngine
110          * @param eCOMPComponentName
111          * @param decisionAttributes
112          * @return
113          */
114         public static ArrayList<String> getDecision(PolicyEngine policyEngine,
115                         String eCOMPComponentName, Map<String, String> decisionAttributes) {
116                 ArrayList<String> resultReceived = new ArrayList<String>();
117                 // Decision example
118                 try {
119                         UUID requestID = UUID.randomUUID();
120                         DecisionResponse policyDecision = policyEngine.getDecision(
121                                         eCOMPComponentName, decisionAttributes, requestID);
122                         resultReceived.add(policyDecision.getDecision().toString());
123                 } catch (PolicyDecisionException e) {
124 //                      e.printStackTrace();
125                         resultReceived.add(""+e);
126                 }
127                 return resultReceived;
128         }
129
130         /**
131          * This function makes API call to policy engine to get action. And returns
132          * responseMessage and responseStatus
133          * 
134          * @param org.openecomp.policyEngine
135          * @param eventAttributes
136          * @return
137          */
138         public static ArrayList<String> getAction(PolicyEngine policyEngine,
139                         Map<String, String> eventAttributes) {
140                 ArrayList<String> resultReceived = new ArrayList<String>();
141                 try {
142                         UUID requestID = UUID.randomUUID();
143                         Collection<PolicyResponse> policyResponses = policyEngine
144                                         .sendEvent(eventAttributes, requestID);
145                         if (policyResponses != null && !policyResponses.isEmpty()) {
146                                 for (PolicyResponse policyResponse : policyResponses) {
147                                         resultReceived.add(policyResponse
148                                                         .getPolicyResponseMessage()
149                                                         + " : "
150                                                         + policyResponse.getPolicyResponseStatus());
151                                 }
152                         }
153                 } catch (PolicyEventException e) {
154 //                      e.printStackTrace();
155                         resultReceived.add(""+e);
156                 }
157                 return resultReceived;
158         }
159
160         /**
161          * This function makes API call to policy engine to get manual
162          * notifications.
163          * 
164          * @param org.openecomp.policyEngine
165          */
166
167         public static void getManualNotifications(PolicyEngine policyEngine) {
168                 policyEngine.setScheme(NotificationScheme.MANUAL_ALL_NOTIFICATIONS);
169                 System.out.println(policyEngine.getNotification().getNotificationType());
170                 for (LoadedPolicy updated : policyEngine.getNotification().getLoadedPolicies()) {
171                         System.out.println(updated.getPolicyName());
172                         System.out.println(updated.getVersionNo());
173                         System.out.println(updated.getMatches());
174                 }
175                 for (RemovedPolicy removed : policyEngine.getNotification()
176                                 .getRemovedPolicies()) {
177                         System.out.println(removed.getPolicyName());
178                         System.out.println(removed.getVersionNo());
179                 }
180         }
181
182         /**
183          * This function makes API call to policy engine to get automatic
184          * notifications.
185          * 
186          * @param org.openecomp.policyEngine
187          */
188         public static void getAutoNotifications(PolicyEngine policyEngine) {
189                 Handler handler = new Handler();
190                 policyEngine.setNotification(NotificationScheme.AUTO_ALL_NOTIFICATIONS,
191                                 handler);
192                 //
193                 System.out.println("Enter a any key to exit");
194                 try {
195                         System.in.read();
196                 } catch (IOException e) {
197                         //
198                 }
199         }
200
201 }