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
8 package org.opendaylight.protocol.pcep.impl;
10 import static org.mockito.ArgumentMatchers.any;
11 import static org.mockito.Mockito.doAnswer;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.mock;
15 import io.netty.channel.Channel;
16 import io.netty.channel.ChannelFuture;
17 import io.netty.channel.ChannelHandler;
18 import io.netty.channel.ChannelPipeline;
19 import io.netty.channel.DefaultChannelPromise;
20 import io.netty.channel.EventLoop;
21 import io.netty.util.concurrent.GenericFutureListener;
22 import io.netty.util.concurrent.ScheduledFuture;
23 import java.net.InetSocketAddress;
24 import java.net.SocketAddress;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.concurrent.TimeUnit;
28 import org.junit.Before;
29 import org.mockito.Mock;
30 import org.mockito.MockitoAnnotations;
31 import org.opendaylight.protocol.util.InetSocketAddressUtil;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.Close;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.CloseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.Keepalive;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.KeepaliveBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.Open;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.OpenBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.Starttls;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.StarttlsBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.close.message.CCloseMessageBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.close.object.CCloseBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.keepalive.message.KeepaliveMessageBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.message.OpenMessageBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.start.tls.message.StartTlsMessageBuilder;
45 import org.opendaylight.yangtools.yang.binding.Notification;
46 import org.opendaylight.yangtools.yang.common.Uint8;
48 public class AbstractPCEPSessionTest {
50 protected static final Uint8 KEEP_ALIVE = Uint8.valueOf(15);
51 protected static final Uint8 DEADTIMER = Uint8.valueOf(40);
54 protected Channel channel;
57 private ChannelFuture channelFuture;
60 private EventLoop eventLoop;
63 private ScheduledFuture<?> future;
66 private ChannelPipeline pipeline;
69 private SocketAddress address;
71 protected final String ipAddress = InetSocketAddressUtil.getRandomLoopbackIpAddress();
72 protected final int port = InetSocketAddressUtil.getRandomPort();
73 protected final List<Notification> msgsSend = new ArrayList<>();
75 protected Open openMsg;
77 protected Close closeMsg;
79 protected Starttls startTlsMsg;
81 protected Keepalive kaMsg;
83 protected SimpleSessionListener listener;
85 @SuppressWarnings("unchecked")
87 public final void setUp() {
88 MockitoAnnotations.initMocks(this);
89 final ChannelFuture cfuture = new DefaultChannelPromise(this.channel);
90 doAnswer(invocation -> {
91 final Object[] args = invocation.getArguments();
92 AbstractPCEPSessionTest.this.msgsSend.add((Notification) args[0]);
94 }).when(this.channel).writeAndFlush(any(Notification.class));
95 doReturn(this.channelFuture).when(this.channel).closeFuture();
96 doReturn(this.channelFuture).when(this.channelFuture).addListener(any(GenericFutureListener.class));
97 doReturn("TestingChannel").when(this.channel).toString();
98 doReturn(this.pipeline).when(this.channel).pipeline();
99 doReturn(this.address).when(this.channel).localAddress();
100 doReturn(this.address).when(this.channel).remoteAddress();
101 doReturn(this.eventLoop).when(this.channel).eventLoop();
102 doReturn(true).when(this.future).cancel(false);
103 doReturn(this.future).when(this.eventLoop).schedule(any(Runnable.class), any(long.class), any(TimeUnit.class));
104 doReturn(this.pipeline).when(this.pipeline).replace(any(ChannelHandler.class), any(String.class),
105 any(ChannelHandler.class));
106 doReturn(this.pipeline).when(this.pipeline).addFirst(any(ChannelHandler.class));
107 doReturn(true).when(this.channel).isActive();
108 doReturn(mock(ChannelFuture.class)).when(this.channel).close();
109 doReturn(new InetSocketAddress(this.ipAddress, this.port)).when(this.channel).remoteAddress();
110 doReturn(new InetSocketAddress(this.ipAddress, this.port)).when(this.channel).localAddress();
111 this.openMsg = new OpenBuilder()
112 .setOpenMessage(new OpenMessageBuilder()
113 .setOpen(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109
114 .open.object.OpenBuilder()
115 .setDeadTimer(DEADTIMER)
116 .setKeepalive(KEEP_ALIVE)
117 .setSessionId(Uint8.ZERO)
121 this.kaMsg = new KeepaliveBuilder().setKeepaliveMessage(new KeepaliveMessageBuilder().build()).build();
122 this.startTlsMsg = new StarttlsBuilder().setStartTlsMessage(new StartTlsMessageBuilder().build()).build();
123 this.closeMsg = new CloseBuilder().setCCloseMessage(new CCloseMessageBuilder()
124 .setCClose(new CCloseBuilder().setReason(Uint8.valueOf(6)).build()).build()).build();
126 this.listener = new SimpleSessionListener();