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.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 ClosedLoopPolicyPerformanceMetricClient {
47 //For updating a ClosedLoop_Fault policy set the "isEdit" flag to true.
48 //For creating a ClosedLoop_Fault policy set the "isEdit" flag to false.
49 static Boolean isEdit = true;
51 //Builds JSONObject from File
52 private static JsonObject buildJSON(File jsonInput, String jsonString) throws FileNotFoundException {
53 JsonObject json = null;;
54 JsonReader jsonReader = null;
55 if (jsonString != null && jsonInput == null) {
56 StringReader in = null;
57 in = new StringReader(jsonString);
58 jsonReader = Json.createReader(in);
59 json = jsonReader.readObject();
63 InputStream in = null;
64 in = new FileInputStream(jsonInput);
65 jsonReader = Json.createReader(in);
66 json = jsonReader.readObject();
69 } catch (IOException e) {
70 System.err.println("Exception Occured while closing input stream"+e);
77 public static void main(String[] args) {
79 PolicyEngine policyEngine = new PolicyEngine("config.properties");
80 PolicyParameters policyParameters = new PolicyParameters();
82 policyParameters.setPolicyConfigType(PolicyConfigType.ClosedLoop_PM);
83 policyParameters.setPolicyName("MikeAPItests.ClosedLoopPmApiTest");
84 policyParameters.setPolicyDescription("This is a sample ClosedLoop_PM policy CREATE example");
85 //policyParameters.setPolicyScope("MikeAPItests");
87 // Set up Micro Services Attributes
89 String MSjsonString= null;
90 Path file = Paths.get("C:\\policyAPI\\ClosedLoopJSON\\pmTestJson.json");
91 jsonFile = file.toFile();
93 policyParameters.setConfigBody(buildJSON(jsonFile, MSjsonString).toString());
94 policyParameters.setConfigBodyType(PolicyType.JSON);
96 policyParameters.setRequestID(UUID.randomUUID());
97 // Set Safe Policy value for Risk Type
98 SimpleDateFormat dateformat3 = new SimpleDateFormat("dd/MM/yyyy");
99 Date date = dateformat3.parse("15/10/2016");
100 policyParameters.setTtlDate(date);
101 // Set Safe Policy value for Guard
102 policyParameters.setGuard(true);
103 // Set Safe Policy value for Risk Level
104 policyParameters.setRiskLevel("5");
105 // Set Safe Policy value for Risk Type
106 policyParameters.setRiskType("PROD");
108 // API method to create or update Policy.
109 PolicyChangeResponse response = null;
111 response = policyEngine.createPolicy(policyParameters);
114 response = policyEngine.updatePolicy(policyParameters);
117 if(response.getResponseCode()==200){
118 System.out.println(response.getResponseMessage());
119 System.out.println("Policy Created Successfully!");
121 System.out.println("Error! " + response.getResponseMessage());
123 } catch (Exception e) {
124 System.err.println(e.getMessage());