b72da6a09055c94be5fd8db9b61949690206976b
[bgpcep.git] / pcep / 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.mockito.Matchers.any;
12 import static org.mockito.Mockito.doAnswer;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.mock;
15
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.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
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.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.Ero;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.EroBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.IpPrefixCase;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.IpPrefixCaseBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.NetworkTopologyPcepService;
58 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
59 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
60 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
61 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
62 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
63 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
64 import org.opendaylight.yangtools.yang.binding.Notification;
65
66 public abstract class AbstractPCEPSessionTest<T extends TopologySessionListenerFactory> extends AbstractDataBrokerTest {
67
68     protected static final String TEST_TOPOLOGY_NAME = "testtopo";
69     protected static final InstanceIdentifier<Topology> TOPO_IID = InstanceIdentifier.builder(NetworkTopology.class).child(
70             Topology.class, new TopologyKey(new TopologyId(TEST_TOPOLOGY_NAME))).build();
71     protected static final String TEST_ADDRESS = "127.0.0.1";
72     protected static final NodeId NODE_ID = new NodeId("pcc://" + TEST_ADDRESS);
73     protected static final String TEST_LSP_NAME = "tunnel0";
74     protected static final String IPV4_MASK = "/32";
75     protected static final String ERO_IP_PREFIX = TEST_ADDRESS + IPV4_MASK;
76     protected static final String NEW_DESTINATION_ADDRESS = "127.0.1.0";
77     protected static final String DST_IP_PREFIX = NEW_DESTINATION_ADDRESS + IPV4_MASK;
78     protected static final short DEAD_TIMER = 30;
79     protected static final short KEEP_ALIVE = 10;
80     protected static final int RPC_TIMEOUT = 4;
81
82     protected List<Notification> receivedMsgs;
83
84     @Mock
85     private EventLoop eventLoop;
86
87     @Mock
88     private Channel clientListener;
89
90     @Mock
91     private ChannelPipeline pipeline;
92
93     @Mock
94     private ChannelFuture channelFuture;
95
96     private T listenerFactory;
97
98     private final Open localPrefs = new OpenBuilder().setDeadTimer((short) 30).setKeepalive((short) 10).setSessionId((short) 0).build();
99
100     private final Open remotePrefs = localPrefs;
101
102     protected ServerSessionManager manager;
103
104     protected NetworkTopologyPcepService topologyRpcs;
105
106     private DefaultPCEPSessionNegotiator neg;
107
108     @Before
109     public void setUp() throws Exception {
110         MockitoAnnotations.initMocks(this);
111         this.receivedMsgs = new ArrayList<>();
112         doAnswer(new Answer<Object>() {
113             @Override
114             public Object answer(final InvocationOnMock invocation) {
115                 final Object[] args = invocation.getArguments();
116                 AbstractPCEPSessionTest.this.receivedMsgs.add((Notification) args[0]);
117                 return channelFuture;
118             }
119         }).when(this.clientListener).writeAndFlush(any(Notification.class));
120         doReturn(null).when(this.channelFuture).addListener(Mockito.<GenericFutureListener<? extends Future<? super Void>>>any());
121         doReturn("TestingChannel").when(this.clientListener).toString();
122         doReturn(this.pipeline).when(this.clientListener).pipeline();
123         doReturn(this.pipeline).when(this.pipeline).replace(any(ChannelHandler.class), any(String.class), any(ChannelHandler.class));
124         doReturn(this.eventLoop).when(this.clientListener).eventLoop();
125         doReturn(null).when(this.eventLoop).schedule(any(Runnable.class), any(long.class), any(TimeUnit.class));
126         doReturn(true).when(this.clientListener).isActive();
127         final SocketAddress ra = new InetSocketAddress(TEST_ADDRESS, 4189);
128         doReturn(ra).when(this.clientListener).remoteAddress();
129         final SocketAddress la = new InetSocketAddress(TEST_ADDRESS, 30000);
130         doReturn(la).when(this.clientListener).localAddress();
131
132         doReturn(mock(ChannelFuture.class)).when(this.clientListener).close();
133
134         this.listenerFactory = (T) ((Class)((ParameterizedType)this.getClass().getGenericSuperclass()).getActualTypeArguments()[0]).newInstance();
135         this.manager = new ServerSessionManager(getDataBroker(), TOPO_IID, this.listenerFactory, RPC_TIMEOUT);
136
137         this.neg = new DefaultPCEPSessionNegotiator(mock(Promise.class), this.clientListener, this.manager.getSessionListener(), (short) 1, 5, this.localPrefs);
138         this.topologyRpcs = new TopologyRPCs(this.manager);
139     }
140
141     @After
142     public void tearDown() throws TransactionCommitFailedException {
143         this.manager.close();
144     }
145
146     protected Optional<Topology> getTopology() throws InterruptedException, ExecutionException {
147         try (ReadOnlyTransaction t = getDataBroker().newReadOnlyTransaction()) {
148             return t.read(LogicalDatastoreType.OPERATIONAL, TOPO_IID).get();
149         }
150     }
151
152     protected Ero createEroWithIpPrefixes(final List<String> ipPrefixes) {
153         final List<Subobject> subobjs = new ArrayList<Subobject>(ipPrefixes.size());
154         final SubobjectBuilder subobjBuilder = new SubobjectBuilder();
155         for (final String ipPrefix : ipPrefixes) {
156             subobjBuilder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(new IpPrefixBuilder().setIpPrefix(
157                 new IpPrefix(new Ipv4Prefix(ipPrefix))).build()).build());
158             subobjs.add(subobjBuilder.build());
159         }
160         return new EroBuilder().setSubobject(subobjs).build();
161     }
162
163     protected String getLastEroIpPrefix(final Ero ero) {
164         return ((IpPrefixCase)ero.getSubobject().get(ero.getSubobject().size() - 1).getSubobjectType()).getIpPrefix().getIpPrefix().getIpv4Prefix().getValue();
165     }
166
167     protected Open getLocalPref() {
168         return this.localPrefs;
169     }
170
171     protected Open getRemotePref() {
172         return this.remotePrefs;
173     }
174
175     protected PCEPSessionListener getSessionListener() {
176         return this.manager.getSessionListener();
177     }
178
179     protected final PCEPSession getPCEPSession(final Open localOpen, final Open remoteOpen) {
180         return neg.createSession(this.clientListener, localOpen, remoteOpen);
181     }
182 }