Optimize PAP policy constructor with builder
[policy/engine.git] / PolicyEngineClient / src / test / java / org / onap / policyengine / BrmsRawPolicyClient.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.BufferedReader;
24 import java.io.File;
25 import java.io.FileReader;
26 import java.io.IOException;
27 import java.nio.file.Path;
28 import java.nio.file.Paths;
29 import java.text.SimpleDateFormat;
30 import java.util.Date;
31 import java.util.HashMap;
32 import java.util.Map;
33 import java.util.UUID;
34
35 import org.onap.policy.api.AttributeType;
36 import org.onap.policy.api.PolicyChangeResponse;
37 import org.onap.policy.api.PolicyConfigType;
38 import org.onap.policy.api.PolicyEngine;
39 import org.onap.policy.api.PolicyParameters;
40 import org.onap.policy.api.PolicyType;
41
42 public class BrmsRawPolicyClient {
43     static Boolean isEdit = true;
44
45     //Reads a File and converts into a String.
46     private static String readFile( String file ) throws IOException {
47
48         String         line = null;
49         StringBuilder  stringBuilder = new StringBuilder();
50         String         ls = System.getProperty("line.separator");
51
52         try (BufferedReader reader = new BufferedReader( new FileReader (file))) {
53             while( ( line = reader.readLine() ) != null ) {
54                 stringBuilder.append( line );
55                 stringBuilder.append( ls );
56             }
57             return stringBuilder.toString();
58         }
59     }
60
61
62     public static void main(String[] args) {
63         try {
64             PolicyEngine policyEngine = new PolicyEngine("config.properties");
65             PolicyParameters policyParameters = new PolicyParameters();
66             Map<String, String> attrib= new HashMap<>();
67             attrib.put("cpu","80");
68             attrib.put("memory", "50");
69             Map<AttributeType, Map<String, String>> attributes = new HashMap<>();
70             attributes.put(AttributeType.RULE, attrib);
71
72             // Set Policy Type
73             policyParameters.setPolicyConfigType(PolicyConfigType.BRMS_RAW); //required
74             policyParameters.setPolicyName("Lakshman.testBRMSRawAPITwo"); //required
75             policyParameters.setPolicyDescription("This is a sample BRMS Raw policy body");  //optional
76             policyParameters.setAttributes(attributes);
77             //policyParameters.setPolicyScope("Lakshman"); //Directory will be created where the Policies are saved... this displays a a subscope on the GUI
78             policyParameters.setRequestID(UUID.randomUUID());
79
80             // Set Safe Policy value for Risk Type
81             SimpleDateFormat dateformat3 = new SimpleDateFormat("dd/MM/yyyy");
82             Date date = dateformat3.parse("15/10/2016");
83             policyParameters.setTtlDate(date);
84             // Set Safe Policy value for Guard
85             policyParameters.setGuard(true);
86             // Set Safe Policy value for Risk Level
87             policyParameters.setRiskLevel("5");
88             // Set Safe Policy value for Risk Type
89             policyParameters.setRiskType("PROD");
90
91             File rawBodyFile = null;
92
93             Path file = Paths.get("C:\\Users\\testuser\\Documents\\API\\com.Config_BRMS_Raw_TestBrmsPolicy.1.txt");
94             rawBodyFile = file.toFile();
95
96             policyParameters.setConfigBody(readFile(rawBodyFile.toString()));
97             policyParameters.setConfigBodyType(PolicyType.OTHER);
98
99             // API method to create Policy or update policy
100             PolicyChangeResponse response = null;
101             if (!isEdit) {
102                 response = policyEngine.createPolicy(policyParameters);
103             } else {
104                 response = policyEngine.updatePolicy(policyParameters);
105             }
106
107             if(response.getResponseCode()==200){
108                 System.out.println(response.getResponseMessage());
109                 System.out.println("Policy Created Successfully!");
110             }else{
111                 System.out.println("Error! " + response.getResponseMessage());
112             }
113         } catch (Exception e) {
114             System.err.println(e.getMessage());
115         }
116     }
117
118 }
119
120