Initial OpenECOMP policy/engine commit
[policy/engine.git] / PolicyEngineClient / src / main / java / org / openecomp / 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.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 import javax.json.Json;
34 import javax.json.JsonObject;
35 import javax.json.JsonReader;
36
37 import org.openecomp.policy.api.PolicyChangeResponse;
38 import org.openecomp.policy.api.PolicyConfigType;
39 import org.openecomp.policy.api.PolicyEngine;
40 import org.openecomp.policy.api.PolicyParameters;
41 import org.openecomp.policy.api.PolicyType;
42 public class MicroServicesPolicyClient {
43      
44 //For updating a Micro Services policy set the "isEdit" flag to true.   
45 //For creating a Micro Services policy set the "isEdit" flag to false.  
46 static Boolean isEdit = false;
47      
48 //Builds JSONObject from File  
49 private static JsonObject buildJSON(File jsonInput, String jsonString) throws FileNotFoundException {
50     JsonObject json = null;;
51          
52     if (jsonString != null && jsonInput == null) {
53         StringReader in = null;
54             in = new StringReader(jsonString);
55         JsonReader jsonReader = Json.createReader(in);
56             json = jsonReader.readObject();
57     }
58     else {
59         InputStream in = null;
60         in = new FileInputStream(jsonInput);
61         JsonReader jsonReader = Json.createReader(in);
62         json = jsonReader.readObject();
63     }
64          
65     return json;
66 }
67 public static void main(String[] args) {
68                 try {
69                         PolicyEngine policyEngine = new PolicyEngine("config.properties");
70                         PolicyParameters policyParameters = new PolicyParameters();
71                         // Set Policy Type 
72                         policyParameters.setPolicyConfigType(PolicyConfigType.MicroService);
73                         policyParameters.setPolicyName("Katrina.configuration_dcae_microservice_stringmatcher");
74                         //policyParameters.setPolicyDescription("This is a sample Micro Service policy Create example");
75                         policyParameters.setEcompName("DCAE");
76                         //policyParameters.setConfigName("Collector");
77                         //policyParameters.setPriority("1");
78                         //policyParameters.setPolicyScope("service=vSCP;resource=F5;type=configuration;closedLoopControlName=vSCP_F5_Firewall_d925ed73-8231-4d02-9545-db4e113213abab322");
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\\MicroServicesJSON\\testStringMatching.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 }