Initial OpenECOMP policy/engine commit
[policy/engine.git] / PolicyEngineClient / src / main / java / org / openecomp / policyEngine / ClosedLoopPolicyClient.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 ClosedLoopPolicyClient {
45         //For updating a ClosedLoop_Fault policy set the "isEdit" flag to true.   
46         //For creating a ClosedLoop_Fault policy set the "isEdit" flag to false.  
47         static Boolean isEdit = false;
48              
49         //Builds JSONObject from File  
50         private static JsonObject buildJSON(File jsonInput, String jsonString) throws FileNotFoundException {
51             JsonObject json = null;;
52                  
53             if (jsonString != null && jsonInput == null) {
54                 StringReader in = null;
55                     in = new StringReader(jsonString);
56                 JsonReader jsonReader = Json.createReader(in);
57                     json = jsonReader.readObject();
58             }
59             else {
60                 InputStream in = null;
61                 in = new FileInputStream(jsonInput);
62                 JsonReader jsonReader = Json.createReader(in);
63                 json = jsonReader.readObject();
64             }
65                  
66             return json;
67         }
68         public static void main(String[] args) {
69                         try {
70                                 PolicyEngine policyEngine = new PolicyEngine("config.properties");
71                                 PolicyParameters policyParameters = new PolicyParameters();
72                                 // Set Policy Type 
73                                 policyParameters.setPolicyConfigType(PolicyConfigType.ClosedLoop_Fault);
74                                 policyParameters.setPolicyName("MikeAPItests.ClosedLoopFaultApiTest45");
75                                 policyParameters.setPolicyDescription("This is a sample ClosedLoop_Fault policy CREATE example");
76                                 //policyParameters.setPolicyScope("MikeAPItests");
77                                 
78                                 // Set up Micro Services Attributes 
79                                 File jsonFile = null;
80                                 String MSjsonString= null;
81                                 if (MSjsonString == null) {
82                                         Path file = Paths.get("C:\\policyAPI\\ClosedLoopJSON\\faultTestJson.json");
83                                         jsonFile = file.toFile();
84                                 }
85                                 policyParameters.setConfigBody(buildJSON(jsonFile, MSjsonString).toString());           
86                                 policyParameters.setConfigBodyType(PolicyType.JSON);
87
88                                 policyParameters.setRequestID(UUID.randomUUID());
89                     // Set Safe Policy value for Risk Type
90                                 SimpleDateFormat dateformat3 = new SimpleDateFormat("dd/MM/yyyy");
91                                 Date date = dateformat3.parse("15/10/2016");
92                                 policyParameters.setTtlDate(date);
93                                 // Set Safe Policy value for Guard
94                                 policyParameters.setGuard(true);
95                                 // Set Safe Policy value for Risk Level
96                                 policyParameters.setRiskLevel("5");
97                                 // Set Safe Policy value for Risk Type
98                                 policyParameters.setRiskType("PROD");
99                                 
100                                 // API method to create or update Policy. 
101                         PolicyChangeResponse response = null;
102                         if (!isEdit) {
103                             response = policyEngine.createPolicy(policyParameters);
104                         } 
105                         else {  
106                                 response = policyEngine.updatePolicy(policyParameters);
107                         }
108                         
109                                 if(response.getResponseCode()==200){
110                                         System.out.println(response.getResponseMessage());
111                                         System.out.println("Policy Created Successfully!");
112                                 }else{
113                                         System.out.println("Error! " + response.getResponseMessage());
114                                 }
115                         } catch (Exception e) {
116                                 System.err.println(e.getMessage());
117                         }
118                 }
119 }