Switch to JDT annotations for Nullable and NonNull
[netvirt.git] / aclservice / impl / src / main / java / org / opendaylight / netvirt / aclservice / listeners / AclNodeListener.java
1 /*
2  * Copyright (c) 2016, 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
9 package org.opendaylight.netvirt.aclservice.listeners;
10
11 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
12
13 import java.math.BigInteger;
14 import java.util.Collections;
15 import javax.annotation.PostConstruct;
16 import javax.inject.Inject;
17 import javax.inject.Singleton;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
22 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
23 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
24 import org.opendaylight.genius.mdsalutil.MDSALUtil;
25 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
26 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
27 import org.opendaylight.netvirt.aclservice.utils.AclConstants;
28 import org.opendaylight.netvirt.aclservice.utils.AclNodeDefaultFlowsTxBuilder;
29 import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
30 import org.opendaylight.serviceutils.srm.RecoverableListener;
31 import org.opendaylight.serviceutils.srm.ServiceRecoveryRegistry;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.config.rev160806.AclserviceConfig;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.config.rev160806.AclserviceConfig.SecurityGroupMode;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * Listener to handle flow capable node updates. Configures default ACL flows
44  * during when node is discovered.
45  */
46 @Singleton
47 public class AclNodeListener extends AsyncDataTreeChangeListenerBase<FlowCapableNode, AclNodeListener>
48         implements RecoverableListener {
49
50     private static final Logger LOG = LoggerFactory.getLogger(AclNodeListener.class);
51
52     private final IMdsalApiManager mdsalManager;
53     private final AclserviceConfig config;
54     private final DataBroker dataBroker;
55     private final ManagedNewTransactionRunner txRunner;
56     private final AclServiceUtils aclServiceUtils;
57     private final JobCoordinator jobCoordinator;
58
59     @Nullable
60     private SecurityGroupMode securityGroupMode = null;
61
62     @Inject
63     public AclNodeListener(final IMdsalApiManager mdsalManager, DataBroker dataBroker, AclserviceConfig config,
64             AclServiceUtils aclServiceUtils, JobCoordinator jobCoordinator,
65             ServiceRecoveryRegistry serviceRecoveryRegistry) {
66         super(FlowCapableNode.class, AclNodeListener.class);
67
68         this.mdsalManager = mdsalManager;
69         this.dataBroker = dataBroker;
70         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
71         this.config = config;
72         this.aclServiceUtils = aclServiceUtils;
73         this.jobCoordinator = jobCoordinator;
74         serviceRecoveryRegistry.addRecoverableListener(AclServiceUtils.getRecoverServiceRegistryKey(), this);
75     }
76
77     @Override
78     @PostConstruct
79     public void init() {
80         LOG.info("{} start", getClass().getSimpleName());
81         if (config != null) {
82             this.securityGroupMode = config.getSecurityGroupMode();
83         }
84         registerListener();
85         LOG.info("AclserviceConfig: {}", this.config);
86     }
87
88     @Override
89     public void registerListener() {
90         this.aclServiceUtils.createRemoteAclIdPool();
91         registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
92     }
93
94     @Override
95     public  void deregisterListener() {
96         super.deregisterListener();
97         this.aclServiceUtils.deleteRemoteAclIdPool();
98     }
99
100     @Override
101     public void close() {
102         super.close();
103         this.aclServiceUtils.deleteRemoteAclIdPool();
104     }
105
106     @Override
107     protected InstanceIdentifier<FlowCapableNode> getWildCardPath() {
108         return InstanceIdentifier.create(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class);
109     }
110
111     @Override
112     protected void remove(InstanceIdentifier<FlowCapableNode> key, FlowCapableNode dataObjectModification) {
113         // do nothing
114     }
115
116     @Override
117     protected void update(InstanceIdentifier<FlowCapableNode> key, FlowCapableNode dataObjectModificationBefore,
118             FlowCapableNode dataObjectModificationAfter) {
119         // do nothing
120     }
121
122     @Override
123     protected void add(InstanceIdentifier<FlowCapableNode> key, FlowCapableNode dataObjectModification) {
124         NodeKey nodeKey = key.firstKeyOf(Node.class);
125         BigInteger dpId = MDSALUtil.getDpnIdFromNodeName(nodeKey.getId());
126         LOG.info("Received ACL node [{}] add event", dpId);
127
128         if (securityGroupMode != null && securityGroupMode != SecurityGroupMode.Stateful) {
129             LOG.error("Invalid security group mode ({}) obtained from AclserviceConfig. dpId={}", securityGroupMode,
130                     dpId);
131             return;
132         }
133         jobCoordinator.enqueueJob(String.valueOf(dpId),
134             () -> Collections.singletonList(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, tx -> {
135                 new AclNodeDefaultFlowsTxBuilder(dpId, mdsalManager, config, tx).build();
136
137                 LOG.info("Adding default ACL flows for dpId={}", dpId);
138             })), AclConstants.JOB_MAX_RETRIES);
139
140         LOG.trace("FlowCapableNode (dpid: {}) add event is processed.", dpId);
141     }
142
143     @Override
144     protected AclNodeListener getDataTreeChangeListener() {
145         return AclNodeListener.this;
146     }
147 }