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.util.Arrays;
24 import java.util.HashMap;
25 import java.util.LinkedList;
26 import java.util.List;
28 import java.util.UUID;
30 import org.openecomp.policy.api.AttributeType;
31 import org.openecomp.policy.api.PolicyChangeResponse;
32 import org.openecomp.policy.api.PolicyClass;
33 import org.openecomp.policy.api.PolicyEngine;
34 import org.openecomp.policy.api.PolicyParameters;
36 public class DecisionPolicyClient {
37 static Boolean isEdit = true;
38 public static void main(String[] args) {
40 PolicyEngine policyEngine = new PolicyEngine("config.properties");
41 PolicyParameters policyParameters = new PolicyParameters();
43 policyParameters.setPolicyClass(PolicyClass.Decision); //required
44 policyParameters.setPolicyName("MikeAPItests.testDecisionAPI"); //required
45 policyParameters.setEcompName("java"); //required
46 policyParameters.setPolicyDescription("This is a sample Decision policy UPDATE example with Settings"); //optional
47 //policyParameters.setPolicyScope("MikeAPItests"); //Directory will be created where the Policies are saved... this displays a a subscope on the GUI
49 //Set the Component Attributes... These are Optional
50 Map<String, String> configAttributes = new HashMap<String, String>();
51 configAttributes.put("Template", "UpdateTemplate");
52 configAttributes.put("controller", "default");
53 configAttributes.put("SamPoll", "30");
54 configAttributes.put("value", "abcd");
56 Map<AttributeType, Map<String,String>> attributes = new HashMap<AttributeType, Map<String,String>>();
57 attributes.put(AttributeType.MATCHING, configAttributes);
59 //Set the settings... These are Optional
60 Map<String, String> settingsMap = new HashMap<String, String>();
61 settingsMap.put("server", "5");
63 attributes.put(AttributeType.SETTINGS, settingsMap);
64 policyParameters.setAttributes(attributes);
66 //Set the Rule Algorithm... These are Optional
67 Map<String, Map<String,Map<String,String>>> ruleAlgorithm = new HashMap<String, Map<String,Map<String,String>>>();
69 List<String> dynamicRuleAlgorithmLabels = new LinkedList<String>();
70 List<String> dynamicRuleAlgorithmFunctions = new LinkedList<String>();
71 List<String> dynamicRuleAlgorithmField1 = new LinkedList<String>();
72 List<String> dynamicRuleAlgorithmField2 = new LinkedList<String>();
74 //Example of a complex Rule algorithm using the settings in the Field1
75 /* label field1 function field2
76 * *****************************************************
77 * A1 S_server integer-equal 90
78 * A2 cap string-contains ca
79 * A3 cobal integer-equal 90
81 * A5 Config integer-greater-than 45
85 dynamicRuleAlgorithmLabels = Arrays.asList("A1","A2","A3","A4","A5","A6","A7");
86 dynamicRuleAlgorithmField1 = Arrays.asList("S_server","cap","cobal","A2","Config","A4","A1");
87 dynamicRuleAlgorithmFunctions = Arrays.asList("integer-equal","string-contains","integer-equal","and","integer-greater-than","or","and");
88 dynamicRuleAlgorithmField2 = Arrays.asList("90","ca","90","A3","45","A5","A6");
90 policyParameters.setDynamicRuleAlgorithmLabels(dynamicRuleAlgorithmLabels);
91 policyParameters.setDynamicRuleAlgorithmField1(dynamicRuleAlgorithmField1);
92 policyParameters.setDynamicRuleAlgorithmFunctions(dynamicRuleAlgorithmFunctions);
93 policyParameters.setDynamicRuleAlgorithmField2(dynamicRuleAlgorithmField2);
95 policyParameters.setRequestID(UUID.randomUUID());
97 // API method to create Policy or update policy
98 PolicyChangeResponse response = null;
100 response = policyEngine.createPolicy(policyParameters);
102 response = policyEngine.updatePolicy(policyParameters);
105 if(response.getResponseCode()==200){
106 System.out.println(response.getResponseMessage());
107 System.out.println("Policy Created Successfully!");
109 System.out.println("Error! " + response.getResponseMessage());
111 } catch (Exception e) {
112 System.err.println(e.getMessage());