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.PolicyConfigStatus;
42 import org.openecomp.policy.api.PolicyEngine;
43 import org.openecomp.policy.api.PolicyEngineException;
44 import org.openecomp.policy.api.PolicyType;
45 import org.openecomp.policy.api.RemovedPolicy;
46 import org.w3c.dom.Document;
48 public class MainClient {
49 public static void main(String[] args) {
50 PolicyEngine policyEngine;
52 policyEngine = new PolicyEngine("config.properties");
53 String eCOMPComponentName = ".*" ;
54 String configName = ".*" ;
55 Map<String, String> configAttributes = new HashMap<String,String>();
56 configAttributes.put("java", "java");
57 configAttributes.put("peach", "Tar");
58 configAttributes.put("true", "false");
59 configAttributes.put("small", "testPass");
60 Map<String, String> eventAttributes = new HashMap<String,String>();
61 eventAttributes.put("true", "true");
62 eventAttributes.put("cpu", "91");
63 Map<String, String> decisionAttributes = new HashMap<String,String>();
64 decisionAttributes.put("Key", "Value");
68 Collection<PolicyConfig> policyConfigs = policyEngine.getConfigByPolicyName(".*");//(eCOMPComponentName, configName, configAttributes);
69 if(policyConfigs!=null && !policyConfigs.isEmpty()){
70 for(PolicyConfig policyConfig: policyConfigs){
71 System.out.println("\nConfig Message: "+ policyConfig.getPolicyConfigMessage());
72 System.out.println("Config Status: " + policyConfig.getPolicyConfigStatus());
73 System.out.println("Policy Name: "+ policyConfig.getPolicyName());
74 System.out.println("policy Version: " + policyConfig.getPolicyVersion());
75 /*System.out.println("policy Type: " + policyConfig.getType().toString());
76 System.out.println("Matching Conditions: " + policyConfig.getMatchingConditions());
77 System.out.println("Body is : ");
78 if(policyConfig.getPolicyConfigStatus().equals(PolicyConfigStatus.CONFIG_RETRIEVED)){
79 if(policyConfig.getType().equals(PolicyType.OTHER)){
80 System.out.println(policyConfig.toOther());
81 }else if(policyConfig.getType().equals(PolicyType.JSON)){
82 System.out.println(policyConfig.toJSON().toString());
83 }else if(policyConfig.getType().equals(PolicyType.PROPERTIES)){
84 System.out.println(policyConfig.toProperties().toString());
85 }else if(policyConfig.getType().equals(PolicyType.XML)){
87 printDocument(policyConfig.toXML(), System.out);
88 } catch (Exception e) {
95 } catch (PolicyConfigException e) {
100 Collection<PolicyResponse> policyResponses = org.openecomp.policyEngine.sendEvent(eventAttributes);
101 if(policyResponses!=null && !policyResponses.isEmpty()){
102 for(PolicyResponse policyResponse: policyResponses){
103 System.out.println(policyResponse.getPolicyResponseMessage() + " : " + policyResponse.getPolicyResponseStatus());
104 System.out.println(policyResponse.getActionAdvised());
105 System.out.println(policyResponse.getActionTaken());
106 System.out.println(policyResponse.getRequestAttributes());
109 }catch (PolicyEventException e){
112 /*// Decision example
114 PolicyDecision policyDecision = org.openecomp.policyEngine.getDecision(eCOMPComponentName, decisionAttributes);
115 System.out.println(policyDecision.toString());
116 } catch(PolicyDecisionException e){
120 // Manual Notifications..
121 policyEngine.setScheme(NotificationScheme.MANUAL_ALL_NOTIFICATIONS);
122 if(policyEngine.getNotification()!=null){
123 System.out.println(policyEngine.getNotification().getNotificationType());
124 for(LoadedPolicy updated: policyEngine.getNotification().getLoadedPolicies()){
125 System.out.println(updated.getPolicyName());
126 System.out.println(updated.getVersionNo());
127 System.out.println(updated.getMatches());
129 for(RemovedPolicy removed: policyEngine.getNotification().getRemovedPolicies()){
130 System.out.println(removed.getPolicyName());
131 System.out.println(removed.getVersionNo());
134 // Auto Notifications..
135 Handler handler = new Handler();
136 policyEngine.setNotification(NotificationScheme.AUTO_ALL_NOTIFICATIONS, handler);
138 System.out.println("Enter a any key to exit");
141 } catch (IOException e) {
145 } catch (PolicyEngineException e1) {
146 e1.printStackTrace();
150 public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException {
151 TransformerFactory tf = TransformerFactory.newInstance();
152 Transformer transformer = tf.newTransformer();
153 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
154 transformer.setOutputProperty(OutputKeys.METHOD, "xml");
155 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
156 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
157 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
159 transformer.transform(new DOMSource(doc),
160 new StreamResult(new OutputStreamWriter(out, "UTF-8")));