2 * Copyright (c) 2016 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
8 package org.opendaylight.netvirt.elan.internal;
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.List;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
15 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.genius.mdsalutil.AbstractDataChangeListener;
18 import org.opendaylight.genius.mdsalutil.ActionInfo;
19 import org.opendaylight.genius.mdsalutil.ActionType;
20 import org.opendaylight.genius.mdsalutil.FlowEntity;
21 import org.opendaylight.genius.mdsalutil.InstructionInfo;
22 import org.opendaylight.genius.mdsalutil.InstructionType;
23 import org.opendaylight.genius.mdsalutil.MDSALUtil;
24 import org.opendaylight.genius.mdsalutil.MatchInfo;
25 import org.opendaylight.genius.mdsalutil.NwConstants;
26 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
27 import org.opendaylight.netvirt.elan.utils.ElanConstants;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
31 import org.opendaylight.yangtools.concepts.ListenerRegistration;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public class ElanNodeListener extends AbstractDataChangeListener<Node> implements AutoCloseable {
38 private static final Logger LOG = LoggerFactory.getLogger(ElanNodeListener.class);
40 private final DataBroker broker;
41 private final IMdsalApiManager mdsalManager;
43 private ListenerRegistration<DataChangeListener> listenerRegistration;
45 public ElanNodeListener(DataBroker dataBroker, IMdsalApiManager mdsalManager) {
47 this.broker = dataBroker;
48 this.mdsalManager = mdsalManager;
52 registerListener(broker);
55 private void registerListener(final DataBroker db) {
56 listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
57 getWildCardPath(), ElanNodeListener.this, AsyncDataBroker.DataChangeScope.SUBTREE);
60 private InstanceIdentifier<Node> getWildCardPath() {
61 return InstanceIdentifier.create(Nodes.class).child(Node.class);
65 protected void remove(InstanceIdentifier<Node> identifier, Node del) {
69 protected void update(InstanceIdentifier<Node> identifier, Node original, Node update) {
73 protected void add(InstanceIdentifier<Node> identifier, Node add) {
74 NodeId nodeId = add.getId();
75 String[] node = nodeId.getValue().split(":");
76 if (node.length < 2) {
77 LOG.warn("Unexpected nodeId {}", nodeId.getValue());
80 BigInteger dpId = new BigInteger(node[1]);
81 createTableMissEntry(dpId);
84 public void createTableMissEntry(BigInteger dpnId) {
85 setupTableMissSmacFlow(dpnId);
86 setupTableMissDmacFlow(dpnId);
89 private void setupTableMissSmacFlow(BigInteger dpId) {
90 List<InstructionInfo> mkInstructions = new ArrayList<>();
91 List<ActionInfo> actionsInfos = new ArrayList<>();
92 actionsInfos.add(new ActionInfo(ActionType.punt_to_controller, new String[] {}));
93 mkInstructions.add(new InstructionInfo(InstructionType.apply_actions, actionsInfos));
94 mkInstructions.add(new InstructionInfo(InstructionType.goto_table, new long[] { NwConstants.ELAN_DMAC_TABLE }));
96 List<MatchInfo> mkMatches = new ArrayList<>();
97 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_SMAC_TABLE,
98 getTableMissFlowRef(NwConstants.ELAN_SMAC_TABLE), 0, "ELAN sMac Table Miss Flow", 0, 0,
99 ElanConstants.COOKIE_ELAN_KNOWN_SMAC, mkMatches, mkInstructions);
100 mdsalManager.installFlow(flowEntity);
103 private void setupTableMissDmacFlow(BigInteger dpId) {
104 List<MatchInfo> mkMatches = new ArrayList<>();
106 List<InstructionInfo> mkInstructions = new ArrayList<>();
108 new InstructionInfo(InstructionType.goto_table, new long[] { NwConstants.ELAN_UNKNOWN_DMAC_TABLE }));
110 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_DMAC_TABLE,
111 getTableMissFlowRef(NwConstants.ELAN_DMAC_TABLE), 0, "ELAN dMac Table Miss Flow", 0, 0,
112 ElanConstants.COOKIE_ELAN_KNOWN_DMAC, mkMatches, mkInstructions);
113 mdsalManager.installFlow(flowEntity);
116 private String getTableMissFlowRef(long tableId) {
117 return new StringBuffer().append(tableId).toString();
121 public void close() throws Exception {
122 if (listenerRegistration != null) {
123 listenerRegistration.close();