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 org.opendaylight.controller.md.sal.binding.api.DataBroker;
11 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
12 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.netvirt.elan.utils.ElanConstants;
15 import org.opendaylight.genius.mdsalutil.AbstractDataChangeListener;
16 import org.opendaylight.genius.mdsalutil.*;
17 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
21 import org.opendaylight.yangtools.concepts.ListenerRegistration;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 import java.math.BigInteger;
27 import java.util.ArrayList;
28 import java.util.List;
30 public class ElanNodeListener extends AbstractDataChangeListener<Node> {
32 private static final Logger logger = LoggerFactory.getLogger(ElanNodeListener.class);
33 private static volatile ElanNodeListener elanNodeListener = null;
34 private ElanServiceProvider elanServiceProvider = null;
35 private ListenerRegistration<DataChangeListener> listenerRegistration;
36 public static ElanNodeListener getElanNodeListener(ElanServiceProvider elanServiceProvider) {
37 if (elanNodeListener == null)
38 synchronized (ElanNodeListener.class) {
39 if (elanNodeListener == null)
41 ElanNodeListener elanNodeListener = new ElanNodeListener(elanServiceProvider);
42 return elanNodeListener;
46 return elanNodeListener;
49 public ElanNodeListener(ElanServiceProvider elanServiceProvider) {
51 this.elanServiceProvider= elanServiceProvider;
52 registerListener(this.elanServiceProvider.getBroker());
55 private void registerListener(final DataBroker db) {
57 listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
58 getWildCardPath(), ElanNodeListener.this, AsyncDataBroker.DataChangeScope.SUBTREE);
59 } catch (final Exception e) {
60 logger.error("ElanNodeListener: DataChange listener registration fail!", e);
61 throw new IllegalStateException("ElanNodeListener: registration Listener failed.", e);
65 private InstanceIdentifier<Node> getWildCardPath() {
66 return InstanceIdentifier.create(Nodes.class).child(Node.class);
71 protected void remove(InstanceIdentifier<Node> identifier, Node del) {
76 protected void update(InstanceIdentifier<Node> identifier, Node original, Node update) {
81 protected void add(InstanceIdentifier<Node> identifier, Node add) {
82 NodeId nodeId = add.getId();
83 String[] node = nodeId.getValue().split(":");
84 if (node.length < 2) {
85 logger.warn("Unexpected nodeId {}", nodeId.getValue());
88 BigInteger dpId = new BigInteger(node[1]);
89 createTableMissEntry(dpId);
92 public void createTableMissEntry(BigInteger dpnId) {
93 setupTableMissSmacFlow(dpnId);
94 setupTableMissDmacFlow(dpnId);
97 private void setupTableMissSmacFlow(BigInteger dpId) {
98 List<MatchInfo> mkMatches = new ArrayList<MatchInfo>();
99 List<InstructionInfo> mkInstructions = new ArrayList<InstructionInfo>();
100 List <ActionInfo> actionsInfos = new ArrayList <ActionInfo> ();
101 actionsInfos.add(new ActionInfo(ActionType.punt_to_controller, new String[] {}));
102 mkInstructions.add(new InstructionInfo(InstructionType.apply_actions, actionsInfos));
103 mkInstructions.add(new InstructionInfo(InstructionType.goto_table, new long[] { NwConstants.ELAN_DMAC_TABLE }));
105 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_SMAC_TABLE, getTableMissFlowRef(NwConstants.ELAN_SMAC_TABLE),
106 0, "ELAN sMac Table Miss Flow", 0, 0, ElanConstants.COOKIE_ELAN_KNOWN_SMAC,
107 mkMatches, mkInstructions);
108 this.elanServiceProvider.getMdsalManager().installFlow(flowEntity);
111 private void setupTableMissDmacFlow(BigInteger dpId) {
112 List<MatchInfo> mkMatches = new ArrayList<MatchInfo>();
114 List<InstructionInfo> mkInstructions = new ArrayList<InstructionInfo>();
115 mkInstructions.add(new InstructionInfo(InstructionType.goto_table, new long[] { NwConstants.ELAN_UNKNOWN_DMAC_TABLE }));
117 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_DMAC_TABLE, getTableMissFlowRef(NwConstants.ELAN_DMAC_TABLE),
118 0, "ELAN dMac Table Miss Flow", 0, 0, ElanConstants.COOKIE_ELAN_KNOWN_DMAC,
119 mkMatches, mkInstructions);
120 this.elanServiceProvider.getMdsalManager().installFlow(flowEntity);
123 private String getTableMissFlowRef(long tableId) {
124 return new StringBuffer().append(tableId).toString();