Remove policy-yaml from policy/engine
[policy/engine.git] / PolicyEngineClient / src / main / java / org / onap / 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.onap.policyEngine;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.StringReader;
29 import java.nio.file.Path;
30 import java.nio.file.Paths;
31 import java.text.SimpleDateFormat;
32 import java.util.Date;
33 import java.util.UUID;
34
35 import javax.json.Json;
36 import javax.json.JsonObject;
37 import javax.json.JsonReader;
38
39 import org.onap.policy.api.PolicyChangeResponse;
40 import org.onap.policy.api.PolicyConfigType;
41 import org.onap.policy.api.PolicyEngine;
42 import org.onap.policy.api.PolicyParameters;
43 import org.onap.policy.api.PolicyType;
44
45 public class ConfigFirewallPolicyClient {
46         static Boolean isEdit = false;
47         public static void main(String[] args) {
48                 try{    
49                 PolicyEngine policyEngine = new PolicyEngine("config.properties");
50                 PolicyParameters policyParameters = new PolicyParameters();
51                 // Set Policy Type
52                 policyParameters.setPolicyConfigType(PolicyConfigType.Firewall); //required
53                 policyParameters.setPolicyName("MikeAPItesting.testConfigFirewallPolicy1607_1"); //required
54                 //policyParameters.setPolicyScope("MikeAPItesting"); //Directory will be created where the Policies are saved... this displays a a subscope on the GUI
55                 policyParameters.setRequestID(UUID.randomUUID());
56                 
57             // Set Safe Policy value for Risk Type
58                         SimpleDateFormat dateformat3 = new SimpleDateFormat("dd/MM/yyyy");
59                         Date date = dateformat3.parse("15/10/2016");
60                         policyParameters.setTtlDate(date);
61                         // Set Safe Policy value for Guard
62                         policyParameters.setGuard(true);
63                         // Set Safe Policy value for Risk Level
64                         policyParameters.setRiskLevel("5");
65                         // Set Safe Policy value for Risk Type
66                         policyParameters.setRiskType("PROD");
67                         File jsonFile = null;
68                         String 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                 JsonReader jsonReader = null;
98                 if (jsonString != null && jsonInput == null) {
99                         StringReader in = null;
100                         in = new StringReader(jsonString);
101                         jsonReader = Json.createReader(in);
102                         json = jsonReader.readObject();
103                         in.close();
104                 } else {
105                         InputStream in = null;
106                         in = new FileInputStream(jsonInput); 
107                         jsonReader = Json.createReader(in);
108                         json = jsonReader.readObject();
109                         try {
110                                 in.close();
111                         } catch (IOException e) {
112                                 System.err.println("Exception Occured while closing input stream"+e);
113                         }
114                 }
115                 jsonReader.close();
116                 return json;
117         }
118
119 }