Initial OpenECOMP policy/engine commit
[policy/engine.git] / PolicyEngineClient / src / main / java / org / openecomp / policyEngine / ConfigFirewallPolicyClient.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.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.InputStream;
27 import java.io.StringReader;
28 import java.nio.file.Path;
29 import java.nio.file.Paths;
30 import java.text.SimpleDateFormat;
31 import java.util.Date;
32 import java.util.UUID;
33
34 import javax.json.Json;
35 import javax.json.JsonObject;
36 import javax.json.JsonReader;
37
38 import org.openecomp.policy.api.PolicyChangeResponse;
39 import org.openecomp.policy.api.PolicyConfigType;
40 import org.openecomp.policy.api.PolicyEngine;
41 import org.openecomp.policy.api.PolicyParameters;
42 import org.openecomp.policy.api.PolicyType;
43
44 public class ConfigFirewallPolicyClient {
45         static Boolean isEdit = false;
46         public static void main(String[] args) {
47                 try{    
48                 PolicyEngine policyEngine = new PolicyEngine("config.properties");
49                 PolicyParameters policyParameters = new PolicyParameters();
50                 // Set Policy Type
51                 policyParameters.setPolicyConfigType(PolicyConfigType.Firewall); //required
52                 policyParameters.setPolicyName("MikeAPItesting.testConfigFirewallPolicy1607_1"); //required
53                 //policyParameters.setPolicyScope("MikeAPItesting"); //Directory will be created where the Policies are saved... this displays a a subscope on the GUI
54                 policyParameters.setRequestID(UUID.randomUUID());
55                 
56             // Set Safe Policy value for Risk Type
57                         SimpleDateFormat dateformat3 = new SimpleDateFormat("dd/MM/yyyy");
58                         Date date = dateformat3.parse("15/10/2016");
59                         policyParameters.setTtlDate(date);
60                         // Set Safe Policy value for Guard
61                         policyParameters.setGuard(true);
62                         // Set Safe Policy value for Risk Level
63                         policyParameters.setRiskLevel("5");
64                         // Set Safe Policy value for Risk Type
65                         policyParameters.setRiskType("PROD");
66                         File jsonFile = null;
67                         String jsonRuleList = null;
68                         if (jsonRuleList == null) {
69                                 Path file = Paths.get("C:\\policyAPI\\firewallRulesJSON\\Config_FW_1607Rule.json");
70                                 jsonFile = file.toFile();
71                         }
72                         //buildJSON(jsonFile, jsonRuleList);
73                         policyParameters.setConfigBody(buildJSON(jsonFile, jsonRuleList).toString());           
74                         policyParameters.setConfigBodyType(PolicyType.JSON);
75             // API method to create Policy or update policy
76             PolicyChangeResponse response = null;
77             if (!isEdit) {
78                 response = policyEngine.createPolicy(policyParameters);
79             } else {
80                 response = policyEngine.updatePolicy(policyParameters);
81             }
82             
83             if(response.getResponseCode()==200){
84                     System.out.println(response.getResponseMessage());
85                     System.out.println("Policy Created Successfully!");
86                 }else{
87                     System.out.println("Error! " + response.getResponseMessage());
88                 }
89             } catch (Exception e) {
90                 System.err.println(e.getMessage());
91             }
92                 
93 }
94         
95         private static JsonObject buildJSON(File jsonInput, String jsonString) throws FileNotFoundException {
96                 JsonObject json = null;;
97                 if (jsonString != null && jsonInput == null) {
98                         StringReader in = null;
99                         
100                         in = new StringReader(jsonString);
101                         
102                         JsonReader jsonReader = Json.createReader(in);
103                         json = jsonReader.readObject();
104                         
105                         
106                 } else {
107                         InputStream in = null;
108                         in = new FileInputStream(jsonInput); 
109                         
110                         JsonReader jsonReader = Json.createReader(in);
111                         json = jsonReader.readObject();
112                 }
113
114                 return json;
115         }
116
117 }