2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017, 2019 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 OptimizationPolicyJavaAPIClient {
48 * THIS IS AN EXAMPLE JAVA CLIENT API FOR CREATE/UPDATE Optimization Policies
50 // For updating a Optmization policy set the "isEdit" flag to true.
51 // For creating a Optmization policy set the "isEdit" flag to false.
52 static Boolean isEdit = false;
54 // Builds JSONObject from File
55 private static JsonObject buildJson(File jsonInput, String jsonString) throws FileNotFoundException {
56 JsonObject json = null;;
57 JsonReader jsonReader = null;
58 if (jsonString != null && jsonInput == null) {
59 StringReader in = null;
60 in = new StringReader(jsonString);
61 jsonReader = Json.createReader(in);
62 json = jsonReader.readObject();
65 InputStream in = null;
66 in = new FileInputStream(jsonInput);
67 jsonReader = Json.createReader(in);
68 json = jsonReader.readObject();
71 } catch (IOException e) {
72 System.err.println("Exception Occured while closing input stream" + e);
82 * @param args String[] args
84 public static void main(String[] args) {
86 PolicyEngine policyEngine = new PolicyEngine("config.properties");
87 PolicyParameters policyParameters = new PolicyParameters();
89 policyParameters.setPolicyConfigType(PolicyConfigType.Optimization);
90 policyParameters.setPolicyName("Mike.testOOFPolicyFromJavaClient");
91 policyParameters.setPolicyDescription("This is a sample Optimization policy");
92 policyParameters.setOnapName("OOF");
95 String oofJsonString = null;
97 // path where you store the json payload
98 Path file = Paths.get("/home/users/PolicyEngine/json/optimizationPolicy.json");
99 jsonFile = file.toFile();
101 policyParameters.setConfigBody(buildJson(jsonFile, oofJsonString).toString());
102 policyParameters.setConfigBodyType(PolicyType.JSON);
104 policyParameters.setRequestID(UUID.randomUUID());
105 // Set Safe Policy value for Risk Type
106 SimpleDateFormat dateformat3 = new SimpleDateFormat("dd/MM/yyyy");
107 Date date = dateformat3.parse("15/10/2016");
108 policyParameters.setTtlDate(date);
109 // Set Safe Policy value for Guard
110 policyParameters.setGuard(false);
111 // Set Safe Policy value for Risk Level
112 policyParameters.setRiskLevel("5");
113 // Set Safe Policy value for Risk Type
114 policyParameters.setRiskType("TEST");
116 // API method to create or update Policy.
117 PolicyChangeResponse response = null;
119 response = policyEngine.createPolicy(policyParameters);
121 response = policyEngine.updatePolicy(policyParameters);
124 if (response.getResponseCode() == 200) {
125 System.out.println(response.getResponseMessage());
126 System.out.println("Policy Created Successfully!");
128 System.out.println("Error! " + response.getResponseMessage());
130 } catch (Exception e) {
131 System.err.println(e.getMessage());