2 * Copyright (c) 2016, 2017 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.genius.itm.confighelpers;
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.HashMap;
14 import java.util.List;
16 import java.util.Objects;
17 import org.opendaylight.genius.infra.Datastore;
18 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
19 import org.opendaylight.genius.infra.TypedWriteTransaction;
20 import org.opendaylight.genius.itm.globals.ITMConstants;
21 import org.opendaylight.genius.itm.impl.ItmUtils;
22 import org.opendaylight.genius.mdsalutil.MDSALUtil;
23 import org.opendaylight.mdsal.binding.api.DataBroker;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.NotHostedTransportZones;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.TransportZones;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.not.hosted.transport.zones.TepsInNotHostedTransportZone;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.not.hosted.transport.zones.TepsInNotHostedTransportZoneBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.not.hosted.transport.zones.TepsInNotHostedTransportZoneKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.not.hosted.transport.zones.tepsinnothostedtransportzone.UnknownVteps;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.not.hosted.transport.zones.tepsinnothostedtransportzone.UnknownVtepsBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.not.hosted.transport.zones.tepsinnothostedtransportzone.UnknownVtepsKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.TransportZone;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.TransportZoneBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.TransportZoneKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.Vteps;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.VtepsBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.VtepsKey;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.opendaylight.yangtools.yang.common.Uint64;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 public final class OvsdbTepAddConfigHelper {
47 private static final Logger LOG = LoggerFactory.getLogger(OvsdbTepAddConfigHelper.class);
49 private OvsdbTepAddConfigHelper() {
54 * Adds the TEP into ITM configuration/operational Datastore in one of the following cases.
55 * 1) default transport zone
56 * 2) Configured transport zone
57 * 3) Unhosted transport zone
59 * @param tepIp TEP-IP address in string
60 * @param strDpnId bridge datapath ID in string
61 * @param tzName transport zone name in string
62 * @param ofTunnel boolean flag for TEP to enable/disable of-tunnel feature on it
63 * @param dataBroker data broker handle to perform operations on config/operational datastore
64 * @param txRunner ManagedTransactionRunner object
67 public static List<ListenableFuture<Void>> addTepReceivedFromOvsdb(String tepIp, String strDpnId, String tzName,
68 boolean ofTunnel, DataBroker dataBroker,
69 ManagedNewTransactionRunner txRunner) {
70 List<ListenableFuture<Void>> futures = new ArrayList<>();
71 Uint64 dpnId = Uint64.ZERO;
73 if (strDpnId != null && !strDpnId.isEmpty()) {
74 dpnId = MDSALUtil.getDpnId(strDpnId);
78 IpAddress tepIpAddress = IpAddressBuilder.getDefaultInstance(tepIp);
79 TransportZone tzone = null;
81 // Case: TZ name is not given with OVS TEP.
83 tzName = ITMConstants.DEFAULT_TRANSPORT_ZONE;
84 // add TEP into default-TZ
85 tzone = ItmUtils.getTransportZoneFromConfigDS(tzName, dataBroker);
87 // Case: default-TZ is not yet created, then add TEP into "teps-in-not-hosted-transport-zone"
88 LOG.trace("Adding TEP with default TZ into teps-in-not-hosted-transport-zone.");
89 return addUnknownTzTepIntoTepsNotHostedAndReturnFutures(tzName, tepIpAddress, dpnId, ofTunnel,
90 dataBroker, txRunner);
92 LOG.trace("Add TEP into default-transport-zone.");
94 // Case: Add TEP into corresponding TZ created from Northbound.
95 tzone = ItmUtils.getTransportZoneFromConfigDS(tzName, dataBroker);
97 // Case: TZ is not configured from Northbound, then add TEP into "teps-in-not-hosted-transport-zone"
98 LOG.trace("Adding TEP with unknown TZ into teps-in-not-hosted-transport-zone.");
99 return addUnknownTzTepIntoTepsNotHostedAndReturnFutures(tzName, tepIpAddress, dpnId, ofTunnel,
100 dataBroker, txRunner);
102 LOG.trace("Add TEP into transport-zone already configured by Northbound.");
107 final Uint64 id = dpnId;
108 final String name = tzName;
109 futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(Datastore.CONFIGURATION,
110 tx -> addConfig(name, id, tepIpAddress, ofTunnel, tx)));
115 * Adds the TEP into Vtep list in the subnet list in the transport zone list
116 * from ITM configuration Datastore by merge operation with write transaction.
118 * @param updatedVtepList updated Vteps list object which will have new TEP for addition
119 * @param tepIpAddress TEP IP address in IpAddress object
120 * @param tzName transport zone name in string
121 * @param dpid bridge datapath ID
122 * @param ofTunnel boolean flag for TEP to enable/disable of-tunnel feature on it
123 * @param tx TypedWriteTransaction object
125 public static void addVtepInITMConfigDS(List<Vteps> updatedVtepList, IpAddress tepIpAddress, String tzName,
126 Uint64 dpid, boolean ofTunnel,
127 TypedWriteTransaction<Datastore.Configuration> tx) {
128 //Create TZ node path
129 InstanceIdentifier<TransportZone> tranzportZonePath =
130 InstanceIdentifier.builder(TransportZones.class)
131 .child(TransportZone.class, new TransportZoneKey(tzName)).build();
133 // this check is needed to reuse same function from TransportZoneListener
134 // when VTEP is moved from TepsNotHosted list to TZ configured from Northbound.
135 if (dpid.compareTo(Uint64.ZERO) > 0) {
137 VtepsKey vtepkey = new VtepsKey(dpid);
139 new VtepsBuilder().setDpnId(dpid).setIpAddress(tepIpAddress).withKey(vtepkey)
140 .setOptionOfTunnel(ofTunnel).build();
142 // Add vtep obtained from bridge into list
143 updatedVtepList.add(vtepObj);
145 LOG.trace("Adding TEP (TZ: {} TEP IP: {} DPID: {}, of-tunnel: {}) in ITM Config DS.", tzName,
146 tepIpAddress, dpid, ofTunnel);
148 // this is case when this function is called while TEPs movement from tepsNotHosted list when
149 // corresponding TZ is configured from northbound.
150 for (Vteps vtep: updatedVtepList) {
151 LOG.trace("Moving TEP (TEP IP: {} DPID: {}, of-tunnel: {})"
152 + "from not-hosted-transport-zone {} into ITM Config DS.",
153 vtep.getIpAddress(), vtep.getDpnId(), ofTunnel, tzName);
157 // create TZ node with updated subnet having new vtep
158 TransportZone updatedTzone =
159 new TransportZoneBuilder().withKey(new TransportZoneKey(tzName)).setVteps(updatedVtepList)
160 .setZoneName(tzName).build();
162 // Update TZ in Config DS to add vtep in TZ
163 tx.mergeParentStructureMerge(tranzportZonePath, updatedTzone);
167 * Adds the TEP into Vtep list in the subnet list in the transport zone list
168 * from ITM operational Datastore by merge operation with write transaction.
170 * @param tzName transport zone name in string
171 * @param tepIpAddress TEP IP address in IpAddress object
172 * @param dpid bridge datapath ID
173 * @param ofTunnel boolean flag for TEP to enable/disable of-tunnel feature on it
174 * @param dataBroker data broker handle to perform operations on operational datastore
175 * @param tx TypedWriteTransaction object
177 protected static void addUnknownTzTepIntoTepsNotHosted(String tzName, IpAddress tepIpAddress,
178 Uint64 dpid, boolean ofTunnel, DataBroker dataBroker,
179 TypedWriteTransaction<Datastore.Operational> tx) {
180 Map<UnknownVtepsKey, UnknownVteps> vtepList;
181 TepsInNotHostedTransportZone tepsInNotHostedTransportZone =
182 ItmUtils.getUnknownTransportZoneFromITMOperDS(tzName, dataBroker);
183 if (tepsInNotHostedTransportZone == null) {
184 LOG.trace("Unhosted TransportZone ({}) does not exist in OperDS.", tzName);
185 vtepList = new HashMap<>();
186 addVtepIntoTepsNotHosted(addVtepToUnknownVtepsList(vtepList, tepIpAddress, dpid, ofTunnel), tzName, tx);
188 vtepList = tepsInNotHostedTransportZone.getUnknownVteps();
189 if (vtepList == null || vtepList.isEmpty()) {
190 // case: vtep list does not exist or it has no elements
191 if (vtepList == null) {
192 LOG.trace("Add TEP into unhosted TZ ({}) when no vtep-list in the TZ.", tzName);
193 vtepList = new HashMap<>();
195 LOG.trace("Add TEP into unhosted TZ ({}) when no vtep-list in the TZ.", tzName);
196 addVtepIntoTepsNotHosted(addVtepToUnknownVtepsList(vtepList, tepIpAddress, dpid, ofTunnel), tzName, tx);
198 // case: vtep list has elements
199 boolean vtepFound = false;
200 UnknownVteps oldVtep = null;
202 for (UnknownVteps vtep : vtepList.values()) {
203 if (Objects.equals(vtep.getDpnId(), dpid)) {
210 addVtepIntoTepsNotHosted(addVtepToUnknownVtepsList(vtepList,
211 tepIpAddress, dpid, ofTunnel), tzName, tx);
213 // vtep is found, update it with tep-ip
214 vtepList.remove(oldVtep.key());
215 addVtepIntoTepsNotHosted(addVtepToUnknownVtepsList(vtepList,
216 tepIpAddress, dpid, ofTunnel), tzName, tx);
223 * Adds the TEP into Unknown Vtep list under the transport zone in the TepsNotHosted list
224 * from ITM operational Datastore by merge operation with write transaction.
226 * @param updatedVtepList updated UnknownVteps list object which will have new TEP for addition
228 * @param tzName transport zone name in string
229 * @param tx TypedWriteTransaction object
231 protected static void addVtepIntoTepsNotHosted(Map<UnknownVtepsKey, UnknownVteps> updatedVtepList, String tzName,
232 TypedWriteTransaction<Datastore.Operational> tx) {
233 //Create TZ node path
234 InstanceIdentifier<TepsInNotHostedTransportZone> tepsInNotHostedTransportZoneIid =
235 InstanceIdentifier.builder(NotHostedTransportZones.class)
236 .child(TepsInNotHostedTransportZone.class,
237 new TepsInNotHostedTransportZoneKey(tzName)).build();
239 // create unknown TZ node with updated vtep list
240 TepsInNotHostedTransportZone updatedTzone = new TepsInNotHostedTransportZoneBuilder()
241 .withKey(new TepsInNotHostedTransportZoneKey(tzName)).setZoneName(tzName)
242 .setUnknownVteps(updatedVtepList).build();
244 // Update TZ in Oper DS.
245 tx.mergeParentStructureMerge(tepsInNotHostedTransportZoneIid, updatedTzone);
248 private static void addConfig(String tzName, Uint64 dpnId, IpAddress ipAdd,
249 boolean ofTunnel, TypedWriteTransaction<Datastore.Configuration> tx) {
250 List<Vteps> vtepList = new ArrayList<>();
252 LOG.trace("Add TEP in transport-zone when no vtep-list for specific subnet.");
253 addVtepInITMConfigDS(vtepList, ipAdd, tzName, dpnId, ofTunnel, tx);
256 private static List<ListenableFuture<Void>> addUnknownTzTepIntoTepsNotHostedAndReturnFutures(String tzName,
257 IpAddress tepIpAddress, Uint64 id, boolean ofTunnel,
258 DataBroker dataBroker, ManagedNewTransactionRunner txRunner) {
259 return Collections.singletonList(txRunner.callWithNewWriteOnlyTransactionAndSubmit(Datastore.OPERATIONAL,
260 tx -> addUnknownTzTepIntoTepsNotHosted(tzName, tepIpAddress, id, ofTunnel, dataBroker, tx)));
263 private static Map<UnknownVtepsKey, UnknownVteps> addVtepToUnknownVtepsList(Map<UnknownVtepsKey,
264 UnknownVteps> updatedVtepList, IpAddress tepIpAddress, Uint64 dpid, boolean ofTunnel) {
266 UnknownVtepsKey vtepkey = new UnknownVtepsKey(dpid);
267 UnknownVteps vtepObj =
268 new UnknownVtepsBuilder().setDpnId(dpid).setIpAddress(tepIpAddress).withKey(vtepkey)
269 .setOfTunnel(ofTunnel).build();
271 // Add vtep obtained into unknown TZ tep list
272 updatedVtepList.put(vtepObj.key(),vtepObj);
273 LOG.trace("Adding TEP (DPID: {}, TEP IP: {}, of-tunnel: {}) into unhosted Transport Zone"
274 + "inside ITM Oper DS.", dpid, tepIpAddress, ofTunnel);
275 return updatedVtepList;