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.List;
14 import java.util.Objects;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.genius.infra.Datastore;
17 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
18 import org.opendaylight.genius.infra.TypedWriteTransaction;
19 import org.opendaylight.genius.itm.globals.ITMConstants;
20 import org.opendaylight.genius.itm.impl.ItmUtils;
21 import org.opendaylight.genius.mdsalutil.MDSALUtil;
22 import org.opendaylight.mdsal.binding.api.DataBroker;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.NotHostedTransportZones;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.TransportZones;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.not.hosted.transport.zones.TepsInNotHostedTransportZone;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.not.hosted.transport.zones.TepsInNotHostedTransportZoneKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.not.hosted.transport.zones.tepsinnothostedtransportzone.UnknownVteps;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.not.hosted.transport.zones.tepsinnothostedtransportzone.UnknownVtepsKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.TransportZone;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.TransportZoneKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.Vteps;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.VtepsKey;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.opendaylight.yangtools.yang.common.Uint64;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public final class OvsdbTepRemoveConfigHelper {
42 private static final Logger LOG = LoggerFactory.getLogger(OvsdbTepRemoveConfigHelper.class);
44 private OvsdbTepRemoveConfigHelper() {
49 * Removes the TEP from ITM configuration/operational Datastore in one of the following cases.
50 * 1) default transport zone
51 * 2) Configured transport zone
52 * 3) Unhosted transport zone
53 * Function checks for above three cases and calls other sub-function to remove the TEP
55 * @param tepIp TEP-IP address in string
56 * @param strDpnId bridge datapath ID in string
57 * @param tzName transport zone name in string
58 * @param dataBroker data broker handle to perform operations on config/operational datastore
59 * @param txRunner ManagedNewTransactionRunner object
62 public static List<ListenableFuture<Void>> removeTepReceivedFromOvsdb(String tepIp, String strDpnId, String tzName,
63 DataBroker dataBroker,
64 ManagedNewTransactionRunner txRunner) {
65 List<ListenableFuture<Void>> futures = new ArrayList<>();
66 Uint64 dpnId = Uint64.ZERO;
67 LOG.trace("Remove TEP: TEP-IP: {}, TZ name: {}, DPID: {}", tepIp, tzName, strDpnId);
69 if (strDpnId != null && !strDpnId.isEmpty()) {
70 dpnId = MDSALUtil.getDpnId(strDpnId);
73 IpAddress tepIpAddress = IpAddressBuilder.getDefaultInstance(tepIp);
74 TransportZone transportZone;
76 // Case: TZ name is not given from OVS's other_config parameters.
78 tzName = ITMConstants.DEFAULT_TRANSPORT_ZONE;
79 // add TEP into default-TZ
80 transportZone = ItmUtils.getTransportZoneFromConfigDS(tzName, dataBroker);
81 if (transportZone == null) {
82 LOG.error("Error: default-transport-zone is not yet created.");
85 LOG.trace("Remove TEP from default-transport-zone.");
87 // Case: Add TEP into corresponding TZ created from Northbound.
88 transportZone = ItmUtils.getTransportZoneFromConfigDS(tzName, dataBroker);
91 if (transportZone == null) {
92 // Case: TZ is not configured from Northbound, then add TEP into
93 // "teps-in-not-hosted-transport-zone"
94 LOG.trace("Removing TEP from teps-in-not-hosted-transport-zone list.");
95 futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(Datastore.OPERATIONAL,
96 tx -> removeUnknownTzTepFromTepsNotHosted(name, tepIpAddress, id, dataBroker, tx)));
99 LOG.trace("Remove TEP from transport-zone already configured by Northbound.");
103 // Remove TEP from (default transport-zone) OR (transport-zone already configured by Northbound)
105 @Nullable Map<VtepsKey, Vteps> vtepList = transportZone.getVteps();
106 if (vtepList == null || vtepList.isEmpty()) {
107 // case: vtep list does not exist or it has no elements
108 LOG.trace("No vtep list in subnet list of transport-zone. Nothing to do.");
110 // case: vtep list has elements
111 boolean vtepFound = false;
112 Vteps oldVtep = null;
114 for (Vteps vtep : vtepList.values()) {
115 if (Objects.equals(vtep.getDpnId(), dpnId)) {
122 // vtep is found, update it with tep-ip
123 LOG.trace("Remove TEP from vtep list in subnet list of transport-zone.");
124 dpnId = oldVtep.getDpnId();
125 String name = tzName;
127 futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(Datastore.CONFIGURATION,
128 tx -> removeVtepFromTZConfig(name, id, tx)));
131 "TEP is not found in the vtep list in subnet list of transport-zone. Nothing to do.");
138 * Removes the TEP from subnet list in the transport zone list
139 * from ITM configuration Datastore by delete operation with write transaction.
141 * @param dpnId bridge datapath ID
142 * @param tzName transport zone name in string
143 * @param tx TypedWriteTransaction object
145 private static void removeVtepFromTZConfig(String tzName, Uint64 dpnId,
146 TypedWriteTransaction<Datastore.Configuration> tx) {
148 VtepsKey vtepkey = new VtepsKey(dpnId);
150 InstanceIdentifier<Vteps> vtepPath = InstanceIdentifier.builder(TransportZones.class)
151 .child(TransportZone.class, new TransportZoneKey(tzName))
152 .child(Vteps.class, vtepkey).build();
154 LOG.trace("Removing TEP from (TZ: {} DPN-ID: {}) inside ITM Config DS.",
161 * Removes the TEP from the not-hosted transport zone in the TepsNotHosted list
162 * from ITM Operational Datastore.
164 * @param tzName transport zone name in string
165 * @param tepIpAddress TEP IP address in IpAddress object
166 * @param dpnId bridge datapath ID
167 * @param dataBroker data broker handle to perform operations on operational datastore
168 * @param tx TypedWriteTransaction object
170 public static void removeUnknownTzTepFromTepsNotHosted(String tzName, IpAddress tepIpAddress,
171 Uint64 dpnId, DataBroker dataBroker,
172 TypedWriteTransaction<Datastore.Operational> tx) {
173 Map<UnknownVtepsKey, UnknownVteps> vtepList;
174 TepsInNotHostedTransportZone tepsInNotHostedTransportZone =
175 ItmUtils.getUnknownTransportZoneFromITMOperDS(tzName, dataBroker);
176 if (tepsInNotHostedTransportZone == null) {
177 LOG.trace("Unhosted TransportZone ({}) does not exist in OperDS. Nothing to do for TEP removal.", tzName);
180 vtepList = tepsInNotHostedTransportZone.getUnknownVteps();
181 if (vtepList == null || vtepList.isEmpty()) {
182 // case: vtep list does not exist or it has no elements
183 LOG.trace("Remove TEP from unhosted TZ ({}) when no vtep-list in the TZ. Nothing to do.", tzName);
185 // case: vtep list has elements
186 boolean vtepFound = false;
188 for (UnknownVteps vtep : vtepList.values()) {
189 if (Objects.equals(vtep.getDpnId(), dpnId)) {
195 // vtep is found, update it with tep-ip
197 "Remove TEP with IP ({}) from unhosted TZ ({}) inside not-hosted-transport-zones list.",
198 tepIpAddress, tzName);
199 if (vtepList.size() == 1) {
200 removeTzFromTepsNotHosted(tzName, tx);
202 removeVtepFromTepsNotHosted(tzName, dpnId, tx);
210 * Removes the TEP from unknown vtep list under the transport zone in the TepsNotHosted list
211 * from ITM operational Datastore by delete operation with write transaction.
213 * @param tzName transport zone name in string
214 * @param dpnId bridge datapath ID
215 * @param tx TypedWriteTransaction object
217 private static void removeVtepFromTepsNotHosted(String tzName, Uint64 dpnId,
218 TypedWriteTransaction<Datastore.Operational> tx) {
219 InstanceIdentifier<UnknownVteps> vtepPath = InstanceIdentifier.builder(NotHostedTransportZones.class)
220 .child(TepsInNotHostedTransportZone.class, new TepsInNotHostedTransportZoneKey(tzName))
221 .child(UnknownVteps.class, new UnknownVtepsKey(dpnId)).build();
222 LOG.trace("Removing TEP from unhosted (TZ: {}, DPID: {}) inside ITM Oper DS.", tzName, dpnId);
227 * Removes the transport zone in the TepsNotHosted list
228 * from ITM operational Datastore by delete operation with write transaction.
230 * @param tzName transport zone name in string
231 * @param tx TypedWriteTransaction object
233 private static void removeTzFromTepsNotHosted(String tzName, TypedWriteTransaction<Datastore.Operational> tx) {
234 InstanceIdentifier<TepsInNotHostedTransportZone> tepsInNotHostedTransportZoneIid =
235 InstanceIdentifier.builder(NotHostedTransportZones.class).child(TepsInNotHostedTransportZone.class,
236 new TepsInNotHostedTransportZoneKey(tzName)).build();
237 LOG.trace("Removing TZ ({})from not-hosted-transport-zones list inside ITM Oper DS.", tzName);
238 tx.delete(tepsInNotHostedTransportZoneIid);