Fixes for sonar critical issues
[policy/engine.git] / PolicyEngineClient / src / test / java / org / onap / policyengine / BrmsParamPolicyClient.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.onap.policyengine;
22
23 import java.text.SimpleDateFormat;
24 import java.util.Date;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.UUID;
28
29 import org.onap.policy.api.AttributeType;
30 import org.onap.policy.api.PolicyChangeResponse;
31 import org.onap.policy.api.PolicyConfigType;
32 import org.onap.policy.api.PolicyEngine;
33 import org.onap.policy.api.PolicyParameters;
34
35
36 public class BrmsParamPolicyClient {
37         
38         public static void main(String[] args) {
39                 try {
40
41             PolicyEngine policyEngine = new PolicyEngine("config.properties");
42
43             PolicyParameters policyParameters = new PolicyParameters();
44
45             // Set Policy Type(Mandatory) 
46             policyParameters.setPolicyConfigType(PolicyConfigType.BRMS_PARAM);
47             
48             // Set Policy Name(Mandatory) 
49             policyParameters.setPolicyName("Lakshman.testParamPolicyThree");
50             
51             // Set Safe Policy value for Risk Type
52                         SimpleDateFormat dateformat3 = new SimpleDateFormat("dd/MM/yyyy");
53                         Date date = dateformat3.parse("15/10/2016");
54                         policyParameters.setTtlDate(date);
55                         // Set Safe Policy value for Guard
56                         policyParameters.setGuard(true);
57                         // Set Safe Policy value for Risk Level
58                         policyParameters.setRiskLevel("5");
59                         // Set Safe Policy value for Risk Type
60                         policyParameters.setRiskType("PROD");
61             
62             // Set description of the policy(Optional)
63             policyParameters.setPolicyDescription("This is a sample BRMS Param policy creation example");
64             
65             // Set BRMS Param Template Attributes(Mandatory) 
66             Map<String, String> ruleAttributes = new HashMap<>();
67             ruleAttributes.put("templateName", "Sample"); // This sampleTemplate is the Template name from dictionary. 
68             ruleAttributes.put("controller", "default"); // Set Rule to a PDP Controller, default is the controller name.
69             ruleAttributes.put("SamPoll", "300"); // Template specific key and value set by us. 
70             ruleAttributes.put("value", "abcd"); // Template specific key and value set by us. 
71             Map<AttributeType, Map<String, String>> attributes = new HashMap<>();
72             attributes.put(AttributeType.RULE, ruleAttributes);
73             policyParameters.setAttributes(attributes);
74             
75             //Set a random UUID(Mandatory)
76             policyParameters.setRequestID(UUID.randomUUID());
77
78             // CreatePolicy method to create Policy. 
79
80             PolicyChangeResponse response = policyEngine.updatePolicy(policyParameters);
81
82             if(response.getResponseCode()==200){
83                 System.out.println(response.getResponseMessage());
84                 System.out.println("Policy Created Successfully!");
85             }else{
86                 System.out.println("Error! " + response.getResponseMessage());
87             }
88         } catch (Exception e) {
89             System.err.println(e.getMessage() + e);
90         }
91                 
92         }
93         
94 }
95
96