NETVIRT-1197: Stale entries exists in ACL caches
[netvirt.git] / neutronvpn / impl / src / main / java / org / opendaylight / netvirt / neutronvpn / NeutronSecurityGroupListener.java
1 /*
2  * Copyright (c) 2018 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.neutronvpn;
9
10 import javax.annotation.PostConstruct;
11 import javax.inject.Inject;
12 import javax.inject.Singleton;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
16 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
17 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.AccessLists;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.Acl;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.AclKey;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.groups.attributes.SecurityGroups;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.groups.attributes.security.groups.SecurityGroup;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 @Singleton
29 public class NeutronSecurityGroupListener
30         extends AsyncDataTreeChangeListenerBase<SecurityGroup, NeutronSecurityGroupListener> {
31     private static final Logger LOG = LoggerFactory.getLogger(NeutronSecurityGroupListener.class);
32
33     private final DataBroker dataBroker;
34
35     @Inject
36     public NeutronSecurityGroupListener(final DataBroker dataBroker) {
37         super(SecurityGroup.class, NeutronSecurityGroupListener.class);
38         this.dataBroker = dataBroker;
39     }
40
41     @Override
42     @PostConstruct
43     public void init() {
44         LOG.info("{} init", getClass().getSimpleName());
45         registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
46     }
47
48     @Override
49     protected InstanceIdentifier<SecurityGroup> getWildCardPath() {
50         return InstanceIdentifier.create(Neutron.class).child(SecurityGroups.class).child(SecurityGroup.class);
51     }
52
53     @Override
54     protected void add(InstanceIdentifier<SecurityGroup> instanceIdentifier, SecurityGroup securityGroup) {
55         LOG.trace("Received add event for securityGroup: {}", securityGroup);
56         // ACLs will be added through security rule listener
57     }
58
59     private InstanceIdentifier<Acl> getAclInstanceIdentifier(SecurityGroup securityGroup) {
60         return InstanceIdentifier.builder(AccessLists.class)
61                 .child(Acl.class, new AclKey(securityGroup.getUuid().getValue(), NeutronSecurityRuleConstants.ACLTYPE))
62                 .build();
63     }
64
65     @Override
66     protected void remove(InstanceIdentifier<SecurityGroup> instanceIdentifier, SecurityGroup securityGroup) {
67         LOG.trace("Received remove event for securityGroup: {}", securityGroup);
68         try {
69             InstanceIdentifier<Acl> identifier = getAclInstanceIdentifier(securityGroup);
70             SingleTransactionDataBroker.syncDelete(dataBroker, LogicalDatastoreType.CONFIGURATION, identifier);
71         } catch (TransactionCommitFailedException e) {
72             LOG.warn("Exception occurred while removing acl for security group: {}", securityGroup, e);
73         }
74     }
75
76     @Override
77     protected void update(InstanceIdentifier<SecurityGroup> instanceIdentifier, SecurityGroup oldSecurityGroup,
78             SecurityGroup updatedSecurityGroup) {
79         LOG.trace("Received update event for securityGroup: {}", updatedSecurityGroup);
80         // ACLs will be updated through security rule listener
81     }
82
83     @Override
84     protected NeutronSecurityGroupListener getDataTreeChangeListener() {
85         return this;
86     }
87 }