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