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 java.math.BigInteger;
11 import java.util.List;
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.genius.itm.globals.ITMConstants;
16 import org.opendaylight.genius.itm.impl.ItmUtils;
17 import org.opendaylight.genius.mdsalutil.MDSALUtil;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressBuilder;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.NotHostedTransportZones;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.TransportZones;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.not.hosted.transport.zones.TepsInNotHostedTransportZone;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.not.hosted.transport.zones.TepsInNotHostedTransportZoneKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.not.hosted.transport.zones.tepsinnothostedtransportzone.UnknownVteps;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.not.hosted.transport.zones.tepsinnothostedtransportzone.UnknownVtepsKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.TransportZone;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.TransportZoneKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.Subnets;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.SubnetsKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.subnets.Vteps;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.subnets.VtepsKey;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 public final class OvsdbTepRemoveConfigHelper {
39 private static final Logger LOG = LoggerFactory.getLogger(OvsdbTepRemoveConfigHelper.class);
41 private OvsdbTepRemoveConfigHelper() { }
44 * Removes the TEP from ITM configuration/operational Datastore in one of the following cases.
45 * 1) default transport zone
46 * 2) Configured transport zone
47 * 3) Unhosted transport zone
48 * Function checks for above three cases and calls other sub-function to remove the TEP
50 * @param tepIp TEP-IP address in string
51 * @param strDpnId bridge datapath ID in string
52 * @param tzName transport zone name in string
53 * @param dataBroker data broker handle to perform operations on config/operational datastore
54 * @param wrTx WriteTransaction object
57 public static void removeTepReceivedFromOvsdb(String tepIp, String strDpnId, String tzName,
58 DataBroker dataBroker, WriteTransaction wrTx) {
59 BigInteger dpnId = BigInteger.valueOf(0);
61 LOG.trace("Remove TEP: TEP-IP: {}, TZ name: {}, DPID: {}", tepIp, tzName, strDpnId);
63 if (strDpnId != null && !strDpnId.isEmpty()) {
64 dpnId = MDSALUtil.getDpnId(strDpnId);
68 IpAddress tepIpAddress = IpAddressBuilder.getDefaultInstance(tepIp);
69 TransportZone transportZone = null;
71 // Case: TZ name is not given from OVS's other_config parameters.
73 tzName = ITMConstants.DEFAULT_TRANSPORT_ZONE;
74 // add TEP into default-TZ
75 transportZone = ItmUtils.getTransportZoneFromConfigDS(tzName, dataBroker);
76 if (transportZone == null) {
77 LOG.error("Error: default-transport-zone is not yet created.");
80 LOG.trace("Remove TEP from default-transport-zone.");
82 // Case: Add TEP into corresponding TZ created from Northbound.
83 transportZone = ItmUtils.getTransportZoneFromConfigDS(tzName, dataBroker);
84 if (transportZone == null) {
85 // Case: TZ is not configured from Northbound, then add TEP into
86 // "teps-in-not-hosted-transport-zone"
87 LOG.trace("Removing TEP from teps-in-not-hosted-transport-zone list.");
88 removeUnknownTzTepFromTepsNotHosted(tzName, tepIpAddress, dpnId, dataBroker, wrTx);
91 LOG.trace("Remove TEP from transport-zone already configured by Northbound.");
95 // Remove TEP from (default transport-zone) OR (transport-zone already configured by Northbound)
97 // Get subnet list of corresponding TZ created from Northbound.
98 List<Subnets> subnetList = transportZone.getSubnets();
100 if (subnetList == null || subnetList.isEmpty()) {
101 LOG.trace("No subnet list in transport-zone. Nothing to do.");
103 IpPrefix subnetMaskObj = ItmUtils.getDummySubnet();
105 List<Vteps> vtepList = null;
107 // subnet list already exists case; check for dummy-subnet
108 for (Subnets subnet : subnetList) {
109 if (subnet.key().getPrefix().equals(subnetMaskObj)) {
110 LOG.trace("Subnet exists in the subnet list of transport-zone {}.", tzName);
111 // get vtep list of existing subnet
112 vtepList = subnet.getVteps();
117 if (vtepList == null || vtepList.isEmpty()) {
118 // case: vtep list does not exist or it has no elements
119 LOG.trace("No vtep list in subnet list of transport-zone. Nothing to do.");
121 // case: vtep list has elements
122 boolean vtepFound = false;
123 Vteps oldVtep = null;
125 for (Vteps vtep : vtepList) {
126 if (vtep.getDpnId().equals(dpnId)) {
133 // vtep is found, update it with tep-ip
134 LOG.trace("Remove TEP from vtep list in subnet list of transport-zone.");
135 dpnId = oldVtep.getDpnId();
136 String portName = oldVtep.getPortname();
137 removeVtepFromTZConfig(subnetMaskObj, tzName, dpnId, portName, wrTx);
140 "TEP is not found in the vtep list in subnet list of transport-zone. Nothing to do.");
147 * Removes the TEP from subnet list in the transport zone list
148 * from ITM configuration Datastore by delete operation with write transaction.
150 * @param subnetMaskObj subnet mask in IpPrefix object
151 * @param dpnId bridge datapath ID in BigInteger
152 * @param tzName transport zone name in string
153 * @param portName port name as a part of VtepsKey
154 * @param wrTx WriteTransaction object
156 private static void removeVtepFromTZConfig(IpPrefix subnetMaskObj, String tzName, BigInteger dpnId,
157 String portName, WriteTransaction wrTx) {
158 SubnetsKey subnetsKey = new SubnetsKey(subnetMaskObj);
159 VtepsKey vtepkey = new VtepsKey(dpnId, portName);
161 InstanceIdentifier<Vteps> vtepPath = InstanceIdentifier.builder(TransportZones.class)
162 .child(TransportZone.class, new TransportZoneKey(tzName))
163 .child(Subnets.class, subnetsKey).child(Vteps.class, vtepkey).build();
165 LOG.trace("Removing TEP from (TZ: {} Subnet: {} DPN-ID: {}) inside ITM Config DS.",
166 tzName, subnetMaskObj, dpnId);
168 wrTx.delete(LogicalDatastoreType.CONFIGURATION, vtepPath);
172 * Removes the TEP from the not-hosted transport zone in the TepsNotHosted list
173 * from ITM Operational Datastore.
175 * @param tzName transport zone name in string
176 * @param tepIpAddress TEP IP address in IpAddress object
177 * @param dpnId bridge datapath ID in BigInteger
178 * @param dataBroker data broker handle to perform operations on operational datastore
179 * @param wrTx WriteTransaction object
181 public static void removeUnknownTzTepFromTepsNotHosted(String tzName, IpAddress tepIpAddress,
182 BigInteger dpnId, DataBroker dataBroker,
183 WriteTransaction wrTx) {
184 List<UnknownVteps> vtepList = null;
186 TepsInNotHostedTransportZone tepsInNotHostedTransportZone =
187 ItmUtils.getUnknownTransportZoneFromITMOperDS(tzName, dataBroker);
188 if (tepsInNotHostedTransportZone == null) {
189 LOG.trace("Unhosted TransportZone ({}) does not exist in OperDS. Nothing to do for TEP removal.", tzName);
192 vtepList = tepsInNotHostedTransportZone.getUnknownVteps();
193 if (vtepList == null || vtepList.isEmpty()) {
194 // case: vtep list does not exist or it has no elements
195 LOG.trace("Remove TEP from unhosted TZ ({}) when no vtep-list in the TZ. Nothing to do.", tzName);
197 // case: vtep list has elements
198 boolean vtepFound = false;
199 UnknownVteps foundVtep = null;
201 for (UnknownVteps vtep : vtepList) {
202 if (vtep.getDpnId().equals(dpnId)) {
209 // vtep is found, update it with tep-ip
211 "Remove TEP with IP ({}) from unhosted TZ ({}) inside not-hosted-transport-zones list.",
212 tepIpAddress, tzName);
213 if (vtepList.size() == 1) {
214 removeTzFromTepsNotHosted(tzName, wrTx);
216 removeVtepFromTepsNotHosted(tzName, dpnId, wrTx);
218 vtepList.remove(foundVtep);
225 * Removes the TEP from unknown vtep list under the transport zone in the TepsNotHosted list
226 * from ITM operational Datastore by delete operation with write transaction.
228 * @param tzName transport zone name in string
229 * @param dpnId bridge datapath ID in BigInteger
230 * @param wrTx WriteTransaction object
232 private static void removeVtepFromTepsNotHosted(String tzName, BigInteger dpnId,
233 WriteTransaction wrTx) {
235 UnknownVtepsKey unknownVtepkey = new UnknownVtepsKey(dpnId);
236 InstanceIdentifier<UnknownVteps> vtepPath = InstanceIdentifier.builder(NotHostedTransportZones.class)
237 .child(TepsInNotHostedTransportZone.class, new TepsInNotHostedTransportZoneKey(tzName))
238 .child(UnknownVteps.class, unknownVtepkey).build();
240 LOG.trace("Removing TEP from unhosted (TZ: {}, DPID: {}) inside ITM Oper DS.",
243 wrTx.delete(LogicalDatastoreType.OPERATIONAL, vtepPath);
247 * Removes the transport zone in the TepsNotHosted list
248 * from ITM operational Datastore by delete operation with write transaction.
250 * @param tzName transport zone name in string
251 * @param wrTx WriteTransaction object
253 private static void removeTzFromTepsNotHosted(String tzName, WriteTransaction wrTx) {
254 InstanceIdentifier<TepsInNotHostedTransportZone> tepsInNotHostedTransportZoneIid =
255 InstanceIdentifier.builder(NotHostedTransportZones.class)
256 .child(TepsInNotHostedTransportZone.class,
257 new TepsInNotHostedTransportZoneKey(tzName)).build();
259 LOG.trace("Removing TZ ({})from not-hosted-transport-zones list inside ITM Oper DS.", tzName);
260 // remove TZ from TepsNotHosted list
261 wrTx.delete(LogicalDatastoreType.OPERATIONAL, tepsInNotHostedTransportZoneIid);