MRI version bump for Aluminium
[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 com.google.common.util.concurrent.ListenableFuture;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Map;
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;
39
40 public final class OvsdbTepRemoveConfigHelper {
41
42     private static final Logger LOG = LoggerFactory.getLogger(OvsdbTepRemoveConfigHelper.class);
43
44     private OvsdbTepRemoveConfigHelper() {
45
46     }
47
48     /**
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
54      *
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
60      */
61
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);
68
69         if (strDpnId != null && !strDpnId.isEmpty()) {
70             dpnId = MDSALUtil.getDpnId(strDpnId);
71         }
72         // Get tep IP
73         IpAddress tepIpAddress = IpAddressBuilder.getDefaultInstance(tepIp);
74         TransportZone transportZone;
75
76         // Case: TZ name is not given from OVS's other_config parameters.
77         if (tzName == null) {
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.");
83                 return futures;
84             }
85             LOG.trace("Remove TEP from default-transport-zone.");
86         } else {
87             // Case: Add TEP into corresponding TZ created from Northbound.
88             transportZone = ItmUtils.getTransportZoneFromConfigDS(tzName, dataBroker);
89             String name = tzName;
90             Uint64 id = dpnId;
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)));
97                 return futures;
98             } else {
99                 LOG.trace("Remove TEP from transport-zone already configured by Northbound.");
100             }
101         }
102
103         // Remove TEP from (default transport-zone) OR (transport-zone already configured by Northbound)
104
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.");
109         } else {
110             //  case: vtep list has elements
111             boolean vtepFound = false;
112             Vteps oldVtep = null;
113
114             for (Vteps vtep : vtepList.values()) {
115                 if (Objects.equals(vtep.getDpnId(), dpnId)) {
116                     vtepFound = true;
117                     oldVtep = vtep;
118                     break;
119                 }
120             }
121             if (vtepFound) {
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;
126                 Uint64 id = dpnId;
127                 futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(Datastore.CONFIGURATION,
128                     tx -> removeVtepFromTZConfig(name, id, tx)));
129             } else {
130                 LOG.trace(
131                         "TEP is not found in the vtep list in subnet list of transport-zone. Nothing to do.");
132             }
133         }
134         return futures;
135     }
136
137     /**
138      * Removes the TEP from subnet list in the transport zone list
139      * from ITM configuration Datastore by delete operation with write transaction.
140      *
141      * @param dpnId bridge datapath ID
142      * @param tzName transport zone name in string
143      * @param tx TypedWriteTransaction object
144      */
145     private static void removeVtepFromTZConfig(String tzName, Uint64 dpnId,
146                                                TypedWriteTransaction<Datastore.Configuration> tx) {
147
148         VtepsKey vtepkey = new VtepsKey(dpnId);
149
150         InstanceIdentifier<Vteps> vtepPath = InstanceIdentifier.builder(TransportZones.class)
151                 .child(TransportZone.class, new TransportZoneKey(tzName))
152                 .child(Vteps.class, vtepkey).build();
153
154         LOG.trace("Removing TEP from (TZ: {} DPN-ID: {}) inside ITM Config DS.",
155                 tzName, dpnId);
156         // remove vtep
157         tx.delete(vtepPath);
158     }
159
160     /**
161      * Removes the TEP from the not-hosted transport zone in the TepsNotHosted list
162      * from ITM Operational Datastore.
163      *
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
169      */
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);
178             return;
179         } else {
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);
184             } else {
185                 //  case: vtep list has elements
186                 boolean vtepFound = false;
187
188                 for (UnknownVteps vtep : vtepList.values()) {
189                     if (Objects.equals(vtep.getDpnId(), dpnId)) {
190                         vtepFound = true;
191                         break;
192                     }
193                 }
194                 if (vtepFound) {
195                     // vtep is found, update it with tep-ip
196                     LOG.trace(
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);
201                     } else {
202                         removeVtepFromTepsNotHosted(tzName, dpnId, tx);
203                     }
204                 }
205             }
206         }
207     }
208
209     /**
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.
212      *
213      * @param tzName transport zone name in string
214      * @param dpnId bridge datapath ID
215      * @param tx TypedWriteTransaction object
216      */
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);
223         tx.delete(vtepPath);
224     }
225
226     /**
227      * Removes the transport zone in the TepsNotHosted list
228      * from ITM operational Datastore by delete operation with write transaction.
229      *
230      * @param tzName transport zone name in string
231      * @param tx TypedWriteTransaction object
232      */
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);
239     }
240 }