e171c22225ad0c9567fbc7e9f2bcfe1cb2f88330
[netvirt.git] / vpnservice / aclservice / impl / src / main / java / org / opendaylight / netvirt / aclservice / listeners / AclVpnChangeListener.java
1 /*
2  * Copyright (c) 2017 Ericsson India Global Services Pvt Ltd. 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 package org.opendaylight.netvirt.aclservice.listeners;
9
10 import javax.annotation.PostConstruct;
11 import javax.annotation.PreDestroy;
12 import javax.inject.Inject;
13 import javax.inject.Singleton;
14
15 import org.opendaylight.netvirt.aclservice.api.AclServiceManager;
16 import org.opendaylight.netvirt.aclservice.api.AclServiceManager.Action;
17 import org.opendaylight.netvirt.aclservice.api.utils.AclInterface;
18 import org.opendaylight.netvirt.aclservice.api.utils.AclInterfaceCacheUtil;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.AddDpnEvent;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.AddInterfaceToDpnOnVpnEvent;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.OdlL3vpnListener;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.RemoveDpnEvent;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.RemoveInterfaceFromDpnOnVpnEvent;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.add._interface.to.dpn.on.vpn.event.AddInterfaceEventData;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.remove._interface.from.dpn.on.vpn.event.RemoveInterfaceEventData;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 @Singleton
30 public class AclVpnChangeListener implements OdlL3vpnListener {
31     private static final Logger LOG = LoggerFactory.getLogger(AclVpnChangeListener.class);
32     private final AclServiceManager aclServiceManager;
33
34     @Inject
35     public AclVpnChangeListener(AclServiceManager aclServiceManager) {
36         this.aclServiceManager = aclServiceManager;
37     }
38
39     @PostConstruct
40     public void init() {
41         LOG.trace("Initializing singleton..");
42     }
43
44     @PreDestroy
45     public void close() {
46         LOG.trace("Destroying singleton...");
47     }
48
49     @Override
50     public void onAddDpnEvent(AddDpnEvent notification) {
51     }
52
53     @Override
54     public void onRemoveDpnEvent(RemoveDpnEvent notification) {
55     }
56
57     @Override
58     public void onAddInterfaceToDpnOnVpnEvent(AddInterfaceToDpnOnVpnEvent notification) {
59         AddInterfaceEventData data = notification.getAddInterfaceEventData();
60         LOG.trace("Processing vpn interface {} addition", data.getInterfaceName());
61         Long vpnId = data.getVpnId();
62         AclInterface aclInterface = AclInterfaceCacheUtil.getAclInterfaceFromCache(data.getInterfaceName());
63         if (null != aclInterface && aclInterface.isPortSecurityEnabled() && !vpnId.equals(aclInterface.getVpnId())) {
64             aclServiceManager.notify(aclInterface, null, Action.UNBIND);
65             aclInterface.setVpnId(vpnId);
66             aclServiceManager.notify(aclInterface, null, Action.BIND);
67         }
68     }
69
70     @Override
71     public void onRemoveInterfaceFromDpnOnVpnEvent(RemoveInterfaceFromDpnOnVpnEvent notification) {
72         RemoveInterfaceEventData data = notification.getRemoveInterfaceEventData();
73         LOG.trace("Processing vpn interface {} deletion", data.getInterfaceName());
74         Long vpnId = data.getVpnId();
75         AclInterface aclInterface = AclInterfaceCacheUtil.getAclInterfaceFromCache(data.getInterfaceName());
76         if (null != aclInterface && aclInterface.isPortSecurityEnabled() && vpnId.equals(aclInterface.getVpnId())) {
77             aclServiceManager.notify(aclInterface, null, Action.UNBIND);
78             aclInterface.setVpnId(null);
79             aclServiceManager.notify(aclInterface, null, Action.BIND);
80         }
81     }
82 }
83