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.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;
43 * Listener to handle flow capable node updates. Configures default ACL flows
44 * during when node is discovered.
47 public class AclNodeListener extends AsyncDataTreeChangeListenerBase<FlowCapableNode, AclNodeListener>
48 implements RecoverableListener {
50 private static final Logger LOG = LoggerFactory.getLogger(AclNodeListener.class);
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;
60 private SecurityGroupMode securityGroupMode = null;
63 public AclNodeListener(final IMdsalApiManager mdsalManager, DataBroker dataBroker, AclserviceConfig config,
64 AclServiceUtils aclServiceUtils, JobCoordinator jobCoordinator,
65 ServiceRecoveryRegistry serviceRecoveryRegistry) {
66 super(FlowCapableNode.class, AclNodeListener.class);
68 this.mdsalManager = mdsalManager;
69 this.dataBroker = dataBroker;
70 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
72 this.aclServiceUtils = aclServiceUtils;
73 this.jobCoordinator = jobCoordinator;
74 serviceRecoveryRegistry.addRecoverableListener(AclServiceUtils.getRecoverServiceRegistryKey(), this);
80 LOG.info("{} start", getClass().getSimpleName());
82 this.securityGroupMode = config.getSecurityGroupMode();
85 LOG.info("AclserviceConfig: {}", this.config);
89 public void registerListener() {
90 registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
94 protected InstanceIdentifier<FlowCapableNode> getWildCardPath() {
95 return InstanceIdentifier.create(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class);
99 protected void remove(InstanceIdentifier<FlowCapableNode> key, FlowCapableNode dataObjectModification) {
104 protected void update(InstanceIdentifier<FlowCapableNode> key, FlowCapableNode dataObjectModificationBefore,
105 FlowCapableNode dataObjectModificationAfter) {
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);
115 if (securityGroupMode != null && securityGroupMode != SecurityGroupMode.Stateful) {
116 LOG.error("Invalid security group mode ({}) obtained from AclserviceConfig. dpId={}", securityGroupMode,
120 jobCoordinator.enqueueJob(dpId.toString(),
121 () -> Collections.singletonList(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, tx -> {
122 new AclNodeDefaultFlowsTxBuilder(dpId, mdsalManager, config, tx).build();
124 LOG.info("Adding default ACL flows for dpId={}", dpId);
125 })), AclConstants.JOB_MAX_RETRIES);
127 LOG.trace("FlowCapableNode (dpid: {}) add event is processed.", dpId);
131 protected AclNodeListener getDataTreeChangeListener() {
132 return AclNodeListener.this;