From f583aa9031e4f73ae0004bf972ac7a6c3afa4b71 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Sat, 29 Feb 2020 13:20:06 -0500 Subject: Add sequence number to Actors When actors with duplicate names are loaded, a way is needed to determine which should take precedence. Added a sequence number to Actor to facilitate this. Unfortunately, couldn't use OrderedServiceImpl, because it only creates the Actors once, which would break a number of junit tests. Issue-ID: POLICY-1625 Signed-off-by: Jim Hahn Change-Id: I1ade92f4fe516cfc54bb92d0a3624c67e8981075 --- .../actor/appc/AppcActorServiceProvider.java | 8 ++++ .../actor/appc/AppcServiceProviderTest.java | 1 + .../actorserviceprovider/ActorService.java | 43 ++++++++++++++++------ .../actorserviceprovider/impl/ActorImpl.java | 8 ++++ .../actorserviceprovider/spi/Actor.java | 10 ++++- .../actorserviceprovider/ActorServiceTest.java | 16 +++++++- .../actorserviceprovider/impl/ActorImplTest.java | 1 + 7 files changed, 73 insertions(+), 14 deletions(-) (limited to 'models-interactions/model-actors') diff --git a/models-interactions/model-actors/actor.appc/src/main/java/org/onap/policy/controlloop/actor/appc/AppcActorServiceProvider.java b/models-interactions/model-actors/actor.appc/src/main/java/org/onap/policy/controlloop/actor/appc/AppcActorServiceProvider.java index 76aa828c1..117bd5c86 100644 --- a/models-interactions/model-actors/actor.appc/src/main/java/org/onap/policy/controlloop/actor/appc/AppcActorServiceProvider.java +++ b/models-interactions/model-actors/actor.appc/src/main/java/org/onap/policy/controlloop/actor/appc/AppcActorServiceProvider.java @@ -81,6 +81,14 @@ public class AppcActorServiceProvider extends BidirectionalTopicActor> { Map map = new HashMap<>(); - Iterator iter = loadActors().iterator(); - while (iter.hasNext()) { - - Actor newActor; - try { - newActor = iter.next(); - } catch (ServiceConfigurationError e) { - logger.warn("unable to load actor", e); - continue; - } - + for (Actor newActor : buildList()) { map.compute(newActor.getName(), (name, existingActor) -> { if (existingActor == null) { return newActor; @@ -80,6 +72,35 @@ public class ActorService extends StartConfigPartial> { name2actor = ImmutableMap.copyOf(map); } + /** + * Builds the list of actors, discarding those that cannot be constructed. + * + * @return the list of actors, sorted by ascending sequence number + */ + private List buildList() { + List actors = new LinkedList<>(); + + Iterator iter = loadActors().iterator(); + while (iter.hasNext()) { + try { + actors.add(iter.next()); + } catch (ServiceConfigurationError e) { + logger.warn("unable to load actor", e); + } + } + + actors.sort((actor1, actor2) -> { + int cmp = Integer.compare(actor1.getSequenceNumber(), actor2.getSequenceNumber()); + if (cmp != 0) { + return cmp; + } + + return actor1.getClass().getName().compareTo(actor2.getClass().getName()); + }); + + return actors; + } + /** * Gets a particular actor. * diff --git a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/ActorImpl.java b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/ActorImpl.java index 0c88ebee2..12af030d5 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/ActorImpl.java +++ b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/ActorImpl.java @@ -55,6 +55,14 @@ public class ActorImpl extends StartConfigPartial> implement super(name); } + /** + * This method simply returns {@code 0}. + */ + @Override + public int getSequenceNumber() { + return 0; + } + /** * Adds an operator supported by this actor. * diff --git a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/spi/Actor.java b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/spi/Actor.java index 53bee5f00..52d1a5a9a 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/spi/Actor.java +++ b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/spi/Actor.java @@ -36,7 +36,7 @@ import org.onap.policy.controlloop.actorserviceprovider.Operator; * @author pameladragosh * */ -public interface Actor extends Startable, Configurable> { +public interface Actor extends Startable, Configurable> { /** * Gets the name of the actor. @@ -68,6 +68,14 @@ public interface Actor extends Startable, Configurable> { */ public Set getOperationNames(); + /** + * Gets the actor sequence number. Lower numbered actors take precedence over higher + * numbered actors. + * + * @return the actor sequence number + */ + public int getSequenceNumber(); + // TODO old code: remove lines down to **HERE** diff --git a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/ActorServiceTest.java b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/ActorServiceTest.java index 401e0ca58..0f282f662 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/ActorServiceTest.java +++ b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/ActorServiceTest.java @@ -91,15 +91,27 @@ public class ActorServiceTest { } @Test - public void testActorService() { + public void testActorService_testBuildList() { /* * make a service where actors two and four have names that are duplicates of the * others */ + + /* + * actor0 has a higher sequence number than actor1, so it should be discarded, + * even though it will be examined first + */ + Actor actor0 = spy(new ActorImpl(ACTOR1) { + @Override + public int getSequenceNumber() { + return 10000; + } + }); + actor2 = spy(new ActorImpl(ACTOR1)); actor4 = spy(new ActorImpl(ACTOR3)); - service = makeService(actor1, actor2, actor3, actor4); + service = makeService(actor0, actor1, actor2, actor3, actor4); assertEquals(2, service.getActorNames().size()); diff --git a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/ActorImplTest.java b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/ActorImplTest.java index 92cbbe774..681378de4 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/ActorImplTest.java +++ b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/ActorImplTest.java @@ -93,6 +93,7 @@ public class ActorImplTest { public void testActorImpl_testGetName() { assertEquals(ACTOR_NAME, actor.getName()); assertEquals(4, actor.getOperationNames().size()); + assertEquals(0, actor.getSequenceNumber()); } @Test -- cgit 1.2.3-korg