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