Merge "Sonar fix forONAP-PAP-REST critical sonar issues"
[policy/engine.git] / PolicyEngineClient / src / test / java / org / onap / policyengine / ConfigBasePolicyClient.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 import org.onap.policy.api.PolicyType;
35
36 public class ConfigBasePolicyClient{
37         static Boolean isEdit = false;
38         public static void main(String[] args) {
39         try {
40             PolicyEngine policyEngine = new PolicyEngine("config.properties");
41             PolicyParameters policyParameters = new PolicyParameters();
42             // Set Policy Type
43             policyParameters.setPolicyConfigType(PolicyConfigType.Base); //required
44             policyParameters.setPolicyName("MikeConsole.testDeleteAPI6"); //required
45             policyParameters.setPolicyDescription("This is a sample Config Base policy creation example");  //optional
46             policyParameters.setOnapName("DCAE");  //required
47             policyParameters.setConfigName("testBase"); //required
48             policyParameters.setConfigBodyType(PolicyType.OTHER); //required
49             policyParameters.setConfigBody("testing");  //required
50             //policyParameters.setPolicyScope("MikeConsole"); //Directory will be created where the Policies are saved... this displays a a subscope on the GUI
51             
52             //Set the Config Attributes... These are Optional
53             Map<String, String> configAttributes = new HashMap<>(); 
54             configAttributes.put("Template", "SampleTemplate");
55             configAttributes.put("controller", "default"); 
56             configAttributes.put("SamPoll", "30");
57             configAttributes.put("value", "abcd"); 
58             Map<AttributeType, Map<String,String>> attributes = new HashMap<>();
59             attributes.put(AttributeType.MATCHING, configAttributes);
60             policyParameters.setAttributes(attributes);
61             policyParameters.setRequestID(UUID.randomUUID());
62             // Set Safe Policy value for Risk Type
63                         SimpleDateFormat dateformat3 = new SimpleDateFormat("dd/MM/yyyy");
64                         Date date = dateformat3.parse("15/10/2016");
65                         policyParameters.setTtlDate(date);
66                         // Set Safe Policy value for Guard
67                         policyParameters.setGuard(true);
68                         // Set Safe Policy value for Risk Level
69                         policyParameters.setRiskLevel("5");
70                         // Set Safe Policy value for Risk Type
71                         policyParameters.setRiskType("PROD");
72             
73             // API method to create Policy or update policy
74             PolicyChangeResponse response = null;
75             if (!isEdit) {
76                 response = policyEngine.createPolicy(policyParameters);
77             } else {
78                 response = policyEngine.updatePolicy(policyParameters);
79             }
80             
81             if(response.getResponseCode()==200){
82                 System.out.println(response.getResponseMessage());
83                 System.out.println("Policy Created Successfully!");
84             }else{
85                 System.out.println("Error! " + response.getResponseMessage());
86             }
87         } catch (Exception e) {
88             System.err.println(e.getMessage());
89         }
90         }
91         
92 }