From 97e19c931535a5b474d4942a9982f0b7a1cf7414 Mon Sep 17 00:00:00 2001 From: jhh Date: Sun, 22 Mar 2020 19:37:21 -0500 Subject: Fix op policies distribution to controllers Fix a bug when distributing a policy to the right controller plus additional junits to catch similar issues. Issue-ID: POLICY-2356 Signed-off-by: jhh Change-Id: I25fefa64ad54c55e6516ae18c6895b6de9407419 Signed-off-by: jhh --- .../onap/policy/drools/lifecycle/LifecycleFsm.java | 42 +++- .../lifecycle/PolicyTypeDroolsController.java | 63 ++++-- .../PolicyTypeNativeArtifactController.java | 2 +- .../PolicyTypeNativeDroolsController.java | 21 +- .../server/restful/RestLifecycleManager.java | 225 +++++++++++++++++- .../policy/drools/lifecycle/ControllerSupport.java | 6 +- .../LifecycleStateActivePoliciesTest.java | 252 +++++++++++++++++++++ .../drools/lifecycle/LifecycleStateActiveTest.java | 24 +- .../lifecycle/LifecycleStatePassiveTest.java | 15 +- .../lifecycle/LifecycleStateRunningTest.java | 3 + .../PolicyTypeNativeArtifactControllerTest.java | 2 +- .../PolicyTypeNativeDroolsControllerTest.java | 6 +- .../server/restful/RestLifecycleManagerTest.java | 79 ++++++- .../tosca-policy-native-artifact-example.json | 9 +- .../tosca-policy-native-artifact-foo.json | 28 +++ .../tosca-policy-native-controller-example.json | 6 +- .../tosca-policy-native-controller-foo.json | 21 ++ 17 files changed, 726 insertions(+), 78 deletions(-) create mode 100644 feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActivePoliciesTest.java create mode 100644 feature-lifecycle/src/test/resources/tosca-policy-native-artifact-foo.json create mode 100644 feature-lifecycle/src/test/resources/tosca-policy-native-controller-foo.json (limited to 'feature-lifecycle/src') diff --git a/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleFsm.java b/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleFsm.java index a435f02e..6953d831 100644 --- a/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleFsm.java +++ b/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleFsm.java @@ -68,24 +68,32 @@ import org.slf4j.LoggerFactory; */ public class LifecycleFsm implements Startable { + /** + * Default Status Timer in seconds. + */ + public static final long DEFAULT_STATUS_TIMER_SECONDS = 120L; + private static final Logger logger = LoggerFactory.getLogger(LifecycleFsm.class); protected static final String CONFIGURATION_PROPERTIES_NAME = "feature-lifecycle"; protected static final String GROUP_NAME = "lifecycle.pdp.group"; protected static final String DEFAULT_PDP_GROUP = "defaultGroup"; - protected static final long DEFAULT_STATUS_TIMER_SECONDS = 120L; protected static final long MIN_STATUS_INTERVAL_SECONDS = 5L; protected static final String PDP_MESSAGE_NAME = "messageName"; protected static final ToscaPolicyTypeIdentifier POLICY_TYPE_DROOLS_NATIVE_RULES = - new ToscaPolicyTypeIdentifier("onap.policies.native.Drools", "1.0.0"); + new ToscaPolicyTypeIdentifier("onap.policies.native.drools.Artifact", "1.0.0"); - protected static final ToscaPolicyTypeIdentifier POLICY_TYPE_DROOLS_CONTROLLER = - new ToscaPolicyTypeIdentifier("onap.policies.drools.Controller", "1.0.0"); + protected static final ToscaPolicyTypeIdentifier POLICY_TYPE_DROOLS_NATIVE_CONTROLLER = + new ToscaPolicyTypeIdentifier("onap.policies.native.drools.Controller", "1.0.0"); + @Getter protected final Properties properties; + @Getter protected TopicSource source; + + @Getter protected TopicSinkClient client; @Getter @@ -113,14 +121,17 @@ public class LifecycleFsm implements Startable { protected long statusTimerSeconds = DEFAULT_STATUS_TIMER_SECONDS; @Getter - private final String group; + @Setter + private String group; @Getter + @Setter protected String subgroup; @Getter protected final Map policyTypesMap = new HashMap<>(); + @Getter protected final Map policiesMap = new HashMap<>(); /** @@ -130,9 +141,8 @@ public class LifecycleFsm implements Startable { this.properties = SystemPersistenceConstants.getManager().getProperties(CONFIGURATION_PROPERTIES_NAME); this.group = this.properties.getProperty(GROUP_NAME, DEFAULT_PDP_GROUP); - this.policyTypesMap.put( - POLICY_TYPE_DROOLS_CONTROLLER, - new PolicyTypeNativeDroolsController(this, POLICY_TYPE_DROOLS_CONTROLLER)); + this.policyTypesMap.put(POLICY_TYPE_DROOLS_NATIVE_CONTROLLER, + new PolicyTypeNativeDroolsController(this, POLICY_TYPE_DROOLS_NATIVE_CONTROLLER)); this.policyTypesMap.put( POLICY_TYPE_DROOLS_NATIVE_RULES, new PolicyTypeNativeArtifactController(this, POLICY_TYPE_DROOLS_NATIVE_RULES)); @@ -171,7 +181,12 @@ public class LifecycleFsm implements Startable { logger.info("lifecycle event: start controller: {}", controller.getName()); for (ToscaPolicyTypeIdentifier id : controller.getPolicyTypes()) { if (isToscaPolicyType(id.getName())) { - policyTypesMap.put(id, new PolicyTypeDroolsController(this, id, controller)); + PolicyTypeDroolsController ptDroolsController = (PolicyTypeDroolsController) policyTypesMap.get(id); + if (ptDroolsController == null) { + policyTypesMap.put(id, new PolicyTypeDroolsController(this, id, controller)); + } else { + ptDroolsController.add(controller); + } } } } @@ -188,7 +203,14 @@ public class LifecycleFsm implements Startable { public synchronized void stop(@NonNull PolicyController controller) { logger.info("lifecycle event: stop controller: {}", controller.getName()); for (ToscaPolicyTypeIdentifier id : controller.getPolicyTypes()) { - policyTypesMap.remove(id); + if (!policyTypesMap.containsKey(id)) { + continue; + } + PolicyTypeDroolsController ptDroolsController = (PolicyTypeDroolsController) policyTypesMap.get(id); + ptDroolsController.remove(controller); + if (ptDroolsController.controllers().isEmpty()) { + policyTypesMap.remove(id); + } } } diff --git a/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/PolicyTypeDroolsController.java b/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/PolicyTypeDroolsController.java index 36c52e2b..2772d854 100644 --- a/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/PolicyTypeDroolsController.java +++ b/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/PolicyTypeDroolsController.java @@ -26,6 +26,7 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; import lombok.Getter; +import lombok.NonNull; import org.apache.commons.lang3.StringUtils; import org.onap.policy.common.gson.annotation.GsonJsonIgnore; import org.onap.policy.common.utils.coder.CoderException; @@ -71,15 +72,53 @@ public class PolicyTypeDroolsController implements PolicyTypeController { } @Override - public boolean deploy(ToscaPolicy policy) { + public boolean deploy(@NonNull ToscaPolicy policy) { return perform(policy, (PolicyController controller) -> controller.offer(policy)); } + /** + * Adds a controller to support this policy type. + */ + public void add(@NonNull PolicyController controller) { + if (!controller.getPolicyTypes().contains(this.policyType)) { + throw new IllegalArgumentException( + "controller " + controller.getName() + " does not support " + this.policyType); + } + controllers.put(controller.getName(), controller); + } + + /** + * Removes a controller from this policy type. + */ + public void remove(@NonNull PolicyController controller) { + controllers.remove(controller.getName()); + } + @Override - public boolean undeploy(ToscaPolicy policy) { + public boolean undeploy(@NonNull ToscaPolicy policy) { return perform(policy, (PolicyController controller) -> controller.getDrools().delete(policy)); } + /** + * Get all controllers that support the policy type. + */ + public List controllers() { + return List.copyOf(controllers.values()); + } + + private List controllers(String controllerName) { + if (StringUtils.isBlank(controllerName)) { + /* this policy applies to all controllers */ + return controllers(); + } + + if (!this.controllers.containsKey(controllerName)) { + return List.of(); + } + + return List.of(this.controllers.get(controllerName)); + } + private boolean perform(ToscaPolicy policy, Function operation) { try { List selected = selectControllers(policy); @@ -116,24 +155,4 @@ public class PolicyTypeDroolsController implements PolicyTypeController { } return selected; } - - private List controllers(String controllerName) { - if (StringUtils.isBlank(controllerName)) { - /* this policy applies to all controllers */ - return controllers(); - } - - if (!this.controllers.containsKey(controllerName)) { - return List.of(); - } - - return List.of(this.controllers.get(controllerName)); - } - - /** - * Get all controllers that support the policy type. - */ - public List controllers() { - return List.copyOf(controllers.values()); - } } diff --git a/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/PolicyTypeNativeArtifactController.java b/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/PolicyTypeNativeArtifactController.java index 31e9059d..d91ecccf 100644 --- a/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/PolicyTypeNativeArtifactController.java +++ b/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/PolicyTypeNativeArtifactController.java @@ -57,7 +57,7 @@ public class PolicyTypeNativeArtifactController implements PolicyTypeController nativePolicy = fsm.getDomainMaker().convertTo(policy, NativeArtifactPolicy.class); controller = PolicyControllerConstants.getFactory().get(nativePolicy.getProperties().getController().getName()); - } catch (CoderException e) { + } catch (CoderException | RuntimeException e) { logger.warn("Invalid Policy: {}", policy); return false; } diff --git a/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/PolicyTypeNativeDroolsController.java b/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/PolicyTypeNativeDroolsController.java index b0118fbe..2a417dcb 100644 --- a/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/PolicyTypeNativeDroolsController.java +++ b/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/PolicyTypeNativeDroolsController.java @@ -21,6 +21,7 @@ package org.onap.policy.drools.lifecycle; import com.fasterxml.jackson.annotation.JsonIgnore; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Properties; @@ -142,6 +143,10 @@ public class PolicyTypeNativeDroolsController implements PolicyTypeController { } private boolean configControllerSources(ControllerProperties controllerConfig, Properties controllerProps) { + if (controllerConfig.getSourceTopics() == null) { + return true; + } + for (ControllerSourceTopic configSourceTopic : controllerConfig.getSourceTopics()) { List sources = TopicEndpointManager.getManager().getTopicSources(List.of(configSourceTopic.getTopicName())); @@ -161,6 +166,10 @@ public class PolicyTypeNativeDroolsController implements PolicyTypeController { } private boolean configControllerSinks(ControllerProperties controllerConfig, Properties controllerProps) { + if (controllerConfig.getSinkTopics() == null) { + return true; + } + for (ControllerSinkTopic configSinkTopic : controllerConfig.getSinkTopics()) { List sinks = TopicEndpointManager.getManager().getTopicSinks(List.of(configSinkTopic.getTopicName())); @@ -236,13 +245,21 @@ public class PolicyTypeNativeDroolsController implements PolicyTypeController { } private List sourceTopics(List sourceTopics) { + if (sourceTopics == null) { + return Collections.EMPTY_LIST; + } + return sourceTopics.stream() .map(ControllerSourceTopic::getTopicName) .collect(Collectors.toList()); } - private List sinkTopics(List sourceTopics) { - return sourceTopics.stream() + private List sinkTopics(List sinkTopics) { + if (sinkTopics == null) { + return Collections.EMPTY_LIST; + } + + return sinkTopics.stream() .map(ControllerSinkTopic::getTopicName) .collect(Collectors.toList()); } diff --git a/feature-lifecycle/src/main/java/org/onap/policy/drools/server/restful/RestLifecycleManager.java b/feature-lifecycle/src/main/java/org/onap/policy/drools/server/restful/RestLifecycleManager.java index 44113779..3e55a10c 100644 --- a/feature-lifecycle/src/main/java/org/onap/policy/drools/server/restful/RestLifecycleManager.java +++ b/feature-lifecycle/src/main/java/org/onap/policy/drools/server/restful/RestLifecycleManager.java @@ -20,47 +20,250 @@ package org.onap.policy.drools.server.restful; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import java.util.Properties; import javax.ws.rs.Consumes; import javax.ws.rs.GET; +import javax.ws.rs.PUT; import javax.ws.rs.Path; +import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import org.onap.policy.common.endpoints.event.comm.TopicSink; +import org.onap.policy.common.endpoints.event.comm.TopicSource; import org.onap.policy.common.endpoints.http.server.YamlMessageBodyHandler; import org.onap.policy.drools.lifecycle.LifecycleFeature; -import org.onap.policy.drools.lifecycle.LifecycleFsm; +import org.onap.policy.drools.lifecycle.PolicyTypeController; +import org.onap.policy.models.pdp.concepts.PdpStateChange; +import org.onap.policy.models.pdp.enums.PdpState; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; /** * REST Lifecycle Manager. */ -@Path("/policy/pdp") +@Path("/policy/pdp/engine/lifecycle") @Produces({MediaType.APPLICATION_JSON, YamlMessageBodyHandler.APPLICATION_YAML}) @Consumes({MediaType.APPLICATION_JSON, YamlMessageBodyHandler.APPLICATION_YAML}) @Api public class RestLifecycleManager { + /** + * GET group. + */ + @GET - @Path("engine/lifecycle/fsm/group") - @ApiOperation(value = "Retrieves the Lifecycle FSM", - notes = "Lifecycle FSM", response = LifecycleFsm.class) + @Path("group") + @ApiOperation(value = "Retrieves the Lifecycle group", + notes = "Lifecycle Group", response = String.class) public Response group() { return Response.status(Response.Status.OK).entity(LifecycleFeature.fsm.getGroup()).build(); } + /** + * PUT group. + */ + + @PUT + @Path("group/{group}") + @ApiOperation(value = "Updates the Lifecycle group", + notes = "Lifecycle Group", response = String.class) + public Response updateGroup( + @ApiParam(value = "Group", required = true) @PathParam("group") String group) { + LifecycleFeature.fsm.setGroup(group); + return Response.status(Response.Status.OK).entity(LifecycleFeature.fsm.getGroup()).build(); + } + + /** + * GET subgroup. + */ + @GET - @Path("engine/lifecycle/fsm/subgroup") - @ApiOperation(value = "Retrieves the Lifecycle FSM", - notes = "Lifecycle FSM", response = LifecycleFsm.class) + @Path("subgroup") + @ApiOperation(value = "Retrieves the Lifecycle subgroup", + notes = "Lifecycle Subgroup", response = String.class) public Response subgroup() { return Response.status(Response.Status.OK).entity(LifecycleFeature.fsm.getSubgroup()).build(); } + /** + * PUT subgroup. + */ + + @PUT + @Path("subgroup/{subgroup}") + @ApiOperation(value = "Retrieves the Lifecycle subgroup", + notes = "Lifecycle Subgroup", response = String.class) + public Response subgroup( + @ApiParam(value = "Subgroup", required = true) @PathParam("subgroup") String subgroup) { + LifecycleFeature.fsm.setSubgroup(subgroup); + return Response.status(Response.Status.OK).entity(LifecycleFeature.fsm.getSubgroup()).build(); + } + + /** + * GET properties. + */ + @GET - @Path("engine/lifecycle/fsm/state") - @ApiOperation(value = "Retrieves the Lifecycle FSM", - notes = "Lifecycle FSM", response = LifecycleFsm.class) + @Path("properties") + @ApiOperation(value = "Retrieves the Lifecycle properties", + notes = "Lifecycle Properties", response = Properties.class) + public Response properties() { + return Response.status(Response.Status.OK).entity(LifecycleFeature.fsm.getProperties()).build(); + } + + /** + * GET state. + */ + + @GET + @Path("state") + @ApiOperation(value = "Retrieves the Lifecycle state", notes = "Lifecycle State", response = PdpState.class) public Response state() { return Response.status(Response.Status.OK).entity(LifecycleFeature.fsm.state()).build(); } + + /** + * PUT state. + */ + + @PUT + @Path("state/{state}") + @ApiOperation(value = "updates the Lifecycle state", notes = "Lifecycle State", response = Boolean.class) + public Response updateState( + @ApiParam(value = "state", required = true) @PathParam("state") String state) { + + PdpStateChange change = new PdpStateChange(); + change.setPdpGroup(LifecycleFeature.fsm.getGroup()); + change.setPdpSubgroup(LifecycleFeature.fsm.getSubgroup()); + change.setState(PdpState.valueOf(state)); + change.setName(LifecycleFeature.fsm.getName()); + + return Response.status(Response.Status.OK).entity(LifecycleFeature.fsm.stateChange(change)).build(); + } + + /** + * GET topic source. + */ + + @GET + @Path("topic/source") + @ApiOperation(value = "Retrieves the Lifecycle topic source", + notes = "Lifecycle Topic Source", response = TopicSource.class) + public Response source() { + return Response.status(Response.Status.OK).entity(LifecycleFeature.fsm.getSource()).build(); + } + + /** + * GET topic sink. + */ + + @GET + @Path("topic/sink") + @ApiOperation(value = "Retrieves the Lifecycle topic sink", + notes = "Lifecycle Topic Sink", response = TopicSink.class) + public Response sink() { + return Response.status(Response.Status.OK).entity(LifecycleFeature.fsm.getClient()).build(); + } + + /** + * GET status interval. + */ + + @GET + @Path("status/interval") + @ApiOperation(value = "Retrieves the Lifecycle Status Timer Interval in seconds", + notes = "Lifecycle Status Timer Interval in seconds", response = Long.class) + public Response updateStatusTimer() { + return Response.status(Response.Status.OK).entity(LifecycleFeature.fsm.getStatusTimerSeconds()).build(); + } + + /** + * PUT timeout. + */ + + @PUT + @Path("status/interval/{timeout}") + @ApiOperation(value = "Updates the Lifecycle Status Timer Interval in seconds", + notes = "Lifecycle Status Timer Interval in seconds", response = Long.class) + public Response statusTimer( + @ApiParam(value = "timeout", required = true) @PathParam("timeout") Long timeout) { + LifecycleFeature.fsm.setStatusTimerSeconds(timeout); + return Response.status(Response.Status.OK).entity(LifecycleFeature.fsm.getStatusTimerSeconds()).build(); + } + + /** + * GET policy types. + */ + + @GET + @Path("policyTypes") + @ApiOperation(value = "List of supported policy types", + notes = "Lifecycle Policy Types", responseContainer = "List") + public Response policyTypes() { + return Response.status(Response.Status.OK) + .entity(LifecycleFeature.fsm.getPolicyTypesMap().keySet()) + .build(); + } + + /** + * GET controllers. + */ + + @GET + @Path("policyTypes/{policyType}/{policyVersion}") + @ApiOperation(value = "Entities associated with a policy type", + notes = "Lifecycle policy Types", response = PolicyTypeController.class) + public Response policyType( + @ApiParam(value = "Policy Type", required = true) @PathParam("policyType") String policyType, + @ApiParam(value = "Policy Type Version", required = true) @PathParam("policyVersion") String policyVersion) { + return Response.status(Response.Status.OK) + .entity(LifecycleFeature.fsm + .getPolicyTypesMap() + .get(new ToscaPolicyTypeIdentifier(policyType, policyVersion))) + .build(); + } + + /** + * GET policies. + */ + + @GET + @Path("policies") + @ApiOperation(value = "List of tracked policies", + notes = "Lifecycle Policies", responseContainer = "List") + public Response policies() { + return Response.status(Response.Status.OK) + .entity(LifecycleFeature.fsm.getPoliciesMap().keySet()) + .build(); + + } + + /** + * GET a policy. + */ + + @GET + @Path("policies/{policy}/{policyVersion}") + @ApiOperation(value = "Lifecycle tracked policy", + notes = "Lifecycle Tracked Policy", response = ToscaPolicy.class) + public Response policy( + @ApiParam(value = "Policy", required = true) @PathParam("policyName") String policyName, + @ApiParam(value = "Policy Version", required = true) @PathParam("policyVersion") String policyVersion) { + + ToscaPolicy policy = LifecycleFeature.fsm + .getPoliciesMap() + .get(new ToscaPolicyTypeIdentifier(policyName, policyVersion)); + if (policy != null) { + return + Response.status(Response.Status.OK) + .entity(LifecycleFeature.fsm.getPolicyTypesMap() + .get(new ToscaPolicyTypeIdentifier(policyName, policyVersion))) + .build(); + } + + return Response.status(Response.Status.NOT_FOUND).build(); + } } diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/ControllerSupport.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/ControllerSupport.java index c6cf15c6..9b673dd5 100644 --- a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/ControllerSupport.java +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/ControllerSupport.java @@ -44,7 +44,8 @@ public class ControllerSupport { protected static final String JUNIT_KJAR_DRL_PATH = "src/main/resources/kbLifecycle/org/onap/policy/drools/test/"; - protected static final String POLICY_TYPE = "onap.policies.controlloop.Operational"; + protected static final String POLICY_TYPE_LEGACY_OP = "onap.policies.controlloop.Operational"; + protected static final String POLICY_TYPE_COMPLIANT_OP = "onap.policies.controlloop.operational.common.Drools"; protected static final String POLICY_TYPE_VERSION = "1.0.0"; protected static final String SESSION_NAME = "junits"; @@ -103,7 +104,8 @@ public class ControllerSupport { * Get Policy Type. */ public static String getPolicyType() { - return POLICY_TYPE + ":" + POLICY_TYPE_VERSION; + return POLICY_TYPE_LEGACY_OP + ":" + POLICY_TYPE_VERSION + "," + + POLICY_TYPE_COMPLIANT_OP + ":" + POLICY_TYPE_VERSION; } /** diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActivePoliciesTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActivePoliciesTest.java new file mode 100644 index 00000000..0e5937ff --- /dev/null +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActivePoliciesTest.java @@ -0,0 +1,252 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2020 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * =============LICENSE_END======================================================== + */ + +package org.onap.policy.drools.lifecycle; + +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager; +import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.network.NetworkUtil; +import org.onap.policy.models.pdp.concepts.PdpStateChange; +import org.onap.policy.models.pdp.concepts.PdpUpdate; +import org.onap.policy.models.pdp.enums.PdpState; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; + +/** + * Lifecycle State Active Test. + */ +public class LifecycleStateActivePoliciesTest extends LifecycleStateRunningTest { + + private static final String EXAMPLE_NATIVE_DROOLS_CONTROLLER_POLICY_NAME = "example.controller"; + private static final String EXAMPLE_NATIVE_DROOLS_ARTIFACT_POLICY_NAME = "example.artifact"; + private static final String FOO_NATIVE_DROOLS_CONTROLLER_POLICY_NAME = "foo.controller"; + + private static final String EXAMPLE_NATIVE_DROOLS_POLICY_JSON = + "src/test/resources/tosca-policy-native-controller-example.json"; + private static final String EXAMPLE_NATIVE_ARTIFACT_POLICY_JSON = + "src/test/resources/tosca-policy-native-artifact-example.json"; + private static final String FOO_NATIVE_DROOLS_POLICY_JSON = + "src/test/resources/tosca-policy-native-controller-foo.json"; + + /** + * Start tests in the Active state. + */ + @Before + public void startActive() throws CoderException { + fsm = makeFsmWithPseudoTime(); + + fsm.setStatusTimerSeconds(15); + assertTrue(fsm.start()); + + PdpStateChange change = new PdpStateChange(); + change.setPdpGroup("A"); + change.setPdpSubgroup("a"); + change.setState(PdpState.ACTIVE); + change.setName(fsm.getName()); + + fsm.setSubGroupAction("a"); + fsm.source.offer(new StandardCoder().encode(change)); + controllerSupport.getController().start(); + } + + @Test + public void testUpdatePolicies() throws IOException, CoderException { + assertEquals(2, fsm.policyTypesMap.size()); + assertNotNull(fsm.getPolicyTypesMap().get( + new ToscaPolicyTypeIdentifier("onap.policies.native.drools.Controller", "1.0.0"))); + assertNotNull(fsm.getPolicyTypesMap().get( + new ToscaPolicyTypeIdentifier("onap.policies.native.drools.Artifact", "1.0.0"))); + + // + // create controller using native policy + // + + ToscaPolicy policyNativeController = + getPolicyFromFile(EXAMPLE_NATIVE_DROOLS_POLICY_JSON, EXAMPLE_NATIVE_DROOLS_CONTROLLER_POLICY_NAME); + + PdpUpdate update = new PdpUpdate(); + update.setName(NetworkUtil.getHostname()); + update.setPdpGroup("W"); + update.setPdpSubgroup("w"); + update.setPolicies(List.of(policyNativeController)); + + assertFalse(fsm.update(update)); + assertEquals(0, fsm.getPoliciesMap().size()); + + // add topics + + Properties noopTopicProperties = new Properties(); + noopTopicProperties.put(PolicyEndPointProperties.PROPERTY_NOOP_SOURCE_TOPICS, "DCAE_TOPIC"); + noopTopicProperties.put(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS, "APPC-CL"); + TopicEndpointManager.getManager().addTopics(noopTopicProperties); + + assertTrue(fsm.update(update)); + assertEquals(1, fsm.getPoliciesMap().size()); + + // + // add an artifact policy + // + + ToscaPolicy policyNativeArtifact = + getPolicyFromFile(EXAMPLE_NATIVE_ARTIFACT_POLICY_JSON, EXAMPLE_NATIVE_DROOLS_ARTIFACT_POLICY_NAME); + + Map controllerMap = + (Map) policyNativeArtifact.getProperties().get("controller"); + controllerMap.put("name", "xyz987"); + update.setPolicies(List.of(policyNativeController, policyNativeArtifact)); + assertFalse(fsm.update(update)); + + // add a registered controller + + controllerMap.put("name", "lifecycle"); + update.setPolicies(List.of(policyNativeController, policyNativeArtifact)); + assertTrue(fsm.update(update)); + + assertEquals(2, fsm.getPoliciesMap().size()); + assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier())); + assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier())); + + // + // add a legacy operational policy + // + + String restart = Files.readString(Paths.get("src/test/resources/tosca-policy-operational-restart.json")); + ToscaPolicy opPolicyRestart = new StandardCoder().decode(restart, ToscaPolicy.class); + update.setPolicies(List.of(policyNativeController, policyNativeArtifact, opPolicyRestart)); + assertFalse(fsm.update(update)); + + assertEquals(2, fsm.getPoliciesMap().size()); + assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier())); + assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier())); + + // register controller + + fsm.start(controllerSupport.getController()); + + assertEquals(4, fsm.policyTypesMap.size()); + assertNotNull(fsm.getPolicyTypesMap().get( + new ToscaPolicyTypeIdentifier("onap.policies.native.drools.Controller", "1.0.0"))); + assertNotNull(fsm.getPolicyTypesMap().get( + new ToscaPolicyTypeIdentifier("onap.policies.native.drools.Artifact", "1.0.0"))); + assertNotNull(fsm.getPolicyTypesMap().get( + new ToscaPolicyTypeIdentifier("onap.policies.controlloop.Operational", "1.0.0"))); + assertNotNull(fsm.getPolicyTypesMap().get( + new ToscaPolicyTypeIdentifier("onap.policies.controlloop.operational.common.Drools", + "1.0.0"))); + + // invalid controller name + + opPolicyRestart.getProperties().put("controllerName", "xyz987"); + assertFalse(fsm.update(update)); + assertEquals(2, fsm.getPoliciesMap().size()); + assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier())); + assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier())); + + // no controller name + + opPolicyRestart.getProperties().remove("controllerName"); + assertTrue(fsm.update(update)); + assertEquals(3, fsm.getPoliciesMap().size()); + assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier())); + assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier())); + assertEquals(opPolicyRestart, fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier())); + + List factPolicies = controllerSupport.getFacts(ToscaPolicy.class); + assertEquals(1, factPolicies.size()); + assertEquals(opPolicyRestart, factPolicies.get(0)); + + // upgrade operational policy with valid controller name + + String restartV2 = Files.readString( + Paths.get("src/test/resources/tosca-policy-operational-restart.v2.json")); + ToscaPolicy opPolicyRestartV2 = new StandardCoder().decode(restartV2, ToscaPolicy.class); + opPolicyRestartV2.getProperties().put("controllerName", "lifecycle"); + update.setPolicies(List.of(policyNativeController, policyNativeArtifact, opPolicyRestartV2)); + assertTrue(fsm.update(update)); + + assertEquals(3, fsm.getPoliciesMap().size()); + assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier())); + assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier())); + assertEquals(opPolicyRestartV2, fsm.getPoliciesMap().get(opPolicyRestartV2.getIdentifier())); + assertNull(fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier())); + + factPolicies = controllerSupport.getFacts(ToscaPolicy.class); + assertEquals(1, factPolicies.size()); + assertEquals(opPolicyRestartV2, factPolicies.get(0)); + + update.setPolicies(List.of(policyNativeController, policyNativeArtifact)); + assertTrue(fsm.update(update)); + + assertEquals(2, fsm.getPoliciesMap().size()); + assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier())); + assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier())); + assertNull(fsm.getPoliciesMap().get(opPolicyRestartV2.getIdentifier())); + assertNull(fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier())); + + factPolicies = controllerSupport.getFacts(ToscaPolicy.class); + assertEquals(0, factPolicies.size()); + assertTrue(controllerSupport.getController().getDrools().isBrained()); + + update.setPolicies(List.of(policyNativeController)); + assertTrue(fsm.update(update)); + assertFalse(controllerSupport.getController().getDrools().isBrained()); + assertEquals(1, fsm.getPoliciesMap().size()); + assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier())); + assertNull(fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier())); + assertNull(fsm.getPoliciesMap().get(opPolicyRestartV2.getIdentifier())); + assertNull(fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier())); + + ToscaPolicy policyNativeFooController = + getPolicyFromFile(FOO_NATIVE_DROOLS_POLICY_JSON, FOO_NATIVE_DROOLS_CONTROLLER_POLICY_NAME); + update.setPolicies(List.of(policyNativeController, policyNativeFooController)); + assertTrue(fsm.update(update)); + assertEquals(2, fsm.getPoliciesMap().size()); + assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier())); + assertEquals(policyNativeFooController, fsm.getPoliciesMap().get(policyNativeFooController.getIdentifier())); + + update.setPolicies(Collections.EMPTY_LIST); + assertTrue(fsm.update(update)); + assertThatIllegalArgumentException().isThrownBy(() -> controllerSupport.getController().getDrools()); + assertNull(fsm.getPoliciesMap().get(policyNativeController.getIdentifier())); + assertNull(fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier())); + assertNull(fsm.getPoliciesMap().get(opPolicyRestartV2.getIdentifier())); + assertNull(fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier())); + + fsm.shutdown(); + } +} diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActiveTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActiveTest.java index c61227b9..f370d0db 100644 --- a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActiveTest.java +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActiveTest.java @@ -24,6 +24,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.io.IOException; @@ -45,6 +46,7 @@ import org.onap.policy.models.pdp.concepts.PdpStatus; import org.onap.policy.models.pdp.concepts.PdpUpdate; import org.onap.policy.models.pdp.enums.PdpState; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; /** * Lifecycle State Active Test. @@ -207,7 +209,16 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest { assertTrue(fsm.update(update)); assertEquals(qlength + 1, fsm.client.getSink().getRecentEvents().length); - assertEquals(3, fsm.policyTypesMap.size()); + assertEquals(4, fsm.policyTypesMap.size()); + assertNotNull(fsm.getPolicyTypesMap().get( + new ToscaPolicyTypeIdentifier("onap.policies.native.drools.Controller", "1.0.0"))); + assertNotNull(fsm.getPolicyTypesMap().get( + new ToscaPolicyTypeIdentifier("onap.policies.native.drools.Artifact", "1.0.0"))); + assertNotNull(fsm.getPolicyTypesMap().get( + new ToscaPolicyTypeIdentifier("onap.policies.controlloop.Operational", "1.0.0"))); + assertNotNull(fsm.getPolicyTypesMap().get( + new ToscaPolicyTypeIdentifier("onap.policies.controlloop.operational.common.Drools", + "1.0.0"))); PdpStatus cachedStatus = new StandardCoder() .decode(fsm.client.getSink().getRecentEvents()[qlength], PdpStatus.class); assertEquals(new ArrayList<>(fsm.policiesMap.keySet()), cachedStatus.getPolicies()); @@ -221,7 +232,7 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest { assertTrue(fsm.update(update)); assertEquals(qlength + 2, fsm.client.getSink().getRecentEvents().length); - assertEquals(3, fsm.policyTypesMap.size()); + assertEquals(4, fsm.policyTypesMap.size()); cachedStatus = new StandardCoder() .decode(fsm.client.getSink().getRecentEvents()[qlength + 1], PdpStatus.class); assertEquals(new ArrayList<>(fsm.policiesMap.keySet()), cachedStatus.getPolicies()); @@ -237,7 +248,7 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest { update.setPolicies(Collections.emptyList()); assertTrue(fsm.update(update)); assertEquals(qlength + 3, fsm.client.getSink().getRecentEvents().length); - assertEquals(3, fsm.policyTypesMap.size()); + assertEquals(4, fsm.policyTypesMap.size()); cachedStatus = new StandardCoder() .decode(fsm.client.getSink().getRecentEvents()[qlength + 2], PdpStatus.class); assertEquals(new ArrayList<>(fsm.policiesMap.keySet()), cachedStatus.getPolicies()); @@ -251,7 +262,7 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest { update.setPolicies(Arrays.asList(toscaPolicyRestartV1)); assertTrue(fsm.update(update)); assertEquals(qlength + 4, fsm.client.getSink().getRecentEvents().length); - assertEquals(3, fsm.policyTypesMap.size()); + assertEquals(4, fsm.policyTypesMap.size()); cachedStatus = new StandardCoder() .decode(fsm.client.getSink().getRecentEvents()[qlength + 3], PdpStatus.class); assertEquals(new ArrayList<>(fsm.policiesMap.keySet()), cachedStatus.getPolicies()); @@ -269,7 +280,7 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest { update.setPolicies(Arrays.asList(toscaPolicyRestartV2)); assertTrue(fsm.update(update)); assertEquals(qlength + 5, fsm.client.getSink().getRecentEvents().length); - assertEquals(3, fsm.policyTypesMap.size()); + assertEquals(4, fsm.policyTypesMap.size()); cachedStatus = new StandardCoder() .decode(fsm.client.getSink().getRecentEvents()[qlength + 4], PdpStatus.class); assertEquals(new ArrayList<>(fsm.policiesMap.keySet()), cachedStatus.getPolicies()); @@ -288,7 +299,7 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest { update.setPolicies(Arrays.asList(toscaPolicyRestartV2, toscaPolicyFirewall)); assertTrue(fsm.update(update)); assertEquals(qlength + 6, fsm.client.getSink().getRecentEvents().length); - assertEquals(3, fsm.policyTypesMap.size()); + assertEquals(4, fsm.policyTypesMap.size()); cachedStatus = new StandardCoder() .decode(fsm.client.getSink().getRecentEvents()[qlength + 5], PdpStatus.class); assertEquals(new ArrayList<>(fsm.policiesMap.keySet()), cachedStatus.getPolicies()); @@ -313,4 +324,5 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest { fsm.shutdown(); } + } diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStatePassiveTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStatePassiveTest.java index 219aa0fb..7ccbd1f2 100644 --- a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStatePassiveTest.java +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStatePassiveTest.java @@ -76,11 +76,11 @@ public class LifecycleStatePassiveTest extends LifecycleStateRunningTest { assertSame(controllerSupport.getController(), ((PolicyTypeDroolsController) fsm.getController( new ToscaPolicyTypeIdentifier( - ControllerSupport.POLICY_TYPE, ControllerSupport.POLICY_TYPE_VERSION))) + ControllerSupport.POLICY_TYPE_LEGACY_OP, ControllerSupport.POLICY_TYPE_VERSION))) .controllers().get(0)); fsm.stop(controllerSupport.getController()); - assertNull(fsm.getController(new ToscaPolicyTypeIdentifier(ControllerSupport.POLICY_TYPE, + assertNull(fsm.getController(new ToscaPolicyTypeIdentifier(ControllerSupport.POLICY_TYPE_LEGACY_OP, ControllerSupport.POLICY_TYPE_VERSION))); fsm.shutdown(); @@ -211,11 +211,11 @@ public class LifecycleStatePassiveTest extends LifecycleStateRunningTest { assertTrue(fsm.policiesMap.isEmpty()); fsm.start(controllerSupport.getController()); - assertEquals(3, fsm.policyTypesMap.size()); + assertEquals(4, fsm.policyTypesMap.size()); assertTrue(fsm.policiesMap.isEmpty()); assertTrue(fsm.update(update)); - assertEquals(3, fsm.policyTypesMap.size()); + assertEquals(4, fsm.policyTypesMap.size()); assertEquals(1, fsm.policiesMap.size()); assertEquals(fsm.policiesMap.get(toscaPolicy.getIdentifier()), toscaPolicy); assertEquals(PdpState.PASSIVE, fsm.state()); @@ -229,7 +229,7 @@ public class LifecycleStatePassiveTest extends LifecycleStateRunningTest { update.setPdpSubgroup(null); update.setPolicies(Collections.emptyList()); assertTrue(fsm.update(update)); - assertEquals(3, fsm.policyTypesMap.size()); + assertEquals(4, fsm.policyTypesMap.size()); assertEquals(0, fsm.policiesMap.size()); assertEquals(PdpState.PASSIVE, fsm.state()); assertEquals(interval, fsm.getStatusTimerSeconds()); @@ -241,6 +241,7 @@ public class LifecycleStatePassiveTest extends LifecycleStateRunningTest { fsm.shutdown(); } + @Test public void testStateChange() throws CoderException, IOException { /* no name */ @@ -269,11 +270,11 @@ public class LifecycleStatePassiveTest extends LifecycleStateRunningTest { controllerSupport.getController().start(); fsm.start(controllerSupport.getController()); - assertEquals(3, fsm.policyTypesMap.size()); + assertEquals(4, fsm.policyTypesMap.size()); assertTrue(fsm.policiesMap.isEmpty()); assertTrue(fsm.update(update)); - assertEquals(3, fsm.policyTypesMap.size()); + assertEquals(4, fsm.policyTypesMap.size()); assertEquals(1, fsm.policiesMap.size()); assertEquals(fsm.policiesMap.get(toscaPolicy.getIdentifier()), toscaPolicy); assertEquals(PdpState.PASSIVE, fsm.state()); diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateRunningTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateRunningTest.java index 1d03124a..16474785 100644 --- a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateRunningTest.java +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateRunningTest.java @@ -28,6 +28,7 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import org.junit.AfterClass; import org.junit.BeforeClass; +import org.onap.policy.common.endpoints.event.comm.bus.NoopTopicFactories; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.time.PseudoScheduledExecutorService; @@ -66,6 +67,8 @@ public abstract class LifecycleStateRunningTest { @AfterClass public static void tearDown() { controllerSupport.destroyController(); + NoopTopicFactories.getSourceFactory().destroy(); + NoopTopicFactories.getSinkFactory().destroy(); try { Files.deleteIfExists(Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), CONTROLLER_NAME + "-controller.properties.bak")); diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/PolicyTypeNativeArtifactControllerTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/PolicyTypeNativeArtifactControllerTest.java index cb55e637..cd17385d 100644 --- a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/PolicyTypeNativeArtifactControllerTest.java +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/PolicyTypeNativeArtifactControllerTest.java @@ -43,7 +43,7 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifi */ public class PolicyTypeNativeArtifactControllerTest extends LifecycleStateRunningTest { // Native Drools Policy - private static final String EXAMPLE_NATIVE_DROOLS_POLICY_NAME = "example"; + private static final String EXAMPLE_NATIVE_DROOLS_POLICY_NAME = "example.artifact"; private static final String EXAMPLE_NATIVE_DROOLS_POLICY_JSON = "src/test/resources/tosca-policy-native-artifact-example.json"; diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/PolicyTypeNativeDroolsControllerTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/PolicyTypeNativeDroolsControllerTest.java index 1aa3a684..a230ced8 100644 --- a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/PolicyTypeNativeDroolsControllerTest.java +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/PolicyTypeNativeDroolsControllerTest.java @@ -41,7 +41,7 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; */ public class PolicyTypeNativeDroolsControllerTest extends LifecycleStateRunningTest { // Native Drools Policy - private static final String EXAMPLE_NATIVE_DROOLS_POLICY_NAME = "example"; + private static final String EXAMPLE_NATIVE_DROOLS_POLICY_NAME = "example.controller"; private static final String EXAMPLE_NATIVE_DROOLS_POLICY_JSON = "src/test/resources/tosca-policy-native-controller-example.json"; @@ -86,8 +86,6 @@ public class PolicyTypeNativeDroolsControllerTest extends LifecycleStateRunningT noopTopicProperties.put(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS, "APPC-CL"); TopicEndpointManager.getManager().addTopics(noopTopicProperties); - controller.deploy(policy); - - return; + assertTrue(controller.deploy(policy)); } } \ No newline at end of file diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/server/restful/RestLifecycleManagerTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/server/restful/RestLifecycleManagerTest.java index ae62095e..511fcc9b 100644 --- a/feature-lifecycle/src/test/java/org/onap/policy/drools/server/restful/RestLifecycleManagerTest.java +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/server/restful/RestLifecycleManagerTest.java @@ -19,7 +19,12 @@ package org.onap.policy.drools.server.restful; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import java.util.Collections; +import java.util.List; +import java.util.Properties; +import javax.ws.rs.client.Entity; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; import org.junit.After; @@ -80,16 +85,82 @@ public class RestLifecycleManagerTest { @Test public void testFsm() { - Response response = HttpClientFactoryInstance.getClientFactory().get("lifecycle").get("fsm/group"); + + HttpClient client = HttpClientFactoryInstance.getClientFactory().get("lifecycle"); + Response response; + + /* group */ + + response = client.put("group/GG", Entity.json(""), Collections.emptyMap()); + assertEquals(Status.OK.getStatusCode(), response.getStatus()); + assertEquals("GG", HttpClient.getBody(response, String.class)); + + response = HttpClientFactoryInstance.getClientFactory().get("lifecycle").get("group"); + assertEquals(Status.OK.getStatusCode(), response.getStatus()); assertEquals(LifecycleFeature.fsm.getGroup(), HttpClient.getBody(response, String.class)); + + /* subgroup */ + + response = client.put("subgroup/YY", Entity.json(""), Collections.emptyMap()); assertEquals(Status.OK.getStatusCode(), response.getStatus()); + assertEquals("YY", HttpClient.getBody(response, String.class)); - response = HttpClientFactoryInstance.getClientFactory().get("lifecycle").get("fsm/subgroup"); - assertEquals("", HttpClient.getBody(response, String.class)); + response = client.get("subgroup"); assertEquals(Status.OK.getStatusCode(), response.getStatus()); + assertEquals(LifecycleFeature.fsm.getSubgroup(), HttpClient.getBody(response, String.class)); - response = HttpClientFactoryInstance.getClientFactory().get("lifecycle").get("fsm/state"); + /* properties */ + + response = client.get("properties"); + assertEquals(Status.OK.getStatusCode(), response.getStatus()); + assertEquals(LifecycleFeature.fsm.getProperties(), HttpClient.getBody(response, Properties.class)); + + /* state (disallowed state change as has not been started) */ + + response = client.put("state/PASSIVE", Entity.json(""), Collections.emptyMap()); + assertEquals(Status.OK.getStatusCode(), response.getStatus()); + assertEquals(Boolean.FALSE, HttpClient.getBody(response, Boolean.class)); + + response = client.get("state"); + assertEquals(Status.OK.getStatusCode(), response.getStatus()); assertEquals(PdpState.TERMINATED, HttpClient.getBody(response, PdpState.class)); + + /* topics */ + + assertEquals(Status.OK.getStatusCode(), client.get("topic/source").getStatus()); + assertEquals(Status.OK.getStatusCode(), client.get("topic/sink").getStatus()); + + /* status interval */ + + response = client.put("status/interval/1000", Entity.json(""), Collections.emptyMap()); + assertEquals(Status.OK.getStatusCode(), response.getStatus()); + assertEquals(Long.valueOf(1000L), HttpClient.getBody(response, Long.class)); + + response = client.get("status/interval"); + assertEquals(Status.OK.getStatusCode(), response.getStatus()); + assertEquals(Long.valueOf(1000L), HttpClient.getBody(response, Long.class)); + + /* policy types */ + + response = client.get("policyTypes"); assertEquals(Status.OK.getStatusCode(), response.getStatus()); + assertEquals(2, HttpClient.getBody(response, List.class).size()); + + response = client.get("policyTypes/onap.policies.native.drools.Artifact/1.0.0"); + assertEquals(Status.OK.getStatusCode(), response.getStatus()); + assertNotNull(HttpClient.getBody(response, String.class)); + + response = client.get("policyTypes/onap.policies.native.drools.Controller/1.0.0"); + assertEquals(Status.OK.getStatusCode(), response.getStatus()); + assertNotNull(HttpClient.getBody(response, String.class)); + + /* policies */ + + response = client.get("policies"); + assertEquals(Status.OK.getStatusCode(), response.getStatus()); + assertEquals(0, HttpClient.getBody(response, List.class).size()); + + response = client.get("policies/onap.policies.controlloop.Operational"); + assertEquals(Status.NOT_FOUND.getStatusCode(), response.getStatus()); } } diff --git a/feature-lifecycle/src/test/resources/tosca-policy-native-artifact-example.json b/feature-lifecycle/src/test/resources/tosca-policy-native-artifact-example.json index f521953d..b6774124 100644 --- a/feature-lifecycle/src/test/resources/tosca-policy-native-artifact-example.json +++ b/feature-lifecycle/src/test/resources/tosca-policy-native-artifact-example.json @@ -3,13 +3,13 @@ "topology_template": { "policies": [ { - "example": { + "example.artifact": { "type": "onap.policies.native.drools.Artifact", "type_version": "1.0.0", "version": "1.0.0", - "name": "example", + "name": "example.artifact", "metadata": { - "policy-id": "example" + "policy-id": "example.artifact" }, "properties": { "rulesArtifact": { @@ -18,8 +18,7 @@ "version": "1.0.0" }, "controller": { - "name": "lifecycle", - "version": "1.0.0" + "name": "lifecycle" } } } diff --git a/feature-lifecycle/src/test/resources/tosca-policy-native-artifact-foo.json b/feature-lifecycle/src/test/resources/tosca-policy-native-artifact-foo.json new file mode 100644 index 00000000..afdc0a83 --- /dev/null +++ b/feature-lifecycle/src/test/resources/tosca-policy-native-artifact-foo.json @@ -0,0 +1,28 @@ +{ + "tosca_definitions_version": "tosca_simple_yaml_1_0_0", + "topology_template": { + "policies": [ + { + "foo.artifact": { + "type": "onap.policies.native.drools.Artifact", + "type_version": "1.0.0", + "version": "1.0.0", + "name": "foo.artifact", + "metadata": { + "policy-id": "foo.artifact" + }, + "properties": { + "rulesArtifact": { + "groupId": "org.onap.policy.drools.test", + "artifactId": "lifecycle", + "version": "1.0.0" + }, + "controller": { + "name": "foo" + } + } + } + } + ] + } +} \ No newline at end of file diff --git a/feature-lifecycle/src/test/resources/tosca-policy-native-controller-example.json b/feature-lifecycle/src/test/resources/tosca-policy-native-controller-example.json index 3d716845..2b98a242 100644 --- a/feature-lifecycle/src/test/resources/tosca-policy-native-controller-example.json +++ b/feature-lifecycle/src/test/resources/tosca-policy-native-controller-example.json @@ -3,13 +3,13 @@ "topology_template": { "policies": [ { - "example": { + "example.controller": { "type": "onap.policies.native.drools.Controller", "type_version": "1.0.0", "version": "1.0.0", - "name": "example", + "name": "example.controller", "metadata": { - "policy-id": "example" + "policy-id": "example.controller" }, "properties": { "controllerName": "lifecycle", diff --git a/feature-lifecycle/src/test/resources/tosca-policy-native-controller-foo.json b/feature-lifecycle/src/test/resources/tosca-policy-native-controller-foo.json new file mode 100644 index 00000000..7ee0149b --- /dev/null +++ b/feature-lifecycle/src/test/resources/tosca-policy-native-controller-foo.json @@ -0,0 +1,21 @@ +{ + "tosca_definitions_version": "tosca_simple_yaml_1_0_0", + "topology_template": { + "policies": [ + { + "foo.controller": { + "type": "onap.policies.native.drools.Controller", + "type_version": "1.0.0", + "version": "1.0.0", + "name": "foo.controller", + "metadata": { + "policy-id": "foo.controller" + }, + "properties": { + "controllerName": "foo" + } + } + } + ] + } +} \ No newline at end of file -- cgit 1.2.3-korg