Update css file name in conf.py
[policy/engine.git] / PolicyEngineClient / src / test / java / org / onap / policyengine / DecisionPolicyClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * PolicyEngineClient
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policyengine;
22
23 import java.util.Arrays;
24 import java.util.HashMap;
25 import java.util.LinkedList;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.UUID;
29
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;
35
36 public class DecisionPolicyClient {
37     static Boolean isEdit = true;
38
39     /**
40      * main.
41      *
42      * @param args String[] args
43      */
44     public static void main(String[] args) {
45         try {
46             PolicyEngine policyEngine = new PolicyEngine("config.properties");
47             PolicyParameters policyParameters = new PolicyParameters();
48             // Set Policy Type
49             policyParameters.setPolicyClass(PolicyClass.Decision); // required
50             policyParameters.setPolicyName("MikeAPItests.testDecisionAPI"); // required
51             policyParameters.setOnapName("java"); // required
52             policyParameters.setPolicyDescription(
53                     "This is a sample Decision policy UPDATE example with Settings"); // optional
54             // policyParameters.setPolicyScope("MikeAPItests"); //Directory will be created where the Policies are
55             // saved... this displays a a subscope on the GUI
56
57             // Set the Component Attributes... These are Optional
58             Map<String, String> configAttributes = new HashMap<>();
59             configAttributes.put("Template", "UpdateTemplate");
60             configAttributes.put("controller", "default");
61             configAttributes.put("SamPoll", "30");
62             configAttributes.put("value", "abcd");
63
64             Map<AttributeType, Map<String, String>> attributes = new HashMap<>();
65             attributes.put(AttributeType.MATCHING, configAttributes);
66
67             // Set the settings... These are Optional
68             Map<String, String> settingsMap = new HashMap<>();
69             settingsMap.put("server", "5");
70
71             attributes.put(AttributeType.SETTINGS, settingsMap);
72             policyParameters.setAttributes(attributes);
73
74             List<String> dynamicRuleAlgorithmLabels = new LinkedList<>();
75             List<String> dynamicRuleAlgorithmFunctions = new LinkedList<>();
76             List<String> dynamicRuleAlgorithmField1 = new LinkedList<>();
77
78             // Example of a complex Rule algorithm using the settings in the Field1
79             /*
80              * label field1 function field2
81              * *****************************************************
82              * A1 S_server integer-equal 90
83              * A2 cap string-contains ca
84              * A3 cobal integer-equal 90
85              * A4 A2 and A3
86              * A5 Config integer-greater-than 45
87              * A6 A4 ` or A5
88              * A7 A1 and A6
89              */
90             dynamicRuleAlgorithmLabels = Arrays.asList("A1", "A2", "A3", "A4", "A5", "A6", "A7");
91             dynamicRuleAlgorithmField1 = Arrays.asList("S_server", "cap", "cobal", "A2", "Config", "A4", "A1");
92             dynamicRuleAlgorithmFunctions = Arrays.asList("integer-equal", "string-contains", "integer-equal", "and",
93                     "integer-greater-than", "or", "and");
94             List<String> dynamicRuleAlgorithmField2 = new LinkedList<>();
95             dynamicRuleAlgorithmField2 = Arrays.asList("90", "ca", "90", "A3", "45", "A5", "A6");
96
97             policyParameters.setDynamicRuleAlgorithmLabels(dynamicRuleAlgorithmLabels);
98             policyParameters.setDynamicRuleAlgorithmField1(dynamicRuleAlgorithmField1);
99             policyParameters.setDynamicRuleAlgorithmFunctions(dynamicRuleAlgorithmFunctions);
100             policyParameters.setDynamicRuleAlgorithmField2(dynamicRuleAlgorithmField2);
101
102             policyParameters.setRequestID(UUID.randomUUID());
103
104             // API method to create Policy or update policy
105             PolicyChangeResponse response = null;
106             if (!isEdit) {
107                 response = policyEngine.createPolicy(policyParameters);
108             } else {
109                 response = policyEngine.updatePolicy(policyParameters);
110             }
111
112             if (response.getResponseCode() == 200) {
113                 System.out.println(response.getResponseMessage());
114                 System.out.println("Policy Created Successfully!");
115             } else {
116                 System.out.println("Error! " + response.getResponseMessage());
117             }
118         } catch (Exception e) {
119             System.err.println(e.getMessage());
120         }
121     }
122
123 }