[POLICY-73] replace openecomp for policy-engine
[policy/engine.git] / PolicyEngineClient / src / main / java / org / onap / policyEngine / ActionPolicyClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * PolicyEngineClient
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
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 ActionPolicyClient {
37         static Boolean isEdit = true;
38         public static void main(String[] args) {
39                 try {
40                 PolicyEngine policyEngine = new PolicyEngine("config.properties");
41                 PolicyParameters policyParameters = new PolicyParameters();
42                 // Set Policy Type
43                 policyParameters.setPolicyClass(PolicyClass.Action); //required
44                 policyParameters.setPolicyName("MikeAPItesting.testActionAPI5"); //required
45                 policyParameters.setPolicyDescription("This is a sample Action policy update example with no Action Body");  //optional
46                 //policyParameters.setPolicyScope("MikeAPItesting"); //Directory will be created where the Policies are saved... this displays a a subscope on the GUI
47                 
48                 //Set the Component Attributes... These are Optional
49                 Map<String, String> configAttributes = new HashMap<>(); 
50                 configAttributes.put("Template", "UpdateTemplate");
51                 configAttributes.put("controller", "default"); 
52                 configAttributes.put("SamPoll", "30");
53                 configAttributes.put("value", "abcd"); 
54                 
55                 Map<AttributeType, Map<String,String>> attributes = new HashMap<>();
56                 attributes.put(AttributeType.MATCHING, configAttributes);
57                 policyParameters.setAttributes(attributes);
58
59                 
60                         List<String> dynamicRuleAlgorithmLabels = new LinkedList<>();
61                         List<String> dynamicRuleAlgorithmFunctions = new LinkedList<>();
62                         List<String> dynamicRuleAlgorithmField1 = new LinkedList<>();
63                         List<String> dynamicRuleAlgorithmField2 = new LinkedList<>();
64                         
65                         //Example of a complex Rule algorithm
66                         /* label        field1          function                                field2
67                          * *****************************************************
68                          * A1           cobal           integer-equal                           90
69                          * A2           cap             string-contains                 ca
70                          * A3           cobal           integer-equal                           90      
71                          * A4           A2                      and                                                     A3
72                          * A5           Config          integer-greater-than            45
73                          * A6           A4      `               or                                                      A5
74                          * A7           A1                      and                                                     A6
75                          */
76                         dynamicRuleAlgorithmLabels = Arrays.asList("A1","A2","A3","A4","A5","A6","A7");
77                         dynamicRuleAlgorithmField1 = Arrays.asList("cobal","cap","cobal","A2","Config","A4","A1");
78                         dynamicRuleAlgorithmFunctions = Arrays.asList("integer-equal","string-contains","integer-equal","and","integer-greater-than","or","and");
79                         dynamicRuleAlgorithmField2 = Arrays.asList("90","ca","90","A3","45","A5","A6");
80                                 
81                         policyParameters.setDynamicRuleAlgorithmLabels(dynamicRuleAlgorithmLabels);
82                         policyParameters.setDynamicRuleAlgorithmField1(dynamicRuleAlgorithmField1);
83                         policyParameters.setDynamicRuleAlgorithmFunctions(dynamicRuleAlgorithmFunctions);
84                         policyParameters.setDynamicRuleAlgorithmField2(dynamicRuleAlgorithmField2);
85                         
86                 policyParameters.setActionPerformer("PEP");
87                 policyParameters.setActionAttribute("mikeTest2");
88                 policyParameters.setRequestID(UUID.randomUUID());
89                 
90                 // API method to create Policy or update policy
91                 PolicyChangeResponse response = null;
92                 if (!isEdit) {
93                     response = policyEngine.createPolicy(policyParameters);
94                 } else {
95                         response = policyEngine.updatePolicy(policyParameters);
96                 }
97                 
98                 if(response.getResponseCode()==200){
99                     System.out.println(response.getResponseMessage());
100                     System.out.println("Policy Created Successfully!");
101                 }else{
102                     System.out.println("Error! " + response.getResponseMessage());
103                 }
104                 } catch (Exception e) {
105                         System.err.println(e.getMessage() + e);
106                 }
107         }
108         
109 }
110
111