f8090b4aa3f7a9cbc91d90871b45269850922617
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / AbstractRIBTestSetup.java
1 /*
2  * Copyright (c) 2015 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 package org.opendaylight.protocol.bgp.rib.impl;
9
10 import static org.mockito.Matchers.any;
11 import static org.mockito.Mockito.doReturn;
12
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;
22 import java.util.Map;
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;
93
94 public class AbstractRIBTestSetup {
95
96     private RIBImpl rib;
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();
108
109     @Mock
110     private BGPDispatcher dispatcher;
111
112     @Mock
113     private DOMDataBroker dom;
114
115     @Mock
116     private BindingTransactionChain chain;
117
118     @Mock
119     private WriteTransaction transWrite;
120
121     @Mock
122     private DOMTransactionChain domChain;
123
124     @Mock
125     private DOMDataWriteTransaction domTransWrite;
126
127     @Mock
128     private CheckedFuture<?,?> future;
129
130     @Mock
131     private Optional<Rib> o;
132
133     @Mock
134     private DOMDataTreeChangeService service;
135
136     @Mock
137     private ClusterSingletonServiceProvider clusterSingletonServiceProvider;
138
139     @Before
140     public void setUp() throws Exception {
141         mockRib();
142     }
143
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));
151
152         this.a1 = new RIBActivator();
153         this.a1.startRIBExtensionProvider(context);
154         mockedMethods();
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();
162     }
163
164     private static ModuleInfoBackedContext createClassLoadingStrategy() {
165         final ModuleInfoBackedContext ctx = ModuleInfoBackedContext.create();
166         try {
167             ctx.registerModuleInfo(BindingReflections.getModuleInfo(Ipv4Route.class));
168         } catch (final Exception e) {
169             throw Throwables.propagate(e);
170         }
171         return ctx;
172     }
173
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));
178         return codec;
179     }
180
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();
208     }
209
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());
225
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();
230             children.add(child);
231         }
232         Mockito.doReturn(children).when(rootNode).getChildNodes();
233         col.add(candidate);
234         return col;
235     }
236
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();
240     }
241
242     public RIBImpl getRib() {
243         return this.rib;
244     }
245
246     public DOMDataBroker getDOMBroker() {
247         return this.dom;
248     }
249
250     public DOMDataWriteTransaction getTransaction() {
251         return this.domTransWrite;
252     }
253
254     @After
255     public void tearDown() {
256         this.a1.close();
257     }
258
259     private class listenerRegistration implements ListenerRegistration {
260         @Override
261         public Object getInstance() {
262             return null;
263         }
264
265         @Override
266         public void close() {
267         }
268     }
269 }