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.onap.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.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;
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 {
49 StringBuilder stringBuilder = new StringBuilder();
50 String ls = System.getProperty("line.separator");
52 try (BufferedReader reader = new BufferedReader( new FileReader (file))) {
53 while( ( line = reader.readLine() ) != null ) {
54 stringBuilder.append( line );
55 stringBuilder.append( ls );
57 return stringBuilder.toString();
62 public static void main(String[] args) {
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);
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());
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");
91 File rawBodyFile = null;
93 Path file = Paths.get("C:\\Users\\testuser\\Documents\\API\\com.Config_BRMS_Raw_TestBrmsPolicy.1.txt");
94 rawBodyFile = file.toFile();
96 policyParameters.setConfigBody(readFile(rawBodyFile.toString()));
97 policyParameters.setConfigBodyType(PolicyType.OTHER);
99 // API method to create Policy or update policy
100 PolicyChangeResponse response = null;
102 response = policyEngine.createPolicy(policyParameters);
104 response = policyEngine.updatePolicy(policyParameters);
107 if(response.getResponseCode()==200){
108 System.out.println(response.getResponseMessage());
109 System.out.println("Policy Created Successfully!");
111 System.out.println("Error! " + response.getResponseMessage());
113 } catch (Exception e) {
114 System.err.println(e.getMessage());