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.annotation.PreDestroy;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
19 import org.opendaylight.netvirt.aclservice.api.AclServiceManager;
20 import org.opendaylight.netvirt.aclservice.api.utils.AclInterface;
21 import org.opendaylight.netvirt.aclservice.api.utils.AclInterfaceCacheUtil;
22 import org.opendaylight.netvirt.aclservice.utils.AclClusterUtil;
23 import org.opendaylight.netvirt.aclservice.utils.AclDataUtil;
24 import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.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.opendaylight.netvirt.aclservice.rev160608.InterfaceAcl;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 public class AclInterfaceListener extends AsyncDataTreeChangeListenerBase<Interface, AclInterfaceListener>
35 implements ClusteredDataTreeChangeListener<Interface> {
36 private static final Logger LOG = LoggerFactory.getLogger(AclInterfaceListener.class);
38 private final AclServiceManager aclServiceManager;
39 private final AclClusterUtil aclClusterUtil;
40 private final DataBroker dataBroker;
43 public AclInterfaceListener(AclServiceManager aclServiceManager, AclClusterUtil aclClusterUtil,
44 DataBroker dataBroker) {
45 super(Interface.class, AclInterfaceListener.class);
46 this.aclServiceManager = aclServiceManager;
47 this.aclClusterUtil = aclClusterUtil;
48 this.dataBroker = dataBroker;
53 LOG.info("{} start", getClass().getSimpleName());
54 registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
59 public void close() throws Exception {
64 protected InstanceIdentifier<Interface> getWildCardPath() {
65 return InstanceIdentifier
66 .create(Interfaces.class)
67 .child(Interface.class);
71 protected void remove(InstanceIdentifier<Interface> key, Interface port) {
72 AclInterfaceCacheUtil.removeAclInterfaceFromCache(port.getName());
76 protected void update(InstanceIdentifier<Interface> key, Interface portBefore, Interface portAfter) {
78 InterfaceAcl aclInPortAfter = portAfter.getAugmentation(InterfaceAcl.class);
79 InterfaceAcl aclInPortBefore = portBefore.getAugmentation(InterfaceAcl.class);
80 if (aclInPortAfter != null && aclInPortAfter.isPortSecurityEnabled()
81 || aclInPortBefore != null && aclInPortBefore.isPortSecurityEnabled()) {
82 String interfaceId = portAfter.getName();
83 AclInterface aclInterface = null;
84 if (aclInPortBefore == null) {
85 aclInterface = addAclInterfaceToCache(interfaceId, aclInPortAfter);
87 aclInterface = updateAclInterfaceInCache(interfaceId, aclInPortAfter);
89 AclInterface oldAclInterface = getOldAclInterfaceObject(aclInterface, aclInPortBefore);
90 List<Uuid> addedAclList = AclServiceUtils.getUpdatedAclList(aclInterface.getSecurityGroups(),
91 oldAclInterface.getSecurityGroups());
92 List<Uuid> deletedAclList = AclServiceUtils.getUpdatedAclList(oldAclInterface.getSecurityGroups(),
93 aclInterface.getSecurityGroups());
94 if (addedAclList != null && !addedAclList.isEmpty()) {
95 AclDataUtil.addAclInterfaceMap(addedAclList, aclInterface);
97 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state
98 .Interface interfaceState = AclServiceUtils.getInterfaceStateFromOperDS(
99 dataBroker, portAfter.getName());
100 if (aclClusterUtil.isEntityOwner() && interfaceState != null && interfaceState.getOperStatus()
101 .equals(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508
102 .interfaces.state.Interface.OperStatus.Up)) {
103 aclServiceManager.notify(aclInterface, oldAclInterface, AclServiceManager.Action.UPDATE);
105 if (deletedAclList != null && !deletedAclList.isEmpty()) {
106 AclDataUtil.removeAclInterfaceMap(deletedAclList, aclInterface);
111 private AclInterface updateAclInterfaceInCache(String interfaceId, InterfaceAcl aclInPortAfter) {
112 AclInterface aclInterface = AclInterfaceCacheUtil.getAclInterfaceFromCache(interfaceId);
113 if (aclInterface != null) {
114 boolean portSecurityEnabled = aclInPortAfter.isPortSecurityEnabled();
115 aclInterface.setPortSecurityEnabled(portSecurityEnabled);
116 if (portSecurityEnabled) {
117 aclInterface.setSecurityGroups(aclInPortAfter.getSecurityGroups());
118 aclInterface.setAllowedAddressPairs(aclInPortAfter.getAllowedAddressPairs());
121 // Control should not come here
122 LOG.error("Unable to find Acl Interface details for {}", interfaceId);
127 private AclInterface getOldAclInterfaceObject(AclInterface aclInterface, InterfaceAcl aclInPortBefore) {
128 AclInterface oldAclInterface = new AclInterface();
129 if (aclInPortBefore == null) {
130 oldAclInterface.setPortSecurityEnabled(false);
132 oldAclInterface.setInterfaceId(aclInterface.getInterfaceId());
133 oldAclInterface.setDpId(aclInterface.getDpId());
134 oldAclInterface.setLPortTag(aclInterface.getLPortTag());
136 oldAclInterface.setPortSecurityEnabled(aclInPortBefore.isPortSecurityEnabled());
137 oldAclInterface.setAllowedAddressPairs(aclInPortBefore.getAllowedAddressPairs());
138 oldAclInterface.setSecurityGroups(aclInPortBefore.getSecurityGroups());
140 return oldAclInterface;
144 protected void add(InstanceIdentifier<Interface> key, Interface port) {
145 InterfaceAcl aclInPort = port.getAugmentation(InterfaceAcl.class);
146 if (aclInPort != null && aclInPort.isPortSecurityEnabled()) {
147 addAclInterfaceToCache(port.getName(), aclInPort);
151 private AclInterface addAclInterfaceToCache(String interfaceId, InterfaceAcl aclInPort) {
152 AclInterface aclInterface = new AclInterface();
153 aclInterface.setInterfaceId(interfaceId);
154 aclInterface.setPortSecurityEnabled(aclInPort.isPortSecurityEnabled());
155 aclInterface.setSecurityGroups(aclInPort.getSecurityGroups());
156 aclInterface.setAllowedAddressPairs(aclInPort.getAllowedAddressPairs());
157 AclInterfaceCacheUtil.addAclInterfaceToCache(interfaceId, aclInterface);
162 protected AclInterfaceListener getDataTreeChangeListener() {