2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. 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.annotation.PostConstruct;
12 import javax.inject.Inject;
13 import javax.inject.Singleton;
14 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
18 import org.opendaylight.netvirt.aclservice.api.AclServiceManager;
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.interfaces.rev140508.Interfaces;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.InterfaceAcl;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 public class AclInterfaceListener extends AsyncDataTreeChangeListenerBase<Interface, AclInterfaceListener>
34 implements ClusteredDataTreeChangeListener<Interface> {
35 private static final Logger LOG = LoggerFactory.getLogger(AclInterfaceListener.class);
37 private final AclServiceManager aclServiceManager;
38 private final AclClusterUtil aclClusterUtil;
39 private final DataBroker dataBroker;
40 private final AclDataUtil aclDataUtil;
43 public AclInterfaceListener(AclServiceManager aclServiceManager, AclClusterUtil aclClusterUtil,
44 DataBroker dataBroker, AclDataUtil aclDataUtil) {
45 super(Interface.class, AclInterfaceListener.class);
46 this.aclServiceManager = aclServiceManager;
47 this.aclClusterUtil = aclClusterUtil;
48 this.dataBroker = dataBroker;
49 this.aclDataUtil = aclDataUtil;
55 LOG.info("{} start", getClass().getSimpleName());
56 registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
60 protected InstanceIdentifier<Interface> getWildCardPath() {
61 return InstanceIdentifier
62 .create(Interfaces.class)
63 .child(Interface.class);
67 protected void remove(InstanceIdentifier<Interface> key, Interface port) {
68 AclInterfaceCacheUtil.removeAclInterfaceFromCache(port.getName());
72 protected void update(InstanceIdentifier<Interface> key, Interface portBefore, Interface portAfter) {
74 InterfaceAcl aclInPortAfter = portAfter.getAugmentation(InterfaceAcl.class);
75 InterfaceAcl aclInPortBefore = portBefore.getAugmentation(InterfaceAcl.class);
76 if (aclInPortAfter != null && aclInPortAfter.isPortSecurityEnabled()
77 || aclInPortBefore != null && aclInPortBefore.isPortSecurityEnabled()) {
78 String interfaceId = portAfter.getName();
79 AclInterface aclInterface = null;
80 if (aclInPortBefore == null) {
81 aclInterface = addAclInterfaceToCache(interfaceId, aclInPortAfter);
83 aclInterface = updateAclInterfaceInCache(interfaceId, aclInPortAfter);
85 AclInterface oldAclInterface = getOldAclInterfaceObject(aclInterface, aclInPortBefore);
86 List<Uuid> addedAclList = AclServiceUtils.getUpdatedAclList(aclInterface.getSecurityGroups(),
87 oldAclInterface.getSecurityGroups());
88 List<Uuid> deletedAclList = AclServiceUtils.getUpdatedAclList(oldAclInterface.getSecurityGroups(),
89 aclInterface.getSecurityGroups());
90 if (addedAclList != null && !addedAclList.isEmpty()) {
91 aclDataUtil.addAclInterfaceMap(addedAclList, aclInterface);
93 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state
94 .Interface interfaceState = AclServiceUtils.getInterfaceStateFromOperDS(
95 dataBroker, portAfter.getName());
96 if (aclClusterUtil.isEntityOwner() && interfaceState != null && interfaceState.getOperStatus()
97 .equals(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508
98 .interfaces.state.Interface.OperStatus.Up)) {
99 aclServiceManager.notify(aclInterface, oldAclInterface, AclServiceManager.Action.UPDATE);
101 if (deletedAclList != null && !deletedAclList.isEmpty()) {
102 aclDataUtil.removeAclInterfaceMap(deletedAclList, aclInterface);
107 private AclInterface updateAclInterfaceInCache(String interfaceId, InterfaceAcl aclInPortAfter) {
108 AclInterface aclInterface = AclInterfaceCacheUtil.getAclInterfaceFromCache(interfaceId);
109 if (aclInterface != null) {
110 boolean portSecurityEnabled = aclInPortAfter.isPortSecurityEnabled();
111 aclInterface.setPortSecurityEnabled(portSecurityEnabled);
112 if (portSecurityEnabled) {
113 aclInterface.setSecurityGroups(aclInPortAfter.getSecurityGroups());
114 aclInterface.setAllowedAddressPairs(aclInPortAfter.getAllowedAddressPairs());
117 // Control should not come here
118 LOG.error("Unable to find Acl Interface details for {}", interfaceId);
123 private AclInterface getOldAclInterfaceObject(AclInterface aclInterface, InterfaceAcl aclInPortBefore) {
124 AclInterface oldAclInterface = new AclInterface();
125 if (aclInPortBefore == null) {
126 oldAclInterface.setPortSecurityEnabled(false);
128 oldAclInterface.setInterfaceId(aclInterface.getInterfaceId());
129 oldAclInterface.setDpId(aclInterface.getDpId());
130 oldAclInterface.setLPortTag(aclInterface.getLPortTag());
132 oldAclInterface.setPortSecurityEnabled(aclInPortBefore.isPortSecurityEnabled());
133 oldAclInterface.setAllowedAddressPairs(aclInPortBefore.getAllowedAddressPairs());
134 oldAclInterface.setSecurityGroups(aclInPortBefore.getSecurityGroups());
136 return oldAclInterface;
140 protected void add(InstanceIdentifier<Interface> key, Interface port) {
141 InterfaceAcl aclInPort = port.getAugmentation(InterfaceAcl.class);
142 if (aclInPort != null && aclInPort.isPortSecurityEnabled()) {
143 addAclInterfaceToCache(port.getName(), aclInPort);
147 private AclInterface addAclInterfaceToCache(String interfaceId, InterfaceAcl aclInPort) {
148 AclInterface aclInterface = new AclInterface();
149 aclInterface.setInterfaceId(interfaceId);
150 aclInterface.setPortSecurityEnabled(aclInPort.isPortSecurityEnabled());
151 aclInterface.setSecurityGroups(aclInPort.getSecurityGroups());
152 aclInterface.setAllowedAddressPairs(aclInPort.getAllowedAddressPairs());
153 AclInterfaceCacheUtil.addAclInterfaceToCache(interfaceId, aclInterface);
158 protected AclInterfaceListener getDataTreeChangeListener() {