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