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