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.IpAddressNoZone;
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.Ipv4AddressNoZone;
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.rev181109.explicit.route.object.Ero;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.EroBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.ero.Subobject;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.ero.SubobjectBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.object.Open;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.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.rev181109.NetworkTopologyPcepService;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.Node1;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.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;
71 import org.opendaylight.yangtools.yang.common.Uint16;
72 import org.opendaylight.yangtools.yang.common.Uint8;
74 public abstract class AbstractPCEPSessionTest<T extends TopologySessionListenerFactory>
75 extends AbstractConcurrentDataBrokerTest {
77 static final short DEAD_TIMER = 30;
78 static final short KEEP_ALIVE = 10;
79 static final short RPC_TIMEOUT = 4;
80 private static final TopologyId TEST_TOPOLOGY_ID = new TopologyId("testtopo");
81 static final InstanceIdentifier<Topology> TOPO_IID = InstanceIdentifier.builder(NetworkTopology.class)
82 .child(Topology.class, new TopologyKey(TEST_TOPOLOGY_ID)).build();
83 private static final String IPV4_MASK = "/32";
84 final String testAddress = InetSocketAddressUtil.getRandomLoopbackIpAddress();
85 final NodeId nodeId = new NodeId("pcc://" + this.testAddress);
86 protected final InstanceIdentifier<PathComputationClient> pathComputationClientIId = TOPO_IID.builder()
87 .child(Node.class, new NodeKey(this.nodeId)).augmentation(Node1.class).child(PathComputationClient.class
89 final String eroIpPrefix = this.testAddress + IPV4_MASK;
90 final String newDestinationAddress = InetSocketAddressUtil.getRandomLoopbackIpAddress();
91 final String dstIpPrefix = this.newDestinationAddress + IPV4_MASK;
92 private final Open localPrefs = new OpenBuilder().setDeadTimer(Uint8.valueOf(30)).setKeepalive(Uint8.valueOf(10))
93 .setSessionId(Uint8.ZERO).build();
94 private final Open remotePrefs = this.localPrefs;
95 List<Notification> receivedMsgs;
96 ServerSessionManager manager;
97 NetworkTopologyPcepService topologyRpcs;
99 private EventLoop eventLoop;
101 private Channel clientListener;
103 private ChannelPipeline pipeline;
105 private ChannelFuture channelFuture;
107 private TopologySessionStatsRegistry statsRegistry;
109 private PCEPTopologyProviderDependencies topologyDependencies;
111 private SessionConfig sessionConfig;
113 private Topology topology;
115 private Promise<PCEPSessionImpl> promise;
116 private DefaultPCEPSessionNegotiator neg;
119 public void setUp() throws Exception {
120 MockitoAnnotations.initMocks(this);
121 this.receivedMsgs = new ArrayList<>();
122 doAnswer(invocation -> {
123 final Object[] args = invocation.getArguments();
124 AbstractPCEPSessionTest.this.receivedMsgs.add((Notification) args[0]);
125 return this.channelFuture;
126 }).when(this.clientListener).writeAndFlush(any(Notification.class));
127 doReturn(null).when(this.channelFuture).addListener(Mockito.any());
128 doReturn("TestingChannel").when(this.clientListener).toString();
129 doReturn(this.pipeline).when(this.clientListener).pipeline();
130 doReturn(this.pipeline).when(this.pipeline).replace(any(ChannelHandler.class), any(String.class),
131 any(ChannelHandler.class));
132 doReturn(this.eventLoop).when(this.clientListener).eventLoop();
133 doNothing().when(this.statsRegistry).bind(any(), any());
134 doNothing().when(this.statsRegistry).unbind(any());
135 doReturn(null).when(this.eventLoop).schedule(any(Runnable.class), any(long.class),
136 any(TimeUnit.class));
137 doReturn(true).when(this.clientListener).isActive();
138 final InetSocketAddress ra = new InetSocketAddress(this.testAddress, 4189);
139 doReturn(ra).when(this.clientListener).remoteAddress();
140 final InetSocketAddress la = new InetSocketAddress(this.testAddress, InetSocketAddressUtil.getRandomPort());
141 doReturn(la).when(this.clientListener).localAddress();
143 doReturn(mock(ChannelFuture.class)).when(this.clientListener).close();
145 doReturn(getDataBroker()).when(this.topologyDependencies).getDataBroker();
146 doReturn(this.statsRegistry).when(this.topologyDependencies).getStateRegistry();
148 @SuppressWarnings("unchecked") final T listenerFactory = (T) ((Class) ((ParameterizedType) this.getClass()
149 .getGenericSuperclass()).getActualTypeArguments()[0]).newInstance();
150 doReturn(new IpAddressNoZone(new Ipv4AddressNoZone(this.testAddress))).when(this.sessionConfig)
152 doReturn(new PortNumber(Uint16.valueOf(4189))).when(this.sessionConfig).getListenPort();
153 doReturn(RPC_TIMEOUT).when(this.sessionConfig).getRpcTimeout();
154 doReturn(TEST_TOPOLOGY_ID).when(this.topology).getTopologyId();
155 doReturn(Collections.emptyList()).when(this.topology).getNode();
157 final PCEPTopologyConfiguration configDep = new PCEPTopologyConfiguration(this.sessionConfig, this.topology);
158 this.manager = new ServerSessionManager(this.topologyDependencies, listenerFactory, configDep);
159 startSessionManager();
160 this.neg = new DefaultPCEPSessionNegotiator(this.promise, this.clientListener,
161 this.manager.getSessionListener(), (short) 1, 5, this.localPrefs);
162 this.topologyRpcs = new TopologyRPCs(this.manager);
165 void startSessionManager() throws Exception {
166 this.manager.instantiateServiceInstance();
167 checkEquals(() -> assertFalse(this.manager.isClosed.get()));
170 void stopSessionManager() {
171 this.manager.closeServiceInstance();
175 public void tearDown() {
176 stopSessionManager();
179 Ero createEroWithIpPrefixes(final List<String> ipPrefixes) {
180 final List<Subobject> subobjs = new ArrayList<>(ipPrefixes.size());
181 final SubobjectBuilder subobjBuilder = new SubobjectBuilder();
182 for (final String ipPrefix : ipPrefixes) {
183 subobjBuilder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(new IpPrefixBuilder()
184 .setIpPrefix(new IpPrefix(new Ipv4Prefix(ipPrefix))).build()).build());
185 subobjs.add(subobjBuilder.build());
187 return new EroBuilder().setSubobject(subobjs).build();
190 String getLastEroIpPrefix(final Ero ero) {
191 return ((IpPrefixCase) ero.getSubobject().get(ero.getSubobject().size() - 1).getSubobjectType()).getIpPrefix()
192 .getIpPrefix().getIpv4Prefix().getValue();
195 protected Open getLocalPref() {
196 return this.localPrefs;
199 protected Open getRemotePref() {
200 return this.remotePrefs;
203 protected PCEPSessionListener getSessionListener() {
204 return this.manager.getSessionListener();
207 protected final PCEPSessionImpl getPCEPSession(final Open localOpen, final Open remoteOpen) {
208 return this.neg.createSession(this.clientListener, localOpen, remoteOpen);