2 * Copyright © 2015 Dell, Inc. and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.openstack.netvirt.sfc;
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;
30 * Data tree listener for Classifier.
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;
40 * {@link NetvirtSfcClassifierListener} constructor.
41 * @param provider OpenFlow 1.3 Provider
42 * @param db MdSal {@link DataBroker}
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);
51 private void registrationListener(final DataBroker db) {
52 final DataTreeIdentifier<Classifier> treeId =
53 new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, getClassifierIid());
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);
66 if (listenerRegistration != null) {
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);
73 listenerRegistration = null;
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));
85 LOG.debug("IETF ACL with name ={} is not yet configured. skip this operation", aclName);
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);
100 public void update(final InstanceIdentifier<Classifier> identifier,
101 final Classifier original, final Classifier update) {
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));
115 LOG.debug("IETF ACL with name ={} is not yet configured. skip this operation", aclName);
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);
129 public InstanceIdentifier<Classifier> getClassifierIid() {
130 return InstanceIdentifier.create(Classifiers.class).child(Classifier.class);
133 private InstanceIdentifier<Acl> getIetfAclIid(String aclName) {
134 return InstanceIdentifier.create(AccessLists.class).child(Acl.class, new AclKey(aclName));