New Optimization Policy
[policy/engine.git] / PolicyEngineClient / src / test / java / org / onap / policyengine / OptimizationPolicyJavaAPIClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * PolicyEngineClient
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
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 import javax.json.Json;
35 import javax.json.JsonObject;
36 import javax.json.JsonReader;
37
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 {
44         
45 /*
46  * THIS IS AN EXAMPLE JAVA CLIENT API FOR CREATE/UPDATE Optimization Policies
47  */
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;
51      
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();
61             in.close();
62     }
63     else {
64         InputStream in = null;
65         in = new FileInputStream(jsonInput);
66         jsonReader = Json.createReader(in);
67         json = jsonReader.readObject();
68         try {
69                         in.close();
70                 } catch (IOException e) {
71                         System.err.println("Exception Occured while closing input stream"+e);
72                 }
73     }
74     jsonReader.close();    
75     return json;
76 }
77 public static void main(String[] args) {
78                 try {
79                         PolicyEngine policyEngine = new PolicyEngine("config.properties");
80                         PolicyParameters policyParameters = new PolicyParameters();
81                         // Set Policy Type 
82                         policyParameters.setPolicyConfigType(PolicyConfigType.Optimization);
83                         policyParameters.setPolicyName("Mike.testOOFPolicyFromJavaClient");
84                         policyParameters.setPolicyDescription("This is a sample Optimization policy");
85                         policyParameters.setOnapName("OOF");
86
87                         File jsonFile = null;
88                         String OOFjsonString= null;
89                         
90                         //path where you store the json payload
91                         Path file = Paths.get("/home/users/PolicyEngine/json/optimizationPolicy.json");
92                         jsonFile = file.toFile();
93                         
94                         policyParameters.setConfigBody(buildJSON(jsonFile, OOFjsonString).toString());          
95                         policyParameters.setConfigBodyType(PolicyType.JSON);
96
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");
108                         
109                         // API method to create or update Policy. 
110                 PolicyChangeResponse response = null;
111                 if (!isEdit) {
112                     response = policyEngine.createPolicy(policyParameters);
113                 } 
114                 else {  
115                         response = policyEngine.updatePolicy(policyParameters);
116                 }
117                 
118                         if(response.getResponseCode()==200){
119                                 System.out.println(response.getResponseMessage());
120                                 System.out.println("Policy Created Successfully!");
121                         }else{
122                                 System.out.println("Error! " + response.getResponseMessage());
123                         }
124                 } catch (Exception e) {
125                         System.err.println(e.getMessage());
126                 }
127         }
128 }