Add missing license headers
[vpnservice.git] / bgpmanager / bgpmanager-impl / src / test / java / org / opendaylight / vpnservice / bgpmanager / test / MockFibManager.java
1 /*
2  * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. 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.vpnservice.bgpmanager.test;
9
10 import java.util.Collection;
11
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
14 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
15 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.FibEntries;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.fibentries.VrfTables;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.vrfentries.VrfEntry;
20 import org.opendaylight.yangtools.concepts.ListenerRegistration;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22
23 public class MockFibManager extends AbstractMockFibManager<VrfEntry> {
24
25     private int nFibEntries = 0;
26
27     private ListenerRegistration<MockFibManager> listenerRegistration;
28
29     public MockFibManager( final DataBroker db) {
30         super() ;
31         registerListener(db) ;
32     }
33
34     private void registerListener(final DataBroker db) {
35         final DataTreeIdentifier<VrfEntry> treeId = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, getWildCardPath());
36         try {
37             listenerRegistration = db.registerDataTreeChangeListener(treeId, MockFibManager.this);
38         } catch (final Exception e) {
39             throw new IllegalStateException("FibManager registration Listener fail! System needs restart.", e);
40         }
41     }
42
43     private InstanceIdentifier<VrfEntry> getWildCardPath() {
44             return InstanceIdentifier.create(FibEntries.class).child(VrfTables.class).child(VrfEntry.class);
45      }
46
47     @Override
48     public void onDataTreeChanged(Collection<DataTreeModification<VrfEntry>> changes) {
49         for (DataTreeModification<VrfEntry> change : changes) {
50             final InstanceIdentifier<VrfEntry> key = change.getRootPath().getRootIdentifier();
51             final DataObjectModification<VrfEntry> mod = change.getRootNode();
52
53                 switch (mod.getModificationType()) {
54                 case DELETE:
55                     nFibEntries -= 1;
56                     break;
57                 case WRITE:
58                     if (mod.getDataBefore() == null) {
59                         nFibEntries += 1;
60                     } else {
61                         // UPDATE COUNT UNCHANGED
62                     }
63                     break;
64                 default:
65                     throw new IllegalArgumentException("Unhandled modification  type " + mod.getModificationType());
66                 }
67             }
68      }
69
70     public int getDataChgCount() {
71         return nFibEntries;
72     }
73 }