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