e492c4ed3d211881d234b20c5550a43fd9039f0e
[genius.git] / interfacemanager / interfacemanager-impl / src / main / java / org / opendaylight / genius / interfacemanager / renderer / ovs / statehelpers / OvsInterfaceStateAddHelper.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.genius.interfacemanager.renderer.ovs.statehelpers;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
13 import org.opendaylight.genius.interfacemanager.IfmUtil;
14 import org.opendaylight.genius.interfacemanager.IfmConstants;
15 import org.opendaylight.genius.interfacemanager.commons.AlivenessMonitorUtils;
16 import org.opendaylight.genius.interfacemanager.commons.InterfaceManagerCommonUtils;
17 import org.opendaylight.genius.interfacemanager.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
18 import org.opendaylight.genius.mdsalutil.NwConstants;
19 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.ParentRefs;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.AlivenessMonitorService;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfTunnel;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import java.math.BigInteger;
33 import java.util.ArrayList;
34 import java.util.List;
35
36 /**
37  * This worker is responsible for adding the openflow-interfaces/of-port-info container
38  * in odl-interface-openflow yang.
39  * Where applicable:
40  * Create the entries in Interface-State OperDS.
41  * Create the entries in Inventory OperDS.
42  */
43
44 public class OvsInterfaceStateAddHelper {
45     private static final Logger LOG = LoggerFactory.getLogger(OvsInterfaceStateAddHelper.class);
46
47     public static List<ListenableFuture<Void>> addState(DataBroker dataBroker, IdManagerService idManager,
48             IMdsalApiManager mdsalApiManager, AlivenessMonitorService alivenessMonitorService,
49             NodeConnectorId nodeConnectorId, String interfaceName, FlowCapableNodeConnector fcNodeConnectorNew) {
50         //Retrieve Port No from nodeConnectorId
51         long portNo = IfmUtil.getPortNumberFromNodeConnectorId(nodeConnectorId);
52         if (portNo == IfmConstants.INVALID_PORT_NO) {
53             LOG.trace("Cannot derive port number, not proceeding with Interface State "
54                     + "addition for interface: {}", interfaceName);
55             return null;
56         }
57
58         //Retrieve PbyAddress & OperState from the DataObject
59         LOG.debug("Adding Interface State to Oper DS for interface: {}", interfaceName);
60         List<ListenableFuture<Void>> futures = new ArrayList<>();
61         WriteTransaction defaultOperationalShardTransaction = dataBroker.newWriteOnlyTransaction();
62         PhysAddress physAddress = IfmUtil.getPhyAddress(portNo, fcNodeConnectorNew);
63
64         Interface.OperStatus operStatus = Interface.OperStatus.Up;
65         Interface.AdminStatus adminStatus = Interface.AdminStatus.Up;
66
67         // Fetch the interface from config DS if exists
68         InterfaceKey interfaceKey = new InterfaceKey(interfaceName);
69         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface =
70                 InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey, dataBroker);
71
72         if (InterfaceManagerCommonUtils.isTunnelPort(interfaceName)) {
73             if (!validateTunnelPortAttributes(nodeConnectorId, iface)) {
74                 return futures;
75             }
76         }
77
78         Interface ifState = InterfaceManagerCommonUtils.addStateEntry(iface, interfaceName,
79                 defaultOperationalShardTransaction, idManager, physAddress, operStatus, adminStatus, nodeConnectorId);
80
81         // If this interface is a tunnel interface, create the tunnel ingress flow,and start tunnel monitoring
82         if (InterfaceManagerCommonUtils.isTunnelInterface(iface)) {
83             handleTunnelMonitoringAddition(futures, dataBroker, mdsalApiManager, alivenessMonitorService,
84                     nodeConnectorId, defaultOperationalShardTransaction, ifState.getIfIndex(),
85                     iface.getAugmentation(IfTunnel.class), interfaceName, portNo);
86             return futures;
87         }
88
89         // install ingress flow if this is an l2vlan interface
90         if (InterfaceManagerCommonUtils.isVlanInterface(iface) && iface.isEnabled() && ifState
91                 .getOperStatus() == org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus.Up) {
92             BigInteger dpId = IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId);
93             FlowBasedServicesUtils.installLportIngressFlow(dpId, portNo, iface, futures, dataBroker,
94                     ifState.getIfIndex());
95             FlowBasedServicesUtils.bindDefaultEgressDispatcherService(dataBroker, futures, iface, Long.toString(portNo),
96                     interfaceName, ifState.getIfIndex());
97         }
98
99         futures.add(defaultOperationalShardTransaction.submit());
100         return futures;
101     }
102
103     public static void handleTunnelMonitoringAddition(List<ListenableFuture<Void>> futures, DataBroker dataBroker,
104             IMdsalApiManager mdsalApiManager, AlivenessMonitorService alivenessMonitorService,
105             NodeConnectorId nodeConnectorId, WriteTransaction transaction, Integer ifindex, IfTunnel ifTunnel,
106             String interfaceName, long portNo) {
107         BigInteger dpId = IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId);
108         InterfaceManagerCommonUtils.makeTunnelIngressFlow(futures, mdsalApiManager, ifTunnel, dpId, portNo,
109                 interfaceName, ifindex, NwConstants.ADD_FLOW);
110         futures.add(transaction.submit());
111         AlivenessMonitorUtils.startLLDPMonitoring(alivenessMonitorService, dataBroker, ifTunnel, interfaceName);
112     }
113
114     public static boolean validateTunnelPortAttributes(NodeConnectorId nodeConnectorId,
115             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface) {
116         BigInteger currentDpnId = IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId);
117         if (iface != null) {
118             ParentRefs parentRefs = iface.getAugmentation(ParentRefs.class);
119             if (!currentDpnId.equals(parentRefs.getDatapathNodeIdentifier())) {
120                 LOG.warn(
121                         "Received tunnel state add notification for tunnel {} from dpn {} where as "
122                                 + "the northbound configured dpn is {}",
123                         iface.getName(), currentDpnId, parentRefs.getDatapathNodeIdentifier());
124                 return false;
125             }
126         } else {
127             LOG.warn("Received tunnel state add notification for a tunnel which is not configured {}", iface.getName());
128             return false;
129         }
130         return true;
131     }
132 }