74776698cb219d469a354acd76910172fd8556a4
[ovsdb.git] / openstack / net-virt-sfc / impl / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / sfc / NetvirtSfcAclListener.java
1 /*
2  * Copyright © 2015 Dell, 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.ovsdb.openstack.netvirt.sfc;
10
11 import com.google.common.base.Preconditions;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.ovsdb.utils.mdsal.utils.MdsalUtils;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev150317.AccessLists;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev150317.access.lists.Acl;
18 import org.opendaylight.yangtools.concepts.ListenerRegistration;
19 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Data tree listener for AccessList.
25  */
26 public class NetvirtSfcAclListener extends AbstractDataTreeListener<Acl> {
27     private static final Logger LOG = LoggerFactory.getLogger(NetvirtSfcAclListener.class);
28     private ListenerRegistration<NetvirtSfcAclListener> listenerRegistration;
29
30     /**
31      * {@link NetvirtSfcAclListener} constructor.
32      * @param provider OpenFlow 1.3 Provider
33      * @param db MdSal {@link DataBroker}
34      */
35     public NetvirtSfcAclListener(final INetvirtSfcOF13Provider provider, final DataBroker db) {
36         super(provider, Acl.class);
37         Preconditions.checkNotNull(db, "DataBroker can not be null!");
38
39         registrationListener(db);
40     }
41
42     private void registrationListener(final DataBroker db) {
43         final DataTreeIdentifier<Acl> treeId =
44                 new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, getIetfAclIid());
45         try {
46             LOG.info("Registering Data Change Listener for NetvirtSfc AccessList configuration.");
47             listenerRegistration = db.registerDataTreeChangeListener(treeId, this);
48         } catch (final Exception e) {
49             LOG.warn("Netvirt AccesList DataChange listener registration fail!");
50             throw new IllegalStateException("NetvirtSfcAccessListListener startup fail! System needs restart.", e);
51         }
52     }
53
54     @Override
55     public void close() {
56         if (listenerRegistration != null) {
57             try {
58                 listenerRegistration.close();
59             } catch (final Exception e) {
60                 LOG.warn("Error while stopping IETF ACL ChangeListener: {}", e.getMessage());
61                 LOG.debug("Error while stopping IETF ACL ChangeListener..", e);
62             }
63             listenerRegistration = null;
64         }
65     }
66
67     @Override
68     public void remove(final InstanceIdentifier<Acl> identifier,
69                        final Acl removeDataObj) {
70         Preconditions.checkNotNull(removeDataObj, "Removed object can not be null!");
71         provider.removeClassifierRules(removeDataObj);
72     }
73
74     @Override
75     public void update(final InstanceIdentifier<Acl> identifier,
76                        final Acl original, final Acl update) {
77     }
78
79     @Override
80     public void add(final InstanceIdentifier<Acl> identifier,
81                     final Acl addDataObj) {
82         Preconditions.checkNotNull(addDataObj, "Added object can not be null!");
83         LOG.debug("Adding accesslist iid = {}, dataObj = {}", identifier, addDataObj);
84         provider.addClassifierRules(addDataObj);
85     }
86
87     public InstanceIdentifier<Acl> getIetfAclIid() {
88         return InstanceIdentifier.create(AccessLists.class).child(Acl.class);
89     }
90 }