Initial OpenECOMP policy/engine commit
[policy/engine.git] / PolicyEngineClient / src / main / java / org / openecomp / policyEngine / ClosedLoopPolicyPerformanceMetricClient.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.openecomp.policyEngine;
22
23 import java.io.File;
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;
33
34 import javax.json.Json;
35 import javax.json.JsonObject;
36 import javax.json.JsonReader;
37
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;
43
44 public class ClosedLoopPolicyPerformanceMetricClient {
45         
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;
49              
50         //Builds JSONObject from File  
51         private static JsonObject buildJSON(File jsonInput, String jsonString) throws FileNotFoundException {
52             JsonObject json = null;;
53                  
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();
59             }
60             else {
61                 InputStream in = null;
62                 in = new FileInputStream(jsonInput);
63                 JsonReader jsonReader = Json.createReader(in);
64                 json = jsonReader.readObject();
65             }
66                  
67             return json;
68         }
69         
70         public static void main(String[] args) {
71                         try {
72                                 PolicyEngine policyEngine = new PolicyEngine("config.properties");
73                                 PolicyParameters policyParameters = new PolicyParameters();
74                                 // Set Policy Type 
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");
79                                 
80                                 // Set up Micro Services Attributes 
81                                 File jsonFile = null;
82                                 String MSjsonString= null;
83                                 if (MSjsonString == null) {
84                                         Path file = Paths.get("C:\\policyAPI\\ClosedLoopJSON\\pmTestJson.json");
85                                         jsonFile = file.toFile();
86                                 }
87                                 policyParameters.setConfigBody(buildJSON(jsonFile, MSjsonString).toString());           
88                                 policyParameters.setConfigBodyType(PolicyType.JSON);
89
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");
101                                 
102                                 // API method to create or update Policy. 
103                         PolicyChangeResponse response = null;
104                         if (!isEdit) {
105                             response = policyEngine.createPolicy(policyParameters);
106                         } 
107                         else {  
108                                 response = policyEngine.updatePolicy(policyParameters);
109                         }
110                         
111                                 if(response.getResponseCode()==200){
112                                         System.out.println(response.getResponseMessage());
113                                         System.out.println("Policy Created Successfully!");
114                                 }else{
115                                         System.out.println("Error! " + response.getResponseMessage());
116                                 }
117                         } catch (Exception e) {
118                                 System.err.println(e.getMessage());
119                         }
120                 }
121 }