aboutsummaryrefslogtreecommitdiffstats
path: root/tutorials/tutorial-enforcement
diff options
context:
space:
mode:
Diffstat (limited to 'tutorials/tutorial-enforcement')
-rw-r--r--tutorials/tutorial-enforcement/README.md20
-rw-r--r--tutorials/tutorial-enforcement/src/main/java/org/onap/policy/tutorial/policyenforcement/App.java58
-rw-r--r--tutorials/tutorial-enforcement/src/test/resources/postman/Policy_Enforcement_Tutorial.postman_collection.json58
-rw-r--r--tutorials/tutorial-enforcement/src/test/resources/postman/policy_update_notification.json9
4 files changed, 57 insertions, 88 deletions
diff --git a/tutorials/tutorial-enforcement/README.md b/tutorials/tutorial-enforcement/README.md
index 07b55984..d65ca3bb 100644
--- a/tutorials/tutorial-enforcement/README.md
+++ b/tutorials/tutorial-enforcement/README.md
@@ -14,16 +14,14 @@ The Policy Enforcement Tutorial can be built:
Be sure to start the Policy Framework application components in *docker* if you are not testing this in a lab.
- Ensure you have docker and docker-compose installed
- Check out the *policy/docker* repo from the ONAP gerrit or from github: https://github.com/onap/policy-docker
-- In a console, change directory into the *csit* directory of the *policy/docker* repo
-- Start *xacml-pdp* by running the *start-containter.sh* script
+- In a console, change directory into the *compose* directory of the *policy/docker* repo
+- Start *xacml-pdp* by running the *start-compose.sh* script
- start-container.sh xacml-pdp
+ start-compose.sh xacml-pdp
-- Ensure that DMaaP is up
-
- > curl -X POST http://0.0.0.0:30227/events/POLICY-PDP-PAP
- Should return JSON similar to this:
- {"serverTimeMs":0,"count":0}
+- Run python script ```kafka_producer.py``` under docker repository with topic name and message as parameters.
+ i.e. ```python3 /docker/csit/resources/tests/kafka_producer.py POLICY-PDP-PAP "message"```
+ (update "message" to the usual json body)
- Run the xacml-pdp health check
@@ -58,11 +56,11 @@ Be sure to start the Policy Framework application components in *docker* if you
# Run the Tutorial
You can run the application via code by running the *App.main* method with command line argument with IP then Port
-of the XACML PDP, followed by the IP then Port of Dmaap.
+of the XACML PDP, followed by the IP then Port of kafka.
- App.main(new String[] {"0.0.0.0", "6969", "0.0.0.0", "3904"});
+ App.main(new String[] {"0.0.0.0", "6969", "0.0.0.0", "9092"});
or from Eclipse by right-clicking App.java and selecting *Run As* and select *Java Application*. Edit the
-configuration by adding these command line arguments: "0.0.0.0" "6969" "0.0.0.0" "3904"
+configuration by adding these command line arguments: "0.0.0.0" "6969" "0.0.0.0" "9092"
Quit the application by typing 'q' into stdin.
diff --git a/tutorials/tutorial-enforcement/src/main/java/org/onap/policy/tutorial/policyenforcement/App.java b/tutorials/tutorial-enforcement/src/main/java/org/onap/policy/tutorial/policyenforcement/App.java
index 78d06841..0697e822 100644
--- a/tutorials/tutorial-enforcement/src/main/java/org/onap/policy/tutorial/policyenforcement/App.java
+++ b/tutorials/tutorial-enforcement/src/main/java/org/onap/policy/tutorial/policyenforcement/App.java
@@ -1,7 +1,7 @@
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2023 Nordix Foundation.
+ * Modifications Copyright (C) 2023-2024 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,9 +21,9 @@ package org.onap.policy.tutorial.policyenforcement;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.core.MediaType;
-import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
@@ -46,11 +46,11 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class App extends Thread implements TopicListener {
- private static Logger logger = LoggerFactory.getLogger(App.class);
- private static final String MY_POLICYTYPEID = "onap.policies.monitoring.MyAnalytic";
- private String xacmlPdpHost;
- private String xacmlPdpPort;
- private DecisionRequest decisionRequest = new DecisionRequest();
+ private static final Logger logger = LoggerFactory.getLogger(App.class);
+ private static final String MY_POLICY_TYPE_ID = "onap.policies.monitoring.MyAnalytic";
+ private final String xacmlPdpHost;
+ private final String xacmlPdpPort;
+ private final DecisionRequest decisionRequest = new DecisionRequest();
private Integer requestId = 1;
private HttpClient client = null;
@@ -64,23 +64,23 @@ public class App extends Thread implements TopicListener {
xacmlPdpPort = args[1];
var params = new TopicParameters();
- params.setTopicCommInfrastructure("dmaap");
+ params.setTopicCommInfrastructure("kafka");
params.setFetchLimit(1);
params.setFetchTimeout(5000);
- params.setTopic("POLICY-NOTIFICATION");
- params.setServers(Arrays.asList(args[2] + ":" + args[3]));
+ params.setTopic("policy-notification");
+ params.setServers(List.of(args[2] + ":" + args[3]));
var topicParams = new TopicParameterGroup();
- topicParams.setTopicSources(Arrays.asList(params));
+ topicParams.setTopicSources(List.of(params));
TopicEndpointManager.getManager().addTopics(topicParams);
- TopicEndpointManager.getManager().getDmaapTopicSource("POLICY-NOTIFICATION").register(this);
+ TopicEndpointManager.getManager().getKafkaTopicSource("policy-notification").register(this);
decisionRequest.setOnapComponent("myComponent");
decisionRequest.setOnapName("myName");
decisionRequest.setOnapInstance("myInstanceId");
decisionRequest.setAction("configure");
Map<String, Object> resources = new HashMap<>();
- resources.put("policy-type", MY_POLICYTYPEID);
+ resources.put("policy-type", MY_POLICY_TYPE_ID);
decisionRequest.setResource(resources);
}
@@ -88,19 +88,19 @@ public class App extends Thread implements TopicListener {
* Thread run method that creates a connection and gets an initial Decision on which policy(s)
* we should be enforcing.
* Then sits waiting for the user to enter q or Q from the keyboard to quit. While waiting,
- * listen on Dmaap topic for notification that the policy has changed.
+ * listen on a topic for notification that the policy has changed.
*/
@Override
public void run() {
logger.info("running - type q to stdin to quit");
try {
client = HttpClientFactoryInstance.getClientFactory().build(BusTopicParams.builder()
- .clientName("myClientName").useHttps(true).allowSelfSignedCerts(true)
- .hostname(xacmlPdpHost).port(Integer.parseInt(xacmlPdpPort))
- .userName("healthcheck").password("zb!XztG34").basePath("policy/pdpx/v1")
- .managed(true)
- .serializationProvider("org.onap.policy.common.gson.GsonMessageBodyHandler")
- .build());
+ .clientName("myClientName").useHttps(true).allowSelfSignedCerts(true)
+ .hostname(xacmlPdpHost).port(Integer.parseInt(xacmlPdpPort))
+ .userName("healthcheck").password("zb!XztG34").basePath("policy/pdpx/v1")
+ .managed(true)
+ .serializationProvider("org.onap.policy.common.gson.GsonMessageBodyHandler")
+ .build());
} catch (NumberFormatException | HttpClientConfigException e) {
logger.error("Could not create Http client", e);
return;
@@ -116,7 +116,7 @@ public class App extends Thread implements TopicListener {
TopicEndpointManager.getManager().start();
- @SuppressWarnings("resource") // never close System.in
+ // never close System.in
var input = new Scanner(System.in);
while (!Thread.currentThread().isInterrupted()) {
String quit = input.nextLine();
@@ -148,23 +148,23 @@ public class App extends Thread implements TopicListener {
}
/**
- * Helper method that parses a DMaap message event for POLICY-NOTIFICATION
+ * Helper method that parses a message event for policy-notification
* looking for our supported policy type to enforce.
*
- * @param msg Dmaap Message
- * @return true if MY_POLICYTYPEID is in the message
+ * @param msg topic message
+ * @return true if MY_POLICY_TYPE_ID is in the message
*/
private boolean scanForPolicyType(String msg) {
var gson = new StandardCoder();
try {
PolicyNotification notification = gson.decode(msg, PolicyNotification.class);
for (PolicyStatus added : notification.getAdded()) {
- if (MY_POLICYTYPEID.equals(added.getPolicyTypeId())) {
+ if (MY_POLICY_TYPE_ID.equals(added.getPolicyTypeId())) {
return true;
}
}
for (PolicyStatus deleted : notification.getDeleted()) {
- if (MY_POLICYTYPEID.equals(deleted.getPolicyTypeId())) {
+ if (MY_POLICY_TYPE_ID.equals(deleted.getPolicyTypeId())) {
return true;
}
}
@@ -179,7 +179,7 @@ public class App extends Thread implements TopicListener {
* Helper method that calls the XACML PDP Decision API to get a Decision
* as to which policy we should be enforcing.
*
- * @param client HttpClient to use to make REST call
+ * @param client HttpClient to use to make REST call
* @param decisionRequest DecisionRequest object to send
* @return The Map of policies that was in the DecisionResponse object
*/
@@ -188,12 +188,12 @@ public class App extends Thread implements TopicListener {
requestId++;
Entity<DecisionRequest> entityRequest =
- Entity.entity(decisionRequest, MediaType.APPLICATION_JSON);
+ Entity.entity(decisionRequest, MediaType.APPLICATION_JSON);
var response = client.post("/decision", entityRequest, Collections.emptyMap());
if (response.getStatus() != 200) {
logger.error(
- "Decision API failed - is the IP/port correct? {}", response.getStatus());
+ "Decision API failed - is the IP/port correct? {}", response.getStatus());
return Collections.emptyMap();
}
diff --git a/tutorials/tutorial-enforcement/src/test/resources/postman/Policy_Enforcement_Tutorial.postman_collection.json b/tutorials/tutorial-enforcement/src/test/resources/postman/Policy_Enforcement_Tutorial.postman_collection.json
index 85de39be..8d89d76c 100644
--- a/tutorials/tutorial-enforcement/src/test/resources/postman/Policy_Enforcement_Tutorial.postman_collection.json
+++ b/tutorials/tutorial-enforcement/src/test/resources/postman/Policy_Enforcement_Tutorial.postman_collection.json
@@ -19,7 +19,7 @@
},
{
"key": "username",
- "value": "healthcheck",
+ "value": "policyadmin",
"type": "string"
}
]
@@ -70,7 +70,7 @@
},
{
"key": "username",
- "value": "healthcheck",
+ "value": "policyadmin",
"type": "string"
}
]
@@ -125,7 +125,7 @@
},
{
"key": "username",
- "value": "healthcheck",
+ "value": "policyadmin",
"type": "string"
}
]
@@ -184,7 +184,7 @@
},
{
"key": "username",
- "value": "healthcheck",
+ "value": "policyadmin",
"type": "string"
}
]
@@ -235,7 +235,7 @@
},
{
"key": "username",
- "value": "healthcheck",
+ "value": "policyadmin",
"type": "string"
}
]
@@ -286,7 +286,7 @@
},
{
"key": "username",
- "value": "healthcheck",
+ "value": "policyadmin",
"type": "string"
}
]
@@ -342,7 +342,7 @@
},
{
"key": "username",
- "value": "healthcheck",
+ "value": "policyadmin",
"type": "string"
}
]
@@ -393,7 +393,7 @@
},
{
"key": "username",
- "value": "healthcheck",
+ "value": "policyadmin",
"type": "string"
}
]
@@ -444,7 +444,7 @@
},
{
"key": "username",
- "value": "healthcheck",
+ "value": "policyadmin",
"type": "string"
}
]
@@ -487,44 +487,6 @@
"response": []
},
{
- "name": "Dmaap Simulator - Policy Update Notification",
- "request": {
- "auth": {
- "type": "noauth"
- },
- "method": "POST",
- "header": [
- {
- "key": "Content-Type",
- "type": "text",
- "value": "application/json"
- },
- {
- "key": "Accept",
- "type": "text",
- "value": "application/json"
- }
- ],
- "body": {
- "mode": "raw",
- "raw": "{\n \"messageName\": \"PDP_STATE_CHANGE\",\n \"requestId\": \"05d08a05-e182-46fa-a6d1-5500e52cd3e5\",\n \"timestampMs\": \"1576598570797\", \n \"name\": \"PamelaDragosh.local\",\n \"pdpGroup\": \"defaultGroup\",\n \"pdpSubgroup\":\"XacmlPdpGroup\",\n \"state\":\"ACTIVE\"\n}"
- },
- "url": {
- "raw": "http://localhost:3904/events/POLICY-PDP-PAP",
- "protocol": "http",
- "host": [
- "localhost"
- ],
- "port": "3904",
- "path": [
- "events",
- "POLICY-PDP-PAP"
- ]
- }
- },
- "response": []
- },
- {
"name": "Simple Undeploy Policy Copy",
"request": {
"auth": {
@@ -537,7 +499,7 @@
},
{
"key": "username",
- "value": "healthcheck",
+ "value": "policyadmin",
"type": "string"
}
]
diff --git a/tutorials/tutorial-enforcement/src/test/resources/postman/policy_update_notification.json b/tutorials/tutorial-enforcement/src/test/resources/postman/policy_update_notification.json
new file mode 100644
index 00000000..e462a8e2
--- /dev/null
+++ b/tutorials/tutorial-enforcement/src/test/resources/postman/policy_update_notification.json
@@ -0,0 +1,9 @@
+{
+ "messageName": "PDP_STATE_CHANGE",
+ "requestId": "05d08a05-e182-46fa-a6d1-5500e52cd3e5",
+ "timestampMs": "1576598570797",
+ "name": "PamelaDragosh.local",
+ "pdpGroup": "defaultGroup",
+ "pdpSubgroup":"XacmlPdpGroup",
+ "state":"ACTIVE"
+}