Fix netvirtsfc flows
[ovsdb.git] / openstack / net-virt-sfc / impl / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / sfc / NetvirtSfcClassifierListener.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.openstack.netvirt.sfc.openflow13.INetvirtSfcOF13Provider;
16 import org.opendaylight.ovsdb.utils.mdsal.utils.MdsalUtils;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev150317.AccessLists;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev150317.access.lists.Acl;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev150317.access.lists.AclKey;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.sfc.classifier.rev150105.Classifiers;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.sfc.classifier.rev150105.classifiers.Classifier;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.sfc.classifier.rev150105.classifiers.classifier.bridges.Bridge;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.sfc.classifier.rev150105.classifiers.classifier.sffs.Sff;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Data tree listener for Classifier.
31  *
32  * @author Arun Yerra
33  */
34 public class NetvirtSfcClassifierListener extends AbstractDataTreeListener<Classifier> {
35     private static final Logger LOG = LoggerFactory.getLogger(NetvirtSfcClassifierListener.class);
36     private MdsalUtils dbutils;
37     private ListenerRegistration<NetvirtSfcClassifierListener> listenerRegistration;
38
39     /**
40      * {@link NetvirtSfcClassifierListener} constructor.
41      * @param provider OpenFlow 1.3 Provider
42      * @param db MdSal {@link DataBroker}
43      */
44     public NetvirtSfcClassifierListener(final INetvirtSfcOF13Provider provider, final DataBroker db) {
45         super(provider, Classifier.class);
46         Preconditions.checkNotNull(db, "DataBroker can not be null!");
47         dbutils = new MdsalUtils(db);
48         registrationListener(db);
49     }
50
51     private void registrationListener(final DataBroker db) {
52         final DataTreeIdentifier<Classifier> treeId =
53                 new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, getClassifierIid());
54         try {
55             LOG.info("Registering Data Change Listener for NetvirtSfc Classifier configuration.");
56             listenerRegistration = db.registerDataTreeChangeListener(treeId, this);
57         } catch (final Exception e) {
58             LOG.warn("Netvirt Classifier DataChange listener registration fail!");
59             LOG.debug("Netvirt Classifier DataChange listener registration fail!", e);
60             throw new IllegalStateException("NetvirtSfcClassifierListener startup fail! System needs restart.", e);
61         }
62     }
63
64     @Override
65     public void close() {
66         if (listenerRegistration != null) {
67             try {
68                 listenerRegistration.close();
69             } catch (final Exception e) {
70                 LOG.warn("Error to stop Netvirt Classifier DataChange listener: {}", e.getMessage());
71                 LOG.debug("Error to stop Netvirt Classifier DataChange listener..", e);
72             }
73             listenerRegistration = null;
74         }
75     }
76
77     @Override
78     public void remove(final InstanceIdentifier<Classifier> identifier,
79                        final Classifier removeDataObj) {
80         Preconditions.checkNotNull(removeDataObj, "Added object can not be null!");
81         String aclName = removeDataObj.getAcl();
82         // Read the ACL information from data store and make sure it exists.
83         Acl acl = dbutils.read(LogicalDatastoreType.CONFIGURATION, getIetfAclIid(aclName));
84         if (acl == null) {
85             LOG.debug("IETF ACL with name ={} is not yet configured. skip this operation", aclName);
86             return;
87         }
88
89         if (removeDataObj.getSffs() != null) {
90             for (Sff sff : removeDataObj.getSffs().getSff()) {
91                 // Netvirt classifier binds an ACL with service function forwarder that is identified by SFF name.
92                 // SFF validation can be done with SFC Provider APIs, as SFF is configured within SFC project.  
93                 // Netvirt SFC provider will validate the SFF using SFC provider APIs.
94                 provider.removeClassifierRules(sff, acl);
95             }
96         }
97     }
98
99     @Override
100     public void update(final InstanceIdentifier<Classifier> identifier,
101                        final Classifier original, final Classifier update) {
102         //TODO
103
104     }
105
106     @Override
107     public void add(final InstanceIdentifier<Classifier> identifier,
108                     final Classifier addDataObj) {
109         Preconditions.checkNotNull(addDataObj, "Added object can not be null!");
110         String aclName = addDataObj.getAcl();
111         LOG.debug("Adding classifier iid = {}, dataObj = {}", identifier, addDataObj);
112         // Read the ACL information from data store and make sure it exists.
113         Acl acl = dbutils.read(LogicalDatastoreType.CONFIGURATION,getIetfAclIid(aclName));
114         if (acl == null) {
115             LOG.debug("IETF ACL with name ={} is not yet configured. skip this operation", aclName);
116             return;
117         }
118
119         if (addDataObj.getBridges() != null) {
120             for (Bridge bridge : addDataObj.getBridges().getBridge()) {
121                 // Netvirt classifier binds an ACL with service function forwarder that is identified by SFF name.
122                 // SFF validation can be done with SFC Provider APIs, as SFF is configured within SFC project.  
123                 // Netvirt SFC provider will validate the SFF using SFC provider APIs.
124                 provider.addClassifierRules(bridge, acl);
125             }
126         }
127     }
128
129     public InstanceIdentifier<Classifier> getClassifierIid() {
130         return InstanceIdentifier.create(Classifiers.class).child(Classifier.class);
131     }
132
133     private InstanceIdentifier<Acl> getIetfAclIid(String aclName) {
134         return InstanceIdentifier.create(AccessLists.class).child(Acl.class, new AclKey(aclName));
135     }
136 }