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;
34 import javax.json.Json;
35 import javax.json.JsonObject;
36 import javax.json.JsonReader;
38 import org.onap.policy.api.PolicyChangeResponse;
39 import org.onap.policy.api.PolicyConfigType;
40 import org.onap.policy.api.PolicyEngine;
41 import org.onap.policy.api.PolicyParameters;
42 import org.onap.policy.api.PolicyType;
43 public class OptimizationPolicyJavaAPIClient {
46 * THIS IS AN EXAMPLE JAVA CLIENT API FOR CREATE/UPDATE Optimization Policies
48 //For updating a Optmization policy set the "isEdit" flag to true.
49 //For creating a Optmization policy set the "isEdit" flag to false.
50 static Boolean isEdit = false;
52 //Builds JSONObject from File
53 private static JsonObject buildJSON(File jsonInput, String jsonString) throws FileNotFoundException {
54 JsonObject json = null;;
55 JsonReader jsonReader = null;
56 if (jsonString != null && jsonInput == null) {
57 StringReader in = null;
58 in = new StringReader(jsonString);
59 jsonReader = Json.createReader(in);
60 json = jsonReader.readObject();
64 InputStream in = null;
65 in = new FileInputStream(jsonInput);
66 jsonReader = Json.createReader(in);
67 json = jsonReader.readObject();
70 } catch (IOException e) {
71 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.Optimization);
83 policyParameters.setPolicyName("Mike.testOOFPolicyFromJavaClient");
84 policyParameters.setPolicyDescription("This is a sample Optimization policy");
85 policyParameters.setOnapName("OOF");
88 String OOFjsonString= null;
90 //path where you store the json payload
91 Path file = Paths.get("/home/users/PolicyEngine/json/optimizationPolicy.json");
92 jsonFile = file.toFile();
94 policyParameters.setConfigBody(buildJSON(jsonFile, OOFjsonString).toString());
95 policyParameters.setConfigBodyType(PolicyType.JSON);
97 policyParameters.setRequestID(UUID.randomUUID());
98 // Set Safe Policy value for Risk Type
99 SimpleDateFormat dateformat3 = new SimpleDateFormat("dd/MM/yyyy");
100 Date date = dateformat3.parse("15/10/2016");
101 policyParameters.setTtlDate(date);
102 // Set Safe Policy value for Guard
103 policyParameters.setGuard(false);
104 // Set Safe Policy value for Risk Level
105 policyParameters.setRiskLevel("5");
106 // Set Safe Policy value for Risk Type
107 policyParameters.setRiskType("TEST");
109 // API method to create or update Policy.
110 PolicyChangeResponse response = null;
112 response = policyEngine.createPolicy(policyParameters);
115 response = policyEngine.updatePolicy(policyParameters);
118 if(response.getResponseCode()==200){
119 System.out.println(response.getResponseMessage());
120 System.out.println("Policy Created Successfully!");
122 System.out.println("Error! " + response.getResponseMessage());
124 } catch (Exception e) {
125 System.err.println(e.getMessage());