MDSAL-API Migration
[genius.git] / itm / itm-impl / src / test / java / org / opendaylight / genius / itm / tests / ItmTepAutoConfigTest.java
1 /*
2  * Copyright (c) 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.tests;
9
10 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
11 import static org.opendaylight.mdsal.binding.testutils.AssertDataObjects.assertEqualBeans;
12
13 import com.google.common.util.concurrent.FluentFuture;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.Optional;
18 import javax.inject.Inject;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.junit.Assert;
21 import org.junit.Before;
22 import org.junit.Ignore;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.MethodRule;
26 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
27 import org.opendaylight.genius.datastoreutils.testutils.JobCoordinatorEventsWaiter;
28 import org.opendaylight.genius.datastoreutils.testutils.JobCoordinatorTestModule;
29 import org.opendaylight.genius.datastoreutils.testutils.TestableDataTreeChangeListenerModule;
30 import org.opendaylight.genius.infra.RetryingManagedNewTransactionRunner;
31 import org.opendaylight.genius.itm.globals.ITMConstants;
32 import org.opendaylight.genius.itm.impl.ItmProvider;
33 import org.opendaylight.genius.itm.impl.ItmUtils;
34 import org.opendaylight.genius.itm.tests.xtend.ExpectedDefTransportZoneObjects;
35 import org.opendaylight.genius.itm.tests.xtend.ExpectedTepNotHostedTransportZoneObjects;
36 import org.opendaylight.genius.itm.tests.xtend.ExpectedTransportZoneObjects;
37 import org.opendaylight.infrautils.caches.testutils.CacheModule;
38 import org.opendaylight.infrautils.inject.guice.testutils.GuiceRule;
39 import org.opendaylight.infrautils.testutils.LogRule;
40 import org.opendaylight.mdsal.binding.api.DataBroker;
41 import org.opendaylight.mdsal.binding.api.WriteTransaction;
42 import org.opendaylight.mdsal.common.api.CommitInfo;
43 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeGre;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeVxlan;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.ItmConfig;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.ItmConfigBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.not.hosted.transport.zones.TepsInNotHostedTransportZone;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.not.hosted.transport.zones.tepsinnothostedtransportzone.UnknownVteps;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.TransportZone;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.TransportZoneBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.TransportZoneKey;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.Vteps;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.VtepsBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.VtepsKey;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
58 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
59 import org.opendaylight.yangtools.yang.common.Uint64;
60
61 /**
62  * Component tests for ITM TEP Auto Config feature.
63  */
64 public class ItmTepAutoConfigTest {
65
66     public @Rule LogRule logRule = new LogRule();
67     // TODO public @Rule LogCaptureRule logCaptureRule = new LogCaptureRule();
68     public @Rule MethodRule guice = new GuiceRule(ItmTestModule.class, TestableDataTreeChangeListenerModule.class,
69             JobCoordinatorTestModule.class, CacheModule.class);
70
71     TransportZone transportZone;
72     RetryingManagedNewTransactionRunner txRunner;
73
74     private @Inject DataBroker dataBroker;
75     private @Inject JobCoordinatorEventsWaiter coordinatorEventsWaiter;
76     private @Inject ItmProvider itmProvider;
77
78     @Before
79     public void start() throws InterruptedException {
80         transportZone = new TransportZoneBuilder().setZoneName(ItmTestConstants.TZ_NAME)
81                 .setTunnelType(ItmTestConstants.TUNNEL_TYPE_VXLAN)
82                 .withKey(new TransportZoneKey(ItmTestConstants.TZ_NAME))
83                 .build();
84         this.txRunner = new RetryingManagedNewTransactionRunner(dataBroker);
85     }
86
87     // Common method created for code-reuse
88     private InstanceIdentifier<TransportZone> processDefTzOnItmConfig(boolean defTzEnabledFlag,
89                                                                       String defTzTunnelType) throws Exception {
90
91         ItmConfig itmConfigObj = null;
92         if (defTzTunnelType != null) {
93             // set def-tz-enabled flag and def-tz-tunnel-type
94             itmConfigObj = new ItmConfigBuilder().setDefTzEnabled(defTzEnabledFlag)
95                     .setDefTzTunnelType(defTzTunnelType).build();
96         } else {
97             // set def-tz-enabled flag only
98             itmConfigObj = new ItmConfigBuilder().setDefTzEnabled(defTzEnabledFlag).build();
99         }
100         // creates/deletes default-TZ based on def-tz-enabled flag
101         itmProvider.createDefaultTransportZone(itmConfigObj);
102         coordinatorEventsWaiter.awaitEventsConsumption();
103
104         InstanceIdentifier<TransportZone> tzonePath = ItmTepAutoConfigTestUtil.getTzIid(
105                 ITMConstants.DEFAULT_TRANSPORT_ZONE);
106
107         return tzonePath;
108     }
109
110     @Test
111     public void defTzEnabledFalseConfigTest() throws Exception {
112         InstanceIdentifier<ItmConfig> iid = InstanceIdentifier.create(ItmConfig.class);
113
114         // set def-tz-enabled flag to false
115         ItmConfig itmConfigObj = new ItmConfigBuilder().setDefTzEnabled(false).build();
116
117         // write into config DS
118
119         txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> tx.merge(LogicalDatastoreType.CONFIGURATION,
120                         iid,itmConfigObj));
121         coordinatorEventsWaiter.awaitEventsConsumption();
122
123         // read from config DS
124         boolean defTzEnabled = SingleTransactionDataBroker.syncReadOptional(
125                 dataBroker, LogicalDatastoreType.CONFIGURATION, iid).get().isDefTzEnabled();
126         Assert.assertEquals(defTzEnabled, false);
127     }
128
129     @Test
130     public void defTzEnabledTrueConfigTest() throws Exception {
131         InstanceIdentifier<ItmConfig> iid = InstanceIdentifier.create(ItmConfig.class);
132         // set def-tz-enabled flag to true
133         ItmConfig itmConfigObj = new ItmConfigBuilder().setDefTzEnabled(true).build();
134
135         // write into config DS
136         //txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
137           //  tx -> tx.put(iid, itmConfigObj, true));
138
139         txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> tx.merge(LogicalDatastoreType.CONFIGURATION,
140                 iid,itmConfigObj)).get();
141
142         coordinatorEventsWaiter.awaitEventsConsumption();
143
144         // read from config DS
145         boolean defTzEnabled = SingleTransactionDataBroker.syncReadOptional(
146                 dataBroker, LogicalDatastoreType.CONFIGURATION, iid).get().isDefTzEnabled();
147         Assert.assertEquals(defTzEnabled, true);
148     }
149
150     @Test
151     public void defTzCreationTestWithDefTzEnabledTrueAndVxlanTunnelType() throws Exception {
152         // set def-tz-enabled flag to true
153         boolean defTzEnabledFlag = true;
154         // set def-tz-tunnel-type to VXLAN
155         String defTzTunnelType = ITMConstants.TUNNEL_TYPE_VXLAN;
156
157         /*InstanceIdentifier<TransportZone> tzPath =
158                 ItmUtils.getTZInstanceIdentifier(ITMConstants.DEFAULT_TRANSPORT_ZONE);
159         txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> tx.merge(LogicalDatastoreType.CONFIGURATION,
160                 tzPath, new TransportZoneBuilder().setZoneName(ITMConstants.DEFAULT_TRANSPORT_ZONE)
161                         .setTunnelType(ItmTestConstants.TUNNEL_TYPE_VXLAN)
162                         .withKey(new TransportZoneKey(ITMConstants.DEFAULT_TRANSPORT_ZONE))
163                         .build(), WriteTransaction.CREATE_MISSING_PARENTS)).get();*/
164
165         InstanceIdentifier<TransportZone> tzonePath = processDefTzOnItmConfig(defTzEnabledFlag,
166                 defTzTunnelType);
167         Assert.assertNotNull(tzonePath);
168
169         assertEqualBeans(ExpectedDefTransportZoneObjects.newDefTzWithVxlanTunnelType(),
170                 dataBroker.newReadOnlyTransaction()
171                         .read(LogicalDatastoreType.CONFIGURATION, tzonePath).get().get());
172     }
173
174     @Test
175     public void defTzCreationTestWithDefTzEnabledTrueAndGreTunnelType() throws Exception {
176         // set def-tz-enabled flag to true
177         boolean defTzEnabledFlag = true;
178         // set def-tz-tunnel-type to GRE
179         String defTzTunnelType = ITMConstants.TUNNEL_TYPE_GRE;
180
181         InstanceIdentifier<TransportZone> tzPath =
182                 ItmUtils.getTZInstanceIdentifier(ITMConstants.DEFAULT_TRANSPORT_ZONE);
183         txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> tx.merge(LogicalDatastoreType.CONFIGURATION,
184                 tzPath, new TransportZoneBuilder().setZoneName(ITMConstants.DEFAULT_TRANSPORT_ZONE)
185                         .setTunnelType(TunnelTypeGre.class)
186                         .withKey(new TransportZoneKey(ITMConstants.DEFAULT_TRANSPORT_ZONE))
187                         .build(), WriteTransaction.CREATE_MISSING_PARENTS)).get();
188
189
190         InstanceIdentifier<TransportZone> tzonePath = processDefTzOnItmConfig(defTzEnabledFlag,
191                 defTzTunnelType);
192         Assert.assertNotNull(tzonePath);
193
194         assertEqualBeans(ExpectedDefTransportZoneObjects.newDefTzWithGreTunnelType(),
195                 dataBroker.newReadOnlyTransaction()
196                         .read(LogicalDatastoreType.CONFIGURATION, tzonePath).get().get());
197     }
198
199     @Test
200     public void defTzCreationFailedTestWithDefTzEnabledFalse() throws Exception {
201         // set def-tz-enabled flag to false
202         boolean defTzEnabledFlag = false;
203         // set def-tz-tunnel-type to GRE
204         String defTzTunnelType = null;
205
206         InstanceIdentifier<TransportZone> tzonePath = processDefTzOnItmConfig(defTzEnabledFlag,
207                 defTzTunnelType);
208         Assert.assertNotNull(tzonePath);
209
210         Assert.assertEquals(Optional.empty(), dataBroker.newReadOnlyTransaction()
211                 .read(LogicalDatastoreType.CONFIGURATION, tzonePath).get());
212     }
213
214     @Test
215     public void defTzRecreationTestOnTunnelTypeChange() throws Exception {
216         // set def-tz-enabled flag to true
217         boolean defTzEnabledFlag = true;
218         // set def-tz-tunnel-type to VXLAN
219         String defTzTunnelType = ITMConstants.TUNNEL_TYPE_VXLAN;
220
221         InstanceIdentifier<TransportZone> tzPath =
222                 ItmUtils.getTZInstanceIdentifier(ITMConstants.DEFAULT_TRANSPORT_ZONE);
223         txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> tx.merge(LogicalDatastoreType.CONFIGURATION,
224                 tzPath, new TransportZoneBuilder().setZoneName(ITMConstants.DEFAULT_TRANSPORT_ZONE)
225                         .setTunnelType(TunnelTypeVxlan.class)
226                         .withKey(new TransportZoneKey(ITMConstants.DEFAULT_TRANSPORT_ZONE))
227                         .build(), WriteTransaction.CREATE_MISSING_PARENTS)).get();
228
229         InstanceIdentifier<TransportZone> tzonePath = processDefTzOnItmConfig(defTzEnabledFlag,
230                 defTzTunnelType);
231         Assert.assertNotNull(tzonePath);
232
233         // check default-TZ is created with VXLAN tunnel type
234         assertEqualBeans(ExpectedDefTransportZoneObjects.newDefTzWithVxlanTunnelType().getTunnelType(),
235                 dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION, tzonePath)
236                         .get().get().getTunnelType());
237
238         // now, change def-tz-tunnel-type to GRE
239         defTzTunnelType = ITMConstants.TUNNEL_TYPE_GRE;
240
241
242         txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> tx.merge(LogicalDatastoreType.CONFIGURATION,
243                 tzPath, new TransportZoneBuilder().setZoneName(ITMConstants.DEFAULT_TRANSPORT_ZONE)
244                         .setTunnelType(TunnelTypeGre.class)
245                         .withKey(new TransportZoneKey(ITMConstants.DEFAULT_TRANSPORT_ZONE))
246                         .build(), WriteTransaction.CREATE_MISSING_PARENTS)).get();
247
248
249         tzonePath = processDefTzOnItmConfig(defTzEnabledFlag, defTzTunnelType);
250         Assert.assertNotNull(tzonePath);
251
252         // check default-TZ is re-created with GRE tunnel type
253         assertEqualBeans(ExpectedDefTransportZoneObjects.newDefTzWithGreTunnelType().getTunnelType(),
254                 dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION, tzonePath)
255                         .get().get().getTunnelType());
256     }
257
258     @Test
259     public void defTzDeletionTest() throws Exception {
260         // wait for start-up default-TZ creation task to get over
261         coordinatorEventsWaiter.awaitEventsConsumption();
262
263         // create default-TZ first by setting def-tz-enabled flag to true
264         ItmConfig itmConfigObj = new ItmConfigBuilder().setDefTzEnabled(true)
265                 .setDefTzTunnelType(ITMConstants.TUNNEL_TYPE_GRE).build();
266
267         // creates/deletes default-TZ based on def-tz-enabled flag
268         itmProvider.createDefaultTransportZone(itmConfigObj);
269         coordinatorEventsWaiter.awaitEventsConsumption();
270
271         InstanceIdentifier<TransportZone> tzPath =
272                 ItmUtils.getTZInstanceIdentifier(ITMConstants.DEFAULT_TRANSPORT_ZONE);
273         txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> tx.merge(LogicalDatastoreType.CONFIGURATION,
274                 tzPath, new TransportZoneBuilder().setZoneName(ITMConstants.DEFAULT_TRANSPORT_ZONE)
275                         .setTunnelType(TunnelTypeGre.class)
276                         .withKey(new TransportZoneKey(ITMConstants.DEFAULT_TRANSPORT_ZONE))
277                         .build(), WriteTransaction.CREATE_MISSING_PARENTS)).get();
278
279         InstanceIdentifier<TransportZone> tzonePath = ItmTepAutoConfigTestUtil.getTzIid(
280                 ITMConstants.DEFAULT_TRANSPORT_ZONE);
281         Assert.assertNotNull(tzonePath);
282
283         assertEqualBeans(ExpectedDefTransportZoneObjects.newDefTzWithGreTunnelType(),
284                 dataBroker.newReadOnlyTransaction()
285                         .read(LogicalDatastoreType.CONFIGURATION, tzonePath).get().get());
286
287         // now delete default-TZ first by setting def-tz-enabled flag to false
288         itmConfigObj = new ItmConfigBuilder().setDefTzEnabled(false).build();
289
290         // creates/deletes default-TZ based on def-tz-enabled flag
291         itmProvider.createDefaultTransportZone(itmConfigObj);
292         coordinatorEventsWaiter.awaitEventsConsumption();
293
294         Assert.assertEquals(Optional.empty(), dataBroker.newReadOnlyTransaction()
295                 .read(LogicalDatastoreType.CONFIGURATION, tzonePath).get());
296     }
297
298     @Test
299     public void testAddDeleteTepForDefTz() throws Exception {
300         // wait for start-up default-TZ creation task to get over
301         coordinatorEventsWaiter.awaitEventsConsumption();
302
303         InstanceIdentifier<TransportZone> tzPath =
304                 ItmUtils.getTZInstanceIdentifier(ITMConstants.DEFAULT_TRANSPORT_ZONE);
305         txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> tx.merge(LogicalDatastoreType.CONFIGURATION,
306                 tzPath, new TransportZoneBuilder().setZoneName(ITMConstants.DEFAULT_TRANSPORT_ZONE)
307                         .setTunnelType(TunnelTypeVxlan.class)
308                         .withKey(new TransportZoneKey(ITMConstants.DEFAULT_TRANSPORT_ZONE))
309                         .build(), WriteTransaction.CREATE_MISSING_PARENTS)).get();
310
311         InstanceIdentifier<TransportZone> tzonePath = ItmTepAutoConfigTestUtil.getTzIid(
312                 ITMConstants.DEFAULT_TRANSPORT_ZONE);
313         Assert.assertNotNull(tzonePath);
314
315         IpPrefix subnetMaskObj = ItmUtils.getDummySubnet();
316
317         // add TEP into default-TZ
318         ListenableFuture<Void> futures =
319                 ItmTepAutoConfigTestUtil.addTep(ItmTestConstants.DEF_TZ_TEP_IP,
320                         ItmTestConstants.DEF_BR_DPID,
321                         ITMConstants.DEFAULT_TRANSPORT_ZONE, false, dataBroker, txRunner);
322         futures.get();
323
324         InstanceIdentifier<Vteps> vtepPath = ItmTepAutoConfigTestUtil.getTepIid(subnetMaskObj,
325                 ITMConstants.DEFAULT_TRANSPORT_ZONE, ItmTestConstants.INT_DEF_BR_DPID,
326                 ITMConstants.DUMMY_PORT);
327         Assert.assertNotNull(vtepPath);
328
329         // check TEP is added into default-TZ
330         assertEqualBeans(ExpectedDefTransportZoneObjects.newDefTzWithTep(), dataBroker.newReadOnlyTransaction()
331                 .read(LogicalDatastoreType.CONFIGURATION, tzonePath).get().get());
332
333         // remove tep from default-TZ
334         futures = ItmTepAutoConfigTestUtil.deleteTep(ItmTestConstants.DEF_TZ_TEP_IP, ItmTestConstants.DEF_BR_DPID,
335                 ITMConstants.DEFAULT_TRANSPORT_ZONE, dataBroker, txRunner);
336         futures.get();
337
338         // check TEP is deleted from default-TZ
339         Assert.assertEquals(Optional.empty(), dataBroker.newReadOnlyTransaction()
340                 .read(LogicalDatastoreType.CONFIGURATION, vtepPath).get());
341     }
342
343     @Test
344     public void testAddDeleteTepForTz() throws Exception {
345         // wait for start-up default-TZ creation task to get over
346         coordinatorEventsWaiter.awaitEventsConsumption();
347
348         InstanceIdentifier<TransportZone> tzonePath = ItmTepAutoConfigTestUtil.getTzIid(
349                 ItmTestConstants.TZ_NAME);
350         Assert.assertNotNull(tzonePath);
351
352         // create TZA
353         InstanceIdentifier<TransportZone> tzPath = ItmUtils.getTZInstanceIdentifier(ItmTestConstants.TZ_NAME);
354         txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> tx.merge(LogicalDatastoreType.CONFIGURATION,
355                 tzPath, transportZone, WriteTransaction.CREATE_MISSING_PARENTS)).get();
356
357         // check TZ is created with correct TZ name
358         Assert.assertEquals(ItmTestConstants.TZ_NAME, dataBroker.newReadOnlyTransaction()
359                 .read(LogicalDatastoreType.CONFIGURATION, tzonePath).get().get().getZoneName());
360
361         // add tep
362         ListenableFuture<Void> futures = ItmTepAutoConfigTestUtil.addTep(ItmTestConstants.NB_TZ_TEP_IP,
363                 ItmTestConstants.DEF_BR_DPID, ItmTestConstants.TZ_NAME, false, dataBroker, txRunner);
364         futures.get();
365
366         IpPrefix subnetMaskObj = ItmUtils.getDummySubnet();
367
368         InstanceIdentifier<Vteps> vtepPath = ItmTepAutoConfigTestUtil.getTepIid(subnetMaskObj,
369                 ItmTestConstants.TZ_NAME, ItmTestConstants.INT_DEF_BR_DPID, ITMConstants.DUMMY_PORT);
370         Assert.assertNotNull(vtepPath);
371
372         // check TEP is added into TZ that is already created.
373         assertEqualBeans(ExpectedTransportZoneObjects.newTransportZone(),
374                 dataBroker.newReadOnlyTransaction()
375                         .read(LogicalDatastoreType.CONFIGURATION, tzonePath).get().get());
376
377         // remove tep
378         futures = ItmTepAutoConfigTestUtil.deleteTep(ItmTestConstants.NB_TZ_TEP_IP, ItmTestConstants.DEF_BR_DPID,
379                 ItmTestConstants.TZ_NAME, dataBroker, txRunner);
380         futures.get();
381
382         // check TEP is deleted
383         Assert.assertEquals(Optional.empty(), dataBroker.newReadOnlyTransaction()
384                 .read(LogicalDatastoreType.CONFIGURATION, vtepPath).get());
385
386         // for safe side, check TZ is present
387         Assert.assertEquals(ItmTestConstants.TZ_NAME, dataBroker.newReadOnlyTransaction()
388                 .read(LogicalDatastoreType.CONFIGURATION, tzonePath).get().get().getZoneName());
389     }
390
391     @Test
392     public void tepAddDeleteFromDefTzViaSouthboundTest() throws Exception  {
393         // wait for start-up default-TZ creation task to get over
394         coordinatorEventsWaiter.awaitEventsConsumption();
395
396         String tepIp = ItmTestConstants.DEF_TZ_TEP_IP;
397         // create Network topology node with tep-ip set into ExternalIds list
398         // OvsdbNodeListener would be automatically listen on Node to add TEP
399         ConnectionInfo connInfo = OvsdbTestUtil.getConnectionInfo(ItmTestConstants.OVSDB_CONN_PORT,
400                 ItmTestConstants.LOCALHOST_IP);
401         FluentFuture<? extends @NonNull CommitInfo> future =
402                 OvsdbTestUtil.createNode(connInfo,tepIp,ITMConstants.DEFAULT_TRANSPORT_ZONE,dataBroker);
403         future.get();
404
405         InstanceIdentifier<TransportZone> tzPath =
406                 ItmUtils.getTZInstanceIdentifier(ITMConstants.DEFAULT_TRANSPORT_ZONE);
407         txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> tx.merge(LogicalDatastoreType.CONFIGURATION,
408                 tzPath, new TransportZoneBuilder().setZoneName(ITMConstants.DEFAULT_TRANSPORT_ZONE)
409                         .setTunnelType(ItmTestConstants.TUNNEL_TYPE_VXLAN)
410                         .withKey(new TransportZoneKey(ITMConstants.DEFAULT_TRANSPORT_ZONE))
411                         .build(), WriteTransaction.CREATE_MISSING_PARENTS)).get();
412
413
414         // add bridge into node
415         future = OvsdbTestUtil.addBridgeIntoNode(connInfo, ItmTestConstants.DEF_BR_NAME,
416                 ItmTestConstants.DEF_BR_DPID, dataBroker);
417         future.get();
418         // wait for OvsdbNodeListener to perform config DS update through transaction
419         coordinatorEventsWaiter.awaitEventsConsumption();
420
421         InstanceIdentifier<TransportZone> tzonePath = ItmTepAutoConfigTestUtil.getTzIid(
422                 ITMConstants.DEFAULT_TRANSPORT_ZONE);
423         Assert.assertNotNull(tzonePath);
424
425         // check TEP is added into default-TZ
426         assertEqualBeans(ExpectedDefTransportZoneObjects.newDefTzWithTep(),
427                 dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION, tzonePath)
428                         .get().get());
429
430         // test TEP delete now,
431         // pass tep-ip with NULL value, tep-ip paramtere in external_ids will not be set.
432         tepIp = null;
433         future = OvsdbTestUtil.updateNode(connInfo, tepIp, ITMConstants.DEFAULT_TRANSPORT_ZONE,
434                 null, dataBroker);
435         future.get();
436         // wait for OvsdbNodeListener to perform config DS update through transaction
437         coordinatorEventsWaiter.awaitEventsConsumption();
438
439         IpPrefix subnetMaskObj = ItmUtils.getDummySubnet();
440
441         InstanceIdentifier<Vteps> vtepPath = ItmTepAutoConfigTestUtil.getTepIid(subnetMaskObj,
442                 ITMConstants.DEFAULT_TRANSPORT_ZONE, ItmTestConstants.INT_DEF_BR_DPID,
443                 ITMConstants.DUMMY_PORT);
444         Assert.assertNotNull(vtepPath);
445
446         // check TEP is deleted from default-TZ when TEP-Ip is removed from southbound
447         Assert.assertEquals(Optional.empty(), dataBroker.newReadOnlyTransaction()
448                 .read(LogicalDatastoreType.CONFIGURATION, vtepPath).get());
449     }
450
451     @Test
452     public void tepAddDeleteFromNbTzViaSouthboundTest() throws Exception  {
453         String tepIp = ItmTestConstants.NB_TZ_TEP_IP;
454         // create Network topology node with tep-ip set into ExternalIds list
455         // OvsdbNodeListener would be automatically listen on Node to add TEP
456         ConnectionInfo connInfo = OvsdbTestUtil.getConnectionInfo(ItmTestConstants.OVSDB_CONN_PORT,
457                 ItmTestConstants.LOCALHOST_IP);
458         FluentFuture<? extends @NonNull CommitInfo> future = OvsdbTestUtil.createNode(
459                 connInfo, tepIp, ItmTestConstants.TZ_NAME, dataBroker);
460         future.get();
461
462         // create Transport-zone in advance
463         InstanceIdentifier<TransportZone> tzPath = ItmUtils.getTZInstanceIdentifier(ItmTestConstants.TZ_NAME);
464         txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> tx.merge(LogicalDatastoreType.CONFIGURATION,
465                 tzPath, transportZone, WriteTransaction.CREATE_MISSING_PARENTS)).get();
466
467         // add bridge into node
468         future = OvsdbTestUtil.addBridgeIntoNode(connInfo, ItmTestConstants.DEF_BR_NAME,
469                 ItmTestConstants.DEF_BR_DPID, dataBroker);
470         future.get();
471         // wait for OvsdbNodeListener to perform config DS update through transaction
472         coordinatorEventsWaiter.awaitEventsConsumption();
473
474         InstanceIdentifier<TransportZone> tzonePath = ItmTepAutoConfigTestUtil.getTzIid(
475                 ItmTestConstants.TZ_NAME);
476         Assert.assertNotNull(tzonePath);
477
478         // check TEP is added into NB configured TZ
479         assertEqualBeans(ExpectedTransportZoneObjects.newTransportZone(),
480                 dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION, tzonePath)
481                         .get().get());
482
483         IpPrefix subnetMaskObj = ItmUtils.getDummySubnet();
484
485         InstanceIdentifier<Vteps> vtepPath = ItmTepAutoConfigTestUtil.getTepIid(subnetMaskObj,
486                 ItmTestConstants.TZ_NAME, ItmTestConstants.INT_DEF_BR_DPID, ITMConstants.DUMMY_PORT);
487         Assert.assertNotNull(vtepPath);
488
489         // test TEP delete now,
490         // pass tep-ip with NULL value, tep-ip paramtere in external_ids will not be set.
491         tepIp = null;
492         future = OvsdbTestUtil.updateNode(connInfo, tepIp, ItmTestConstants.TZ_NAME, null, dataBroker);
493         future.get();
494         // wait for OvsdbNodeListener to perform config DS update through transaction
495         coordinatorEventsWaiter.awaitEventsConsumption();
496
497         // check TEP is deleted from default-TZ when TEP-Ip is removed from southbound
498         Assert.assertEquals(Optional.empty(), dataBroker.newReadOnlyTransaction()
499                 .read(LogicalDatastoreType.CONFIGURATION, vtepPath).get());
500     }
501
502     @Test
503     public void tzAddDeleteToNotHostedViaSouthboundTest() throws Exception  {
504         // create Network topology node
505         ConnectionInfo connInfo = OvsdbTestUtil.getConnectionInfo(ItmTestConstants.OVSDB_CONN_PORT,
506                 ItmTestConstants.LOCALHOST_IP);
507         FluentFuture<? extends @NonNull CommitInfo> future = OvsdbTestUtil.createNode(
508                 connInfo, ItmTestConstants.NOT_HOSTED_TZ_TEP_IP, ItmTestConstants.NOT_HOSTED_TZ_NAME,
509                 dataBroker);
510         future.get();
511
512         // add bridge into node
513         future = OvsdbTestUtil.addBridgeIntoNode(connInfo, ItmTestConstants.DEF_BR_NAME,
514                 ItmTestConstants.NOT_HOSTED_DEF_BR_DPID, dataBroker);
515         future.get();
516         // wait for OvsdbNodeListener to perform config DS update through transaction
517         coordinatorEventsWaiter.awaitEventsConsumption();
518
519         InstanceIdentifier<TepsInNotHostedTransportZone> notHostedtzPath = ItmTepAutoConfigTestUtil
520                 .getTepNotHostedInTZIid(ItmTestConstants.NOT_HOSTED_TZ_NAME);
521         Assert.assertNotNull(notHostedtzPath);
522
523         // check not hosted
524         assertEqualBeans(ExpectedTepNotHostedTransportZoneObjects.newTepNotHostedTransportZone(),
525                 dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL,
526                         notHostedtzPath).get().get());
527
528         future = OvsdbTestUtil.updateNode(connInfo, ItmTestConstants.NOT_HOSTED_TZ_TEP_IP, null,
529                 ItmTestConstants.DEF_BR_NAME, dataBroker);
530         future.get();
531         // wait for OvsdbNodeListener to perform config DS update through transaction
532         coordinatorEventsWaiter.awaitEventsConsumption();
533
534         Assert.assertEquals(Optional.empty(),dataBroker.newReadOnlyTransaction()
535                 .read(LogicalDatastoreType.OPERATIONAL, notHostedtzPath).get());
536     }
537
538     @Test
539     public void tepUpdateForTepIpTest() throws Exception {
540         // wait for start-up default-TZ creation task to get over
541         coordinatorEventsWaiter.awaitEventsConsumption();
542
543         String tepIp = ItmTestConstants.DEF_TZ_TEP_IP;
544         // create Network topology node with tep-ip set into ExternalIds list
545         // OvsdbNodeListener would be automatically listen on Node to add TEP
546         ConnectionInfo connInfo = OvsdbTestUtil.getConnectionInfo(ItmTestConstants.OVSDB_CONN_PORT,
547                 ItmTestConstants.LOCALHOST_IP);
548         FluentFuture<? extends @NonNull CommitInfo> future = OvsdbTestUtil.createNode(
549                 connInfo, tepIp, ITMConstants.DEFAULT_TRANSPORT_ZONE, dataBroker);
550         future.get();
551
552         InstanceIdentifier<TransportZone> tzPath =
553                 ItmUtils.getTZInstanceIdentifier(ITMConstants.DEFAULT_TRANSPORT_ZONE);
554         txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> tx.merge(LogicalDatastoreType.CONFIGURATION,
555                 tzPath, new TransportZoneBuilder().setZoneName(ITMConstants.DEFAULT_TRANSPORT_ZONE)
556                         .setTunnelType(ItmTestConstants.TUNNEL_TYPE_VXLAN)
557                         .withKey(new TransportZoneKey(ITMConstants.DEFAULT_TRANSPORT_ZONE))
558                         .build(), WriteTransaction.CREATE_MISSING_PARENTS)).get();
559
560         // add bridge into node
561         future = OvsdbTestUtil.addBridgeIntoNode(connInfo, ItmTestConstants.DEF_BR_NAME,
562                 ItmTestConstants.DEF_BR_DPID, dataBroker);
563         future.get();
564         // wait for OvsdbNodeListener to perform config DS update through transaction
565         coordinatorEventsWaiter.awaitEventsConsumption();
566         //Thread.sleep(5000);
567
568         // iid for default-TZ
569         InstanceIdentifier<TransportZone> defTzonePath = ItmTepAutoConfigTestUtil.getTzIid(
570                 ITMConstants.DEFAULT_TRANSPORT_ZONE);
571         Assert.assertNotNull(defTzonePath);
572
573
574         // check TEP is added into default-TZ
575
576         /*if (dataBroker != null)
577         {
578             String errMsg = "present db :" + dataBroker.newReadOnlyTransaction()
579             .read(LogicalDatastoreType.CONFIGURATION, defTzonePath);
580             throw new NullPointerException(errMsg);
581         }*/
582
583
584         assertEqualBeans(ExpectedDefTransportZoneObjects.newDefTzWithTep(),
585                 dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION, defTzonePath)
586                         .get().get());
587
588         // update OVSDB node with tep-ip in local_ip list
589         tepIp = ItmTestConstants.NB_TZ_TEP_IP;
590         future = OvsdbTestUtil.updateNode(connInfo, tepIp, ITMConstants.DEFAULT_TRANSPORT_ZONE, null, dataBroker);
591         future.get();
592         // wait for OvsdbNodeListener to perform config DS update through transaction
593         coordinatorEventsWaiter.awaitEventsConsumption();
594
595         // check TEP is updated and now it is added with updated tep-ip
596         // when local_ip is updated from southbound
597         assertEqualBeans(ExpectedDefTransportZoneObjects.defTzWithUpdatedTepIp(),
598                 dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION,defTzonePath)
599                         .get().get());
600     }
601
602     @Ignore
603     public void tepUpdateForTzTest() throws Exception {
604         // wait for start-up default-TZ creation task to get over
605         coordinatorEventsWaiter.awaitEventsConsumption();
606
607         String tepIp = ItmTestConstants.DEF_TZ_TEP_IP;
608         // create Network topology node with tep-ip set into ExternalIds list
609         // OvsdbNodeListener would be automatically listen on Node to add TEP
610         ConnectionInfo connInfo = OvsdbTestUtil.getConnectionInfo(ItmTestConstants.OVSDB_CONN_PORT,
611                 ItmTestConstants.LOCALHOST_IP);
612         FluentFuture<? extends @NonNull CommitInfo> future = OvsdbTestUtil.createNode(
613                 connInfo, tepIp, ITMConstants.DEFAULT_TRANSPORT_ZONE, dataBroker);
614         future.get();
615
616         // add bridge into node
617         future = OvsdbTestUtil.addBridgeIntoNode(connInfo, ItmTestConstants.DEF_BR_NAME,
618                 ItmTestConstants.DEF_BR_DPID, dataBroker);
619         future.get();
620         // wait for OvsdbNodeListener to perform config DS update through transaction
621         coordinatorEventsWaiter.awaitEventsConsumption();
622
623         // iid for default-TZ
624         InstanceIdentifier<TransportZone> defTzonePath = ItmTepAutoConfigTestUtil.getTzIid(
625                 ITMConstants.DEFAULT_TRANSPORT_ZONE);
626         Assert.assertNotNull(defTzonePath);
627
628         // check TEP is added into default-TZ
629         assertEqualBeans(ExpectedDefTransportZoneObjects.newDefTzWithTep(),
630                 dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION, defTzonePath)
631                         .get().get());
632
633         IpPrefix subnetMaskObj = ItmUtils.getDummySubnet();
634         InstanceIdentifier<Vteps> oldVTepPath = ItmTepAutoConfigTestUtil.getTepIid(subnetMaskObj,
635                 ITMConstants.DEFAULT_TRANSPORT_ZONE, ItmTestConstants.INT_DEF_BR_DPID,
636                 ITMConstants.DUMMY_PORT);
637         Assert.assertNotNull(oldVTepPath);
638
639         // create Transport-zone TZA
640         InstanceIdentifier<TransportZone> tzPath = ItmUtils.getTZInstanceIdentifier(ItmTestConstants.TZ_NAME);
641         txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> tx.merge(LogicalDatastoreType.CONFIGURATION,
642                 tzPath, transportZone, WriteTransaction.CREATE_MISSING_PARENTS)).get();
643         // iid for TZA configured from NB
644         InstanceIdentifier<TransportZone> tzaTzonePath = ItmTepAutoConfigTestUtil.getTzIid(
645                 ItmTestConstants.TZ_NAME);
646         Assert.assertNotNull(tzaTzonePath);
647
648         // update OVSDB node with tzname=TZA in ExternalIds list
649         String tzName = ItmTestConstants.TZ_NAME;
650         future = OvsdbTestUtil.updateNode(connInfo, tepIp, tzName, null, dataBroker);
651         future.get();
652         // wait for OvsdbNodeListener to perform config DS update through transaction
653         coordinatorEventsWaiter.awaitEventsConsumption();
654
655         // check old TEP which was in default-TZ is deleted
656         Assert.assertEquals(Optional.empty(), dataBroker.newReadOnlyTransaction()
657                 .read(LogicalDatastoreType.CONFIGURATION, oldVTepPath).get());
658
659         // check TEP is updated and now it is added into TZA transport-zone when tzname is updated
660         // to TZA from southbound
661         assertEqualBeans(ExpectedTransportZoneObjects.updatedTransportZone(),
662                 dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION,tzaTzonePath)
663                         .get().get());
664     }
665
666     @Test
667     public void tepUpdateForBrNameTest() throws Exception {
668         String tepIp = ItmTestConstants.NB_TZ_TEP_IP;
669         // prepare OVSDB node with tep-ip set into ExternalIds list
670         // OvsdbNodeListener would be automatically listen on Node to add TEP
671         ConnectionInfo connInfo = OvsdbTestUtil.getConnectionInfo(ItmTestConstants.OVSDB_CONN_PORT,
672                 ItmTestConstants.LOCALHOST_IP);
673         FluentFuture<? extends @NonNull CommitInfo> future = OvsdbTestUtil.createNode(
674                 connInfo, tepIp, ItmTestConstants.TZ_NAME, dataBroker);
675         future.get();
676
677         // create Transport-zone in advance
678         InstanceIdentifier<TransportZone> tzPath = ItmUtils.getTZInstanceIdentifier(ItmTestConstants.TZ_NAME);
679         txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> tx.merge(LogicalDatastoreType.CONFIGURATION,
680                 tzPath, transportZone, WriteTransaction.CREATE_MISSING_PARENTS)).get();
681
682
683         // add bridge into node
684         future = OvsdbTestUtil.addBridgeIntoNode(connInfo, ItmTestConstants.DEF_BR_NAME,
685                 ItmTestConstants.DEF_BR_DPID, dataBroker);
686         future.get();
687         // wait for OvsdbNodeListener to perform config DS update through transaction
688         coordinatorEventsWaiter.awaitEventsConsumption();
689
690         InstanceIdentifier<TransportZone> tzonePath = ItmTepAutoConfigTestUtil.getTzIid(
691                 ItmTestConstants.TZ_NAME);
692         Assert.assertNotNull(tzonePath);
693
694         /*if (dataBroker != null)
695         {
696             String errMsg = "present db :" + dataBroker.newReadOnlyTransaction().
697             read(LogicalDatastoreType.CONFIGURATION, tzonePath);
698             throw new NullPointerException(errMsg);
699         }*/
700
701         // check TEP is added into TZ
702         assertEqualBeans(ExpectedTransportZoneObjects.newTransportZone(), dataBroker.newReadOnlyTransaction()
703                 .read(LogicalDatastoreType.CONFIGURATION, tzonePath).get().get());
704
705         IpPrefix subnetMaskObj = ItmUtils.getDummySubnet();
706
707         InstanceIdentifier<Vteps> oldVTepPath = ItmTepAutoConfigTestUtil.getTepIid(subnetMaskObj,
708                 ItmTestConstants.TZ_NAME, ItmTestConstants.INT_DEF_BR_DPID, ITMConstants.DUMMY_PORT);
709         Assert.assertNotNull(oldVTepPath);
710
711         // add new bridge br2
712         future = OvsdbTestUtil.addBridgeIntoNode(connInfo, ItmTestConstants.BR2_NAME,
713                 ItmTestConstants.BR2_DPID, dataBroker);
714         future.get();
715         // wait for OvsdbNodeListener to perform config DS update through transaction
716         coordinatorEventsWaiter.awaitEventsConsumption();
717
718         // update OVSDB node with br-name=br2 in ExternalIds column
719         String brName = ItmTestConstants.BR2_NAME;
720         future = OvsdbTestUtil.updateNode(connInfo, ItmTestConstants.NB_TZ_TEP_IP,
721                 ItmTestConstants.TZ_NAME, brName, dataBroker);
722         future.get();
723         // wait for OvsdbNodeListener to perform config DS update through transaction
724         coordinatorEventsWaiter.awaitEventsConsumption();
725
726         InstanceIdentifier<Vteps> newVTepPath = ItmTepAutoConfigTestUtil.getTepIid(subnetMaskObj,
727                 ItmTestConstants.TZ_NAME, ItmTestConstants.INT_BR2_DPID, ITMConstants.DUMMY_PORT);
728         Assert.assertNotNull(newVTepPath);
729
730         // check old TEP having default-bridge-DPID is deleted
731         Assert.assertEquals(Optional.empty(), dataBroker.newReadOnlyTransaction()
732                 .read(LogicalDatastoreType.CONFIGURATION, oldVTepPath).get());
733
734         // check TEP is updated with dpnId of br2 when br-name is updated to br2 from southbound
735         Assert.assertEquals(ItmTestConstants.INT_BR2_DPID, dataBroker.newReadOnlyTransaction()
736                 .read(LogicalDatastoreType.CONFIGURATION, newVTepPath).get().get().getDpnId());
737     }
738
739     @Test
740     public void tepAddIntoTepsNotHostedListTest() throws Exception {
741         // add into not hosted list
742         ListenableFuture<Void> future = ItmTepAutoConfigTestUtil.addTep(ItmTestConstants.NOT_HOSTED_TZ_TEP_IP,
743                 ItmTestConstants.NOT_HOSTED_TZ_TEPDPN_ID, ItmTestConstants.NOT_HOSTED_TZ_NAME,
744                 false, dataBroker, txRunner);
745         future.get();
746         InstanceIdentifier<TepsInNotHostedTransportZone> notHostedPath =
747                 ItmTepAutoConfigTestUtil.getTepNotHostedInTZIid(ItmTestConstants.NOT_HOSTED_TZ_NAME);
748         Assert.assertNotNull(notHostedPath);
749
750         assertEqualBeans(ExpectedTepNotHostedTransportZoneObjects.newTepNotHostedTransportZone(),
751                 dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL, notHostedPath)
752                         .get().get());
753     }
754
755     @Test
756     public void tepDeleteFromTepsNotHostedListTest() throws Exception {
757         // add into not hosted list
758         ListenableFuture<Void> future = ItmTepAutoConfigTestUtil.addTep(ItmTestConstants.NOT_HOSTED_TZ_TEP_IP,
759                 ItmTestConstants.NOT_HOSTED_TZ_TEPDPN_ID, ItmTestConstants.NOT_HOSTED_TZ_NAME,
760                 false, dataBroker, txRunner);
761         future.get();
762         InstanceIdentifier<TepsInNotHostedTransportZone> notHostedPath =
763                 ItmTepAutoConfigTestUtil.getTepNotHostedInTZIid(ItmTestConstants.NOT_HOSTED_TZ_NAME);
764         Assert.assertNotNull(notHostedPath);
765
766         assertEqualBeans(ExpectedTepNotHostedTransportZoneObjects.newTepNotHostedTransportZone(),
767                 dataBroker.newReadOnlyTransaction()
768                         .read(LogicalDatastoreType.OPERATIONAL, notHostedPath).get().get());
769
770         //delete from not hosted list
771         future = ItmTepAutoConfigTestUtil.deleteTep(ItmTestConstants.NOT_HOSTED_TZ_TEP_IP,
772                 ItmTestConstants.NOT_HOSTED_TZ_TEPDPN_ID, ItmTestConstants.NOT_HOSTED_TZ_NAME, dataBroker, txRunner);
773         future.get();
774
775         Assert.assertEquals(Optional.empty(),
776                 dataBroker.newReadOnlyTransaction()
777                         .read(LogicalDatastoreType.OPERATIONAL, notHostedPath).get());
778     }
779
780     @Test
781     public void tepMoveFromTepsNotHostedListToTzTest() throws Exception {
782         // add into not hosted list
783         ListenableFuture<Void> future = ItmTepAutoConfigTestUtil.addTep(ItmTestConstants.NOT_HOSTED_TZ_TEP_IP,
784                 ItmTestConstants.NOT_HOSTED_TZ_TEPDPN_ID, ItmTestConstants.NOT_HOSTED_TZ_NAME,
785                 false, dataBroker, txRunner);
786         future.get();
787         InstanceIdentifier<TepsInNotHostedTransportZone> notHostedPath =
788                 ItmTepAutoConfigTestUtil.getTepNotHostedInTZIid(ItmTestConstants.NOT_HOSTED_TZ_NAME);
789         Assert.assertNotNull(notHostedPath);
790
791         assertEqualBeans(ExpectedTepNotHostedTransportZoneObjects.newTepNotHostedTransportZone(),
792                 dataBroker.newReadOnlyTransaction()
793                         .read(LogicalDatastoreType.OPERATIONAL, notHostedPath).get().get());
794
795         // create the same TZ
796         TransportZone transportZoneNorth = new TransportZoneBuilder().setZoneName(ItmTestConstants.NOT_HOSTED_TZ_NAME)
797                 .setTunnelType(ItmTestConstants.TUNNEL_TYPE_VXLAN)
798                 .withKey(new TransportZoneKey(ItmTestConstants.NOT_HOSTED_TZ_NAME)).build();
799         Assert.assertNotNull(transportZoneNorth);
800
801         txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, tx -> tx.put(ItmTepAutoConfigTestUtil
802                 .getTzIid(ItmTestConstants.NOT_HOSTED_TZ_NAME), transportZoneNorth, true)).get();
803
804         // wait for TransportZoneListener to perform config DS update
805         // for TEP movement through transaction
806         coordinatorEventsWaiter.awaitEventsConsumption();
807
808         InstanceIdentifier<TransportZone> tzPath = ItmTepAutoConfigTestUtil.getTzIid(
809                 ItmTestConstants.NOT_HOSTED_TZ_NAME);
810         Assert.assertNotNull(tzPath);
811
812         // check TZ is Moved
813         assertEqualBeans(ExpectedTepNotHostedTransportZoneObjects
814                         .newTepNotHostedTransportZone().getUnknownVteps().get(0).getIpAddress().stringValue(),
815                 dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION, tzPath)
816                         .get().get().getVteps().get(0).getIpAddress().stringValue());
817
818         assertEqualBeans(ExpectedTepNotHostedTransportZoneObjects.newTepNotHostedTransportZone().getUnknownVteps()
819                 .get(0).getDpnId(), dataBroker.newReadOnlyTransaction()
820                 .read(LogicalDatastoreType.CONFIGURATION, tzPath).get()
821                 .get().getVteps().get(0).getDpnId());
822
823         assertEqualBeans(ExpectedTepNotHostedTransportZoneObjects.newTepNotHostedTransportZone().getZoneName(),
824                 dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION, tzPath)
825                         .get().get().getZoneName());
826
827         // check TZ is removed
828         Assert.assertEquals(Optional.empty(), dataBroker.newReadOnlyTransaction()
829                 .read(LogicalDatastoreType.OPERATIONAL, notHostedPath).get());
830     }
831
832     @Test
833     public void tzDeletedAndReaddedTest() throws Exception {
834         // create TZ
835         InstanceIdentifier<TransportZone> tzPath = ItmUtils.getTZInstanceIdentifier(ItmTestConstants.TZ_NAME);
836         txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> tx.merge(LogicalDatastoreType.CONFIGURATION,
837                 tzPath, transportZone, WriteTransaction.CREATE_MISSING_PARENTS)).get();
838
839
840         InstanceIdentifier<TransportZone> tzonePath = ItmTepAutoConfigTestUtil.getTzIid(
841                 ItmTestConstants.TZ_NAME);
842         Assert.assertNotNull(tzonePath);
843
844         // check TZ is created
845         Assert.assertEquals(ItmTestConstants.TZ_NAME, dataBroker.newReadOnlyTransaction()
846                 .read(LogicalDatastoreType.CONFIGURATION, tzonePath).get().get().getZoneName());
847
848         // add tep
849         ListenableFuture<Void> futures =
850                 ItmTepAutoConfigTestUtil.addTep(ItmTestConstants.NB_TZ_TEP_IP,
851                         ItmTestConstants.DEF_BR_DPID, ItmTestConstants.TZ_NAME, false, dataBroker, txRunner);
852         futures.get();
853
854         IpPrefix subnetMaskObj = ItmUtils.getDummySubnet();
855
856         InstanceIdentifier<Vteps> vtepPath = ItmTepAutoConfigTestUtil.getTepIid(subnetMaskObj,
857                 ItmTestConstants.TZ_NAME, ItmTestConstants.INT_DEF_BR_DPID, ITMConstants.DUMMY_PORT);
858         Assert.assertNotNull(vtepPath);
859
860         // check TEP is added into TZ that is already created.
861         assertEqualBeans(ExpectedTransportZoneObjects.newTransportZone(),
862                 dataBroker.newReadOnlyTransaction()
863                         .read(LogicalDatastoreType.CONFIGURATION, tzonePath).get().get());
864
865         // remove Transport Zone
866         txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> tx.delete(LogicalDatastoreType.CONFIGURATION,
867                 tzPath));
868
869         // wait for TransportZoneListener to perform config DS update
870         // for TEP movement through transaction
871         coordinatorEventsWaiter.awaitEventsConsumption();
872         //verify delete
873
874         Assert.assertEquals(Optional.empty(), dataBroker.newReadOnlyTransaction()
875                 .read(LogicalDatastoreType.CONFIGURATION, tzonePath).get());
876
877         //check deleted tz moved to notHosted
878         InstanceIdentifier<TepsInNotHostedTransportZone> notHostedPath =
879                 ItmTepAutoConfigTestUtil.getTepNotHostedInTZIid(ItmTestConstants.TZ_NAME);
880         Assert.assertNotNull(notHostedPath);
881         Assert.assertEquals(ItmTestConstants.TZ_NAME, dataBroker.newReadOnlyTransaction()
882                 .read(LogicalDatastoreType.OPERATIONAL, notHostedPath).get().get().getZoneName());
883
884         //readd the same tz
885         txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> tx.merge(LogicalDatastoreType.CONFIGURATION,
886                 tzPath, transportZone, WriteTransaction.CREATE_MISSING_PARENTS)).get();
887         // wait for TransportZoneListener to perform config DS update
888         // for TEP movement through transaction
889         coordinatorEventsWaiter.awaitEventsConsumption();
890
891         Assert.assertEquals(Optional.empty(), dataBroker.newReadOnlyTransaction()
892                 .read(LogicalDatastoreType.OPERATIONAL, notHostedPath).get());
893
894         assertEqualBeans(ExpectedTransportZoneObjects.newTransportZone(),
895                 dataBroker.newReadOnlyTransaction()
896                         .read(LogicalDatastoreType.CONFIGURATION, tzonePath).get().get());
897     }
898
899     @Test
900     @Ignore
901     public void tzDeletedAndReaddedWithSameVtepsTest() throws Exception {
902         // create TZ
903         InstanceIdentifier<TransportZone> tzPath = ItmUtils.getTZInstanceIdentifier(ItmTestConstants.TZ_NAME);
904         txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> tx.merge(LogicalDatastoreType.CONFIGURATION,
905                 tzPath, transportZone, WriteTransaction.CREATE_MISSING_PARENTS)).get();
906
907         coordinatorEventsWaiter.awaitEventsConsumption();
908
909         InstanceIdentifier<TransportZone> tzonePath = ItmTepAutoConfigTestUtil.getTzIid(
910                 ItmTestConstants.TZ_NAME);
911         Assert.assertNotNull(tzonePath);
912
913         // check TZ is created
914         Assert.assertEquals(ItmTestConstants.TZ_NAME, dataBroker.newReadOnlyTransaction()
915                 .read(LogicalDatastoreType.CONFIGURATION, tzonePath).get().get().getZoneName());
916
917         // add tep
918         ListenableFuture<Void> futures =
919                 ItmTepAutoConfigTestUtil.addTep(ItmTestConstants.NB_TZ_TEP_IP,
920                         ItmTestConstants.DEF_BR_DPID, ItmTestConstants.TZ_NAME, false, dataBroker, txRunner);
921         futures.get();
922         coordinatorEventsWaiter.awaitEventsConsumption();
923
924         IpPrefix subnetMaskObj = ItmUtils.getDummySubnet();
925
926         InstanceIdentifier<Vteps> vtepPath = ItmTepAutoConfigTestUtil.getTepIid(subnetMaskObj,
927                 ItmTestConstants.TZ_NAME, ItmTestConstants.INT_DEF_BR_DPID, ITMConstants.DUMMY_PORT);
928         Assert.assertNotNull(vtepPath);
929
930         // check TEP is added into TZ that is already created.
931         assertEqualBeans(ExpectedTransportZoneObjects.newTransportZone(),
932                 dataBroker.newReadOnlyTransaction()
933                         .read(LogicalDatastoreType.CONFIGURATION, tzonePath).get().get());
934
935         // remove Transport Zone
936         txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> tx.delete(LogicalDatastoreType.CONFIGURATION,
937                 tzPath));
938
939         // wait for TransportZoneListener to perform config DS update
940         // for TEP movement through transaction
941         coordinatorEventsWaiter.awaitEventsConsumption();
942         //verify delete
943
944         Assert.assertEquals(Optional.empty(), dataBroker.newReadOnlyTransaction()
945                 .read(LogicalDatastoreType.CONFIGURATION, tzonePath).get());
946
947         //check deleted tz moved to notHosted
948         InstanceIdentifier<TepsInNotHostedTransportZone> notHostedPath =
949                 ItmTepAutoConfigTestUtil.getTepNotHostedInTZIid(ItmTestConstants.TZ_NAME);
950         coordinatorEventsWaiter.awaitEventsConsumption();
951         Assert.assertNotNull(notHostedPath);
952         Assert.assertEquals(ItmTestConstants.TZ_NAME, dataBroker.newReadOnlyTransaction()
953                 .read(LogicalDatastoreType.OPERATIONAL, notHostedPath).get().get().getZoneName());
954
955         //create vtepList form unknownVtepList
956         List<Vteps> vtepsList = new ArrayList<>();
957         List<UnknownVteps> unknownVtepsList = dataBroker.newReadOnlyTransaction()
958                 .read(LogicalDatastoreType.OPERATIONAL, notHostedPath).get().get().getUnknownVteps();
959
960         for (UnknownVteps unknownVtep:unknownVtepsList) {
961             Vteps vteps = new VtepsBuilder().setDpnId(unknownVtep.getDpnId())
962                     .setIpAddress(unknownVtep.getIpAddress())
963                     .withKey(new VtepsKey(unknownVtep.getDpnId())).build();
964             vtepsList.add(vteps);
965         }
966
967         TransportZone recreatedTransportZone = new TransportZoneBuilder().setZoneName(ItmTestConstants.TZ_NAME)
968                 .setTunnelType(ItmTestConstants.TUNNEL_TYPE_VXLAN).setVteps(vtepsList)
969                 .withKey(new TransportZoneKey(ItmTestConstants.TZ_NAME)).build();
970
971         txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> tx.merge(LogicalDatastoreType.CONFIGURATION,
972                 tzPath, recreatedTransportZone, WriteTransaction.CREATE_MISSING_PARENTS)).get();
973
974         // wait for TransportZoneListener to perform config DS update
975         // for TEP movement through transaction
976         coordinatorEventsWaiter.awaitEventsConsumption();
977
978         // verify TZ is moved from notHosted to transport zone and their should be only one vtep in it.
979
980         Assert.assertEquals(Optional.empty(), dataBroker.newReadOnlyTransaction()
981                 .read(LogicalDatastoreType.OPERATIONAL, notHostedPath).get());
982
983         assertEqualBeans(1, dataBroker.newReadOnlyTransaction()
984                 .read(LogicalDatastoreType.CONFIGURATION, tzonePath).get().get()
985                 .getVteps().size());
986     }
987
988     @Test
989     public void tzReaddWithSameVtepsAndDiffDpnIDTest() throws Exception {
990         // create TZ
991         InstanceIdentifier<TransportZone> tzPath = ItmUtils.getTZInstanceIdentifier(ItmTestConstants.TZ_NAME);
992         txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> tx.merge(LogicalDatastoreType.CONFIGURATION,
993                 tzPath, transportZone, WriteTransaction.CREATE_MISSING_PARENTS)).get();
994
995
996         InstanceIdentifier<TransportZone> tzonePath = ItmTepAutoConfigTestUtil.getTzIid(
997                 ItmTestConstants.TZ_NAME);
998         Assert.assertNotNull(tzonePath);
999
1000         // check TZ is created
1001         Assert.assertEquals(ItmTestConstants.TZ_NAME, dataBroker.newReadOnlyTransaction()
1002                 .read(LogicalDatastoreType.CONFIGURATION, tzonePath).get().get().getZoneName());
1003
1004         // add tep
1005         ListenableFuture<Void> futures =
1006                 ItmTepAutoConfigTestUtil.addTep(ItmTestConstants.NB_TZ_TEP_IP,
1007                         ItmTestConstants.DEF_BR_DPID, ItmTestConstants.TZ_NAME, false, dataBroker, txRunner);
1008         futures.get();
1009
1010         IpPrefix subnetMaskObj = ItmUtils.getDummySubnet();
1011
1012         InstanceIdentifier<Vteps> vtepPath = ItmTepAutoConfigTestUtil.getTepIid(subnetMaskObj,
1013                 ItmTestConstants.TZ_NAME, ItmTestConstants.INT_DEF_BR_DPID, ITMConstants.DUMMY_PORT);
1014         Assert.assertNotNull(vtepPath);
1015
1016         // check TEP is added into TZ that is already created.
1017         assertEqualBeans(ExpectedTransportZoneObjects.newTransportZone(),
1018                 dataBroker.newReadOnlyTransaction()
1019                         .read(LogicalDatastoreType.CONFIGURATION, tzonePath).get().get());
1020
1021         // remove Transport Zone
1022         txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> tx.delete(LogicalDatastoreType.CONFIGURATION,
1023                 tzPath));
1024
1025         // wait for TransportZoneListener to perform config DS update
1026         // for TEP movement through transaction
1027         coordinatorEventsWaiter.awaitEventsConsumption();
1028         //verify delete
1029
1030         Assert.assertEquals(Optional.empty(), dataBroker.newReadOnlyTransaction()
1031                 .read(LogicalDatastoreType.CONFIGURATION, tzonePath).get());
1032
1033         //check deleted tz moved to notHosted
1034         InstanceIdentifier<TepsInNotHostedTransportZone> notHostedPath =
1035                 ItmTepAutoConfigTestUtil.getTepNotHostedInTZIid(ItmTestConstants.TZ_NAME);
1036
1037         Assert.assertNotNull(notHostedPath);
1038         Assert.assertEquals(ItmTestConstants.TZ_NAME, dataBroker.newReadOnlyTransaction()
1039                 .read(LogicalDatastoreType.OPERATIONAL, notHostedPath).get().get().getZoneName());
1040
1041         //create vtepList form unknownVtepList
1042         List<Vteps> vtepsList = new ArrayList<>();
1043         List<UnknownVteps> unknownVtepsList = dataBroker.newReadOnlyTransaction()
1044                 .read(LogicalDatastoreType.OPERATIONAL, notHostedPath).get().get().getUnknownVteps();
1045         //modifing the dpnid and keeping the ip same.
1046         for (UnknownVteps unknownVtep:unknownVtepsList) {
1047             Vteps vteps = new VtepsBuilder().setDpnId(Uint64.valueOf(10))
1048                     .setIpAddress(unknownVtep.getIpAddress())
1049                     .withKey(new VtepsKey(Uint64.valueOf(10))).build();
1050             vtepsList.add(vteps);
1051         }
1052
1053         TransportZone recreatedTransportZone = new TransportZoneBuilder().setZoneName(ItmTestConstants.TZ_NAME)
1054                 .setTunnelType(ItmTestConstants.TUNNEL_TYPE_VXLAN).setVteps(vtepsList)
1055                 .withKey(new TransportZoneKey(ItmTestConstants.TZ_NAME)).build();
1056
1057         txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> tx.merge(LogicalDatastoreType.CONFIGURATION,
1058                 tzPath, recreatedTransportZone, WriteTransaction.CREATE_MISSING_PARENTS)).get();
1059
1060         // wait for TransportZoneListener to perform config DS update
1061         // for TEP movement through transaction
1062         coordinatorEventsWaiter.awaitEventsConsumption();
1063
1064         // verify TZ is moved from notHosted to transport zone and their should be only one vtep in it.
1065
1066         Assert.assertEquals(Optional.empty(), dataBroker.newReadOnlyTransaction()
1067                 .read(LogicalDatastoreType.OPERATIONAL, notHostedPath).get());
1068
1069         assertEqualBeans(1, dataBroker.newReadOnlyTransaction()
1070                 .read(LogicalDatastoreType.CONFIGURATION, tzonePath).get().get()
1071                 .getVteps().size());
1072     }
1073 }
1074