Remove policy-yaml from policy/engine
[policy/engine.git] / PolicyEngineClient / src / main / java / org / onap / policyEngine / MicroServicesPolicyClient.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 MicroServicesPolicyClient {
44      
45 //For updating a Micro Services policy set the "isEdit" flag to true.   
46 //For creating a Micro Services 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     JsonReader jsonReader = null;  
53     if (jsonString != null && jsonInput == null) {
54         StringReader in = null;
55             in = new StringReader(jsonString);
56             jsonReader = Json.createReader(in);
57             json = jsonReader.readObject();
58             in.close();
59     }
60     else {
61         InputStream in = null;
62         in = new FileInputStream(jsonInput);
63         jsonReader = Json.createReader(in);
64         json = jsonReader.readObject();
65         try {
66                         in.close();
67                 } catch (IOException e) {
68                         System.err.println("Exception Occured while closing input stream"+e);
69                 }
70     }
71     jsonReader.close();    
72     return json;
73 }
74 public static void main(String[] args) {
75                 try {
76                         PolicyEngine policyEngine = new PolicyEngine("config.properties");
77                         PolicyParameters policyParameters = new PolicyParameters();
78                         // Set Policy Type 
79                         policyParameters.setPolicyConfigType(PolicyConfigType.MicroService);
80                         policyParameters.setPolicyName("Katrina.configuration_dcae_microservice_stringmatcher");
81                         //policyParameters.setPolicyDescription("This is a sample Micro Service policy Create example");
82                         policyParameters.setOnapName("DCAE");
83                         //policyParameters.setConfigName("Collector");
84                         //policyParameters.setPriority("1");
85                         //policyParameters.setPolicyScope("service=vSCP;resource=F5;type=configuration;closedLoopControlName=vSCP_F5_Firewall_d925ed73-8231-4d02-9545-db4e113213abab322");
86                         
87                         // Set up Micro Services Attributes 
88                         File jsonFile = null;
89                         String MSjsonString= null;
90                         Path file = Paths.get("C:\\policyAPI\\MicroServicesJSON\\testStringMatching.json");
91                         jsonFile = file.toFile();
92                         
93                         policyParameters.setConfigBody(buildJSON(jsonFile, MSjsonString).toString());           
94                         policyParameters.setConfigBodyType(PolicyType.JSON);
95
96                         policyParameters.setRequestID(UUID.randomUUID());
97             // Set Safe Policy value for Risk Type
98                         SimpleDateFormat dateformat3 = new SimpleDateFormat("dd/MM/yyyy");
99                         Date date = dateformat3.parse("15/10/2016");
100                         policyParameters.setTtlDate(date);
101                         // Set Safe Policy value for Guard
102                         policyParameters.setGuard(true);
103                         // Set Safe Policy value for Risk Level
104                         policyParameters.setRiskLevel("5");
105                         // Set Safe Policy value for Risk Type
106                         policyParameters.setRiskType("PROD");
107                         
108                         // API method to create or update Policy. 
109                 PolicyChangeResponse response = null;
110                 if (!isEdit) {
111                     response = policyEngine.createPolicy(policyParameters);
112                 } 
113                 else {  
114                         response = policyEngine.updatePolicy(policyParameters);
115                 }
116                 
117                         if(response.getResponseCode()==200){
118                                 System.out.println(response.getResponseMessage());
119                                 System.out.println("Policy Created Successfully!");
120                         }else{
121                                 System.out.println("Error! " + response.getResponseMessage());
122                         }
123                 } catch (Exception e) {
124                         System.err.println(e.getMessage());
125                 }
126         }
127 }