Merge "alivenessmonitor-impl reduce dependency to interfacemanager-api"
[genius.git] / itm / itm-impl / src / main / java / org / opendaylight / genius / itm / confighelpers / OvsdbTepRemoveConfigHelper.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.genius.itm.confighelpers;
9
10 import java.math.BigInteger;
11 import java.util.List;
12
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;
32
33 public class OvsdbTepRemoveConfigHelper {
34
35     private static final Logger LOG = LoggerFactory.getLogger(OvsdbTepRemoveConfigHelper.class);
36
37     /**
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
43      *
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
49      */
50     public static void removeTepReceivedFromOvsdb(String tepIp, String strDpnId, String tzName,
51         DataBroker dataBroker, WriteTransaction wrTx) {
52         BigInteger dpnId = BigInteger.valueOf(0);
53
54         LOG.trace("Remove TEP: TEP-IP: {}, TZ name: {}, DPN ID: {}", tepIp, tzName, strDpnId);
55
56         if (strDpnId != null && !strDpnId.isEmpty()) {
57             dpnId = MDSALUtil.getDpnId(strDpnId);
58         }
59
60         // Get tep IP
61         IpAddress tepIpAddress = new IpAddress(tepIp.toCharArray());
62         TransportZone tZone = null;
63
64         // Case: TZ name is not given from OVS's other_config parameters.
65         if (tzName == null) {
66             tzName = ITMConstants.DEFAULT_TRANSPORT_ZONE;
67             // add TEP into default-TZ
68             tZone = ItmUtils.getTransportZoneFromConfigDS(tzName, dataBroker);
69             if (tZone == null) {
70                 LOG.error("Error: default-transport-zone is not yet created.");
71                 return;
72             }
73             LOG.trace("Remove TEP from default-transport-zone.");
74         } else {
75             // Case: Add TEP into corresponding TZ created from Northbound.
76             tZone = ItmUtils.getTransportZoneFromConfigDS(tzName, dataBroker);
77             if (tZone == null) {
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);
82                 return;
83             } else {
84                 LOG.trace("Remove TEP from transport-zone already configured by Northbound.");
85             }
86         }
87
88         // Remove TEP from (default transport-zone) OR (transport-zone already configured by Northbound)
89
90         // Get subnet list of corresponding TZ created from Northbound.
91         List<Subnets> subnetList = tZone.getSubnets();
92
93         if (subnetList == null || subnetList.isEmpty()) {
94             LOG.trace("No subnet list in transport-zone. Nothing to do.");
95         } else {
96             String portName = "";
97             IpPrefix subnetMaskObj = ItmUtils.getDummySubnet();
98
99             List<Vteps> vtepList = null;
100
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();
107                     break;
108                 }
109             }
110
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.");
114             } else {
115                 //  case: vtep list has elements
116                 boolean vtepFound = false;
117                 Vteps oldVtep = null;
118
119                 for (Vteps vtep : vtepList) {
120                     if (vtep.getDpnId().equals(dpnId)) {
121                         vtepFound = true;
122                         oldVtep = vtep;
123                         // get portName of existing vtep
124                         portName = vtep.getPortname();
125                         break;
126                     }
127                 }
128                 if (vtepFound) {
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);
134                 } else {
135                     LOG.trace(
136                         "TEP is not found in the vtep list in subnet list of transport-zone. Nothing to do.");
137                 }
138             }
139         }
140     }
141
142     /**
143      * Removes the TEP from subnet list in the transport zone list
144      * from ITM configuration Datastore by delete operation with write transaction.
145      *
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
151      */
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);
156
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();
160
161         LOG.trace("Removing TEP (TZ: {} Subnet: {} DPN-ID: {}) in ITM Config DS.", tzName,
162             subnetMaskObj.getValue().toString(), dpnId);
163         // remove vtep
164         wrTx.delete(LogicalDatastoreType.CONFIGURATION, vTepPath);
165     }
166
167     /**
168      * Removes the TEP from the not-hosted transport zone in the TepsNotHosted list
169      * from ITM configuration Datastore
170      *
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
176      */
177     public static void removeUnknownTzTepFromTepsNotHosted(String tzName, IpAddress tepIpAddress,
178         BigInteger dpnId, DataBroker dataBroker, WriteTransaction wrTx) {
179         List<UnknownVteps> vtepList = null;
180
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.");
185             return;
186         } else {
187             vtepList = unknownTz.getUnknownVteps();
188             if (vtepList == null || vtepList.isEmpty()) {
189                 //  case: vtep list does not exist or it has no elements
190                 LOG.trace(
191                     "Remove TEP in unhosted TZ ({}) when no vtep-list in the TZ. Nothing to do.",
192                     tzName);
193             } else {
194                 //  case: vtep list has elements
195                 boolean vtepFound = false;
196                 UnknownVteps foundVtep = null;
197
198                 for (UnknownVteps vtep : vtepList) {
199                     if (vtep.getDpnId().equals(dpnId)) {
200                         vtepFound = true;
201                         foundVtep = vtep;
202                         break;
203                     }
204                 }
205                 if (vtepFound) {
206                     // vtep is found, update it with tep-ip
207                     LOG.trace(
208                         "Remove TEP with IP ({}) from unhosted TZ ({}) in TepsNotHosted list.",
209                         tepIpAddress, tzName);
210                     if (vtepList.size() == 1) {
211                         removeTzFromTepsNotHosted(tzName, wrTx);
212                     } else {
213                         removeVtepFromTepsNotHosted(tzName, dpnId, wrTx);
214                     }
215                     vtepList.remove(foundVtep);
216                 }
217             }
218         }
219     }
220
221     /**
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.
224      *
225      * @param tzName transport zone name in string
226      * @param dpnId DPN datapath ID in BigInteger
227      * @param wrTx WriteTransaction object
228      */
229     protected static void removeVtepFromTepsNotHosted(String tzName, BigInteger dpnId,
230         WriteTransaction wrTx) {
231
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();
236
237         LOG.trace("Removing TEP from unhosted (TZ: {}, DPID: {}) from ITM Config DS.", tzName,
238             dpnId);
239         // remove vtep
240         wrTx.delete(LogicalDatastoreType.CONFIGURATION, vTepPath);
241     }
242
243     /**
244      * Removes the transport zone in the TepsNotHosted list
245      * from ITM configuration Datastore by delete operation with write transaction.
246      *
247      * @param tzName transport zone name in string
248      * @param wrTx WriteTransaction object
249      */
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();
255
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);
259     }
260 }