2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.policyEngine;
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;
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;
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;
46 public class MainClient {
47 public static void main(String[] args) {
48 PolicyEngine policyEngine;
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");
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());
74 } catch (PolicyConfigException e) {
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());
87 for(RemovedPolicy removed: policyEngine.getNotification().getRemovedPolicies()){
88 System.out.println(removed.getPolicyName());
89 System.out.println(removed.getVersionNo());
92 // Auto Notifications..
93 Handler handler = new Handler();
94 policyEngine.setNotification(NotificationScheme.AUTO_ALL_NOTIFICATIONS, handler);
96 System.out.println("Enter a any key to exit");
99 } catch (IOException e) {
103 } catch (PolicyEngineException e1) {
104 e1.printStackTrace();
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");
117 transformer.transform(new DOMSource(doc),
118 new StreamResult(new OutputStreamWriter(out, "UTF-8")));