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