Path Computation Server
[bgpcep.git] / pcep / topology / topology-provider / src / test / java / org / opendaylight / bgpcep / pcep / topology / provider / AbstractPCEPSessionTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.bgpcep.pcep.topology.provider;
10
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;
18
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.rev200120.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.rev200120.NetworkTopologyPcepService;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.Node1;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.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;
73
74 public abstract class AbstractPCEPSessionTest<T extends TopologySessionListenerFactory>
75         extends AbstractConcurrentDataBrokerTest {
76
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
88             ).build();
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;
98     @Mock
99     private EventLoop eventLoop;
100     @Mock
101     private Channel clientListener;
102     @Mock
103     private ChannelPipeline pipeline;
104     @Mock
105     private ChannelFuture channelFuture;
106     @Mock
107     private TopologySessionStatsRegistry statsRegistry;
108     @Mock
109     private PCEPTopologyProviderDependencies topologyDependencies;
110     @Mock
111     private SessionConfig sessionConfig;
112     @Mock
113     private Topology topology;
114     @Mock
115     private Promise<PCEPSessionImpl> promise;
116     private DefaultPCEPSessionNegotiator neg;
117
118     @Before
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();
142
143         doReturn(mock(ChannelFuture.class)).when(this.clientListener).close();
144
145         doReturn(getDataBroker()).when(this.topologyDependencies).getDataBroker();
146         doReturn(this.statsRegistry).when(this.topologyDependencies).getStateRegistry();
147
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)
151             .getListenAddress();
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();
156         doReturn(null).when(this.topologyDependencies).getPceServerProvider();
157
158         final PCEPTopologyConfiguration configDep = new PCEPTopologyConfiguration(this.sessionConfig, this.topology);
159         this.manager = new ServerSessionManager(this.topologyDependencies, listenerFactory, configDep);
160         startSessionManager();
161         this.neg = new DefaultPCEPSessionNegotiator(this.promise, this.clientListener,
162                 this.manager.getSessionListener(), (short) 1, 5, this.localPrefs);
163         this.topologyRpcs = new TopologyRPCs(this.manager);
164     }
165
166     void startSessionManager() throws Exception {
167         this.manager.instantiateServiceInstance();
168         checkEquals(() -> assertFalse(this.manager.isClosed.get()));
169     }
170
171     void stopSessionManager() {
172         this.manager.closeServiceInstance();
173     }
174
175     @After
176     public void tearDown() {
177         stopSessionManager();
178     }
179
180     Ero createEroWithIpPrefixes(final List<String> ipPrefixes) {
181         final List<Subobject> subobjs = new ArrayList<>(ipPrefixes.size());
182         final SubobjectBuilder subobjBuilder = new SubobjectBuilder();
183         for (final String ipPrefix : ipPrefixes) {
184             subobjBuilder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(new IpPrefixBuilder()
185                     .setIpPrefix(new IpPrefix(new Ipv4Prefix(ipPrefix))).build()).build());
186             subobjs.add(subobjBuilder.build());
187         }
188         return new EroBuilder().setSubobject(subobjs).build();
189     }
190
191     String getLastEroIpPrefix(final Ero ero) {
192         return ((IpPrefixCase) ero.getSubobject().get(ero.getSubobject().size() - 1).getSubobjectType()).getIpPrefix()
193                 .getIpPrefix().getIpv4Prefix().getValue();
194     }
195
196     protected Open getLocalPref() {
197         return this.localPrefs;
198     }
199
200     protected Open getRemotePref() {
201         return this.remotePrefs;
202     }
203
204     protected PCEPSessionListener getSessionListener() {
205         return this.manager.getSessionListener();
206     }
207
208     protected final PCEPSessionImpl getPCEPSession(final Open localOpen, final Open remoteOpen) {
209         return this.neg.createSession(this.clientListener, localOpen, remoteOpen);
210     }
211 }