2 * Copyright (c) 2015 - 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.vpnservice.interfacemgr.renderer.ovs.confighelpers;
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.List;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
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.renderer.ovs.utilities.SouthboundUtils;
22 import org.opendaylight.vpnservice.mdsalutil.NwConstants;
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.Interface;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629.AlivenessMonitorService;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info.InterfaceParentEntry;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info.InterfaceParentEntryKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info._interface.parent.entry.InterfaceChildEntry;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.BridgeEntryKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.bridge.entry.BridgeInterfaceEntryKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge.ref.info.BridgeRefEntry;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge.ref.info.BridgeRefEntryKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfL2vlan;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfTunnel;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.ParentRefs;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 import com.google.common.base.Optional;
44 import com.google.common.util.concurrent.ListenableFuture;
46 public class OvsInterfaceConfigAddHelper {
47 private static final Logger LOG = LoggerFactory.getLogger(OvsInterfaceConfigAddHelper.class);
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<>();
55 IfTunnel ifTunnel = interfaceNew.getAugmentation(IfTunnel.class);
56 if (ifTunnel != null) {
57 addTunnelConfiguration(dataBroker, parentRefs, interfaceNew, idManager, alivenessMonitorService,
58 mdsalApiManager, futures);
62 addVlanConfiguration(interfaceNew, parentRefs, dataBroker, idManager, futures);
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() && IfL2vlan.L2vlanMode.Transparent != ifL2vlan.getL2vlanMode())) {
72 WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
73 if(!InterfaceManagerCommonUtils.createInterfaceChildEntryIfNotPresent(dataBroker, transaction,
74 parentRefs.getParentInterface(), interfaceNew.getName())){
77 LOG.debug("adding vlan configuration for {}",interfaceNew.getName());
79 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
80 InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(parentRefs.getParentInterface(), dataBroker);
82 if (ifState == null) {
83 LOG.debug("could not retrieve interface state corresponding to {}",interfaceNew.getName());
84 futures.add(transaction.submit());
88 InterfaceManagerCommonUtils.addStateEntry(interfaceNew.getName(), transaction, dataBroker, idManager, ifState);
90 InterfaceParentEntryKey interfaceParentEntryKey = new InterfaceParentEntryKey(interfaceNew.getName());
91 InterfaceParentEntry interfaceParentEntry =
92 InterfaceMetaUtils.getInterfaceParentEntryFromConfigDS(interfaceParentEntryKey, dataBroker);
93 if (interfaceParentEntry == null || interfaceParentEntry.getInterfaceChildEntry() == null) {
94 LOG.debug("could not retrieve interface parent info for {}",interfaceNew.getName());
95 futures.add(transaction.submit());
99 //FIXME: If the no. of child entries exceeds 100, perform txn updates in batches of 100.
100 for (InterfaceChildEntry interfaceChildEntry : interfaceParentEntry.getInterfaceChildEntry()) {
101 InterfaceManagerCommonUtils.addStateEntry(interfaceChildEntry.getChildInterface(), transaction, dataBroker, idManager,ifState);
103 futures.add(transaction.submit());
106 private static void addTunnelConfiguration(DataBroker dataBroker, ParentRefs parentRefs,
107 Interface interfaceNew, IdManagerService idManager,
108 AlivenessMonitorService alivenessMonitorService,
109 IMdsalApiManager mdsalApiManager,
110 List<ListenableFuture<Void>> futures) {
111 LOG.debug("adding tunnel configuration for {}", interfaceNew.getName());
112 WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
113 if (parentRefs == null) {
114 LOG.warn("ParentRefs for interface: {} Not Found. " +
115 "Creation of Tunnel OF-Port not supported when dpid not provided.", interfaceNew.getName());
119 BigInteger dpId = parentRefs.getDatapathNodeIdentifier();
121 LOG.warn("dpid for interface: {} Not Found. No DPID provided. " +
122 "Creation of OF-Port not supported.", interfaceNew.getName());
126 BridgeEntryKey bridgeEntryKey = new BridgeEntryKey(dpId);
127 BridgeInterfaceEntryKey bridgeInterfaceEntryKey = new BridgeInterfaceEntryKey(interfaceNew.getName());
129 LOG.debug("creating bridge interfaceEntry in ConfigDS {}", bridgeEntryKey);
130 InterfaceMetaUtils.createBridgeInterfaceEntryInConfigDS(bridgeEntryKey, bridgeInterfaceEntryKey,
131 interfaceNew.getName(), transaction);
132 futures.add(transaction.submit());
134 // create bridge on switch, if switch is connected
135 BridgeRefEntryKey BridgeRefEntryKey = new BridgeRefEntryKey(dpId);
136 InstanceIdentifier<BridgeRefEntry> dpnBridgeEntryIid =
137 InterfaceMetaUtils.getBridgeRefEntryIdentifier(BridgeRefEntryKey);
138 BridgeRefEntry bridgeRefEntry =
139 InterfaceMetaUtils.getBridgeRefEntryFromOperDS(dpnBridgeEntryIid, dataBroker);
140 if(bridgeRefEntry != null && bridgeRefEntry.getBridgeReference() != null) {
141 LOG.debug("creating bridge interface on dpn {}", bridgeEntryKey);
142 InstanceIdentifier<OvsdbBridgeAugmentation> bridgeIid =
143 (InstanceIdentifier<OvsdbBridgeAugmentation>) bridgeRefEntry.getBridgeReference().getValue();
144 Optional<OvsdbBridgeAugmentation> bridgeNodeOptional =
145 IfmUtil.read(LogicalDatastoreType.OPERATIONAL, bridgeIid, dataBroker);
146 if (bridgeNodeOptional.isPresent()) {
147 OvsdbBridgeAugmentation ovsdbBridgeAugmentation = bridgeNodeOptional.get();
148 String bridgeName = ovsdbBridgeAugmentation.getBridgeName().getValue();
149 SouthboundUtils.addPortToBridge(bridgeIid, interfaceNew,
150 ovsdbBridgeAugmentation, bridgeName, interfaceNew.getName(), dataBroker, futures);
152 // if TEP is already configured on switch, start LLDP monitoring and program tunnel ingress flow
153 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
154 InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceNew.getName(), dataBroker);
156 NodeConnectorId ncId = IfmUtil.getNodeConnectorIdFromInterface(ifState);
158 long portNo = Long.valueOf(IfmUtil.getPortNoFromNodeConnectorId(ncId));
159 InterfaceManagerCommonUtils.makeTunnelIngressFlow(futures, mdsalApiManager, interfaceNew.getAugmentation(IfTunnel.class),
160 dpId, portNo, interfaceNew, ifState.getIfIndex(), NwConstants.ADD_FLOW);
161 // start LLDP monitoring for the tunnel interface
162 AlivenessMonitorUtils.startLLDPMonitoring(alivenessMonitorService, dataBroker, interfaceNew);