e98e6d6aec0c69e1085085881ed48763df172ec8
[vpnservice.git] / interfacemgr / interfacemgr-impl / src / main / java / org / opendaylight / vpnservice / interfacemgr / renderer / ovs / confighelpers / OvsInterfaceConfigAddHelper.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.confighelpers;
9
10 import com.google.common.base.Optional;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.idmanager.IdManager;
16 import org.opendaylight.vpnservice.interfacemgr.IfmUtil;
17 import org.opendaylight.vpnservice.interfacemgr.commons.AlivenessMonitorUtils;
18 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceManagerCommonUtils;
19 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceMetaUtils;
20 import org.opendaylight.vpnservice.interfacemgr.renderer.ovs.utilities.SouthboundUtils;
21 import org.opendaylight.vpnservice.mdsalutil.NwConstants;
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.Interface;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629.AlivenessMonitorService;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info.InterfaceParentEntry;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info.InterfaceParentEntryKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info._interface.parent.entry.InterfaceChildEntry;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.BridgeEntryKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.bridge.entry.BridgeInterfaceEntryKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge.ref.info.BridgeRefEntry;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge.ref.info.BridgeRefEntryKey;
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 public class OvsInterfaceConfigAddHelper {
47     private static final Logger LOG = LoggerFactory.getLogger(OvsInterfaceConfigAddHelper.class);
48
49     public static List<ListenableFuture<Void>> addConfiguration(DataBroker dataBroker, ParentRefs parentRefs,
50                                                                 Interface interfaceNew, IdManagerService idManager,
51                                                                 AlivenessMonitorService alivenessMonitorService,
52                                                                 IMdsalApiManager mdsalApiManager) {
53         List<ListenableFuture<Void>> futures = new ArrayList<>();
54
55         IfTunnel ifTunnel = interfaceNew.getAugmentation(IfTunnel.class);
56         if (ifTunnel != null) {
57             addTunnelConfiguration(dataBroker, parentRefs, interfaceNew, idManager, alivenessMonitorService,
58                     mdsalApiManager, futures);
59             return futures;
60         }
61
62         addVlanConfiguration(interfaceNew, parentRefs, dataBroker, idManager, futures);
63         return futures;
64     }
65
66     private static void addVlanConfiguration(Interface interfaceNew, ParentRefs parentRefs, DataBroker dataBroker, IdManagerService idManager,
67                                              List<ListenableFuture<Void>> futures) {
68         IfL2vlan ifL2vlan = interfaceNew.getAugmentation(IfL2vlan.class);
69         if (ifL2vlan == null || IfL2vlan.L2vlanMode.Trunk != ifL2vlan.getL2vlanMode()) {
70             return;
71         }
72         LOG.debug("adding vlan configuration for {}",interfaceNew.getName());
73         WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
74         InterfaceManagerCommonUtils.createInterfaceChildEntry(transaction,
75                 parentRefs.getParentInterface(), interfaceNew.getName());
76
77         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
78                 InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(parentRefs.getParentInterface(), dataBroker);
79
80         if (ifState == null) {
81             LOG.debug("could not retrieve interface state corresponding to {}",interfaceNew.getName());
82             futures.add(transaction.submit());
83             return;
84         }
85
86         InterfaceManagerCommonUtils.addStateEntry(interfaceNew.getName(), transaction, dataBroker, idManager, ifState);
87
88         InterfaceParentEntryKey interfaceParentEntryKey = new InterfaceParentEntryKey(interfaceNew.getName());
89         InterfaceParentEntry interfaceParentEntry =
90                 InterfaceMetaUtils.getInterfaceParentEntryFromConfigDS(interfaceParentEntryKey, dataBroker);
91         if (interfaceParentEntry == null || interfaceParentEntry.getInterfaceChildEntry() == null) {
92             LOG.debug("could not retrieve interface parent info for {}",interfaceNew.getName());
93             futures.add(transaction.submit());
94             return;
95         }
96
97         //FIXME: If the no. of child entries exceeds 100, perform txn updates in batches of 100.
98         for (InterfaceChildEntry interfaceChildEntry : interfaceParentEntry.getInterfaceChildEntry()) {
99             InterfaceManagerCommonUtils.addStateEntry(interfaceChildEntry.getChildInterface(), transaction, dataBroker, idManager,ifState);
100         }
101         futures.add(transaction.submit());
102     }
103
104     private static void addTunnelConfiguration(DataBroker dataBroker, ParentRefs parentRefs,
105                                                Interface interfaceNew, IdManagerService idManager,
106                                                AlivenessMonitorService alivenessMonitorService,
107                                                IMdsalApiManager mdsalApiManager,
108                                                List<ListenableFuture<Void>> futures) {
109         LOG.debug("adding tunnel configuration for {}", interfaceNew.getName());
110         WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
111         if (parentRefs == null) {
112             LOG.warn("ParentRefs for interface: {} Not Found. " +
113                     "Creation of Tunnel OF-Port not supported when dpid not provided.", interfaceNew.getName());
114             return;
115         }
116
117         BigInteger dpId = parentRefs.getDatapathNodeIdentifier();
118         if (dpId == null) {
119             LOG.warn("dpid for interface: {} Not Found. No DPID provided. " +
120                     "Creation of OF-Port not supported.", interfaceNew.getName());
121             return;
122         }
123
124         BridgeEntryKey bridgeEntryKey = new BridgeEntryKey(dpId);
125         BridgeInterfaceEntryKey bridgeInterfaceEntryKey = new BridgeInterfaceEntryKey(interfaceNew.getName());
126
127         LOG.debug("creating bridge interfaceEntry in ConfigDS {}", bridgeEntryKey);
128         InterfaceMetaUtils.createBridgeInterfaceEntryInConfigDS(bridgeEntryKey, bridgeInterfaceEntryKey,
129                 interfaceNew.getName(), transaction);
130         futures.add(transaction.submit());
131
132         // create bridge on switch, if switch is connected
133         BridgeRefEntryKey BridgeRefEntryKey = new BridgeRefEntryKey(dpId);
134         InstanceIdentifier<BridgeRefEntry> dpnBridgeEntryIid =
135                 InterfaceMetaUtils.getBridgeRefEntryIdentifier(BridgeRefEntryKey);
136         BridgeRefEntry bridgeRefEntry =
137                 InterfaceMetaUtils.getBridgeRefEntryFromOperDS(dpnBridgeEntryIid, dataBroker);
138         if(bridgeRefEntry != null && bridgeRefEntry.getBridgeReference() != null) {
139             LOG.debug("creating bridge interface on dpn {}", bridgeEntryKey);
140             InstanceIdentifier<OvsdbBridgeAugmentation> bridgeIid =
141                     (InstanceIdentifier<OvsdbBridgeAugmentation>) bridgeRefEntry.getBridgeReference().getValue();
142             Optional<OvsdbBridgeAugmentation> bridgeNodeOptional =
143                     IfmUtil.read(LogicalDatastoreType.OPERATIONAL, bridgeIid, dataBroker);
144             if (bridgeNodeOptional.isPresent()) {
145                 OvsdbBridgeAugmentation ovsdbBridgeAugmentation = bridgeNodeOptional.get();
146                 String bridgeName = ovsdbBridgeAugmentation.getBridgeName().getValue();
147                 SouthboundUtils.addPortToBridge(bridgeIid, interfaceNew,
148                         ovsdbBridgeAugmentation, bridgeName, interfaceNew.getName(), dataBroker, futures);
149
150                 // if TEP is already configured on switch, start LLDP monitoring and program tunnel ingress flow
151                 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
152                         InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceNew.getName(), dataBroker);
153                 if(ifState != null){
154                     NodeConnectorId ncId = IfmUtil.getNodeConnectorIdFromInterface(ifState);
155                     if(ncId != null) {
156                         long portNo = Long.valueOf(IfmUtil.getPortNoFromNodeConnectorId(ncId));
157                         InterfaceManagerCommonUtils.makeTunnelIngressFlow(futures, mdsalApiManager, interfaceNew.getAugmentation(IfTunnel.class),
158                                 dpId, portNo, interfaceNew, ifState.getIfIndex(), NwConstants.ADD_FLOW);
159                         // start LLDP monitoring for the tunnel interface
160                         AlivenessMonitorUtils.startLLDPMonitoring(alivenessMonitorService, dataBroker, interfaceNew);
161                     }
162                 }
163             }
164         }
165     }
166 }