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.vpnservice.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.vpnservice.mdsalutil.AbstractDataChangeListener;
15 import org.opendaylight.vpnservice.elan.utils.ElanConstants;
16 import org.opendaylight.vpnservice.mdsalutil.*;
17 import org.opendaylight.vpnservice.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);
34 private IMdsalApiManager mdsalManager;
35 private ListenerRegistration<DataChangeListener> listenerRegistration;
36 private final DataBroker broker;
38 public ElanNodeListener(final DataBroker db, IMdsalApiManager mdsalManager) {
41 this.mdsalManager = mdsalManager;
45 private void registerListener(final DataBroker db) {
47 listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
48 getWildCardPath(), ElanNodeListener.this, AsyncDataBroker.DataChangeScope.SUBTREE);
49 } catch (final Exception e) {
50 logger.error("IfmNodeConnectorListener: DataChange listener registration fail!", e);
51 throw new IllegalStateException("IfmNodeConnectorListener: registration Listener failed.", e);
55 private InstanceIdentifier<Node> getWildCardPath() {
56 return InstanceIdentifier.create(Nodes.class).child(Node.class);
61 protected void remove(InstanceIdentifier<Node> identifier, Node del) {
66 protected void update(InstanceIdentifier<Node> identifier, Node original, Node update) {
71 protected void add(InstanceIdentifier<Node> identifier, Node add) {
72 NodeId nodeId = add.getId();
73 String[] node = nodeId.getValue().split(":");
74 BigInteger dpId = new BigInteger(node[1]);
75 createTableMissEntry(dpId);
78 public void createTableMissEntry(BigInteger dpnId) {
79 setupTableMissSmacFlow(dpnId);
80 setupTableMissDmacFlow(dpnId);
83 private void setupTableMissSmacFlow(BigInteger dpId) {
84 List<MatchInfo> mkMatches = new ArrayList<MatchInfo>();
85 List<InstructionInfo> mkInstructions = new ArrayList<InstructionInfo>();
86 List <ActionInfo> actionsInfos = new ArrayList <ActionInfo> ();
87 actionsInfos.add(new ActionInfo(ActionType.punt_to_controller, new String[] {}));
88 mkInstructions.add(new InstructionInfo(InstructionType.apply_actions, actionsInfos));
89 mkInstructions.add(new InstructionInfo(InstructionType.goto_table, new long[] { ElanConstants.ELAN_DMAC_TABLE }));
91 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, ElanConstants.ELAN_SMAC_TABLE, getTableMissFlowRef(ElanConstants.ELAN_SMAC_TABLE),
92 0, "ELAN sMac Table Miss Flow", 0, 0, ElanConstants.COOKIE_ELAN_KNOWN_SMAC,
93 mkMatches, mkInstructions);
94 mdsalManager.installFlow(flowEntity);
97 private void setupTableMissDmacFlow(BigInteger dpId) {
98 List<MatchInfo> mkMatches = new ArrayList<MatchInfo>();
100 List<InstructionInfo> mkInstructions = new ArrayList<InstructionInfo>();
101 mkInstructions.add(new InstructionInfo(InstructionType.goto_table, new long[] { ElanConstants.ELAN_UNKNOWN_DMAC_TABLE }));
103 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, ElanConstants.ELAN_DMAC_TABLE, getTableMissFlowRef(ElanConstants.ELAN_DMAC_TABLE),
104 0, "ELAN dMac Table Miss Flow", 0, 0, ElanConstants.COOKIE_ELAN_KNOWN_DMAC,
105 mkMatches, mkInstructions);
106 mdsalManager.installFlow(flowEntity);
109 private String getTableMissFlowRef(long tableId) {
110 return new StringBuffer().append(tableId).toString();