Remove policy-yaml from policy/engine
[policy/engine.git] / PolicyEngineClient / src / test / java / org / onap / policyengine / MainClient.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.io.OutputStream;
25 import java.io.OutputStreamWriter;
26 import java.util.Collection;
27 import java.util.HashMap;
28 import java.util.Map;
29
30 import javax.xml.transform.OutputKeys;
31 import javax.xml.transform.Transformer;
32 import javax.xml.transform.TransformerException;
33 import javax.xml.transform.TransformerFactory;
34 import javax.xml.transform.dom.DOMSource;
35 import javax.xml.transform.stream.StreamResult;
36
37 import org.onap.policy.api.LoadedPolicy;
38 import org.onap.policy.api.NotificationScheme;
39 import org.onap.policy.api.PolicyConfig;
40 import org.onap.policy.api.PolicyConfigException;
41 import org.onap.policy.api.PolicyEngine;
42 import org.onap.policy.api.PolicyEngineException;
43 import org.onap.policy.api.RemovedPolicy;
44 import org.onap.policy.common.logging.flexlogger.FlexLogger;
45 import org.onap.policy.common.logging.flexlogger.Logger;
46 import org.w3c.dom.Document;
47
48 public class MainClient {
49         
50         private static final Logger LOGGER      = FlexLogger.getLogger(MainClient.class);
51         
52         public static void main(String[] args) {
53                 PolicyEngine policyEngine;
54                 try {
55                         policyEngine = new PolicyEngine("config.properties");
56                         Map<String, String> configAttributes = new HashMap<>();
57                         configAttributes.put("java", "java");
58                         configAttributes.put("peach", "Tar");
59                         configAttributes.put("true", "false");
60                         configAttributes.put("small", "testPass");
61                         Map<String, String> eventAttributes = new HashMap<>();
62                         eventAttributes.put("true", "true");
63                         eventAttributes.put("cpu", "91");
64                         Map<String, String> decisionAttributes = new HashMap<>();
65                         decisionAttributes.put("Key", "Value");
66                         
67                         // Config Example 
68                         try {
69                                 @SuppressWarnings("deprecation")
70                                 Collection<PolicyConfig> policyConfigs = policyEngine.getConfigByPolicyName(".*");//(onapComponentName, configName, configAttributes);
71                                 if(policyConfigs!=null && !policyConfigs.isEmpty()){
72                                         for(PolicyConfig policyConfig: policyConfigs){
73                                                 System.out.println("\nConfig Message: "+ policyConfig.getPolicyConfigMessage());
74                                                 System.out.println("Config Status: " + policyConfig.getPolicyConfigStatus());
75                                                 System.out.println("Policy Name: "+ policyConfig.getPolicyName());
76                                                 System.out.println("policy Version: " + policyConfig.getPolicyVersion());
77                                         }
78                                 }
79                         } catch (PolicyConfigException e) {
80                                 LOGGER.error("Exception Occured"+e);
81                         }
82                         
83                         // Manual Notifications.. 
84                         policyEngine.setScheme(NotificationScheme.MANUAL_ALL_NOTIFICATIONS);
85                         if(policyEngine.getNotification()!=null){
86                                 System.out.println(policyEngine.getNotification().getNotificationType());
87                                 for(LoadedPolicy updated: policyEngine.getNotification().getLoadedPolicies()){
88                                         System.out.println(updated.getPolicyName());
89                                         System.out.println(updated.getVersionNo());
90                                         System.out.println(updated.getMatches());
91                                 }
92                                 for(RemovedPolicy removed: policyEngine.getNotification().getRemovedPolicies()){
93                                         System.out.println(removed.getPolicyName());
94                                         System.out.println(removed.getVersionNo());
95                                 }
96                         }
97                         // Auto Notifications..
98                         Handler handler = new Handler();
99                         policyEngine.setNotification(NotificationScheme.AUTO_ALL_NOTIFICATIONS, handler);
100                         //
101                         System.out.println("Enter a any key to exit");
102                         try {
103                                 System.in.read();
104                         } catch (IOException e) {
105                                 System.err.println("Exception Occured"+e);
106                         }
107                         
108                 } catch (PolicyEngineException e1) {
109                         System.err.println("Exception Occured"+e1);
110                 }
111         }
112         
113         public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException {
114             TransformerFactory tf = TransformerFactory.newInstance();
115             Transformer transformer = tf.newTransformer();
116             transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
117             transformer.setOutputProperty(OutputKeys.METHOD, "xml");
118             transformer.setOutputProperty(OutputKeys.INDENT, "yes");
119             transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
120             transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
121
122             transformer.transform(new DOMSource(doc), 
123                  new StreamResult(new OutputStreamWriter(out, "UTF-8")));
124         }
125 }