Initial OpenECOMP policy/engine commit
[policy/engine.git] / PolicyEngineClient / src / main / java / org / openecomp / 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.openecomp.policyEngine;
22
23 import java.util.Arrays;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.LinkedList;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.UUID;
30
31 import org.openecomp.policy.api.AttributeType;
32 import org.openecomp.policy.api.PolicyChangeResponse;
33 import org.openecomp.policy.api.PolicyClass;
34 import org.openecomp.policy.api.PolicyConfigType;
35 import org.openecomp.policy.api.PolicyEngine;
36 import org.openecomp.policy.api.PolicyParameters;
37 import org.openecomp.policy.api.PolicyType;
38
39 public class ActionPolicyClient {
40         static Boolean isEdit = true;
41         public static void main(String[] args) {
42                 try {
43                 PolicyEngine policyEngine = new PolicyEngine("config.properties");
44                 PolicyParameters policyParameters = new PolicyParameters();
45                 // Set Policy Type
46                 policyParameters.setPolicyClass(PolicyClass.Action); //required
47                 policyParameters.setPolicyName("MikeAPItesting.testActionAPI5"); //required
48                 policyParameters.setPolicyDescription("This is a sample Action policy update example with no Action Body");  //optional
49                 //policyParameters.setPolicyScope("MikeAPItesting"); //Directory will be created where the Policies are saved... this displays a a subscope on the GUI
50                 
51                 //Set the Component Attributes... These are Optional
52                 Map<String, String> configAttributes = new HashMap<String, String>(); 
53                 configAttributes.put("Template", "UpdateTemplate");
54                 configAttributes.put("controller", "default"); 
55                 configAttributes.put("SamPoll", "30");
56                 configAttributes.put("value", "abcd"); 
57                 
58                 Map<AttributeType, Map<String,String>> attributes = new HashMap<AttributeType, Map<String,String>>();
59                 attributes.put(AttributeType.MATCHING, configAttributes);
60                 policyParameters.setAttributes(attributes);
61
62                 //Set the Rule Algorithm
63                 // Map<String, Map<String, Map<String, String>>> translated to Map<Dynamic Label, Map<Field1,Map<Combo Operator, Field2>>>
64                 Map<String, Map<String,Map<String,String>>> ruleAlgorithm = new HashMap<String, Map<String,Map<String,String>>>();
65                 
66                         List<String> dynamicRuleAlgorithmLabels = new LinkedList<String>();
67                         List<String> dynamicRuleAlgorithmFunctions = new LinkedList<String>();
68                         List<String> dynamicRuleAlgorithmField1 = new LinkedList<String>();
69                         List<String> dynamicRuleAlgorithmField2 = new LinkedList<String>();
70                         
71                         //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      
77                          * A4           A2                      and                                                     A3
78                          * A5           Config          integer-greater-than            45
79                          * A6           A4      `               or                                                      A5
80                          * A7           A1                      and                                                     A6
81                          */
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","integer-greater-than","or","and");
85                         dynamicRuleAlgorithmField2 = Arrays.asList("90","ca","90","A3","45","A5","A6");
86                                 
87                         policyParameters.setDynamicRuleAlgorithmLabels(dynamicRuleAlgorithmLabels);
88                         policyParameters.setDynamicRuleAlgorithmField1(dynamicRuleAlgorithmField1);
89                         policyParameters.setDynamicRuleAlgorithmFunctions(dynamicRuleAlgorithmFunctions);
90                         policyParameters.setDynamicRuleAlgorithmField2(dynamicRuleAlgorithmField2);
91                         
92                 policyParameters.setActionPerformer("PEP");
93                 policyParameters.setActionAttribute("mikeTest2");
94                 policyParameters.setRequestID(UUID.randomUUID());
95                 
96                 // API method to create Policy or update policy
97                 PolicyChangeResponse response = null;
98                 if (!isEdit) {
99                     response = policyEngine.createPolicy(policyParameters);
100                 } else {
101                         response = policyEngine.updatePolicy(policyParameters);
102                 }
103                 
104                 if(response.getResponseCode()==200){
105                     System.out.println(response.getResponseMessage());
106                     System.out.println("Policy Created Successfully!");
107                 }else{
108                     System.out.println("Error! " + response.getResponseMessage());
109                 }
110                 } catch (Exception e) {
111                         System.err.println(e.getMessage());
112                 }
113         }
114         
115 }
116
117