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