2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017, 2019 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();
64 * @param args String[] of arguments
66 public static void main(String[] args) {
68 PolicyEngine policyEngine = new PolicyEngine("config.properties");
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);
75 PolicyParameters policyParameters = new PolicyParameters();
77 policyParameters.setPolicyConfigType(PolicyConfigType.BRMS_RAW); // required
78 policyParameters.setPolicyName("Lakshman.testBRMSRawAPITwo"); // required
79 policyParameters.setPolicyDescription("This is a sample BRMS Raw policy body"); // optional
80 policyParameters.setAttributes(attributes);
81 // policyParameters.setPolicyScope("Lakshman"); //Directory will be created where the Policies are saved...
82 // this displays a a subscope on the GUI
83 policyParameters.setRequestID(UUID.randomUUID());
85 // Set Safe Policy value for Risk Type
86 SimpleDateFormat dateformat3 = new SimpleDateFormat("dd/MM/yyyy");
87 Date date = dateformat3.parse("15/10/2016");
88 policyParameters.setTtlDate(date);
89 // Set Safe Policy value for Guard
90 policyParameters.setGuard(true);
91 // Set Safe Policy value for Risk Level
92 policyParameters.setRiskLevel("5");
93 // Set Safe Policy value for Risk Type
94 policyParameters.setRiskType("PROD");
96 File rawBodyFile = null;
98 Path file = Paths.get("C:\\Users\\testuser\\Documents\\API\\com.Config_BRMS_Raw_TestBrmsPolicy.1.txt");
99 rawBodyFile = file.toFile();
101 policyParameters.setConfigBody(readFile(rawBodyFile.toString()));
102 policyParameters.setConfigBodyType(PolicyType.OTHER);
104 // API method to create Policy or update policy
105 PolicyChangeResponse response = null;
107 response = policyEngine.createPolicy(policyParameters);
109 response = policyEngine.updatePolicy(policyParameters);
112 if (response.getResponseCode() == 200) {
113 System.out.println(response.getResponseMessage());
114 System.out.println("Policy Created Successfully!");
116 System.out.println("Error! " + response.getResponseMessage());
118 } catch (Exception e) {
119 System.err.println(e.getMessage());