2 * Copyright (c) 2016, 2018 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
9 package org.opendaylight.netvirt.aclservice.listeners;
11 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
13 import java.util.Collections;
14 import javax.annotation.PreDestroy;
15 import javax.inject.Inject;
16 import javax.inject.Singleton;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
19 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
20 import org.opendaylight.genius.mdsalutil.MDSALUtil;
21 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
22 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
23 import org.opendaylight.infrautils.utils.concurrent.Executors;
24 import org.opendaylight.mdsal.binding.api.DataBroker;
25 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
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.serviceutils.tools.listener.AbstractAsyncDataTreeChangeListener;
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.opendaylight.yangtools.yang.common.Uint64;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * Listener to handle flow capable node updates. Configures default ACL flows
45 * during when node is discovered.
48 public class AclNodeListener extends AbstractAsyncDataTreeChangeListener<FlowCapableNode>
49 implements RecoverableListener {
51 private static final Logger LOG = LoggerFactory.getLogger(AclNodeListener.class);
53 private final IMdsalApiManager mdsalManager;
54 private final AclserviceConfig config;
55 private final DataBroker dataBroker;
56 private final ManagedNewTransactionRunner txRunner;
57 private final AclServiceUtils aclServiceUtils;
58 private final JobCoordinator jobCoordinator;
61 private SecurityGroupMode securityGroupMode = null;
64 public AclNodeListener(final IMdsalApiManager mdsalManager, DataBroker dataBroker, AclserviceConfig config,
65 AclServiceUtils aclServiceUtils, JobCoordinator jobCoordinator,
66 ServiceRecoveryRegistry serviceRecoveryRegistry) {
67 super(dataBroker, LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class).child(Node.class)
68 .augmentation(FlowCapableNode.class),
69 Executors.newListeningSingleThreadExecutor("AclNodeListener", LOG));
71 this.mdsalManager = mdsalManager;
72 this.dataBroker = dataBroker;
73 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
75 this.aclServiceUtils = aclServiceUtils;
76 this.jobCoordinator = jobCoordinator;
77 serviceRecoveryRegistry.addRecoverableListener(AclServiceUtils.getRecoverServiceRegistryKey(), this);
82 LOG.info("{} start", getClass().getSimpleName());
84 this.securityGroupMode = config.getSecurityGroupMode();
86 LOG.info("AclserviceConfig: {}", this.config);
90 public void registerListener() {
95 public void deregisterListener() {
100 public void remove(InstanceIdentifier<FlowCapableNode> key, FlowCapableNode dataObjectModification) {
105 public void update(InstanceIdentifier<FlowCapableNode> key, FlowCapableNode dataObjectModificationBefore,
106 FlowCapableNode dataObjectModificationAfter) {
111 public void add(InstanceIdentifier<FlowCapableNode> key, FlowCapableNode dataObjectModification) {
112 NodeKey nodeKey = key.firstKeyOf(Node.class);
113 Uint64 dpId = MDSALUtil.getDpnIdFromNodeName(nodeKey.getId());
114 LOG.info("Received ACL node [{}] add event", dpId);
116 if (securityGroupMode != null && securityGroupMode != SecurityGroupMode.Stateful) {
117 LOG.error("Invalid security group mode ({}) obtained from AclserviceConfig. dpId={}", securityGroupMode,
121 jobCoordinator.enqueueJob(dpId.toString(),
122 () -> Collections.singletonList(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, tx -> {
123 new AclNodeDefaultFlowsTxBuilder(dpId, mdsalManager, config, tx).build();
125 LOG.info("Adding default ACL flows for dpId={}", dpId);
126 })), AclConstants.JOB_MAX_RETRIES);
128 LOG.trace("FlowCapableNode (dpid: {}) add event is processed.", dpId);
133 public void close() {
135 Executors.shutdownAndAwaitTermination(getExecutorService());