Format source code PolicyEngineClient
[policy/engine.git] / PolicyEngineClient / src / test / java / org / onap / policyengine / OptimizationPolicyJavaAPIClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * PolicyEngineClient
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policyengine;
22
23 import java.io.File;
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
35 import javax.json.Json;
36 import javax.json.JsonObject;
37 import javax.json.JsonReader;
38
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;
44
45 public class OptimizationPolicyJavaAPIClient {
46
47     /*
48      * THIS IS AN EXAMPLE JAVA CLIENT API FOR CREATE/UPDATE Optimization Policies
49      */
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;
53
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();
63             in.close();
64         } else {
65             InputStream in = null;
66             in = new FileInputStream(jsonInput);
67             jsonReader = Json.createReader(in);
68             json = jsonReader.readObject();
69             try {
70                 in.close();
71             } catch (IOException e) {
72                 System.err.println("Exception Occured while closing input stream" + e);
73             }
74         }
75         jsonReader.close();
76         return json;
77     }
78
79     /**
80      * main.
81      *
82      * @param args String[] args
83      */
84     public static void main(String[] args) {
85         try {
86             PolicyEngine policyEngine = new PolicyEngine("config.properties");
87             PolicyParameters policyParameters = new PolicyParameters();
88             // Set Policy Type
89             policyParameters.setPolicyConfigType(PolicyConfigType.Optimization);
90             policyParameters.setPolicyName("Mike.testOOFPolicyFromJavaClient");
91             policyParameters.setPolicyDescription("This is a sample Optimization policy");
92             policyParameters.setOnapName("OOF");
93
94             File jsonFile = null;
95             String oofJsonString = null;
96
97             // path where you store the json payload
98             Path file = Paths.get("/home/users/PolicyEngine/json/optimizationPolicy.json");
99             jsonFile = file.toFile();
100
101             policyParameters.setConfigBody(buildJson(jsonFile, oofJsonString).toString());
102             policyParameters.setConfigBodyType(PolicyType.JSON);
103
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");
115
116             // API method to create or update Policy.
117             PolicyChangeResponse response = null;
118             if (!isEdit) {
119                 response = policyEngine.createPolicy(policyParameters);
120             } else {
121                 response = policyEngine.updatePolicy(policyParameters);
122             }
123
124             if (response.getResponseCode() == 200) {
125                 System.out.println(response.getResponseMessage());
126                 System.out.println("Policy Created Successfully!");
127             } else {
128                 System.out.println("Error! " + response.getResponseMessage());
129             }
130         } catch (Exception e) {
131             System.err.println(e.getMessage());
132         }
133     }
134 }