Update css file name in conf.py
[policy/engine.git] / PolicyEngineClient / src / test / java / org / onap / policyengine / ConfigFirewallPolicyClient.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.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
48     /**
49      * main.
50      *
51      * @param args String[] args
52      */
53     public static void main(String[] args) {
54         try {
55             PolicyEngine policyEngine = new PolicyEngine("config.properties");
56             PolicyParameters policyParameters = new PolicyParameters();
57             // Set Policy Type
58             policyParameters.setPolicyConfigType(PolicyConfigType.Firewall); // required
59             policyParameters.setPolicyName("MikeAPItesting.testConfigFirewallPolicy1607_1"); // required
60             // policyParameters.setPolicyScope("MikeAPItesting"); //Directory will be created where the Policies are
61             // saved... this displays a a subscope on the GUI
62             policyParameters.setRequestID(UUID.randomUUID());
63
64             // Set Safe Policy value for Risk Type
65             SimpleDateFormat dateformat3 = new SimpleDateFormat("dd/MM/yyyy");
66             Date date = dateformat3.parse("15/10/2016");
67             policyParameters.setTtlDate(date);
68             // Set Safe Policy value for Guard
69             policyParameters.setGuard(true);
70             // Set Safe Policy value for Risk Level
71             policyParameters.setRiskLevel("5");
72             // Set Safe Policy value for Risk Type
73             policyParameters.setRiskType("PROD");
74             File jsonFile = null;
75             String jsonRuleList = null;
76             Path file = Paths.get("C:\\policyAPI\\firewallRulesJSON\\Config_FW_1607Rule.json");
77             jsonFile = file.toFile();
78
79             // buildJSON(jsonFile, jsonRuleList);
80             policyParameters.setConfigBody(buildJson(jsonFile, jsonRuleList).toString());
81             policyParameters.setConfigBodyType(PolicyType.JSON);
82             // API method to create Policy or update policy
83             PolicyChangeResponse response = null;
84             if (!isEdit) {
85                 response = policyEngine.createPolicy(policyParameters);
86             } else {
87                 response = policyEngine.updatePolicy(policyParameters);
88             }
89
90             if (response.getResponseCode() == 200) {
91                 System.out.println(response.getResponseMessage());
92                 System.out.println("Policy Created Successfully!");
93             } else {
94                 System.out.println("Error! " + response.getResponseMessage());
95             }
96         } catch (Exception e) {
97             System.err.println(e.getMessage());
98         }
99
100     }
101
102     private static JsonObject buildJson(File jsonInput, String jsonString) throws FileNotFoundException {
103         JsonObject json = null;
104         JsonReader jsonReader = null;
105         if (jsonString != null && jsonInput == null) {
106             StringReader in = null;
107             in = new StringReader(jsonString);
108             jsonReader = Json.createReader(in);
109             json = jsonReader.readObject();
110             in.close();
111         } else {
112             InputStream in = null;
113             in = new FileInputStream(jsonInput);
114             jsonReader = Json.createReader(in);
115             json = jsonReader.readObject();
116             try {
117                 in.close();
118             } catch (IOException e) {
119                 System.err.println("Exception Occured while closing input stream" + e);
120             }
121         }
122         jsonReader.close();
123         return json;
124     }
125
126 }