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.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;
34 import javax.json.Json;
35 import javax.json.JsonObject;
36 import javax.json.JsonReader;
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;
44 public class ClosedLoopPolicyClient {
45 //For updating a ClosedLoop_Fault policy set the "isEdit" flag to true.
46 //For creating a ClosedLoop_Fault policy set the "isEdit" flag to false.
47 static Boolean isEdit = false;
49 //Builds JSONObject from File
50 private static JsonObject buildJSON(File jsonInput, String jsonString) throws FileNotFoundException {
51 JsonObject json = null;;
53 if (jsonString != null && jsonInput == null) {
54 StringReader in = null;
55 in = new StringReader(jsonString);
56 JsonReader jsonReader = Json.createReader(in);
57 json = jsonReader.readObject();
60 InputStream in = null;
61 in = new FileInputStream(jsonInput);
62 JsonReader jsonReader = Json.createReader(in);
63 json = jsonReader.readObject();
68 public static void main(String[] args) {
70 PolicyEngine policyEngine = new PolicyEngine("config.properties");
71 PolicyParameters policyParameters = new PolicyParameters();
73 policyParameters.setPolicyConfigType(PolicyConfigType.ClosedLoop_Fault);
74 policyParameters.setPolicyName("MikeAPItests.ClosedLoopFaultApiTest45");
75 policyParameters.setPolicyDescription("This is a sample ClosedLoop_Fault policy CREATE example");
76 //policyParameters.setPolicyScope("MikeAPItests");
78 // Set up Micro Services Attributes
80 String MSjsonString= null;
81 if (MSjsonString == null) {
82 Path file = Paths.get("C:\\policyAPI\\ClosedLoopJSON\\faultTestJson.json");
83 jsonFile = file.toFile();
85 policyParameters.setConfigBody(buildJSON(jsonFile, MSjsonString).toString());
86 policyParameters.setConfigBodyType(PolicyType.JSON);
88 policyParameters.setRequestID(UUID.randomUUID());
89 // Set Safe Policy value for Risk Type
90 SimpleDateFormat dateformat3 = new SimpleDateFormat("dd/MM/yyyy");
91 Date date = dateformat3.parse("15/10/2016");
92 policyParameters.setTtlDate(date);
93 // Set Safe Policy value for Guard
94 policyParameters.setGuard(true);
95 // Set Safe Policy value for Risk Level
96 policyParameters.setRiskLevel("5");
97 // Set Safe Policy value for Risk Type
98 policyParameters.setRiskType("PROD");
100 // API method to create or update Policy.
101 PolicyChangeResponse response = null;
103 response = policyEngine.createPolicy(policyParameters);
106 response = policyEngine.updatePolicy(policyParameters);
109 if(response.getResponseCode()==200){
110 System.out.println(response.getResponseMessage());
111 System.out.println("Policy Created Successfully!");
113 System.out.println("Error! " + response.getResponseMessage());
115 } catch (Exception e) {
116 System.err.println(e.getMessage());