Cleanup ServerSessionManager
[bgpcep.git] / pcep / topology / topology-provider / src / test / java / org / opendaylight / bgpcep / pcep / topology / provider / AbstractPCEPSessionTest.java
index da820dc03b90ad3d6b57e9447e8336ade4a24808..6223c9ed9d64092e33c3dec2c758e235246f806c 100644 (file)
@@ -5,48 +5,39 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.bgpcep.pcep.topology.provider;
 
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.doAnswer;
-import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
-import static org.opendaylight.protocol.util.CheckTestUtil.checkEquals;
 
 import io.netty.channel.Channel;
 import io.netty.channel.ChannelFuture;
 import io.netty.channel.ChannelHandler;
 import io.netty.channel.ChannelPipeline;
 import io.netty.channel.EventLoop;
+import io.netty.util.HashedWheelTimer;
+import io.netty.util.Timer;
 import io.netty.util.concurrent.Promise;
-import java.lang.reflect.ParameterizedType;
 import java.net.InetSocketAddress;
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.List;
 import java.util.concurrent.TimeUnit;
 import org.junit.After;
 import org.junit.Before;
 import org.mockito.Mock;
-import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
-import org.opendaylight.bgpcep.pcep.topology.provider.config.PCEPTopologyConfiguration;
-import org.opendaylight.bgpcep.pcep.topology.provider.config.PCEPTopologyProviderDependencies;
-import org.opendaylight.bgpcep.pcep.topology.spi.stats.TopologySessionStatsRegistry;
 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractConcurrentDataBrokerTest;
-import org.opendaylight.protocol.pcep.PCEPSessionListener;
 import org.opendaylight.protocol.pcep.impl.DefaultPCEPSessionNegotiator;
 import org.opendaylight.protocol.pcep.impl.PCEPSessionImpl;
 import org.opendaylight.protocol.util.InetSocketAddressUtil;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressNoZone;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.config.rev200120.pcep.config.SessionConfig;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.graph.rev220720.graph.topology.GraphKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stats.rev171113.PcepSessionState;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.Ero;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.EroBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.ero.Subobject;
@@ -56,9 +47,9 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.typ
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.IpPrefixCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.IpPrefixCaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.NetworkTopologyPcepService;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.Node1;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.pcep.client.attributes.PathComputationClient;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.NetworkTopologyPcepService;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.Node1;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev220730.pcep.client.attributes.PathComputationClient;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
@@ -66,33 +57,35 @@ import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
+import org.opendaylight.yangtools.concepts.NoOpObjectRegistration;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
 import org.opendaylight.yangtools.yang.binding.Notification;
-import org.opendaylight.yangtools.yang.common.Uint16;
 import org.opendaylight.yangtools.yang.common.Uint8;
 
