1 .. This work is licensed under a Creative Commons Attribution 4.0 International License.
2 .. http://creativecommons.org/licenses/by/4.0
9 .. warning:: These Legacy APIs are scheduled to be deprecated after the Frankfurt release!
14 The Policy subsystem of ONAP maintains, distributes, and operates on the set of rules that underlie ONAP’s control, orchestration, and management functions. Policy provides a centralized environment for the creation and management of easily-updatable conditional rules. It enables users to validate policies and rules, identify and resolve overlaps and conflicts, and derive additional policies where needed. The following operations are supported by the policy API:
16 * Create policies on the PAP
17 * Update policies on the PAP
18 * Delete policies on the PAP or PDP
19 * Push policies from the PAP to the PDP
20 * List policies on the PDP
21 * Get config data of policies on the PDP
22 * Create Dictionary Items
23 * Update Dictionary Items
24 * Retrieve Dictionary Items
25 * Import Micro Services Models
26 * Retrieve Metrics for policy counts from PDP and PAP
29 POLICY Engine Services
30 ^^^^^^^^^^^^^^^^^^^^^^
32 .. image:: PolicyEngineApiList.png
37 .. swaggerv2doc:: api-docs.json
42 **SAMPLE JAVA CLIENT CODE**
45 :caption: Get Config Example
48 package org.onap.policyEngine;
50 import java.util.Collection;
52 import org.onap.policy.api.ConfigRequestParameters;
53 import org.onap.policy.api.PolicyConfig;
54 import org.onap.policy.api.PolicyEngine;
56 public class GetConfigSample {
58 public static void main(String[] args) throws Exception {
59 PolicyEngine pe = new PolicyEngine("config.properties");
60 ConfigRequestParameters configRequestParams = new ConfigRequestParameters();
61 configRequestParams.setPolicyName(".*");
62 Collection<PolicyConfig> configs = pe.getConfig(configRequestParams);
63 for (PolicyConfig config: configs){
64 System.out.println(config.getPolicyConfigMessage());
65 System.out.println(config.getPolicyConfigStatus());
72 :caption: Create Config FIrewall Policy Example
75 package org.onap.policyEngine;
78 import java.io.FileInputStream;
79 import java.io.FileNotFoundException;
80 import java.io.IOException;
81 import java.io.InputStream;
82 import java.io.StringReader;
83 import java.nio.file.Path;
84 import java.nio.file.Paths;
85 import java.text.SimpleDateFormat;
86 import java.util.Date;
87 import java.util.UUID;
89 import javax.json.Json;
90 import javax.json.JsonObject;
91 import javax.json.JsonReader;
93 import org.onap.policy.api.PolicyChangeResponse;
94 import org.onap.policy.api.PolicyConfigType;
95 import org.onap.policy.api.PolicyEngine;
96 import org.onap.policy.api.PolicyParameters;
97 import org.onap.policy.api.PolicyType;
99 public class ConfigFirewallPolicyClient {
100 static Boolean isEdit = false;
101 public static void main(String[] args) {
103 PolicyEngine policyEngine = new PolicyEngine("config.properties");
104 PolicyParameters policyParameters = new PolicyParameters();
106 policyParameters.setPolicyConfigType(PolicyConfigType.Firewall); //required
107 policyParameters.setPolicyName("MikeAPItesting.testConfigFirewallPolicy1607_1"); //required
108 //policyParameters.setPolicyScope("MikeAPItesting");
109 //Directory will be created where the Policies are saved... this displays a subscope on the GUI
110 policyParameters.setRequestID(UUID.randomUUID());
112 // Set Safe Policy value for Risk Type
113 SimpleDateFormat dateformat3 = new SimpleDateFormat("dd/MM/yyyy");
114 Date date = dateformat3.parse("15/10/2016");
115 policyParameters.setTtlDate(date);
116 // Set Safe Policy value for Guard
117 policyParameters.setGuard(true);
118 // Set Safe Policy value for Risk Level
119 policyParameters.setRiskLevel("5");
120 // Set Safe Policy value for Risk Type
121 policyParameters.setRiskType("PROD");
122 File jsonFile = null;
123 String jsonRuleList = null;
124 Path file = Paths.get("C:\\policyAPI\\firewallRulesJSON\\Config_FW_Sample.json");
125 jsonFile = file.toFile();
127 //buildJSON(jsonFile, jsonRuleList);
128 policyParameters.setConfigBody(buildJSON(jsonFile, jsonRuleList).toString());
129 policyParameters.setConfigBodyType(PolicyType.JSON);
130 // API method to create Policy or update policy
131 PolicyChangeResponse response = null;
133 response = policyEngine.createPolicy(policyParameters);
135 response = policyEngine.updatePolicy(policyParameters);
138 if(response.getResponseCode()==200){
139 System.out.println(response.getResponseMessage());
140 System.out.println("Policy Created Successfully!");
142 System.out.println("Error! " + response.getResponseMessage());
144 } catch (Exception e) {
145 System.err.println(e.getMessage());
150 private static JsonObject buildJSON(File jsonInput, String jsonString) throws FileNotFoundException {
151 JsonObject json = null;
152 JsonReader jsonReader = null;
153 if (jsonString != null && jsonInput == null) {
154 StringReader in = null;
155 in = new StringReader(jsonString);
156 jsonReader = Json.createReader(in);
157 json = jsonReader.readObject();
160 InputStream in = null;
161 in = new FileInputStream(jsonInput);
162 jsonReader = Json.createReader(in);
163 json = jsonReader.readObject();
166 } catch (IOException e) {
167 System.err.println("Exception Occured while closing input stream"+e);
177 :caption: Sample JSON file - Config_FW_Sample.json
181 "serviceTypeId": "/v0/firewall/pan",
182 "configName": "AFTTFwPolicy1Config",
183 "deploymentOption": {
186 "securityZoneId": "cloudsite:dev1a",
189 "description": "Ssh service entry in service list",
191 "transportProtocol": "tcp",
197 "description": "Destination CiscoCVE",
200 "value": "12.63.31.61/12"
203 "name": "HOHOServers",
204 "description": "Source HOHOServers for first testing",
207 "value": "12.60.32.11/23"
210 "firewallRuleList": [{
212 "ruleName": "FWRuleHOHOServerToCiscoVCE",
213 "fromZones": ["UntrustedZoneCiscoCVEName"],
214 "toZones": ["TrustedZoneHOHOName"],
215 "negateSource": false,
216 "negateDestination": false,
219 "name": "HOHOServers"
221 "destinationList": [{
225 "sourceServices": [],
231 "description": "FW rule for HOHO source to CiscoVCE destination",
238 :caption: Delete Policy Example
241 package org.onap.policyEngine;
243 import org.onap.policy.api.DeletePolicyCondition;
244 import org.onap.policy.api.DeletePolicyParameters;
245 import org.onap.policy.api.PolicyChangeResponse;
246 import org.onap.policy.api.PolicyEngine;
248 public class DeletePolicyClient {
250 public static void main(String[] args) {
253 PolicyEngine policyEngine = new PolicyEngine("config.properties");
254 DeletePolicyParameters policyParameters = new DeletePolicyParameters();
256 //Parameter arguments
257 policyParameters.setPolicyName("MikeConsole.Config_testDeleteAPI6.1.xml");
258 policyParameters.setPolicyComponent("PDP");
259 policyParameters.setPdpGroup("default");
260 policyParameters.setDeleteCondition(DeletePolicyCondition.ALL);
261 policyParameters.setRequestID(null);
263 // API method to Push Policy to PDP
264 PolicyChangeResponse response = null;
265 response = policyEngine.deletePolicy(policyParameters);
267 if(response.getResponseCode()==200){
268 System.out.println(response.getResponseMessage());
269 System.out.println("Policy Deleted Successfully!");
271 System.out.println("Error! " + response.getResponseMessage());
274 } catch (Exception e) {
275 System.err.println(e.getMessage());
283 :caption: Push Policy Example
286 package org.onap.policyEngine;
288 import org.onap.policy.api.PolicyChangeResponse;
289 import org.onap.policy.api.PolicyEngine;
290 import org.onap.policy.api.PushPolicyParameters;
292 public class PushPoliciesToPDP {
293 public static void main(String[] args) {
296 PolicyEngine policyEngine = new PolicyEngine("config.properties");
297 PushPolicyParameters policyParameters = new PushPolicyParameters();
299 //Parameter arguments
300 policyParameters.setPolicyName("Mike.testCase1");
301 policyParameters.setPolicyType("Base");
302 //policyParameters.setPolicyScope("MikeAPItesting");
303 policyParameters.setPdpGroup("default");
304 policyParameters.setRequestID(null);
306 // API method to Push Policy to PDP
307 PolicyChangeResponse response = null;
308 response = policyEngine.pushPolicy(policyParameters);
310 if(response.getResponseCode()==204){
311 System.out.println(response.getResponseMessage());
312 System.out.println("Policy Pushed Successfully!");
314 System.out.println("Error! " + response.getResponseMessage());
317 } catch (Exception e) {
318 System.err.println(e.getMessage());
326 :caption: Decision Policy Example
329 package org.onap.policyEngine;
331 import java.util.Arrays;
332 import java.util.HashMap;
333 import java.util.LinkedList;
334 import java.util.List;
335 import java.util.Map;
336 import java.util.UUID;
338 import org.onap.policy.api.AttributeType;
339 import org.onap.policy.api.PolicyChangeResponse;
340 import org.onap.policy.api.PolicyClass;
341 import org.onap.policy.api.PolicyEngine;
342 import org.onap.policy.api.PolicyParameters;
344 public class DecisionPolicyClient {
345 static Boolean isEdit = true;
346 public static void main(String[] args) {
348 PolicyEngine policyEngine = new PolicyEngine("config.properties");
349 PolicyParameters policyParameters = new PolicyParameters();
351 policyParameters.setPolicyClass(PolicyClass.Decision); //required
352 policyParameters.setPolicyName("MikeAPItests.testDecisionAPI"); //required
353 policyParameters.setOnapName("java"); //required
354 policyParameters.setPolicyDescription("This is a sample Decision policy UPDATE example with Settings"); //optional
355 //policyParameters.setPolicyScope("MikeAPItests");
356 //Directory will be created where the Policies are saved... this
357 displays a a subscope on the GUI
359 //Set the Component Attributes... These are Optional
360 Map<String, String> configAttributes = new HashMap<>();
361 configAttributes.put("Template", "UpdateTemplate");
362 configAttributes.put("controller", "default");
363 configAttributes.put("SamPoll", "30");
364 configAttributes.put("value", "abcd");
366 Map<AttributeType, Map<String,String>> attributes = new HashMap<>();
367 attributes.put(AttributeType.MATCHING, configAttributes);
369 //Set the settings... These are Optional
370 Map<String, String> settingsMap = new HashMap<>();
371 settingsMap.put("server", "5");
373 attributes.put(AttributeType.SETTINGS, settingsMap);
374 policyParameters.setAttributes(attributes);
377 List<String> dynamicRuleAlgorithmLabels = new LinkedList<>();
378 List<String> dynamicRuleAlgorithmFunctions = new LinkedList<>();
379 List<String> dynamicRuleAlgorithmField1 = new LinkedList<>();
380 List<String> dynamicRuleAlgorithmField2 = new LinkedList<>();
382 //Example of a complex Rule algorithm using the settings in the Field1
383 /* label field1 function field2
384 * *****************************************************
385 * A1 S_server integer-equal 90
386 * A2 cap string-contains ca
387 * A3 cobal integer-equal 90
389 * A5 Config integer-greater-than 45
393 dynamicRuleAlgorithmLabels = Arrays.asList("A1","A2","A3","A4","A5","A6","A7");
394 dynamicRuleAlgorithmField1 = Arrays.asList("S_server","cap","cobal","A2","Config","A4","A1");
395 dynamicRuleAlgorithmFunctions = Arrays.asList("integer-equal","string-contains","integer-equal","and","integer-greater-than","or","and");
396 dynamicRuleAlgorithmField2 = Arrays.asList("90","ca","90","A3","45","A5","A6");
398 policyParameters.setDynamicRuleAlgorithmLabels(dynamicRuleAlgorithmLabels);
399 policyParameters.setDynamicRuleAlgorithmField1(dynamicRuleAlgorithmField1);
400 policyParameters.setDynamicRuleAlgorithmFunctions(dynamicRuleAlgorithmFunctions);
401 policyParameters.setDynamicRuleAlgorithmField2(dynamicRuleAlgorithmField2);
403 policyParameters.setRequestID(UUID.randomUUID());
405 // API method to create Policy or update policy
406 PolicyChangeResponse response = null;
408 response = policyEngine.createPolicy(policyParameters);
410 response = policyEngine.updatePolicy(policyParameters);
413 if(response.getResponseCode()==200){
414 System.out.println(response.getResponseMessage());
415 System.out.println("Policy Created Successfully!");
417 System.out.println("Error! " + response.getResponseMessage());
419 } catch (Exception e) {
420 System.err.println(e.getMessage());
427 :caption: ListPolicy Example
430 package org.onap.policyengine;
432 import java.util.Collection;
433 import org.onap.policy.api.ConfigNameRequest;
434 import org.onap.policy.api.PolicyEngine;
437 * List Policy Client Code
438 * policyName : ".*" returns list of policy names from PAP.
439 * policyName : "scope + "_" + "policyType" + "_" + policyName" + ".*" returns the matching policy from pap (active version)
442 public class ListPolicyClient {
443 public static void main(String[] args) throws Exception {
444 PolicyEngine pe = new PolicyEngine("config.properties");
445 ConfigNameRequest listPolicyParams = new ConfigNameRequest();
446 listPolicyParams.setPolicyName(".*");
447 Collection<String> policies = pe.listPolicy(listPolicyParams);
448 for (String policy : policies) {
449 System.out.println(policy);
456 :caption: List Config Policy Example
459 package org.onap.policyEngine;
461 import java.util.Collection;
462 import java.util.HashMap;
463 import java.util.Map;
464 import java.util.UUID;
466 import org.onap.policy.api.ConfigRequestParameters;
467 import org.onap.policy.api.PolicyConfigException;
468 import org.onap.policy.api.PolicyEngine;
469 import org.onap.policy.api.PolicyEngineException;
470 import org.onap.policy.common.logging.flexlogger.FlexLogger;
471 import org.onap.policy.common.logging.flexlogger.Logger;
473 public class ListConfigPoliciesClient {
475 private static final Logger LOGGER = FlexLogger.getLogger(ListConfigPoliciesClient.class);
477 public static void main(String[] args) {
478 PolicyEngine policyEngine;
480 // List Config Policies Example
482 policyEngine = new PolicyEngine("config.properties");
483 ConfigRequestParameters parameters = new ConfigRequestParameters();
485 parameters.setPolicyName(".*");
486 parameters.setOnapName(".*");
487 parameters.setConfigName(".*");
489 Map<String, String> configAttributes = new HashMap<>();
490 configAttributes.put("java", "java");
491 configAttributes.put("peach", "Tar");
492 configAttributes.put("true", "false");
493 configAttributes.put("small", "testPass");
494 parameters.setConfigAttributes(configAttributes);
496 parameters.setRequestID(UUID.randomUUID());
498 Collection<String> response = policyEngine.listConfig(parameters);
499 if(response!=null && !response.contains("PE300")){
500 for(String configList : response){
501 System.out.println(configList.toString()+"\n");
504 System.out.println("Error! " +response);
507 } catch (PolicyConfigException e) {
508 LOGGER.error("Exception Occured"+e);
509 } catch (PolicyEngineException e) {
510 LOGGER.error("Exception Occured"+e);
519 :caption: Create Microservice Policy
527 \"service\":\"ControllerServiceSampleSdnlServiceInstance\",
528 \"location\":\"Edge\",
529 \"uuid\":\"TestUUID\",
530 \"policyName\":\"testRestCreateMicroServicesNewParams\",
531 \"description\":\"testing Create\",
532 \"configName\":\"TestName\",
533 \"templateVersion\":\"1604\",
535 \"version\":\"0.1.0-SNAPSHOT\",
536 \"policyScope\":\"resource=F5,service=vSCP,type=configuration,closedLoopControlName=vSCP_F5_Firewall_d925ed73-8231-4d02-9545-db4e101f88f8\",
538 \"taskOrchestratedConfiguration\":\"test\",
539 \"taskCustomConfiguration\":\"set\",
540 \"configuration\":\"test\",
541 \"cdapUrl\":\"testurl\",
542 \"taskName\":\"test\",
543 \"taskNameTEST\":\"TEST\",
544 \"users\":\"[tuser]\",
545 \"adminUsers\":\"[lji]\",
546 \"taskConfigFilePath\":\"test\",
547 \"managerPortNumber\":\"999\",
548 \"taskType\":\"test\",
549 \"taskCommandFilePath\":\"tset\",
550 \"contailIp\":\"test\",
551 \"consoleUsers\":\"[odu-e2e]\",
552 \"taskStatusFilePath\":\"test\"
555 "policyConfigType": "MicroService",
556 "policyName": "MikeAPITesting.testRestCreateMicroServicesNewParams",
562 :caption: Update Microservice Policy
570 \"service\":\"ControllerServiceSampleSdnlServiceInstance\",
571 \"location\":\"Edge\",
572 \"uuid\":\"TestUUID\",
573 \"policyName\":\"testRestCreateMicroServicesNewParams\",
574 \"description\":\"testing Update\",
575 \"configName\":\"TestName\",
576 \"templateVersion\":\"1604\",
578 \"version\":\"0.1.0-SNAPSHOT\",
579 \"policyScope\":\"resource=F5,service=vSCP,type=configuration,closedLoopControlName=vSCP_F5_Firewall_d925ed73-8231-4d02-9545-db4e101f88f8\",
581 \"taskOrchestratedConfiguration\":\"test\",
582 \"taskCustomConfiguration\":\"set\",
583 \"configuration\":\"test\",
584 \"cdapUrl\":\"testurl\",
585 \"taskName\":\"test\",
586 \"taskNameTEST\":\"TEST\",
587 \"users\":\"[tuser]\",
588 \"adminUsers\":\"[lji]\",
589 \"taskConfigFilePath\":\"test\",
590 \"managerPortNumber\":\"999\",
591 \"taskType\":\"test\",
592 \"taskCommandFilePath\":\"tset\",
593 \"contailIp\":\"test\",
594 \"consoleUsers\":\"[odu-e2e]\",
595 \"taskStatusFilePath\":\"test\"
598 "policyConfigType": "MicroService",
599 "policyName": "MikeAPITesting.testRestUpdateMicroServicesNewParams",
607 :caption: Push Policy
609 echo "pushPolicy : PUT : com.vLoadBalancer"
610 echo "pushPolicy : PUT : com.vLoadBalancer"
611 curl -v --silent -X PUT --header 'Content-Type: application/json' --header 'Accept: text/plain' --header 'ClientAuth: XYZ' --header 'Authorization: Basic XYZ' --header 'Environment: TEST' -d '{
612 "pdpGroup": "default",
613 "policyName": "com.vLoadBalancer",
614 "policyType": "MicroService"
615 }' 'http://pdp:8081/pdp/api/pushPolicy'
619 :caption: Delete Policy
621 echo "deletePolicy : DELETE : com.vFirewall"
622 curl -v --silent -X DELETE --header 'Content-Type: application/json' --header 'Accept: text/plain' --header 'ClientAuth: XYZ' --header 'Authorization: Basic XYZ' --header 'Environment: TEST' -d '{
623 "pdpGroup": "default",
624 "policyComponent": "PDP",
625 "policyName": "com.vFirewall",
626 "policyType": "MicroService"
627 }' 'http://pdp:8081/pdp/api/deletePolicy'
633 echo "Get all Config Policy example"
634 curl -i -v -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'ClientAuth: XYZ' -H 'Authorization: Basic XYZ -H 'Environment: TEST' -X POST -d '{
636 }' http://${PDP_IP}:8081/pdp/api/getConfig
639 **ADDITIONAL EXAMPLES**
642 :caption: Deleting a Policy from PAP
644 // Deleting from PAP will remove the policy from the PolicyEntity & PolicyVersion tables (UI-Editor tab).
645 // This means that the policy is no longer be available in Policy System.
648 { "policyName": "com.testpolicy", //scope.policyName
649 "policyType": "Base", //policy type
650 "policyComponent": "PAP", //component name
651 "deleteCondition": "ALL" //versions (ALL or CURRENT)
656 :caption: Deleting a Policy from PDP
658 // Deleting from PDP will delete the policy from the PDP Group. The policy is still available in Policy System.
659 // When the policy is needed again, the policy should be pushed to the PDP.
662 { "policyName": "com.testpolicy", //scope.policyName
663 "policyType": "Base", //policy type
664 "policyComponent": "PDP", //component name
665 "pdpGroup": "Default" //group name