Bug 1699 - FRM: Change data change listener scope from BASE to SUBTREE
[controller.git] / opendaylight / md-sal / forwardingrules-manager / src / main / java / org / opendaylight / controller / frm / impl / MeterForwarder.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 package org.opendaylight.controller.frm.impl;
9
10 import com.google.common.base.Preconditions;
11 import org.opendaylight.controller.frm.ForwardingRulesManager;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
14 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterInputBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.RemoveMeterInputBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterInputBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.meter.update.OriginalMeterBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.meter.update.UpdatedMeterBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterRef;
28 import org.opendaylight.yangtools.concepts.ListenerRegistration;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * MeterForwarder
35  * It implements {@link org.opendaylight.controller.md.sal.binding.api.DataChangeListener}}
36  * for WildCardedPath to {@link Meter} and ForwardingRulesCommiter interface for methods:
37  *  add, update and remove {@link Meter} processing for
38  *  {@link org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent}.
39  *
40  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
41  *
42  */
43 public class MeterForwarder extends AbstractListeningCommiter<Meter> {
44
45     private static final Logger LOG = LoggerFactory.getLogger(MeterForwarder.class);
46
47     private ListenerRegistration<DataChangeListener> listenerRegistration;
48
49     public MeterForwarder (final ForwardingRulesManager manager, final DataBroker db) {
50         super(manager, Meter.class);
51         Preconditions.checkNotNull(db, "DataBroker can not be null!");
52         this.listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
53                 getWildCardPath(), MeterForwarder.this, DataChangeScope.SUBTREE);
54     }
55
56     @Override
57     public void close() {
58         if (listenerRegistration != null) {
59             try {
60                 listenerRegistration.close();
61             } catch (Exception e) {
62                 LOG.error("Error by stop FRM MeterChangeListener.", e);
63             }
64             listenerRegistration = null;
65         }
66     }
67
68     @Override
69     protected InstanceIdentifier<Meter> getWildCardPath() {
70         return InstanceIdentifier.create(Nodes.class).child(Node.class)
71                 .augmentation(FlowCapableNode.class).child(Meter.class);
72     }
73
74     @Override
75     public void remove(final InstanceIdentifier<Meter> identifier, final Meter removeDataObj,
76                        final InstanceIdentifier<FlowCapableNode> nodeIdent) {
77
78         final RemoveMeterInputBuilder builder = new RemoveMeterInputBuilder(removeDataObj);
79
80         builder.setNode(new NodeRef(nodeIdent));
81         builder.setMeterRef(new MeterRef(identifier));
82         builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
83         this.provider.getSalMeterService().removeMeter(builder.build());
84     }
85
86     @Override
87     public void update(final InstanceIdentifier<Meter> identifier,
88                        final Meter original, final Meter update,
89                        final InstanceIdentifier<FlowCapableNode> nodeIdent) {
90
91         final UpdateMeterInputBuilder builder = new UpdateMeterInputBuilder();
92
93         builder.setNode(new NodeRef(nodeIdent));
94         builder.setMeterRef(new MeterRef(identifier));
95         builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
96         builder.setUpdatedMeter((new UpdatedMeterBuilder(update)).build());
97         builder.setOriginalMeter((new OriginalMeterBuilder(original)).build());
98
99         this.provider.getSalMeterService().updateMeter(builder.build());
100     }
101
102     @Override
103     public void add(final InstanceIdentifier<Meter> identifier, final Meter addDataObj,
104                     final InstanceIdentifier<FlowCapableNode> nodeIdent) {
105
106         final AddMeterInputBuilder builder = new AddMeterInputBuilder(addDataObj);
107
108         builder.setNode(new NodeRef(nodeIdent));
109         builder.setMeterRef(new MeterRef(identifier));
110         builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
111         this.provider.getSalMeterService().addMeter(builder.build());
112     }
113 }
114