2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.policyEngine;
23 import java.io.BufferedReader;
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;
33 import java.util.UUID;
35 import org.openecomp.policy.api.AttributeType;
36 import org.openecomp.policy.api.PolicyChangeResponse;
37 import org.openecomp.policy.api.PolicyConfigType;
38 import org.openecomp.policy.api.PolicyEngine;
39 import org.openecomp.policy.api.PolicyParameters;
40 import org.openecomp.policy.api.PolicyType;
42 public class BrmsRawPolicyClient {
43 static Boolean isEdit = true;
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));
49 StringBuilder stringBuilder = new StringBuilder();
50 String ls = System.getProperty("line.separator");
53 while( ( line = reader.readLine() ) != null ) {
54 stringBuilder.append( line );
55 stringBuilder.append( ls );
58 return stringBuilder.toString();
65 public static void main(String[] args) {
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);
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());
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");
94 File rawBodyFile = null;
96 Path file = Paths.get("C:\\Users\\testuser\\Documents\\API\\com.Config_BRMS_Raw_TestBrmsPolicy.1.txt");
97 rawBodyFile = file.toFile();
99 policyParameters.setConfigBody(readFile(rawBodyFile.toString()));
100 policyParameters.setConfigBodyType(PolicyType.OTHER);
102 /*public String createUpdateBRMSRawPolicy(String policyName,
103 String policyDescription,
104 Map<String,String> dyanamicFieldConfigAttributes,
110 // API method to create Policy or update policy
111 PolicyChangeResponse response = null;
113 response = policyEngine.createPolicy(policyParameters);
115 response = policyEngine.updatePolicy(policyParameters);
118 if(response.getResponseCode()==200){
119 System.out.println(response.getResponseMessage());
120 System.out.println("Policy Created Successfully!");
122 System.out.println("Error! " + response.getResponseMessage());
124 } catch (Exception e) {
125 System.err.println(e.getMessage());