2 * ============LICENSE_START=======================================================
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
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.openecomp.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.openecomp.policy.api.PolicyChangeResponse;
40 import org.openecomp.policy.api.PolicyConfigType;
41 import org.openecomp.policy.api.PolicyEngine;
42 import org.openecomp.policy.api.PolicyParameters;
43 import org.openecomp.policy.api.PolicyType;
45 public class ConfigFirewallPolicyClient {
46 static Boolean isEdit = false;
47 public static void main(String[] args) {
49 PolicyEngine policyEngine = new PolicyEngine("config.properties");
50 PolicyParameters policyParameters = new PolicyParameters();
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());
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");
68 String jsonRuleList = null;
69 Path file = Paths.get("C:\\policyAPI\\firewallRulesJSON\\Config_FW_1607Rule.json");
70 jsonFile = file.toFile();
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;
78 response = policyEngine.createPolicy(policyParameters);
80 response = policyEngine.updatePolicy(policyParameters);
83 if(response.getResponseCode()==200){
84 System.out.println(response.getResponseMessage());
85 System.out.println("Policy Created Successfully!");
87 System.out.println("Error! " + response.getResponseMessage());
89 } catch (Exception e) {
90 System.err.println(e.getMessage());
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();
105 InputStream in = null;
106 in = new FileInputStream(jsonInput);
107 jsonReader = Json.createReader(in);
108 json = jsonReader.readObject();
111 } catch (IOException e) {
112 System.err.println("Exception Occured while closing input stream"+e);