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.mockito.Matchers.any;
12 import static org.mockito.Mockito.doAnswer;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.mock;
16 import com.google.common.base.Optional;
17 import io.netty.channel.Channel;
18 import io.netty.channel.ChannelFuture;
19 import io.netty.channel.ChannelHandler;
20 import io.netty.channel.ChannelPipeline;
21 import io.netty.channel.EventLoop;
22 import io.netty.util.concurrent.Future;
23 import io.netty.util.concurrent.GenericFutureListener;
24 import io.netty.util.concurrent.Promise;
25 import java.lang.reflect.ParameterizedType;
26 import java.net.InetSocketAddress;
27 import java.net.SocketAddress;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.concurrent.ExecutionException;
31 import java.util.concurrent.TimeUnit;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.mockito.Mock;
35 import org.mockito.Mockito;
36 import org.mockito.MockitoAnnotations;
37 import org.mockito.invocation.InvocationOnMock;
38 import org.mockito.stubbing.Answer;
39 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
40 import org.opendaylight.controller.md.sal.binding.test.AbstractDataBrokerTest;
41 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
42 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
43 import org.opendaylight.protocol.pcep.PCEPSession;
44 import org.opendaylight.protocol.pcep.PCEPSessionListener;
45 import org.opendaylight.protocol.pcep.impl.DefaultPCEPSessionNegotiator;
46 import org.opendaylight.protocol.util.InetSocketAddressUtil;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.Ero;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.EroBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.IpPrefixCase;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.IpPrefixCaseBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.NetworkTopologyPcepService;
59 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
60 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
61 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
62 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
63 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
64 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
65 import org.opendaylight.yangtools.yang.binding.Notification;
67 public abstract class AbstractPCEPSessionTest<T extends TopologySessionListenerFactory> extends AbstractDataBrokerTest {
69 protected static final String TEST_TOPOLOGY_NAME = "testtopo";
70 protected static final InstanceIdentifier<Topology> TOPO_IID = InstanceIdentifier.builder(NetworkTopology.class).child(
71 Topology.class, new TopologyKey(new TopologyId(TEST_TOPOLOGY_NAME))).build();
72 protected static final String IPV4_MASK = "/32";
73 protected static final short DEAD_TIMER = 30;
74 protected static final short KEEP_ALIVE = 10;
75 protected static final int RPC_TIMEOUT = 4;
77 protected final String testAddress = InetSocketAddressUtil.getRandomLoopbackIpAddress();
78 protected final NodeId nodeId = new NodeId("pcc://" + testAddress);
79 protected final String eroIpPrefix = testAddress + IPV4_MASK;
80 protected final String newDestinationAddress = InetSocketAddressUtil.getRandomLoopbackIpAddress();
81 protected final String dstIpPrefix = newDestinationAddress + IPV4_MASK;
83 protected List<Notification> receivedMsgs;
86 private EventLoop eventLoop;
89 private Channel clientListener;
92 private ChannelPipeline pipeline;
95 private ChannelFuture channelFuture;
97 private T listenerFactory;
99 private final Open localPrefs = new OpenBuilder().setDeadTimer((short) 30).setKeepalive((short) 10).setSessionId((short) 0).build();
101 private final Open remotePrefs = localPrefs;
103 protected ServerSessionManager manager;
105 protected NetworkTopologyPcepService topologyRpcs;
107 private DefaultPCEPSessionNegotiator neg;
110 public void setUp() throws Exception {
111 MockitoAnnotations.initMocks(this);
112 this.receivedMsgs = new ArrayList<>();
113 doAnswer(new Answer<Object>() {
115 public Object answer(final InvocationOnMock invocation) {
116 final Object[] args = invocation.getArguments();
117 AbstractPCEPSessionTest.this.receivedMsgs.add((Notification) args[0]);
118 return channelFuture;
120 }).when(this.clientListener).writeAndFlush(any(Notification.class));
121 doReturn(null).when(this.channelFuture).addListener(Mockito.any());
122 doReturn("TestingChannel").when(this.clientListener).toString();
123 doReturn(this.pipeline).when(this.clientListener).pipeline();
124 doReturn(this.pipeline).when(this.pipeline).replace(any(ChannelHandler.class), any(String.class), any(ChannelHandler.class));
125 doReturn(this.eventLoop).when(this.clientListener).eventLoop();
126 doReturn(null).when(this.eventLoop).schedule(any(Runnable.class), any(long.class), any(TimeUnit.class));
127 doReturn(true).when(this.clientListener).isActive();
128 final SocketAddress ra = new InetSocketAddress(testAddress, 4189);
129 doReturn(ra).when(this.clientListener).remoteAddress();
130 final SocketAddress la = new InetSocketAddress(testAddress, InetSocketAddressUtil.getRandomPort());
131 doReturn(la).when(this.clientListener).localAddress();
133 doReturn(mock(ChannelFuture.class)).when(this.clientListener).close();
135 this.listenerFactory = (T) ((Class)((ParameterizedType)this.getClass().getGenericSuperclass()).getActualTypeArguments()[0]).newInstance();
136 this.manager = new ServerSessionManager(getDataBroker(), TOPO_IID, this.listenerFactory, RPC_TIMEOUT);
138 this.neg = new DefaultPCEPSessionNegotiator(mock(Promise.class), this.clientListener, this.manager.getSessionListener(), (short) 1, 5, this.localPrefs);
139 this.topologyRpcs = new TopologyRPCs(this.manager);
143 public void tearDown() throws TransactionCommitFailedException {
144 this.manager.close();
147 protected Optional<Topology> getTopology() throws InterruptedException, ExecutionException {
148 try (ReadOnlyTransaction t = getDataBroker().newReadOnlyTransaction()) {
149 return t.read(LogicalDatastoreType.OPERATIONAL, TOPO_IID).get();
153 protected Ero createEroWithIpPrefixes(final List<String> ipPrefixes) {
154 final List<Subobject> subobjs = new ArrayList<Subobject>(ipPrefixes.size());
155 final SubobjectBuilder subobjBuilder = new SubobjectBuilder();
156 for (final String ipPrefix : ipPrefixes) {
157 subobjBuilder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(new IpPrefixBuilder().setIpPrefix(
158 new IpPrefix(new Ipv4Prefix(ipPrefix))).build()).build());
159 subobjs.add(subobjBuilder.build());
161 return new EroBuilder().setSubobject(subobjs).build();
164 protected String getLastEroIpPrefix(final Ero ero) {
165 return ((IpPrefixCase)ero.getSubobject().get(ero.getSubobject().size() - 1).getSubobjectType()).getIpPrefix().getIpPrefix().getIpv4Prefix().getValue();
168 protected Open getLocalPref() {
169 return this.localPrefs;
172 protected Open getRemotePref() {
173 return this.remotePrefs;
176 protected PCEPSessionListener getSessionListener() {
177 return this.manager.getSessionListener();
180 protected final PCEPSession getPCEPSession(final Open localOpen, final Open remoteOpen) {
181 return neg.createSession(this.clientListener, localOpen, remoteOpen);