Removed rib dependency on linkstate.
[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.PCEPSessionListener;
44 import org.opendaylight.protocol.pcep.impl.DefaultPCEPSessionNegotiator;
45 import org.opendaylight.protocol.pcep.impl.PCEPSessionImpl;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.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.rev130820.basic.explicit.route.subobjects.subobject.type.IpPrefixCase;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.IpPrefixCaseBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.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))).toInstance();
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
81     protected List<Notification> receivedMsgs;
82
83     protected PCEPSessionImpl session;
84
85     @Mock
86     private EventLoop eventLoop;
87
88     @Mock
89     private Channel clientListener;
90
91     @Mock
92     private ChannelPipeline pipeline;
93
94     @Mock
95     private ChannelFuture channelFuture;
96
97     private T listenerFactory;
98
99     private final Open localPrefs = new OpenBuilder().setDeadTimer((short) 30).setKeepalive((short) 10).setSessionId((short) 0).build();
100
101     protected ServerSessionManager manager;
102
103     protected NetworkTopologyPcepService topologyRpcs;
104
105     @Before
106     public void setUp() throws Exception {
107         MockitoAnnotations.initMocks(this);
108         this.receivedMsgs = new ArrayList<>();
109         doAnswer(new Answer<Object>() {
110             @Override
111             public Object answer(final InvocationOnMock invocation) {
112                 final Object[] args = invocation.getArguments();
113                 AbstractPCEPSessionTest.this.receivedMsgs.add((Notification) args[0]);
114                 return channelFuture;
115             }
116         }).when(this.clientListener).writeAndFlush(any(Notification.class));
117         doReturn(null).when(this.channelFuture).addListener(Mockito.<GenericFutureListener<? extends Future<? super Void>>>any());
118         doReturn("TestingChannel").when(this.clientListener).toString();
119         doReturn(this.pipeline).when(this.clientListener).pipeline();
120         doReturn(this.pipeline).when(this.pipeline).replace(any(ChannelHandler.class), any(String.class), any(ChannelHandler.class));
121         doReturn(this.eventLoop).when(this.clientListener).eventLoop();
122         doReturn(null).when(this.eventLoop).schedule(any(Runnable.class), any(long.class), any(TimeUnit.class));
123         doReturn(true).when(this.clientListener).isActive();
124         final SocketAddress ra = new InetSocketAddress(TEST_ADDRESS, 4189);
125         doReturn(ra).when(this.clientListener).remoteAddress();
126         final SocketAddress la = new InetSocketAddress(TEST_ADDRESS, 30000);
127         doReturn(la).when(this.clientListener).localAddress();
128
129         doReturn(mock(ChannelFuture.class)).when(this.clientListener).close();
130
131         this.listenerFactory = (T) ((Class)((ParameterizedType)this.getClass().getGenericSuperclass()).getActualTypeArguments()[0]).newInstance();
132         this.manager = new ServerSessionManager(getDataBroker(), TOPO_IID, this.listenerFactory);
133
134         final DefaultPCEPSessionNegotiator neg = new DefaultPCEPSessionNegotiator(mock(Promise.class), this.clientListener, this.manager.getSessionListener(), (short) 1, 5, this.localPrefs);
135         this.session = neg.createSession(this.clientListener, getLocalPref(), getLocalPref());
136         this.topologyRpcs = new TopologyRPCs(this.manager);
137     }
138
139     @After
140     public void tearDown() throws TransactionCommitFailedException {
141         this.manager.close();
142     }
143
144     protected Optional<Topology> getTopology() throws InterruptedException, ExecutionException {
145         try (ReadOnlyTransaction t = getDataBroker().newReadOnlyTransaction()) {
146             return t.read(LogicalDatastoreType.OPERATIONAL, TOPO_IID).get();
147         }
148     }
149
150     protected Ero createEroWithIpPrefixes(final List<String> ipPrefixes) {
151         final List<Subobject> subobjs = new ArrayList<Subobject>(ipPrefixes.size());
152         final SubobjectBuilder subobjBuilder = new SubobjectBuilder();
153         for (final String ipPrefix : ipPrefixes) {
154             subobjBuilder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(new IpPrefixBuilder().setIpPrefix(
155                 new IpPrefix(new Ipv4Prefix(ipPrefix))).build()).build());
156             subobjs.add(subobjBuilder.build());
157         }
158         return new EroBuilder().setSubobject(subobjs).build();
159     }
160
161     protected String getLastEroIpPrefix(final Ero ero) {
162         return ((IpPrefixCase)ero.getSubobject().get(ero.getSubobject().size() - 1).getSubobjectType()).getIpPrefix().getIpPrefix().getIpv4Prefix().getValue();
163     }
164
165     protected Open getLocalPref() {
166         return this.localPrefs;
167     }
168
169     protected PCEPSessionListener getSessionListener() {
170         return this.manager.getSessionListener();
171     }
172 }