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.onap.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.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;
48 public class MainClient {
50 private static final Logger LOGGER = FlexLogger.getLogger(MainClient.class);
52 public static void main(String[] args) {
53 PolicyEngine policyEngine;
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");
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());
79 } catch (PolicyConfigException e) {
80 LOGGER.error("Exception Occured"+e);
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());
92 for(RemovedPolicy removed: policyEngine.getNotification().getRemovedPolicies()){
93 System.out.println(removed.getPolicyName());
94 System.out.println(removed.getVersionNo());
97 // Auto Notifications..
98 Handler handler = new Handler();
99 policyEngine.setNotification(NotificationScheme.AUTO_ALL_NOTIFICATIONS, handler);
101 System.out.println("Enter a any key to exit");
104 } catch (IOException e) {
105 System.err.println("Exception Occured"+e);
108 } catch (PolicyEngineException e1) {
109 System.err.println("Exception Occured"+e1);
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");
122 transformer.transform(new DOMSource(doc),
123 new StreamResult(new OutputStreamWriter(out, "UTF-8")));