2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017, 2019 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);
55 * @param args String[] args
57 public static void main(String[] args) {
58 PolicyEngine policyEngine;
60 policyEngine = new PolicyEngine("config.properties");
61 Map<String, String> configAttributes = new HashMap<>();
62 configAttributes.put("java", "java");
63 configAttributes.put("peach", "Tar");
64 configAttributes.put("true", "false");
65 configAttributes.put("small", "testPass");
66 Map<String, String> eventAttributes = new HashMap<>();
67 eventAttributes.put("true", "true");
68 eventAttributes.put("cpu", "91");
69 Map<String, String> decisionAttributes = new HashMap<>();
70 decisionAttributes.put("Key", "Value");
74 @SuppressWarnings("deprecation")
75 Collection<PolicyConfig> policyConfigs = policyEngine.getConfigByPolicyName(".*");
76 if (policyConfigs != null && !policyConfigs.isEmpty()) {
77 for (PolicyConfig policyConfig : policyConfigs) {
78 System.out.println("\nConfig Message: " + policyConfig.getPolicyConfigMessage());
79 System.out.println("Config Status: " + policyConfig.getPolicyConfigStatus());
80 System.out.println("Policy Name: " + policyConfig.getPolicyName());
81 System.out.println("policy Version: " + policyConfig.getPolicyVersion());
84 } catch (PolicyConfigException e) {
85 LOGGER.error("Exception Occured" + e);
88 // Manual Notifications..
89 policyEngine.setScheme(NotificationScheme.MANUAL_ALL_NOTIFICATIONS);
90 if (policyEngine.getNotification() != null) {
91 System.out.println(policyEngine.getNotification().getNotificationType());
92 for (LoadedPolicy updated : policyEngine.getNotification().getLoadedPolicies()) {
93 System.out.println(updated.getPolicyName());
94 System.out.println(updated.getVersionNo());
95 System.out.println(updated.getMatches());
97 for (RemovedPolicy removed : policyEngine.getNotification().getRemovedPolicies()) {
98 System.out.println(removed.getPolicyName());
99 System.out.println(removed.getVersionNo());
102 // Auto Notifications..
103 Handler handler = new Handler();
104 policyEngine.setNotification(NotificationScheme.AUTO_ALL_NOTIFICATIONS, handler);
106 System.out.println("Enter a any key to exit");
109 } catch (IOException e) {
110 System.err.println("Exception Occured" + e);
113 } catch (PolicyEngineException e1) {
114 System.err.println("Exception Occured" + e1);
121 * @param doc Document
122 * @param out OutputStream
123 * @throws IOException IOException
124 * @throws TransformerException TransformerException
126 public static void printDocument(Document doc, OutputStream out)
127 throws IOException, TransformerException {
128 TransformerFactory tf = TransformerFactory.newInstance();
129 Transformer transformer = tf.newTransformer();
130 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
131 transformer.setOutputProperty(OutputKeys.METHOD, "xml");
132 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
133 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
134 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
136 transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8")));