Bug 5182 - Customized name support for VLAN trunk should be supported
[vpnservice.git] / interfacemgr / interfacemgr-impl / src / main / java / org / opendaylight / vpnservice / interfacemgr / renderer / ovs / statehelpers / OvsInterfaceStateAddHelper.java
1 /*
2  * Copyright (c) 2015 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.vpnservice.interfacemgr.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.vpnservice.interfacemgr.IfmConstants;
14 import org.opendaylight.vpnservice.interfacemgr.IfmUtil;
15 import org.opendaylight.vpnservice.interfacemgr.commons.AlivenessMonitorUtils;
16 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceManagerCommonUtils;
17 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceMetaUtils;
18 import org.opendaylight.vpnservice.interfacemgr.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
19 import org.opendaylight.vpnservice.mdsalutil.MatchInfo;
20 import org.opendaylight.vpnservice.mdsalutil.NwConstants;
21 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629.AlivenessMonitorService;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info.InterfaceParentEntry;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info.InterfaceParentEntryKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info._interface.parent.entry.InterfaceChildEntry;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfTunnel;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import java.math.BigInteger;
37 import java.util.ArrayList;
38 import java.util.List;
39
40 /**
41  * This worker is responsible for adding the openflow-interfaces/of-port-info container
42  * in odl-interface-openflow yang.
43  * Where applicable:
44  * Create the entries in Interface-State OperDS.
45  * Create the entries in Inventory OperDS.
46  */
47
48 public class OvsInterfaceStateAddHelper {
49     private static final Logger LOG = LoggerFactory.getLogger(OvsInterfaceStateAddHelper.class);
50
51     public static List<ListenableFuture<Void>> addState(DataBroker dataBroker, IdManagerService idManager,
52                                                         IMdsalApiManager mdsalApiManager,AlivenessMonitorService alivenessMonitorService,
53                                                         NodeConnectorId nodeConnectorId, String portName, FlowCapableNodeConnector fcNodeConnectorNew) {
54         LOG.debug("Adding Interface State to Oper DS for port: {}", portName);
55         List<ListenableFuture<Void>> futures = new ArrayList<>();
56         WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
57
58         //Retrieve PbyAddress & OperState from the DataObject
59         PhysAddress physAddress = new PhysAddress(fcNodeConnectorNew.getHardwareAddress().getValue());
60         /*FIXME
61         State state = fcNodeConnectorNew.getState();
62         Interface.OperStatus operStatus =
63                 fcNodeConnectorNew == null ? Interface.OperStatus.Down : Interface.OperStatus.Up;
64         Interface.AdminStatus adminStatus = state.isBlocked() ? Interface.AdminStatus.Down : Interface.AdminStatus.Up;
65         */
66         Interface.OperStatus operStatus = Interface.OperStatus.Up;
67         Interface.AdminStatus adminStatus = Interface.AdminStatus.Up;
68
69         // Fetch the interface name corresponding to the port Name
70         InterfaceKey interfaceKey = new InterfaceKey(portName);
71         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface =
72                 InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey, dataBroker);
73
74         Interface ifState = InterfaceManagerCommonUtils.addStateEntry(iface, portName, transaction, idManager,
75                 physAddress, operStatus, adminStatus, nodeConnectorId);
76         BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
77         long portNo = Long.valueOf(IfmUtil.getPortNoFromNodeConnectorId(nodeConnectorId));
78         // If this interface is a tunnel interface, create the tunnel ingress flow
79         if(iface != null) {
80             IfTunnel tunnel = iface.getAugmentation(IfTunnel.class);
81             if (tunnel != null) {
82                 InterfaceManagerCommonUtils.makeTunnelIngressFlow(futures, mdsalApiManager, tunnel, dpId, portNo, iface,
83                         ifState.getIfIndex(), NwConstants.ADD_FLOW);
84                 futures.add(transaction.submit());
85                 AlivenessMonitorUtils.startLLDPMonitoring(alivenessMonitorService, dataBroker, iface);
86                 return futures;
87             }
88         }
89
90         // For all other interfaces except tunnel interfaces, interface name won't be same as port name.
91         // In that case fetch the interface corresponding to the portName, and update the state accordingly
92         InterfaceParentEntryKey interfaceParentEntryKey = new InterfaceParentEntryKey(portName);
93         InterfaceParentEntry interfaceParentEntry =
94                 InterfaceMetaUtils.getInterfaceParentEntryFromConfigDS(interfaceParentEntryKey, dataBroker);
95         if (interfaceParentEntry == null || interfaceParentEntry.getInterfaceChildEntry() == null) {
96             futures.add(transaction.submit());
97             return futures;
98         }
99
100         //FIXME: If the no. of child entries exceeds 100, perform txn updates in batches of 100.
101         //List<Trunks> trunks = new ArrayList<>();
102
103         String higherlayerChild = interfaceParentEntry.getInterfaceChildEntry().get(0).getChildInterface();
104         InterfaceManagerCommonUtils.addStateEntry(higherlayerChild, transaction, dataBroker, idManager,
105                 ifState);
106
107         // If this interface maps to a Vlan trunk entity, operational states of all the vlan-trunk-members
108         // should also be created here.
109         InterfaceParentEntryKey higherLayerParentEntryKey = new InterfaceParentEntryKey(higherlayerChild);
110         InterfaceParentEntry higherLayerParent =
111                 InterfaceMetaUtils.getInterfaceParentEntryFromConfigDS(higherLayerParentEntryKey, dataBroker);
112         if(higherLayerParent != null && higherLayerParent.getInterfaceChildEntry() != null) {
113             for (InterfaceChildEntry interfaceChildEntry : higherLayerParent.getInterfaceChildEntry()){
114                 InterfaceManagerCommonUtils.addStateEntry(interfaceChildEntry.getChildInterface(), transaction, dataBroker, idManager,
115                         ifState);
116             }
117         }
118         /** Below code will be needed if we want to update the vlan-trunks on the of-port
119          if (trunks.isEmpty()) {
120          futures.add(t.submit());
121          return futures;
122          }
123
124          BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
125
126          BridgeRefEntryKey BridgeRefEntryKey = new BridgeRefEntryKey(dpId);
127          InstanceIdentifier<BridgeRefEntry> dpnBridgeEntryIid =
128          InterfaceMetaUtils.getBridgeRefEntryIdentifier(BridgeRefEntryKey);
129          BridgeRefEntry bridgeRefEntry =
130          InterfaceMetaUtils.getBridgeRefEntryFromOperDS(dpnBridgeEntryIid, dataBroker);
131          if (bridgeRefEntry == null) {
132          futures.add(t.submit());
133          return futures;
134          }
135
136          InstanceIdentifier<OvsdbBridgeAugmentation> bridgeIid =
137          (InstanceIdentifier<OvsdbBridgeAugmentation>)bridgeRefEntry.getBridgeReference().getValue();
138          VlanTrunkSouthboundUtils.addTerminationPointWithTrunks(bridgeIid, trunks, iface.getName(), t);
139          */
140
141         futures.add(transaction.submit());
142         return futures;
143     }
144 }