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