c6d45fba1b4feb371149a90878cdab383cfe66d4
[groupbasedpolicy.git] / renderers / vpp / src / main / java / org / opendaylight / groupbasedpolicy / renderer / vpp / listener / RendererPolicyListener.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.groupbasedpolicy.renderer.vpp.listener;
10
11 import javax.annotation.Nonnull;
12
13 import org.opendaylight.controller.config.yang.config.vpp_provider.impl.VppRenderer;
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.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.groupbasedpolicy.renderer.vpp.event.RendererPolicyConfEvent;
19 import org.opendaylight.groupbasedpolicy.util.DataTreeChangeHandler;
20 import org.opendaylight.groupbasedpolicy.util.IidFactory;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.RendererPolicy;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import com.google.common.base.Preconditions;
27 import com.google.common.eventbus.EventBus;
28
29 public class RendererPolicyListener extends DataTreeChangeHandler<RendererPolicy> {
30
31     private static final Logger LOG = LoggerFactory.getLogger(RendererPolicyListener.class);
32     private EventBus eventBus;
33
34     public RendererPolicyListener(@Nonnull DataBroker dataProvider, @Nonnull EventBus eventBus) {
35         super(dataProvider);
36         this.eventBus = Preconditions.checkNotNull(eventBus);
37         registerDataTreeChangeListener(new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION,
38                 IidFactory.rendererIid(VppRenderer.NAME).child(RendererPolicy.class)));
39     }
40
41     @Override
42     protected void onWrite(DataObjectModification<RendererPolicy> rootNode,
43             InstanceIdentifier<RendererPolicy> rootIdentifier) {
44         RendererPolicyConfEvent event =
45                 new RendererPolicyConfEvent(rootIdentifier, rootNode.getDataBefore(), rootNode.getDataAfter());
46         LOG.trace("Dispatching event on write: {}", event.getClass());
47         eventBus.post(event);
48     }
49
50     @Override
51     protected void onDelete(DataObjectModification<RendererPolicy> rootNode,
52             InstanceIdentifier<RendererPolicy> rootIdentifier) {
53         RendererPolicyConfEvent event =
54                 new RendererPolicyConfEvent(rootIdentifier, rootNode.getDataBefore(), rootNode.getDataAfter());
55         LOG.trace("Dispatching event on delete: {}", event.getClass());
56         eventBus.post(event);
57     }
58
59     @Override
60     protected void onSubtreeModified(DataObjectModification<RendererPolicy> rootNode,
61             InstanceIdentifier<RendererPolicy> rootIdentifier) {
62         RendererPolicyConfEvent event =
63                 new RendererPolicyConfEvent(rootIdentifier, rootNode.getDataBefore(), rootNode.getDataAfter());
64         LOG.trace("Dispatching event on subtree modified: {}", event.getClass());
65         eventBus.post(event);
66     }
67
68 }