2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.bgpcep.pcep.topology.provider;
11 import static org.junit.Assert.assertFalse;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.Mockito.doAnswer;
14 import static org.mockito.Mockito.doNothing;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
17 import static org.opendaylight.protocol.util.CheckTestUtil.checkEquals;
19 import io.netty.channel.Channel;
20 import io.netty.channel.ChannelFuture;
21 import io.netty.channel.ChannelHandler;
22 import io.netty.channel.ChannelPipeline;
23 import io.netty.channel.EventLoop;
24 import io.netty.util.concurrent.Promise;
25 import java.lang.reflect.ParameterizedType;
26 import java.net.InetSocketAddress;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.List;
30 import java.util.concurrent.TimeUnit;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.mockito.Mock;
34 import org.mockito.Mockito;
35 import org.mockito.MockitoAnnotations;
36 import org.opendaylight.bgpcep.pcep.topology.provider.config.PCEPTopologyConfiguration;
37 import org.opendaylight.bgpcep.pcep.topology.provider.config.PCEPTopologyProviderDependencies;
38 import org.opendaylight.bgpcep.pcep.topology.spi.stats.TopologySessionStatsRegistry;
39 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractConcurrentDataBrokerTest;
40 import org.opendaylight.protocol.pcep.PCEPSessionListener;
41 import org.opendaylight.protocol.pcep.impl.DefaultPCEPSessionNegotiator;
42 import org.opendaylight.protocol.pcep.impl.PCEPSessionImpl;
43 import org.opendaylight.protocol.util.InetSocketAddressUtil;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.config.rev171025.pcep.config.SessionConfig;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.Ero;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.EroBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.IpPrefixCase;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.IpPrefixCaseBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.NetworkTopologyPcepService;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.Node1;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.pcep.client.attributes.PathComputationClient;
62 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
63 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
64 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
65 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
66 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
67 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
68 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
69 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
70 import org.opendaylight.yangtools.yang.binding.Notification;
72 public abstract class AbstractPCEPSessionTest<T extends TopologySessionListenerFactory>
73 extends AbstractConcurrentDataBrokerTest {
75 static final short DEAD_TIMER = 30;
76 static final short KEEP_ALIVE = 10;
77 static final short RPC_TIMEOUT = 4;
78 private static final TopologyId TEST_TOPOLOGY_ID = new TopologyId("testtopo");
79 static final InstanceIdentifier<Topology> TOPO_IID = InstanceIdentifier.builder(NetworkTopology.class)
80 .child(Topology.class, new TopologyKey(TEST_TOPOLOGY_ID)).build();
81 private static final String IPV4_MASK = "/32";
82 final String testAddress = InetSocketAddressUtil.getRandomLoopbackIpAddress();
83 final NodeId nodeId = new NodeId("pcc://" + this.testAddress);
84 protected final InstanceIdentifier<PathComputationClient> pathComputationClientIId = TOPO_IID.builder()
85 .child(Node.class, new NodeKey(this.nodeId)).augmentation(Node1.class).child(PathComputationClient.class
87 final String eroIpPrefix = this.testAddress + IPV4_MASK;
88 final String newDestinationAddress = InetSocketAddressUtil.getRandomLoopbackIpAddress();
89 final String dstIpPrefix = this.newDestinationAddress + IPV4_MASK;
90 private final Open localPrefs = new OpenBuilder().setDeadTimer((short) 30).setKeepalive((short) 10)
91 .setSessionId((short) 0).build();
92 private final Open remotePrefs = this.localPrefs;
93 List<Notification> receivedMsgs;
94 ServerSessionManager manager;
95 NetworkTopologyPcepService topologyRpcs;
97 private EventLoop eventLoop;
99 private Channel clientListener;
101 private ChannelPipeline pipeline;
103 private ChannelFuture channelFuture;
105 private TopologySessionStatsRegistry statsRegistry;
107 private PCEPTopologyProviderDependencies topologyDependencies;
109 private SessionConfig sessionConfig;
111 private Topology topology;
113 private Promise<PCEPSessionImpl> promise;
114 private DefaultPCEPSessionNegotiator neg;
117 public void setUp() throws Exception {
118 MockitoAnnotations.initMocks(this);
119 this.receivedMsgs = new ArrayList<>();
120 doAnswer(invocation -> {
121 final Object[] args = invocation.getArguments();
122 AbstractPCEPSessionTest.this.receivedMsgs.add((Notification) args[0]);
123 return this.channelFuture;
124 }).when(this.clientListener).writeAndFlush(any(Notification.class));
125 doReturn(null).when(this.channelFuture).addListener(Mockito.any());
126 doReturn("TestingChannel").when(this.clientListener).toString();
127 doReturn(this.pipeline).when(this.clientListener).pipeline();
128 doReturn(this.pipeline).when(this.pipeline).replace(any(ChannelHandler.class), any(String.class),
129 any(ChannelHandler.class));
130 doReturn(this.eventLoop).when(this.clientListener).eventLoop();
131 doNothing().when(this.statsRegistry).bind(any(), any());
132 doNothing().when(this.statsRegistry).unbind(any());
133 doReturn(null).when(this.eventLoop).schedule(any(Runnable.class), any(long.class),
134 any(TimeUnit.class));
135 doReturn(true).when(this.clientListener).isActive();
136 final InetSocketAddress ra = new InetSocketAddress(this.testAddress, 4189);
137 doReturn(ra).when(this.clientListener).remoteAddress();
138 final InetSocketAddress la = new InetSocketAddress(this.testAddress, InetSocketAddressUtil.getRandomPort());
139 doReturn(la).when(this.clientListener).localAddress();
141 doReturn(mock(ChannelFuture.class)).when(this.clientListener).close();
143 doReturn(getDataBroker()).when(this.topologyDependencies).getDataBroker();
144 doReturn(this.statsRegistry).when(this.topologyDependencies).getStateRegistry();
146 @SuppressWarnings("unchecked") final T listenerFactory = (T) ((Class) ((ParameterizedType) this.getClass()
147 .getGenericSuperclass()).getActualTypeArguments()[0]).newInstance();
148 doReturn(new IpAddress(new Ipv4Address(this.testAddress))).when(this.sessionConfig).getListenAddress();
149 doReturn(new PortNumber(4189)).when(this.sessionConfig).getListenPort();
150 doReturn(RPC_TIMEOUT).when(this.sessionConfig).getRpcTimeout();
151 doReturn(TEST_TOPOLOGY_ID).when(this.topology).getTopologyId();
152 doReturn(Collections.emptyList()).when(this.topology).getNode();
154 final PCEPTopologyConfiguration configDep = new PCEPTopologyConfiguration(this.sessionConfig, this.topology);
155 this.manager = new ServerSessionManager(this.topologyDependencies, listenerFactory, configDep);
156 startSessionManager();
157 this.neg = new DefaultPCEPSessionNegotiator(this.promise, this.clientListener,
158 this.manager.getSessionListener(), (short) 1, 5, this.localPrefs);
159 this.topologyRpcs = new TopologyRPCs(this.manager);
162 void startSessionManager() throws Exception {
163 this.manager.instantiateServiceInstance();
164 checkEquals(() -> assertFalse(this.manager.isClosed.get()));
167 void stopSessionManager() {
168 this.manager.closeServiceInstance();
172 public void tearDown() {
173 stopSessionManager();
176 Ero createEroWithIpPrefixes(final List<String> ipPrefixes) {
177 final List<Subobject> subobjs = new ArrayList<>(ipPrefixes.size());
178 final SubobjectBuilder subobjBuilder = new SubobjectBuilder();
179 for (final String ipPrefix : ipPrefixes) {
180 subobjBuilder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(new IpPrefixBuilder()
181 .setIpPrefix(new IpPrefix(new Ipv4Prefix(ipPrefix))).build()).build());
182 subobjs.add(subobjBuilder.build());
184 return new EroBuilder().setSubobject(subobjs).build();
187 String getLastEroIpPrefix(final Ero ero) {
188 return ((IpPrefixCase) ero.getSubobject().get(ero.getSubobject().size() - 1).getSubobjectType()).getIpPrefix()
189 .getIpPrefix().getIpv4Prefix().getValue();
192 protected Open getLocalPref() {
193 return this.localPrefs;
196 protected Open getRemotePref() {
197 return this.remotePrefs;
200 protected PCEPSessionListener getSessionListener() {
201 return this.manager.getSessionListener();
204 protected final PCEPSessionImpl getPCEPSession(final Open localOpen, final Open remoteOpen) {
205 return this.neg.createSession(this.clientListener, localOpen, remoteOpen);