Bug 5054: Fix for switch restart
[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.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.idmanager.IdManager;
15 import org.opendaylight.vpnservice.VpnConstants;
16 import org.opendaylight.vpnservice.interfacemgr.IfmConstants;
17 import org.opendaylight.vpnservice.interfacemgr.IfmUtil;
18 import org.opendaylight.vpnservice.interfacemgr.commons.AlivenessMonitorUtils;
19 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceManagerCommonUtils;
20 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceMetaUtils;
21 import org.opendaylight.vpnservice.interfacemgr.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
22 import org.opendaylight.vpnservice.mdsalutil.*;
23 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629.AlivenessMonitorService;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info.InterfaceParentEntry;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info.InterfaceParentEntryKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info._interface.parent.entry.InterfaceChildEntry;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfL2vlan;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfTunnel;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.ParentRefs;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 import java.math.BigInteger;
45 import java.util.ArrayList;
46 import java.util.List;
47
48 /**
49  * This worker is responsible for adding the openflow-interfaces/of-port-info container
50  * in odl-interface-openflow yang.
51  * Where applicable:
52     * Create the entries in Interface-State OperDS.
53     * Create the entries in Inventory OperDS.
54  */
55
56 public class OvsInterfaceStateAddHelper {
57     private static final Logger LOG = LoggerFactory.getLogger(OvsInterfaceStateAddHelper.class);
58
59     public static List<ListenableFuture<Void>> addState(DataBroker dataBroker, IdManagerService idManager,
60                                                         IMdsalApiManager mdsalApiManager,AlivenessMonitorService alivenessMonitorService,
61                                                         NodeConnectorId nodeConnectorId, String portName, FlowCapableNodeConnector fcNodeConnectorNew) {
62         LOG.debug("Adding Interface State to Oper DS for port: {}", portName);
63         List<ListenableFuture<Void>> futures = new ArrayList<>();
64         WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
65
66         //Retrieve PbyAddress & OperState from the DataObject
67         PhysAddress physAddress = new PhysAddress(fcNodeConnectorNew.getHardwareAddress().getValue());
68         /*FIXME
69         State state = fcNodeConnectorNew.getState();
70         Interface.OperStatus operStatus =
71                 fcNodeConnectorNew == null ? Interface.OperStatus.Down : Interface.OperStatus.Up;
72         Interface.AdminStatus adminStatus = state.isBlocked() ? Interface.AdminStatus.Down : Interface.AdminStatus.Up;
73         */
74         Interface.OperStatus operStatus = Interface.OperStatus.Up;
75         Interface.AdminStatus adminStatus = Interface.AdminStatus.Up;
76         InterfaceKey interfaceKey = new InterfaceKey(portName);
77         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface =
78                 InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey, dataBroker);
79
80         if (iface != null && !iface.isEnabled()) {
81             operStatus = Interface.OperStatus.Down;
82         }
83
84         List<String> lowerLayerIfList = new ArrayList<>();
85         lowerLayerIfList.add(nodeConnectorId.getValue());
86
87         Integer ifIndex = IfmUtil.allocateId(idManager, IfmConstants.IFM_IDPOOL_NAME, portName);
88         InstanceIdentifier<Interface> ifStateId = IfmUtil.buildStateInterfaceId(portName);
89         InterfaceBuilder ifaceBuilder = new InterfaceBuilder().setOperStatus(operStatus)
90                 .setAdminStatus(adminStatus).setPhysAddress(physAddress).setIfIndex(ifIndex).setLowerLayerIf(lowerLayerIfList)
91                 .setKey(IfmUtil.getStateInterfaceKeyFromName(portName));
92         if(iface != null) {
93             ifaceBuilder.setType(iface.getType());
94         }
95         transaction.put(LogicalDatastoreType.OPERATIONAL, ifStateId, ifaceBuilder.build(), true);
96
97         // allocate lport tag and set in if-index
98         InterfaceMetaUtils.createLportTagInterfaceMap(transaction, portName, ifIndex);
99         if (iface == null) {
100             futures.add(transaction.submit());
101             return futures;
102         }
103
104         // If this interface is a tunnel interface, create the tunnel ingress flow
105         IfTunnel tunnel = iface.getAugmentation(IfTunnel.class);
106         if(tunnel != null){
107             BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
108             long portNo = Long.valueOf(IfmUtil.getPortNoFromNodeConnectorId(nodeConnectorId));
109             InterfaceManagerCommonUtils.makeTunnelIngressFlow(futures, mdsalApiManager, tunnel,dpId, portNo, iface,
110                     ifIndex, NwConstants.ADD_FLOW);
111             futures.add(transaction.submit());
112             AlivenessMonitorUtils.startLLDPMonitoring(alivenessMonitorService, dataBroker, iface);
113             return futures;
114         }
115
116         // If this interface maps to a Vlan trunk entity, operational states of all the vlan-trunk-members
117         // should also be created here.
118         IfL2vlan ifL2vlan = iface.getAugmentation(IfL2vlan.class);
119         if (ifL2vlan == null || ifL2vlan.getL2vlanMode() != IfL2vlan.L2vlanMode.Trunk) {
120             futures.add(transaction.submit());
121             return futures;
122         }
123
124         InterfaceParentEntryKey interfaceParentEntryKey = new InterfaceParentEntryKey(iface.getName());
125         InterfaceParentEntry interfaceParentEntry =
126                 InterfaceMetaUtils.getInterfaceParentEntryFromConfigDS(interfaceParentEntryKey, dataBroker);
127         if (interfaceParentEntry == null) {
128             futures.add(transaction.submit());
129             return futures;
130         }
131
132         List<InterfaceChildEntry> interfaceChildEntries = interfaceParentEntry.getInterfaceChildEntry();
133         if (interfaceChildEntries == null) {
134             futures.add(transaction.submit());
135             return futures;
136         }
137
138         //FIXME: If the no. of child entries exceeds 100, perform txn updates in batches of 100.
139         //List<Trunks> trunks = new ArrayList<>();
140         for (InterfaceChildEntry interfaceChildEntry : interfaceChildEntries) {
141             InterfaceKey childIfKey = new InterfaceKey(interfaceChildEntry.getChildInterface());
142             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface ifaceChild =
143                     InterfaceManagerCommonUtils.getInterfaceFromConfigDS(childIfKey, dataBroker);
144
145             // IfL2vlan ifL2vlanChild = iface.getAugmentation(IfL2vlan.class);
146             // trunks.add(new TrunksBuilder().setTrunk(ifL2vlanChild.getVlanId()).build());
147
148             if (!ifaceChild.isEnabled()) {
149                 operStatus = Interface.OperStatus.Down;
150             }
151
152             InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifChildStateId =
153                     IfmUtil.buildStateInterfaceId(ifaceChild.getName());
154             List<String> childLowerLayerIfList = new ArrayList<>();
155             childLowerLayerIfList.add(0, nodeConnectorId.getValue());
156             childLowerLayerIfList.add(1, iface.getName());
157             ifIndex = IfmUtil.allocateId(idManager, IfmConstants.IFM_IDPOOL_NAME, ifaceChild.getName());
158             InterfaceBuilder childIfaceBuilder = new InterfaceBuilder().setAdminStatus(adminStatus).setOperStatus(operStatus)
159                     .setPhysAddress(physAddress).setLowerLayerIf(childLowerLayerIfList).setIfIndex(ifIndex);
160             childIfaceBuilder.setKey(IfmUtil.getStateInterfaceKeyFromName(ifaceChild.getName())).setType(ifaceChild.getType());
161             transaction.put(LogicalDatastoreType.OPERATIONAL, ifChildStateId, childIfaceBuilder.build(), true);
162
163             // create lportTag Interface Map
164             InterfaceMetaUtils.createLportTagInterfaceMap(transaction, ifaceChild.getName(), ifIndex);
165         }
166
167         /** Below code will be needed if we want to update the vlan-trunks on the of-port
168         if (trunks.isEmpty()) {
169             futures.add(t.submit());
170             return futures;
171         }
172
173         BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
174
175         BridgeRefEntryKey BridgeRefEntryKey = new BridgeRefEntryKey(dpId);
176         InstanceIdentifier<BridgeRefEntry> dpnBridgeEntryIid =
177                 InterfaceMetaUtils.getBridgeRefEntryIdentifier(BridgeRefEntryKey);
178         BridgeRefEntry bridgeRefEntry =
179                 InterfaceMetaUtils.getBridgeRefEntryFromOperDS(dpnBridgeEntryIid, dataBroker);
180         if (bridgeRefEntry == null) {
181             futures.add(t.submit());
182             return futures;
183         }
184
185         InstanceIdentifier<OvsdbBridgeAugmentation> bridgeIid =
186                 (InstanceIdentifier<OvsdbBridgeAugmentation>)bridgeRefEntry.getBridgeReference().getValue();
187         VlanTrunkSouthboundUtils.addTerminationPointWithTrunks(bridgeIid, trunks, iface.getName(), t);
188          */
189
190         futures.add(transaction.submit());
191         return futures;
192     }
193 }