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.MatchInfoBase;
26 import org.opendaylight.genius.mdsalutil.NwConstants;
27 import org.opendaylight.genius.mdsalutil.NxMatchFieldType;
28 import org.opendaylight.genius.mdsalutil.NxMatchInfo;
29 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
30 import org.opendaylight.netvirt.elan.utils.ElanConstants;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
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.netvirt.elan.config.rev150710.ElanConfig;
35 import org.opendaylight.yangtools.concepts.ListenerRegistration;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public class ElanNodeListener extends AbstractDataChangeListener<Node> implements AutoCloseable {
42 private static final Logger LOG = LoggerFactory.getLogger(ElanNodeListener.class);
43 private static final String LEARN_MATCH_REG4_VALUE = "1";
45 private final DataBroker broker;
46 private final IMdsalApiManager mdsalManager;
47 private final int tempSmacLearnTimeout;
49 private ListenerRegistration<DataChangeListener> listenerRegistration;
51 public ElanNodeListener(DataBroker dataBroker, IMdsalApiManager mdsalManager, ElanConfig elanConfig) {
53 this.broker = dataBroker;
54 this.mdsalManager = mdsalManager;
55 this.tempSmacLearnTimeout = elanConfig.getTempSmacLearnTimeout();
59 registerListener(broker);
62 private void registerListener(final DataBroker db) {
63 listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
64 getWildCardPath(), ElanNodeListener.this, AsyncDataBroker.DataChangeScope.SUBTREE);
67 private InstanceIdentifier<Node> getWildCardPath() {
68 return InstanceIdentifier.create(Nodes.class).child(Node.class);
72 protected void remove(InstanceIdentifier<Node> identifier, Node del) {
76 protected void update(InstanceIdentifier<Node> identifier, Node original, Node update) {
80 protected void add(InstanceIdentifier<Node> identifier, Node add) {
81 NodeId nodeId = add.getId();
82 String[] node = nodeId.getValue().split(":");
83 if (node.length < 2) {
84 LOG.warn("Unexpected nodeId {}", nodeId.getValue());
87 BigInteger dpId = new BigInteger(node[1]);
88 createTableMissEntry(dpId);
91 public void createTableMissEntry(BigInteger dpnId) {
92 setupTableMissSmacFlow(dpnId);
93 setupTableMissDmacFlow(dpnId);
96 private void setupTableMissSmacFlow(BigInteger dpId) {
97 List<ActionInfo> actionsInfos = new ArrayList<>();
98 actionsInfos.add(new ActionInfo(ActionType.punt_to_controller, new String[] {}));
100 String[][] learnActionMatches = new String[2][];
101 learnActionMatches[0] = new String[] { NwConstants.LearnFlowModsType.MATCH_FROM_FIELD.name(),
102 NwConstants.NxmOfFieldType.NXM_OF_ETH_SRC.getHexType(),
103 NwConstants.NxmOfFieldType.NXM_OF_ETH_SRC.getHexType(),
104 NwConstants.NxmOfFieldType.NXM_OF_ETH_SRC.getFlowModHeaderLen() };
105 learnActionMatches[1] = new String[] {
106 NwConstants.LearnFlowModsType.COPY_FROM_VALUE.name(), LEARN_MATCH_REG4_VALUE,
107 NwConstants.NxmOfFieldType.NXM_NX_REG4.getHexType(), "8" };
109 String[] header = new String[] {
111 String.valueOf(tempSmacLearnTimeout),
112 BigInteger.ZERO.toString(),
113 ElanConstants.COOKIE_ELAN_LEARNED_SMAC.toString(),
115 Short.toString(NwConstants.ELAN_SMAC_LEARNED_TABLE),
119 actionsInfos.add(new ActionInfo(ActionType.learn, header, learnActionMatches));
121 List<InstructionInfo> mkInstructions = new ArrayList<>();
122 mkInstructions.add(new InstructionInfo(InstructionType.apply_actions, actionsInfos));
123 mkInstructions.add(new InstructionInfo(InstructionType.goto_table, new long[] { NwConstants.ELAN_DMAC_TABLE }));
125 List<MatchInfo> mkMatches = new ArrayList<>();
126 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_SMAC_TABLE,
127 getTableMissFlowRef(NwConstants.ELAN_SMAC_TABLE), 0, "ELAN sMac Table Miss Flow", 0, 0,
128 ElanConstants.COOKIE_ELAN_KNOWN_SMAC, mkMatches, mkInstructions);
129 mdsalManager.installFlow(flowEntity);
131 addSmacBaseTableFlow(dpId);
132 addSmacLearnedTableFlow(dpId);
135 private void addSmacBaseTableFlow(BigInteger dpId) {
136 //T48 - resubmit to T49 & T50
137 List<ActionInfo> actionsInfo = new ArrayList<>();
138 actionsInfo.add(new ActionInfo(ActionType.nx_resubmit,
139 new String[] {Short.toString(NwConstants.ELAN_SMAC_LEARNED_TABLE) }));
140 actionsInfo.add(new ActionInfo(ActionType.nx_resubmit,
141 new String[] {Short.toString(NwConstants.ELAN_SMAC_TABLE) }));
142 List<InstructionInfo> mkInstruct = new ArrayList<>();
143 mkInstruct.add(new InstructionInfo(InstructionType.apply_actions, actionsInfo));
144 List<MatchInfo> mkMatch = new ArrayList<>();
145 FlowEntity doubleResubmitTable = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_BASE_TABLE,
146 getTableMissFlowRef(NwConstants.ELAN_BASE_TABLE),
147 0, "Elan sMac resubmit table", 0, 0,
148 ElanConstants.COOKIE_ELAN_BASE_SMAC, mkMatch, mkInstruct);
149 mdsalManager.installFlow(doubleResubmitTable);
152 private void addSmacLearnedTableFlow(BigInteger dpId) {
153 //T50 - match on Reg4 and goto T51
154 List<MatchInfoBase> mkMatches = new ArrayList<>();
155 mkMatches.add(new NxMatchInfo(NxMatchFieldType.nxm_reg_4, new long[] {
156 Long.valueOf(LEARN_MATCH_REG4_VALUE).longValue() }));
157 List<InstructionInfo> mkInstructions = new ArrayList<>();
158 mkInstructions.add(new InstructionInfo(InstructionType.goto_table,
159 new long[] { NwConstants.ELAN_DMAC_TABLE }));
160 String flowRef = new StringBuffer().append(NwConstants.ELAN_SMAC_TABLE).append(NwConstants.FLOWID_SEPARATOR)
161 .append(LEARN_MATCH_REG4_VALUE).toString();
162 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_SMAC_TABLE, flowRef,
163 10, "ELAN sMac Table Reg4 Flow", 0, 0,
164 ElanConstants.COOKIE_ELAN_KNOWN_SMAC.add(new BigInteger(LEARN_MATCH_REG4_VALUE)),
165 mkMatches, mkInstructions);
166 mdsalManager.installFlow(flowEntity);
169 private void setupTableMissDmacFlow(BigInteger dpId) {
170 List<MatchInfo> mkMatches = new ArrayList<>();
172 List<InstructionInfo> mkInstructions = new ArrayList<>();
174 new InstructionInfo(InstructionType.goto_table, new long[] { NwConstants.ELAN_UNKNOWN_DMAC_TABLE }));
176 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_DMAC_TABLE,
177 getTableMissFlowRef(NwConstants.ELAN_DMAC_TABLE), 0, "ELAN dMac Table Miss Flow", 0, 0,
178 ElanConstants.COOKIE_ELAN_KNOWN_DMAC, mkMatches, mkInstructions);
179 mdsalManager.installFlow(flowEntity);
182 private String getTableMissFlowRef(long tableId) {
183 return new StringBuffer().append(tableId).toString();
187 public void close() throws Exception {
188 if (listenerRegistration != null) {
189 listenerRegistration.close();