2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policyengine;
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;
35 import javax.json.Json;
36 import javax.json.JsonObject;
37 import javax.json.JsonReader;
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;
45 public class ConfigFirewallPolicyClient {
46 static Boolean isEdit = false;
51 * @param args String[] args
53 public static void main(String[] args) {
55 PolicyEngine policyEngine = new PolicyEngine("config.properties");
56 PolicyParameters policyParameters = new PolicyParameters();
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());
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");
75 String jsonRuleList = null;
76 Path file = Paths.get("C:\\policyAPI\\firewallRulesJSON\\Config_FW_1607Rule.json");
77 jsonFile = file.toFile();
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;
85 response = policyEngine.createPolicy(policyParameters);
87 response = policyEngine.updatePolicy(policyParameters);
90 if (response.getResponseCode() == 200) {
91 System.out.println(response.getResponseMessage());
92 System.out.println("Policy Created Successfully!");
94 System.out.println("Error! " + response.getResponseMessage());
96 } catch (Exception e) {
97 System.err.println(e.getMessage());
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();
112 InputStream in = null;
113 in = new FileInputStream(jsonInput);
114 jsonReader = Json.createReader(in);
115 json = jsonReader.readObject();
118 } catch (IOException e) {
119 System.err.println("Exception Occured while closing input stream" + e);