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