ab2bc1d5c6bb1b2b0b5e2cd44ffefa33019f70d8
[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.util.Collections;
14 import javax.annotation.PostConstruct;
15 import javax.inject.Inject;
16 import javax.inject.Singleton;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
21 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
22 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
23 import org.opendaylight.genius.mdsalutil.MDSALUtil;
24 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
25 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
26 import org.opendaylight.netvirt.aclservice.utils.AclConstants;
27 import org.opendaylight.netvirt.aclservice.utils.AclNodeDefaultFlowsTxBuilder;
28 import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
29 import org.opendaylight.serviceutils.srm.RecoverableListener;
30 import org.opendaylight.serviceutils.srm.ServiceRecoveryRegistry;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.config.rev160806.AclserviceConfig;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.config.rev160806.AclserviceConfig.SecurityGroupMode;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.opendaylight.yangtools.yang.common.Uint64;
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         registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
91     }
92
93     @Override
94     protected InstanceIdentifier<FlowCapableNode> getWildCardPath() {
95         return InstanceIdentifier.create(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class);
96     }
97
98     @Override
99     protected void remove(InstanceIdentifier<FlowCapableNode> key, FlowCapableNode dataObjectModification) {
100         // do nothing
101     }
102
103     @Override
104     protected void update(InstanceIdentifier<FlowCapableNode> key, FlowCapableNode dataObjectModificationBefore,
105             FlowCapableNode dataObjectModificationAfter) {
106         // do nothing
107     }
108
109     @Override
110     protected void add(InstanceIdentifier<FlowCapableNode> key, FlowCapableNode dataObjectModification) {
111         NodeKey nodeKey = key.firstKeyOf(Node.class);
112         Uint64 dpId = MDSALUtil.getDpnIdFromNodeName(nodeKey.getId());
113         LOG.info("Received ACL node [{}] add event", dpId);
114
115         if (securityGroupMode != null && securityGroupMode != SecurityGroupMode.Stateful) {
116             LOG.error("Invalid security group mode ({}) obtained from AclserviceConfig. dpId={}", securityGroupMode,
117                     dpId);
118             return;
119         }
120         jobCoordinator.enqueueJob(dpId.toString(),
121             () -> Collections.singletonList(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, tx -> {
122                 new AclNodeDefaultFlowsTxBuilder(dpId, mdsalManager, config, tx).build();
123
124                 LOG.info("Adding default ACL flows for dpId={}", dpId);
125             })), AclConstants.JOB_MAX_RETRIES);
126
127         LOG.trace("FlowCapableNode (dpid: {}) add event is processed.", dpId);
128     }
129
130     @Override
131     protected AclNodeListener getDataTreeChangeListener() {
132         return AclNodeListener.this;
133     }
134 }