Remove policy-yaml from policy/engine
[policy/engine.git] / PolicyEngineClient / src / main / 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             BufferedReader reader = new BufferedReader( new FileReader (file));
48             String         line = null;
49             StringBuilder  stringBuilder = new StringBuilder();
50             String         ls = System.getProperty("line.separator");
51
52             try {
53                 while( ( line = reader.readLine() ) != null ) {
54                     stringBuilder.append( line );
55                     stringBuilder.append( ls );
56                 }
57
58                 return stringBuilder.toString();
59             } finally {
60                 reader.close();
61             }
62         }
63         
64         
65         public static void main(String[] args) {
66                 try {
67                 PolicyEngine policyEngine = new PolicyEngine("config.properties");
68                 PolicyParameters policyParameters = new PolicyParameters();
69                 Map<String, String> attrib= new HashMap<>();
70                 attrib.put("cpu","80");
71                 attrib.put("memory", "50");
72                 Map<AttributeType, Map<String, String>> attributes = new HashMap<>();
73             attributes.put(AttributeType.RULE, attrib);
74                 
75                 // Set Policy Type
76                 policyParameters.setPolicyConfigType(PolicyConfigType.BRMS_RAW); //required
77                 policyParameters.setPolicyName("Lakshman.testBRMSRawAPITwo"); //required
78                 policyParameters.setPolicyDescription("This is a sample BRMS Raw policy body");  //optional
79                 policyParameters.setAttributes(attributes);
80                 //policyParameters.setPolicyScope("Lakshman"); //Directory will be created where the Policies are saved... this displays a a subscope on the GUI
81                 policyParameters.setRequestID(UUID.randomUUID());
82                 
83             // Set Safe Policy value for Risk Type
84                         SimpleDateFormat dateformat3 = new SimpleDateFormat("dd/MM/yyyy");
85                         Date date = dateformat3.parse("15/10/2016");
86                         policyParameters.setTtlDate(date);
87                         // Set Safe Policy value for Guard
88                         policyParameters.setGuard(true);
89                         // Set Safe Policy value for Risk Level
90                         policyParameters.setRiskLevel("5");
91                         // Set Safe Policy value for Risk Type
92                         policyParameters.setRiskType("PROD");
93                 
94                 File rawBodyFile = null;
95                 
96                         Path file = Paths.get("C:\\Users\\testuser\\Documents\\API\\com.Config_BRMS_Raw_TestBrmsPolicy.1.txt");
97                         rawBodyFile = file.toFile();
98                         
99                         policyParameters.setConfigBody(readFile(rawBodyFile.toString()));               
100                         policyParameters.setConfigBodyType(PolicyType.OTHER);
101                         
102                         /*public String createUpdateBRMSRawPolicy(String policyName, 
103                                         String policyDescription, 
104                                         Map<String,String> dyanamicFieldConfigAttributes,
105                                         String brmsRawBody, 
106                                         String policyScope, 
107                                         Boolean isEdit, 
108                                         UUID requestID) */
109         
110                 // API method to create Policy or update policy
111                 PolicyChangeResponse response = null;
112                 if (!isEdit) {
113                     response = policyEngine.createPolicy(policyParameters);
114                 } else {
115                         response = policyEngine.updatePolicy(policyParameters);
116                 }
117                 
118                 if(response.getResponseCode()==200){
119                     System.out.println(response.getResponseMessage());
120                     System.out.println("Policy Created Successfully!");
121                 }else{
122                     System.out.println("Error! " + response.getResponseMessage());
123                 }
124                 } catch (Exception e) {
125                         System.err.println(e.getMessage());
126                 }
127         }
128         
129 }
130
131