2 * Copyright (c) 2015 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.bgp.rib.impl;
10 import static org.mockito.Matchers.any;
11 import static org.mockito.Mockito.doReturn;
13 import com.google.common.base.Optional;
14 import com.google.common.base.Throwables;
15 import com.google.common.util.concurrent.CheckedFuture;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.List;
23 import java.util.concurrent.Executor;
24 import javassist.ClassPool;
25 import org.junit.After;
26 import org.junit.Before;
27 import org.mockito.Mock;
28 import org.mockito.Mockito;
29 import org.mockito.MockitoAnnotations;
30 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
31 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
32 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
33 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
34 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
35 import org.opendaylight.controller.md.sal.dom.api.ClusteredDOMDataTreeChangeListener;
36 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
37 import org.opendaylight.controller.md.sal.dom.api.DOMDataBrokerExtension;
38 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
39 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier;
40 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
41 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
42 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeFactory;
43 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
44 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
45 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
46 import org.opendaylight.protocol.bgp.inet.RIBActivator;
47 import org.opendaylight.protocol.bgp.mode.impl.base.BasePathSelectionModeFactory;
48 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
49 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
50 import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionProviderContext;
51 import org.opendaylight.protocol.bgp.rib.spi.RIBSupport;
52 import org.opendaylight.protocol.bgp.rib.spi.SimpleRIBExtensionProviderContext;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.routes.ipv4.routes.Ipv4Route;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.BgpRib;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.RibId;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.Rib;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpId;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ClusterIdentifier;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
67 import org.opendaylight.yangtools.binding.data.codec.gen.impl.DataObjectSerializerGenerator;
68 import org.opendaylight.yangtools.binding.data.codec.gen.impl.StreamWriterGenerator;
69 import org.opendaylight.yangtools.binding.data.codec.impl.BindingNormalizedNodeCodecRegistry;
70 import org.opendaylight.yangtools.concepts.ListenerRegistration;
71 import org.opendaylight.yangtools.sal.binding.generator.api.ClassLoadingStrategy;
72 import org.opendaylight.yangtools.sal.binding.generator.impl.GeneratedClassLoadingStrategy;
73 import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
74 import org.opendaylight.yangtools.sal.binding.generator.util.BindingRuntimeContext;
75 import org.opendaylight.yangtools.sal.binding.generator.util.JavassistUtils;
76 import org.opendaylight.yangtools.yang.binding.DataObject;
77 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
78 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
79 import org.opendaylight.yangtools.yang.common.QName;
80 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
81 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
82 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
83 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
84 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
85 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
86 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
87 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
88 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
89 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
90 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
91 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
92 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
94 public class AbstractRIBTestSetup {
97 private static final ClusterIdentifier CLUSTER_ID = new ClusterIdentifier("128.0.0.1");
98 private static final BgpId RIB_ID = new BgpId("127.0.0.1");
99 protected static final Class<? extends AddressFamily> AFI = Ipv4AddressFamily.class;
100 protected static final Class<? extends SubsequentAddressFamily> SAFI = UnicastSubsequentAddressFamily.class;
101 protected static final QName AFI_QNAME = BindingReflections.findQName(AFI).intern();
102 protected static final QName SAFI_QNAME = BindingReflections.findQName(SAFI).intern();
103 protected static final TablesKey KEY = new TablesKey(AFI, SAFI);
104 private BindingCodecTreeFactory codecFactory;
105 private RIBActivator a1;
106 private RIBSupport ribSupport;
107 protected static final QName PREFIX_QNAME = QName.create(Ipv4Route.QNAME, "prefix").intern();
110 private BGPDispatcher dispatcher;
113 private DOMDataBroker dom;
116 private BindingTransactionChain chain;
119 private WriteTransaction transWrite;
122 private DOMTransactionChain domChain;
125 private DOMDataWriteTransaction domTransWrite;
128 private CheckedFuture<?,?> future;
131 private Optional<Rib> o;
134 private DOMDataTreeChangeService service;
137 private ClusterSingletonServiceProvider clusterSingletonServiceProvider;
140 public void setUp() throws Exception {
144 public void mockRib() throws Exception {
145 final RIBExtensionProviderContext context = new SimpleRIBExtensionProviderContext();
146 final ModuleInfoBackedContext strategy = createClassLoadingStrategy();
147 final SchemaContext schemaContext = strategy.tryToCreateSchemaContext().get();
148 this.codecFactory = createCodecFactory(strategy,schemaContext);
149 final List<BgpTableType> localTables = new ArrayList<>();
150 localTables.add(new BgpTableTypeImpl(AFI, SAFI));
152 this.a1 = new RIBActivator();
153 this.a1.startRIBExtensionProvider(context);
155 doReturn(Mockito.mock(ClusterSingletonServiceRegistration.class)).when(this.clusterSingletonServiceProvider)
156 .registerClusterSingletonService(any(ClusterSingletonService.class));
157 this.rib = new RIBImpl(this.clusterSingletonServiceProvider, new RibId("test"), new AsNumber(5L), RIB_ID, CLUSTER_ID, context,
158 this.dispatcher, this.codecFactory, this.dom, localTables, Collections.singletonMap(new TablesKey(AFI, SAFI),
159 BasePathSelectionModeFactory.createBestPathSelectionStrategy()), GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy(), null);
160 this.rib.onGlobalContextUpdated(schemaContext);
161 this.ribSupport = getRib().getRibSupportContext().getRIBSupportContext(KEY).getRibSupport();
164 private static ModuleInfoBackedContext createClassLoadingStrategy() {
165 final ModuleInfoBackedContext ctx = ModuleInfoBackedContext.create();
167 ctx.registerModuleInfo(BindingReflections.getModuleInfo(Ipv4Route.class));
168 } catch (final Exception e) {
169 throw Throwables.propagate(e);
174 private static BindingCodecTreeFactory createCodecFactory(final ClassLoadingStrategy str, final SchemaContext ctx) {
175 final DataObjectSerializerGenerator generator = StreamWriterGenerator.create(JavassistUtils.forClassPool(ClassPool.getDefault()));
176 final BindingNormalizedNodeCodecRegistry codec = new BindingNormalizedNodeCodecRegistry(generator);
177 codec.onBindingRuntimeContextUpdated(BindingRuntimeContext.create(str, ctx));
181 @SuppressWarnings("unchecked")
182 private void mockedMethods() throws Exception {
183 MockitoAnnotations.initMocks(this);
184 final ReadOnlyTransaction readTx = Mockito.mock(ReadOnlyTransaction.class);
185 Mockito.doReturn(new listenerRegistration()).when(this.service).registerDataTreeChangeListener(Mockito.any(DOMDataTreeIdentifier.class), Mockito.any(ClusteredDOMDataTreeChangeListener.class));
186 final Map<Class<? extends DOMDataBrokerExtension>, DOMDataBrokerExtension> map = new HashMap<>();
187 map.put(DOMDataTreeChangeService.class, this.service);
188 Mockito.doNothing().when(readTx).close();
189 final CheckedFuture<Optional<DataObject>, ReadFailedException> readFuture = Mockito.mock(CheckedFuture.class);
190 Mockito.doNothing().when(this.domTransWrite).put(Mockito.eq(LogicalDatastoreType.OPERATIONAL), Mockito.any(YangInstanceIdentifier.class), Mockito.any(NormalizedNode.class));
191 Mockito.doNothing().when(this.domTransWrite).delete(Mockito.eq(LogicalDatastoreType.OPERATIONAL), Mockito.any(YangInstanceIdentifier.class));
192 Mockito.doNothing().when(this.domTransWrite).merge(Mockito.eq(LogicalDatastoreType.OPERATIONAL), Mockito.any(YangInstanceIdentifier.class), Mockito.any(NormalizedNode.class));
193 Mockito.doReturn(Optional.<DataObject>absent()).when(readFuture).checkedGet();
194 Mockito.doReturn(readFuture).when(readTx).read(Mockito.eq(LogicalDatastoreType.OPERATIONAL), Mockito.any(InstanceIdentifier.class));
195 Mockito.doNothing().when(this.domChain).close();
196 Mockito.doReturn(this.domTransWrite).when(this.domChain).newWriteOnlyTransaction();
197 Mockito.doNothing().when(getTransaction()).put(Mockito.eq(LogicalDatastoreType.OPERATIONAL), Mockito.eq(YangInstanceIdentifier.of(BgpRib.QNAME)), Mockito.any(NormalizedNode.class));
198 Mockito.doReturn(map).when(this.dom).getSupportedExtensions();
199 Mockito.doReturn(this.domChain).when(this.dom).createTransactionChain(Mockito.any(BGPPeer.class));
200 Mockito.doReturn(this.transWrite).when(this.chain).newWriteOnlyTransaction();
201 Mockito.doReturn(false).when(this.o).isPresent();
202 Mockito.doReturn(this.o).when(this.future).checkedGet();
203 Mockito.doReturn(this.future).when(this.domTransWrite).submit();
204 Mockito.doNothing().when(this.future).addListener(Mockito.any(Runnable.class), Mockito.any(Executor.class));
205 Mockito.doNothing().when(this.transWrite).put(Mockito.eq(LogicalDatastoreType.OPERATIONAL), Mockito.any(InstanceIdentifier.class), Mockito.any(DataObject.class), Mockito.eq(true));
206 Mockito.doNothing().when(this.transWrite).put(Mockito.eq(LogicalDatastoreType.OPERATIONAL), Mockito.any(InstanceIdentifier.class), Mockito.any(DataObject.class));
207 Mockito.doReturn(this.future).when(this.transWrite).submit();
210 public Collection<DataTreeCandidate> ipv4Input(final YangInstanceIdentifier target, final ModificationType type, final Ipv4Prefix... prefix) {
211 final Collection<DataTreeCandidate> col = new HashSet<>();
212 final DataTreeCandidate candidate = Mockito.mock(DataTreeCandidate.class);
213 final DataTreeCandidateNode rootNode = Mockito.mock(DataTreeCandidateNode.class);
214 Mockito.doReturn(rootNode).when(candidate).getRootNode();
215 Mockito.doReturn(type).when(rootNode).getModificationType();
216 Mockito.doCallRealMethod().when(rootNode).toString();
217 Mockito.doReturn(target).when(candidate).getRootPath();
218 Mockito.doCallRealMethod().when(candidate).toString();
219 final Collection<DataTreeCandidateNode> children = new HashSet<>();
220 for (final Ipv4Prefix p : prefix) {
221 final NodeIdentifierWithPredicates routekey = new NodeIdentifierWithPredicates(Ipv4Route.QNAME, PREFIX_QNAME, p);
222 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> b = ImmutableNodes.mapEntryBuilder();
223 b.withNodeIdentifier(routekey);
224 b.addChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(PREFIX_QNAME)).withValue(p).build());
226 final DataTreeCandidateNode child = Mockito.mock(DataTreeCandidateNode.class);
227 Mockito.doReturn(createIdentifier(p)).when(child).getIdentifier();
228 Mockito.doReturn(Optional.of(b.build())).when(child).getDataAfter();
229 Mockito.doReturn(type).when(child).getModificationType();
232 Mockito.doReturn(children).when(rootNode).getChildNodes();
237 public PathArgument createIdentifier(final Ipv4Prefix prefix) {
238 final NodeIdentifierWithPredicates routekey = new NodeIdentifierWithPredicates(Ipv4Route.QNAME, PREFIX_QNAME, prefix);
239 return YangInstanceIdentifier.of(PREFIX_QNAME).node(routekey).getLastPathArgument();
242 public RIBImpl getRib() {
246 public DOMDataBroker getDOMBroker() {
250 public DOMDataWriteTransaction getTransaction() {
251 return this.domTransWrite;
255 public void tearDown() {
259 private class listenerRegistration implements ListenerRegistration {
261 public Object getInstance() {
266 public void close() {