2 * Copyright (c) 2016 Red Hat, 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
8 package org.opendaylight.netvirt.aclservice.listeners;
10 import java.util.List;
11 import javax.inject.Inject;
12 import javax.inject.Singleton;
13 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
17 import org.opendaylight.netvirt.aclservice.api.AclServiceManager;
18 import org.opendaylight.netvirt.aclservice.api.AclServiceManager.Action;
19 import org.opendaylight.netvirt.aclservice.api.utils.AclInterface;
20 import org.opendaylight.netvirt.aclservice.api.utils.AclInterfaceCacheUtil;
21 import org.opendaylight.netvirt.aclservice.utils.AclClusterUtil;
22 import org.opendaylight.netvirt.aclservice.utils.AclDataUtil;
23 import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 public class AclInterfaceStateListener extends AsyncDataTreeChangeListenerBase<Interface,
35 AclInterfaceStateListener> implements ClusteredDataTreeChangeListener<Interface>, AutoCloseable {
37 private static final Logger LOG = LoggerFactory.getLogger(AclInterfaceStateListener.class);
39 /** Our registration. */
40 public static final TopologyId OVSDB_TOPOLOGY_ID = new TopologyId(new Uri("ovsdb:1"));
41 public static final String EXTERNAL_ID_INTERFACE_ID = "iface-id";
43 private final AclServiceManager aclServiceManger;
44 private final AclClusterUtil aclClusterUtil;
45 private final DataBroker dataBroker;
48 public AclInterfaceStateListener(AclServiceManager aclServiceManger, AclClusterUtil aclClusterUtil,
49 DataBroker dataBroker) {
50 super(Interface.class, AclInterfaceStateListener.class);
51 this.aclServiceManger = aclServiceManger;
52 this.aclClusterUtil = aclClusterUtil;
53 this.dataBroker = dataBroker;
58 LOG.info("{} start", getClass().getSimpleName());
59 registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
63 protected InstanceIdentifier<Interface> getWildCardPath() {
64 return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
68 protected void remove(InstanceIdentifier<Interface> key, Interface dataObjectModification) {
69 String interfaceId = dataObjectModification.getName();
70 AclInterface aclInterface = AclInterfaceCacheUtil.getAclInterfaceFromCache(interfaceId);
71 if (isOfInterest(aclInterface)) {
72 if (aclClusterUtil.isEntityOwner()) {
73 aclServiceManger.notify(aclInterface, null, Action.REMOVE);
75 List<Uuid> aclList = aclInterface.getSecurityGroups();
76 if (aclList != null) {
77 AclDataUtil.removeAclInterfaceMap(aclList, aclInterface);
79 AclInterfaceCacheUtil.removeAclInterfaceFromCache(interfaceId);
84 protected void update(InstanceIdentifier<Interface> key, Interface dataObjectModificationBefore,
85 Interface dataObjectModificationAfter) {
87 * The update is not of interest as the attributes populated from this listener will not change.
88 * The northbound updates are handled in AclInterfaceListener.
93 protected void add(InstanceIdentifier<Interface> key, Interface dataObjectModification) {
94 AclInterface aclInterface = updateAclInterfaceCache(dataObjectModification);
95 if (isOfInterest(aclInterface)) {
96 List<Uuid> aclList = aclInterface.getSecurityGroups();
97 if (aclList != null) {
98 AclDataUtil.addAclInterfaceMap(aclList, aclInterface);
100 if (aclClusterUtil.isEntityOwner()) {
101 aclServiceManger.notify(aclInterface, null, Action.ADD);
106 private boolean isOfInterest(AclInterface aclInterface) {
107 return aclInterface != null && aclInterface.getPortSecurityEnabled() != null
108 && aclInterface.isPortSecurityEnabled();
112 protected AclInterfaceStateListener getDataTreeChangeListener() {
113 return AclInterfaceStateListener.this;
116 private AclInterface updateAclInterfaceCache(Interface dataObjectModification) {
117 String interfaceId = dataObjectModification.getName();
118 AclInterface aclInterface = AclInterfaceCacheUtil.getAclInterfaceFromCache(interfaceId);
119 if (aclInterface == null) {
120 aclInterface = new AclInterface();
121 AclInterfaceCacheUtil.addAclInterfaceToCache(interfaceId, aclInterface);
123 aclInterface.setDpId(AclServiceUtils.getDpIdFromIterfaceState(dataObjectModification));
124 aclInterface.setLPortTag(dataObjectModification.getIfIndex());
125 aclInterface.setIsMarkedForDelete(false);