Leave xacml-pdp REST server always running
[policy/xacml-pdp.git] / main / src / main / java / org / onap / policy / pdpx / main / XacmlState.java
index 03b0e9e..0b6f30c 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -33,15 +33,15 @@ import org.onap.policy.models.pdp.enums.PdpResponseStatus;
 import org.onap.policy.models.pdp.enums.PdpState;
 import org.onap.policy.pdpx.main.rest.XacmlPdpApplicationManager;
 import org.onap.policy.pdpx.main.startstop.XacmlPdpActivator;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * Current state of this XACML PDP.
  */
 public class XacmlState {
-    // The logger for this class
-    private static final Logger LOGGER = LoggerFactory.getLogger(XacmlState.class);
+    /**
+     * Unique name for the xacml-pdp JVM, used in PdpStatus messages.
+     */
+    public static final String PDP_NAME = NetworkUtil.genUniqueName("xacml");
 
     /**
      * The application manager.
@@ -56,15 +56,15 @@ public class XacmlState {
     /**
      * Constructs the object, initializing the state.
      */
-    public XacmlState(XacmlPdpApplicationManager appManager) {
+    public XacmlState(XacmlPdpApplicationManager appManager, String pdpGroupName, String pdpType) {
         this.appManager = appManager;
 
         this.status = new PdpStatus();
-        this.status.setName(NetworkUtil.getHostname());
-        this.status.setPdpType("xacml");
+        this.status.setName(PDP_NAME);
+        this.status.setPdpType(pdpType);
         this.status.setState(PdpState.PASSIVE);
-        this.status.setSupportedPolicyTypes(appManager.getToscaPolicyTypeIdents());
         this.status.setPolicies(Collections.emptyList());
+        this.status.setPdpGroup(pdpGroupName);
     }
 
     /**
@@ -107,9 +107,6 @@ public class XacmlState {
 
         PdpStatus status2 = makeResponse(message, "");
 
-        // start/stop rest controller based on state change
-        handleXacmlRestController();
-
         // these fields aren't needed in the response, so clear them out to avoid sending
         status2.setPolicies(null);
 
@@ -124,7 +121,6 @@ public class XacmlState {
      * @return a response to the message
      */
     public PdpStatus updateInternalState(PdpUpdate message, String errMessage) {
-        status.setPdpGroup(message.getPdpGroup());
         status.setPdpSubgroup(message.getPdpSubgroup());
         status.setPolicies(appManager.getToscaPolicyIdentifiers());
 
@@ -149,7 +145,7 @@ public class XacmlState {
      * @return a new response
      */
     private PdpStatus makeResponse(PdpMessage message, String errMessage) {
-        PdpResponseDetails resp = new PdpResponseDetails();
+        var resp = new PdpResponseDetails();
 
         if (StringUtils.isBlank(errMessage)) {
             resp.setResponseStatus(PdpResponseStatus.SUCCESS);
@@ -159,27 +155,8 @@ public class XacmlState {
         }
         resp.setResponseTo(message.getRequestId());
 
-        PdpStatus status2 = new PdpStatus(status);
+        var status2 = new PdpStatus(status);
         status2.setResponse(resp);
         return status2;
     }
-
-    /**
-     * Manages the Xacml-Pdp rest controller based on the Xacml-Pdp State.
-     * Current supported states:
-     * ACTIVE  - rest service is running and handling requests
-     * PASSIVE - rest service is not running
-     */
-    private void handleXacmlRestController() {
-        if (status.getState() == PdpState.ACTIVE) {
-            LOGGER.info("State change: {} - Starting rest controller", status.getState());
-            XacmlPdpActivator.getCurrent().startXacmlRestController();
-        } else if (status.getState() == PdpState.PASSIVE) {
-            LOGGER.info("State change: {} - Stopping rest controller", status.getState());
-            XacmlPdpActivator.getCurrent().stopXacmlRestController();
-        } else {
-            // unsupported state
-            LOGGER.warn("Unsupported state: {}", status.getState());
-        }
-    }
 }