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.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;
41 import java.util.UUID;
43 import javax.json.Json;
44 import javax.json.JsonObject;
45 import javax.json.JsonReader;
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;
55 public class BrmsRawPolicyClient {
56 static Boolean isEdit = true;
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));
62 StringBuilder stringBuilder = new StringBuilder();
63 String ls = System.getProperty("line.separator");
66 while( ( line = reader.readLine() ) != null ) {
67 stringBuilder.append( line );
68 stringBuilder.append( ls );
71 return stringBuilder.toString();
78 public static void main(String[] args) {
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);
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());
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");
107 File rawBodyFile = null;
109 Path file = Paths.get("C:\\Users\\testuser\\Documents\\API\\com.Config_BRMS_Raw_TestBrmsPolicy.1.txt");
110 rawBodyFile = file.toFile();
112 policyParameters.setConfigBody(readFile(rawBodyFile.toString()));
113 policyParameters.setConfigBodyType(PolicyType.OTHER);
115 /*public String createUpdateBRMSRawPolicy(String policyName,
116 String policyDescription,
117 Map<String,String> dyanamicFieldConfigAttributes,
123 // API method to create Policy or update policy
124 PolicyChangeResponse response = null;
126 response = policyEngine.createPolicy(policyParameters);
128 response = policyEngine.updatePolicy(policyParameters);
131 if(response.getResponseCode()==200){
132 System.out.println(response.getResponseMessage());
133 System.out.println("Policy Created Successfully!");
135 System.out.println("Error! " + response.getResponseMessage());
137 } catch (Exception e) {
138 System.err.println(e.getMessage());