-public abstract class AbstractPCEPSessionTest<T extends TopologySessionListenerFactory>
-        extends AbstractConcurrentDataBrokerTest {
+public abstract class AbstractPCEPSessionTest extends AbstractConcurrentDataBrokerTest {
 
-    static final short DEAD_TIMER = 30;
-    static final short KEEP_ALIVE = 10;
     static final short RPC_TIMEOUT = 4;
-    private static final TopologyId TEST_TOPOLOGY_ID = new TopologyId("testtopo");
-    static final InstanceIdentifier<Topology> TOPO_IID = InstanceIdentifier.builder(NetworkTopology.class)
-            .child(Topology.class, new TopologyKey(TEST_TOPOLOGY_ID)).build();
+    private static final TopologyKey TEST_TOPOLOGY_ID = new TopologyKey(new TopologyId("testtopo"));
+    static final KeyedInstanceIdentifier<Topology, TopologyKey> TOPO_IID =
+        InstanceIdentifier.create(NetworkTopology.class).child(Topology.class, TEST_TOPOLOGY_ID);
     private static final String IPV4_MASK = "/32";
     final String testAddress = InetSocketAddressUtil.getRandomLoopbackIpAddress();
-    final NodeId nodeId = new NodeId("pcc://" + this.testAddress);
+    final NodeId nodeId = new NodeId("pcc://" + testAddress);
     protected final InstanceIdentifier<PathComputationClient> pathComputationClientIId = TOPO_IID.builder()
-            .child(Node.class, new NodeKey(this.nodeId)).augmentation(Node1.class).child(PathComputationClient.class
+            .child(Node.class, new NodeKey(nodeId)).augmentation(Node1.class).child(PathComputationClient.class
             ).build();
-    final String eroIpPrefix = this.testAddress + IPV4_MASK;
+    final String eroIpPrefix = testAddress + IPV4_MASK;
     final String newDestinationAddress = InetSocketAddressUtil.getRandomLoopbackIpAddress();
-    final String dstIpPrefix = this.newDestinationAddress + IPV4_MASK;
-    private final Open localPrefs = new OpenBuilder().setDeadTimer(Uint8.valueOf(30)).setKeepalive(Uint8.valueOf(10))
-            .setSessionId(Uint8.ZERO).build();
-    private final Open remotePrefs = this.localPrefs;
-    List<Notification> receivedMsgs;
+    final String dstIpPrefix = newDestinationAddress + IPV4_MASK;
+    private final Open localPrefs = new OpenBuilder()
+        .setDeadTimer(Uint8.valueOf(30))
+        .setKeepalive(Uint8.TEN)
+        .setSessionId(Uint8.ZERO)
+        .build();
+    private final Open remotePrefs = localPrefs;
+
+    List<Notification<?>> receivedMsgs;
     ServerSessionManager manager;
     NetworkTopologyPcepService topologyRpcs;
     @Mock
@@ -104,77 +97,71 @@ public abstract class AbstractPCEPSessionTest<T extends TopologySessionListenerF
     @Mock
     private ChannelFuture channelFuture;
     @Mock
-    private TopologySessionStatsRegistry statsRegistry;
+    private SessionStateRegistry stateRegistry;
     @Mock
     private PCEPTopologyProviderDependencies topologyDependencies;
     @Mock
-    private SessionConfig sessionConfig;
-    @Mock
-    private Topology topology;
-    @Mock
     private Promise<PCEPSessionImpl> promise;
+
+    private final Timer timer = new HashedWheelTimer();
     private DefaultPCEPSessionNegotiator neg;
 
     @Before
     public void setUp() throws Exception {
         MockitoAnnotations.initMocks(this);
-        this.receivedMsgs = new ArrayList<>();
+        receivedMsgs = new ArrayList<>();
         doAnswer(invocation -> {
-            final Object[] args = invocation.getArguments();
-            AbstractPCEPSessionTest.this.receivedMsgs.add((Notification) args[0]);
-            return this.channelFuture;
-        }).when(this.clientListener).writeAndFlush(any(Notification.class));
-        doReturn(null).when(this.channelFuture).addListener(Mockito.any());
-        doReturn("TestingChannel").when(this.clientListener).toString();
-        doReturn(this.pipeline).when(this.clientListener).pipeline();
-        doReturn(this.pipeline).when(this.pipeline).replace(any(ChannelHandler.class), any(String.class),
-                any(ChannelHandler.class));
-        doReturn(this.eventLoop).when(this.clientListener).eventLoop();
-        doNothing().when(this.statsRegistry).bind(any(), any());
-        doNothing().when(this.statsRegistry).unbind(any());
-        doReturn(null).when(this.eventLoop).schedule(any(Runnable.class), any(long.class),
-                any(TimeUnit.class));
-        doReturn(true).when(this.clientListener).isActive();
-        final InetSocketAddress ra = new InetSocketAddress(this.testAddress, 4189);
-        doReturn(ra).when(this.clientListener).remoteAddress();
-        final InetSocketAddress la = new InetSocketAddress(this.testAddress, InetSocketAddressUtil.getRandomPort());
-        doReturn(la).when(this.clientListener).localAddress();
-
-        doReturn(mock(ChannelFuture.class)).when(this.clientListener).close();
-
-        doReturn(getDataBroker()).when(this.topologyDependencies).getDataBroker();
-        doReturn(this.statsRegistry).when(this.topologyDependencies).getStateRegistry();
-
-        @SuppressWarnings("unchecked") final T listenerFactory = (T) ((Class) ((ParameterizedType) this.getClass()
-                .getGenericSuperclass()).getActualTypeArguments()[0]).newInstance();
-        doReturn(new IpAddressNoZone(new Ipv4AddressNoZone(this.testAddress))).when(this.sessionConfig)
-            .getListenAddress();
-        doReturn(new PortNumber(Uint16.valueOf(4189))).when(this.sessionConfig).getListenPort();
-        doReturn(RPC_TIMEOUT).when(this.sessionConfig).getRpcTimeout();
-        doReturn(TEST_TOPOLOGY_ID).when(this.topology).getTopologyId();
-        doReturn(Collections.emptyList()).when(this.topology).getNode();
-        doReturn(null).when(this.topologyDependencies).getPceServerProvider();
-
-        final PCEPTopologyConfiguration configDep = new PCEPTopologyConfiguration(this.sessionConfig, this.topology);
-        this.manager = new ServerSessionManager(this.topologyDependencies, listenerFactory, configDep);
+            receivedMsgs.add(invocation.getArgument(0, Notification.class));
+            return channelFuture;
+        }).when(clientListener).writeAndFlush(any(Notification.class));
+        doReturn(null).when(channelFuture).addListener(any());
+        doReturn("TestingChannel").when(clientListener).toString();
+        doReturn(pipeline).when(clientListener).pipeline();
+        doReturn(pipeline).when(pipeline).replace(any(ChannelHandler.class), any(String.class),
+            any(ChannelHandler.class));
+        doReturn(eventLoop).when(clientListener).eventLoop();
+        doAnswer(inv -> NoOpObjectRegistration.of(inv.getArgument(1, PcepSessionState.class)))
+            .when(stateRegistry).bind(any(), any());
+        doReturn(null).when(eventLoop).schedule(any(Runnable.class), any(long.class), any(TimeUnit.class));
+        doReturn(true).when(clientListener).isActive();
+        final InetSocketAddress ra = new InetSocketAddress(testAddress, 4189);
+        doReturn(ra).when(clientListener).remoteAddress();
+        final InetSocketAddress la = new InetSocketAddress(testAddress, InetSocketAddressUtil.getRandomPort());
+        doReturn(la).when(clientListener).localAddress();
+
+        doReturn(mock(ChannelFuture.class)).when(clientListener).close();
+
+        doReturn(getDataBroker()).when(topologyDependencies).getDataBroker();
+        doReturn(stateRegistry).when(topologyDependencies).getStateRegistry();
+        doReturn(timer).when(topologyDependencies).getTimer();
+        doReturn(null).when(topologyDependencies).getPceServerProvider();
+
+        manager = customizeSessionManager(new ServerSessionManager(TOPO_IID, topologyDependencies, RPC_TIMEOUT,
+                new GraphKey("graph-test")));
         startSessionManager();
-        this.neg = new DefaultPCEPSessionNegotiator(this.promise, this.clientListener,
-                this.manager.getSessionListener(), (short) 1, 5, this.localPrefs);
-        this.topologyRpcs = new TopologyRPCs(this.manager);
+        neg = new DefaultPCEPSessionNegotiator(promise, clientListener, manager.getSessionListener(), (short) 1, 5,
+            localPrefs);
+        topologyRpcs = new TopologyRPCs(manager);
+    }
+
+    // Visible for TopologyProgrammingTest
+    ServerSessionManager customizeSessionManager(final ServerSessionManager original) {
+        return original;
     }
 
     void startSessionManager() throws Exception {
-        this.manager.instantiateServiceInstance();
-        checkEquals(() -> assertFalse(this.manager.isClosed.get()));
+        assertTrue(manager.start().get());
+        assertFalse(manager.isClosed());
     }
 
     void stopSessionManager() {
-        this.manager.closeServiceInstance();
+        manager.stop();
     }
 
     @After
     public void tearDown() {
         stopSessionManager();
+        timer.stop();
     }
 
     Ero createEroWithIpPrefixes(final List<String> ipPrefixes) {
@@ -194,18 +181,18 @@ public abstract class AbstractPCEPSessionTest<T extends TopologySessionListenerF
     }
 
     protected Open getLocalPref() {
-        return this.localPrefs;
+        return localPrefs;
     }
 
     protected Open getRemotePref() {
-        return this.remotePrefs;
+        return remotePrefs;
     }
 
-    protected PCEPSessionListener getSessionListener() {
-        return this.manager.getSessionListener();
+    protected PCEPTopologySessionListener getSessionListener() {
+        return manager.getSessionListener();
     }
 
     protected final PCEPSessionImpl getPCEPSession(final Open localOpen, final Open remoteOpen) {
-        return this.neg.createSession(this.clientListener, localOpen, remoteOpen);
+        return neg.createSession(clientListener, localOpen, remoteOpen);
     }
 }