Added copyright and updated appropriate log levels
[vpnservice.git] / mdsalutil / mdsalutil-impl / src / test / java / org / opendaylight / vpnservice / test / MockGroupForwarder.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.test;
9
10 import java.util.Collection;
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
16 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
17 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.yangtools.concepts.ListenerRegistration;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
22
23 public class MockGroupForwarder extends AbstractMockForwardingRulesManager<Group>{
24
25     private int nGroupCount = 0;
26     private ListenerRegistration<MockGroupForwarder> listenerRegistration ;
27
28     public MockGroupForwarder( final DataBroker db) {
29         super() ;
30         registerListener(db) ;
31     }
32
33     private void registerListener(final DataBroker db) {
34        final DataTreeIdentifier<Group> treeId = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, getWildCardPath());
35        try {
36            listenerRegistration = db.registerDataTreeChangeListener(treeId, MockGroupForwarder.this);
37        } catch (final Exception e) {
38            throw new IllegalStateException("GroupForwarder registration Listener fail! System needs restart.", e);
39        }
40     }
41
42     private InstanceIdentifier<Group> getWildCardPath() {
43         return InstanceIdentifier.create(Nodes.class).child(Node.class).
44                 augmentation(FlowCapableNode.class).child(Group.class);
45     }
46
47     @Override
48     public void onDataTreeChanged(Collection<DataTreeModification<Group>> changes) {
49         for (DataTreeModification<Group> change : changes) {
50             final InstanceIdentifier<Group> key = change.getRootPath().getRootIdentifier();
51             final DataObjectModification<Group> mod = change.getRootNode();
52
53                 switch (mod.getModificationType()) {
54                 case DELETE:
55                     nGroupCount -= 1;
56                     break;
57                 case SUBTREE_MODIFIED:
58                     // CHECK IF RQD
59                     break;
60                 case WRITE:
61                     if (mod.getDataBefore() == null) {
62                         nGroupCount += 1;
63                     } else {
64                         // UPDATE COUNT UNCHANGED
65                     }
66                     break;
67                 default:
68                     throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
69                 }
70             }
71      }
72
73     public int getDataChgCount() {
74         return nGroupCount;
75     }
76 }