2 * Copyright (c) 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.genius.itm.confighelpers;
10 import java.math.BigInteger;
11 import java.util.List;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.genius.itm.globals.ITMConstants;
17 import org.opendaylight.genius.itm.impl.ItmUtils;
18 import org.opendaylight.genius.mdsalutil.MDSALUtil;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
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.TransportZones;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.*;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.tepsnothostedintransportzone.UnknownVteps;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.tepsnothostedintransportzone.UnknownVtepsKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.Subnets;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.SubnetsKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.subnets.Vteps;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.subnets.VtepsKey;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class OvsdbTepRemoveConfigHelper {
35 private static final Logger LOG = LoggerFactory.getLogger(OvsdbTepRemoveConfigHelper.class);
38 * Removes the TEP from ITM configuration Datastore in one of the following cases:
39 * 1) default transport zone
40 * 2) Configured transport zone
41 * 3) Unhosted transport zone
42 * Function checks for above three cases and calls other sub-function to remove the TEP
44 * @param tepIp TEP-IP address in string
45 * @param strDpnId DPN datapath ID in string
46 * @param tzName transport zone name in string
47 * @param dataBroker data broker handle to perform operations on config datastore
48 * @param wrTx WriteTransaction object
50 public static void removeTepReceivedFromOvsdb(String tepIp, String strDpnId, String tzName,
51 DataBroker dataBroker, WriteTransaction wrTx) {
52 BigInteger dpnId = BigInteger.valueOf(0);
54 LOG.trace("Remove TEP: TEP-IP: {}, TZ name: {}, DPN ID: {}", tepIp, tzName, strDpnId);
56 if (strDpnId != null && !strDpnId.isEmpty()) {
57 dpnId = MDSALUtil.getDpnId(strDpnId);
61 IpAddress tepIpAddress = new IpAddress(tepIp.toCharArray());
62 TransportZone tZone = null;
64 // Case: TZ name is not given from OVS's other_config parameters.
66 tzName = ITMConstants.DEFAULT_TRANSPORT_ZONE;
67 // add TEP into default-TZ
68 tZone = ItmUtils.getTransportZoneFromConfigDS(tzName, dataBroker);
70 LOG.error("Error: default-transport-zone is not yet created.");
73 LOG.trace("Remove TEP from default-transport-zone.");
75 // Case: Add TEP into corresponding TZ created from Northbound.
76 tZone = ItmUtils.getTransportZoneFromConfigDS(tzName, dataBroker);
78 // Case: TZ is not configured from Northbound, then add TEP into
79 // "teps-not-hosted-in-transport-zone"
80 LOG.trace("Removing TEP from unknown TZ into teps-not-hosted-in-transport-zone.");
81 removeUnknownTzTepFromTepsNotHosted(tzName, tepIpAddress, dpnId, dataBroker, wrTx);
84 LOG.trace("Remove TEP from transport-zone already configured by Northbound.");
88 // Remove TEP from (default transport-zone) OR (transport-zone already configured by Northbound)
90 // Get subnet list of corresponding TZ created from Northbound.
91 List<Subnets> subnetList = tZone.getSubnets();
93 if (subnetList == null || subnetList.isEmpty()) {
94 LOG.trace("No subnet list in transport-zone. Nothing to do.");
97 IpPrefix subnetMaskObj = ItmUtils.getDummySubnet();
99 List<Vteps> vtepList = null;
101 // subnet list already exists case; check for dummy-subnet
102 for (Subnets subnet : subnetList) {
103 if (subnet.getKey().getPrefix().equals(subnetMaskObj)) {
104 LOG.trace("Subnet exists in the subnet list of transport-zone {}.", tzName);
105 // get vtep list of existing subnet
106 vtepList = subnet.getVteps();
111 if (vtepList == null || vtepList.isEmpty()) {
112 // case: vtep list does not exist or it has no elements
113 LOG.trace("No vtep list in subnet list of transport-zone. Nothing to do.");
115 // case: vtep list has elements
116 boolean vtepFound = false;
117 Vteps oldVtep = null;
119 for (Vteps vtep : vtepList) {
120 if (vtep.getDpnId().equals(dpnId)) {
123 // get portName of existing vtep
124 portName = vtep.getPortname();
129 // vtep is found, update it with tep-ip
130 LOG.trace("Remove TEP from vtep list in subnet list of transport-zone.");
131 dpnId = oldVtep.getDpnId();
132 portName = oldVtep.getPortname();
133 removeVtepFromTZConfig(subnetMaskObj, tzName, dpnId, portName, wrTx);
136 "TEP is not found in the vtep list in subnet list of transport-zone. Nothing to do.");
143 * Removes the TEP from subnet list in the transport zone list
144 * from ITM configuration Datastore by delete operation with write transaction.
146 * @param subnetMaskObj subnet mask in IpPrefix object
147 * @param dpnId DPN datapath ID in BigInteger
148 * @param tzName transport zone name in string
149 * @param portName port name as a part of VtepsKey
150 * @param wrTx WriteTransaction object
152 protected static void removeVtepFromTZConfig(IpPrefix subnetMaskObj, String tzName, BigInteger dpnId,
153 String portName, WriteTransaction wrTx) {
154 SubnetsKey subnetsKey = new SubnetsKey(subnetMaskObj);
155 VtepsKey vtepkey = new VtepsKey(dpnId, portName);
157 InstanceIdentifier<Vteps> vTepPath = InstanceIdentifier.builder(TransportZones.class)
158 .child(TransportZone.class, new TransportZoneKey(tzName))
159 .child(Subnets.class, subnetsKey).child(Vteps.class, vtepkey).build();
161 LOG.trace("Removing TEP (TZ: {} Subnet: {} DPN-ID: {}) in ITM Config DS.", tzName,
162 subnetMaskObj.getValue().toString(), dpnId);
164 wrTx.delete(LogicalDatastoreType.CONFIGURATION, vTepPath);
168 * Removes the TEP from the not-hosted transport zone in the TepsNotHosted list
169 * from ITM configuration Datastore
171 * @param tzName transport zone name in string
172 * @param tepIpAddress TEP IP address in IpAddress object
173 * @param dpnId DPN datapath ID in BigInteger
174 * @param dataBroker data broker handle to perform operations on config datastore
175 * @param wrTx WriteTransaction object
177 public static void removeUnknownTzTepFromTepsNotHosted(String tzName, IpAddress tepIpAddress,
178 BigInteger dpnId, DataBroker dataBroker, WriteTransaction wrTx) {
179 List<UnknownVteps> vtepList = null;
181 TepsNotHostedInTransportZone unknownTz =
182 ItmUtils.getUnknownTransportZoneFromITMConfigDS(tzName, dataBroker);
183 if (unknownTz == null) {
184 LOG.trace("Unhosted TransportZone does not exist. Nothing to do for TEP removal.");
187 vtepList = unknownTz.getUnknownVteps();
188 if (vtepList == null || vtepList.isEmpty()) {
189 // case: vtep list does not exist or it has no elements
191 "Remove TEP in unhosted TZ ({}) when no vtep-list in the TZ. Nothing to do.",
194 // case: vtep list has elements
195 boolean vtepFound = false;
196 UnknownVteps foundVtep = null;
198 for (UnknownVteps vtep : vtepList) {
199 if (vtep.getDpnId().equals(dpnId)) {
206 // vtep is found, update it with tep-ip
208 "Remove TEP with IP ({}) from unhosted TZ ({}) in TepsNotHosted list.",
209 tepIpAddress, tzName);
210 if (vtepList.size() == 1) {
211 removeTzFromTepsNotHosted(tzName, wrTx);
213 removeVtepFromTepsNotHosted(tzName, dpnId, wrTx);
215 vtepList.remove(foundVtep);
222 * Removes the TEP from unknown vtep list under the transport zone in the TepsNotHosted list
223 * from ITM configuration Datastore by delete operation with write transaction.
225 * @param tzName transport zone name in string
226 * @param dpnId DPN datapath ID in BigInteger
227 * @param wrTx WriteTransaction object
229 protected static void removeVtepFromTepsNotHosted(String tzName, BigInteger dpnId,
230 WriteTransaction wrTx) {
232 UnknownVtepsKey unknownVtepkey = new UnknownVtepsKey(dpnId);
233 InstanceIdentifier<UnknownVteps> vTepPath = InstanceIdentifier.builder(TransportZones.class)
234 .child(TepsNotHostedInTransportZone.class, new TepsNotHostedInTransportZoneKey(tzName))
235 .child(UnknownVteps.class, unknownVtepkey).build();
237 LOG.trace("Removing TEP from unhosted (TZ: {}, DPID: {}) from ITM Config DS.", tzName,
240 wrTx.delete(LogicalDatastoreType.CONFIGURATION, vTepPath);
244 * Removes the transport zone in the TepsNotHosted list
245 * from ITM configuration Datastore by delete operation with write transaction.
247 * @param tzName transport zone name in string
248 * @param wrTx WriteTransaction object
250 protected static void removeTzFromTepsNotHosted(String tzName, WriteTransaction wrTx) {
251 InstanceIdentifier<TepsNotHostedInTransportZone> tzTepsNotHostedTepPath =
252 InstanceIdentifier.builder(TransportZones.class)
253 .child(TepsNotHostedInTransportZone.class,
254 new TepsNotHostedInTransportZoneKey(tzName)).build();
256 LOG.trace("Removing TZ ({})from TepsNotHosted list from ITM Config DS.", tzName);
257 // remove TZ from TepsNotHosted list
258 wrTx.delete(LogicalDatastoreType.CONFIGURATION, tzTepsNotHostedTepPath);