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.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.onap.policy.api.AttributeType;
31 import org.onap.policy.api.PolicyChangeResponse;
32 import org.onap.policy.api.PolicyClass;
33 import org.onap.policy.api.PolicyEngine;
34 import org.onap.policy.api.PolicyParameters;
36 public class ActionPolicyClient {
37 static Boolean isEdit = true;
42 * @param args String[] args
44 public static void main(String[] args) {
46 PolicyEngine policyEngine = new PolicyEngine("config.properties");
47 PolicyParameters policyParameters = new PolicyParameters();
49 policyParameters.setPolicyClass(PolicyClass.Action); // required
50 policyParameters.setPolicyName("MikeAPItesting.testActionAPI5"); // required
51 policyParameters.setPolicyDescription(
52 "This is a sample Action policy update example with no Action Body"); // optional
53 // saved... this displays a a subscope on the GUI
55 // Set the Component Attributes... These are Optional
56 Map<String, String> configAttributes = new HashMap<>();
57 configAttributes.put("Template", "UpdateTemplate");
58 configAttributes.put("controller", "default");
59 configAttributes.put("SamPoll", "30");
60 configAttributes.put("value", "abcd");
62 Map<AttributeType, Map<String, String>> attributes = new HashMap<>();
63 attributes.put(AttributeType.MATCHING, configAttributes);
64 policyParameters.setAttributes(attributes);
66 List<String> dynamicRuleAlgorithmLabels = new LinkedList<>();
67 List<String> dynamicRuleAlgorithmFunctions = new LinkedList<>();
68 List<String> dynamicRuleAlgorithmField1 = new LinkedList<>();
70 // Example of a complex Rule algorithm
72 * label field1 function field2
73 * *****************************************************
74 * A1 cobal integer-equal 90
75 * A2 cap string-contains ca
76 * A3 cobal integer-equal 90
78 * A5 Config integer-greater-than 45
82 dynamicRuleAlgorithmLabels = Arrays.asList("A1", "A2", "A3", "A4", "A5", "A6", "A7");
83 dynamicRuleAlgorithmField1 = Arrays.asList("cobal", "cap", "cobal", "A2", "Config", "A4", "A1");
84 dynamicRuleAlgorithmFunctions = Arrays.asList("integer-equal", "string-contains", "integer-equal", "and",
85 "integer-greater-than", "or", "and");
86 List<String> dynamicRuleAlgorithmField2 = new LinkedList<>();
87 dynamicRuleAlgorithmField2 = Arrays.asList("90", "ca", "90", "A3", "45", "A5", "A6");
89 policyParameters.setDynamicRuleAlgorithmLabels(dynamicRuleAlgorithmLabels);
90 policyParameters.setDynamicRuleAlgorithmField1(dynamicRuleAlgorithmField1);
91 policyParameters.setDynamicRuleAlgorithmFunctions(dynamicRuleAlgorithmFunctions);
92 policyParameters.setDynamicRuleAlgorithmField2(dynamicRuleAlgorithmField2);
94 policyParameters.setActionPerformer("PEP");
95 policyParameters.setActionAttribute("mikeTest2");
96 policyParameters.setRequestID(UUID.randomUUID());
98 // API method to create Policy or update policy
99 PolicyChangeResponse response = null;
101 response = policyEngine.createPolicy(policyParameters);
103 response = policyEngine.updatePolicy(policyParameters);
106 if (response.getResponseCode() == 200) {
107 System.out.println(response.getResponseMessage());
108 System.out.println("Policy Created Successfully!");
110 System.out.println("Error! " + response.getResponseMessage());
112 } catch (Exception e) {
113 System.err.println(e.getMessage() + e);