Fixup Augmentable and Identifiable methods changing
[genius.git] / itm / itm-impl / src / main / java / org / opendaylight / genius / itm / confighelpers / OvsdbTepRemoveConfigHelper.java
1 /*
2  * Copyright (c) 2016, 2017 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 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;
36
37 public final class OvsdbTepRemoveConfigHelper {
38
39     private static final Logger LOG = LoggerFactory.getLogger(OvsdbTepRemoveConfigHelper.class);
40
41     private OvsdbTepRemoveConfigHelper() { }
42
43     /**
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
49      *
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
55      */
56
57     public static void removeTepReceivedFromOvsdb(String tepIp, String strDpnId, String tzName,
58                                                   DataBroker dataBroker, WriteTransaction wrTx) {
59         BigInteger dpnId = BigInteger.valueOf(0);
60
61         LOG.trace("Remove TEP: TEP-IP: {}, TZ name: {}, DPID: {}", tepIp, tzName, strDpnId);
62
63         if (strDpnId != null && !strDpnId.isEmpty()) {
64             dpnId = MDSALUtil.getDpnId(strDpnId);
65         }
66
67         // Get tep IP
68         IpAddress tepIpAddress = IpAddressBuilder.getDefaultInstance(tepIp);
69         TransportZone transportZone = null;
70
71         // Case: TZ name is not given from OVS's other_config parameters.
72         if (tzName == null) {
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.");
78                 return;
79             }
80             LOG.trace("Remove TEP from default-transport-zone.");
81         } else {
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);
89                 return;
90             } else {
91                 LOG.trace("Remove TEP from transport-zone already configured by Northbound.");
92             }
93         }
94
95         // Remove TEP from (default transport-zone) OR (transport-zone already configured by Northbound)
96
97         // Get subnet list of corresponding TZ created from Northbound.
98         List<Subnets> subnetList = transportZone.getSubnets();
99
100         if (subnetList == null || subnetList.isEmpty()) {
101             LOG.trace("No subnet list in transport-zone. Nothing to do.");
102         } else {
103             IpPrefix subnetMaskObj = ItmUtils.getDummySubnet();
104
105             List<Vteps> vtepList = null;
106
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();
113                     break;
114                 }
115             }
116
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.");
120             } else {
121                 //  case: vtep list has elements
122                 boolean vtepFound = false;
123                 Vteps oldVtep = null;
124
125                 for (Vteps vtep : vtepList) {
126                     if (vtep.getDpnId().equals(dpnId)) {
127                         vtepFound = true;
128                         oldVtep = vtep;
129                         break;
130                     }
131                 }
132                 if (vtepFound) {
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);
138                 } else {
139                     LOG.trace(
140                         "TEP is not found in the vtep list in subnet list of transport-zone. Nothing to do.");
141                 }
142             }
143         }
144     }
145
146     /**
147      * Removes the TEP from subnet list in the transport zone list
148      * from ITM configuration Datastore by delete operation with write transaction.
149      *
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
155      */
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);
160
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();
164
165         LOG.trace("Removing TEP from (TZ: {} Subnet: {} DPN-ID: {}) inside ITM Config DS.",
166                 tzName, subnetMaskObj, dpnId);
167         // remove vtep
168         wrTx.delete(LogicalDatastoreType.CONFIGURATION, vtepPath);
169     }
170
171     /**
172      * Removes the TEP from the not-hosted transport zone in the TepsNotHosted list
173      * from ITM Operational Datastore.
174      *
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
180      */
181     public static void removeUnknownTzTepFromTepsNotHosted(String tzName, IpAddress tepIpAddress,
182                                                            BigInteger dpnId, DataBroker dataBroker,
183                                                            WriteTransaction wrTx) {
184         List<UnknownVteps> vtepList = null;
185
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);
190             return;
191         } else {
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);
196             } else {
197                 //  case: vtep list has elements
198                 boolean vtepFound = false;
199                 UnknownVteps foundVtep = null;
200
201                 for (UnknownVteps vtep : vtepList) {
202                     if (vtep.getDpnId().equals(dpnId)) {
203                         vtepFound = true;
204                         foundVtep = vtep;
205                         break;
206                     }
207                 }
208                 if (vtepFound) {
209                     // vtep is found, update it with tep-ip
210                     LOG.trace(
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);
215                     } else {
216                         removeVtepFromTepsNotHosted(tzName, dpnId, wrTx);
217                     }
218                     vtepList.remove(foundVtep);
219                 }
220             }
221         }
222     }
223
224     /**
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.
227      *
228      * @param tzName transport zone name in string
229      * @param dpnId bridge datapath ID in BigInteger
230      * @param wrTx WriteTransaction object
231      */
232     private static void removeVtepFromTepsNotHosted(String tzName, BigInteger dpnId,
233                                                       WriteTransaction wrTx) {
234
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();
239
240         LOG.trace("Removing TEP from unhosted (TZ: {}, DPID: {}) inside ITM Oper DS.",
241                 tzName, dpnId);
242         // remove vtep
243         wrTx.delete(LogicalDatastoreType.OPERATIONAL, vtepPath);
244     }
245
246     /**
247      * Removes the transport zone in the TepsNotHosted list
248      * from ITM operational Datastore by delete operation with write transaction.
249      *
250      * @param tzName transport zone name in string
251      * @param wrTx WriteTransaction object
252      */
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();
258
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);
262     }
263 }