e3e4e684d9027a51d4f552597089702be3285ae2
[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.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.infra.RetryingManagedNewTransactionRunner;
33 import org.opendaylight.genius.itm.globals.ITMConstants;
34 import org.opendaylight.genius.itm.impl.ItmProvider;
35 import org.opendaylight.genius.itm.impl.ItmUtils;
36 import org.opendaylight.genius.itm.tests.xtend.ExpectedDefTransportZoneObjects;
37 import org.opendaylight.genius.itm.tests.xtend.ExpectedTepNotHostedTransportZoneObjects;
38 import org.opendaylight.genius.itm.tests.xtend.ExpectedTransportZoneObjects;
39 import org.opendaylight.infrautils.caches.testutils.CacheModule;
40 import org.opendaylight.infrautils.inject.guice.testutils.GuiceRule;
41 import org.opendaylight.infrautils.testutils.LogRule;
42 import org.opendaylight.mdsal.binding.api.DataBroker;
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(tx -> tx.merge(LogicalDatastoreType.CONFIGURATION,
122                         iid,itmConfigObj));
123         coordinatorEventsWaiter.awaitEventsConsumption();
124
125         // read from config DS
126         boolean defTzEnabled = SingleTransactionDataBroker.syncReadOptional(
127                 dataBroker, LogicalDatastoreType.CONFIGURATION, iid).get().isDefTzEnabled();
128         Assert.assertEquals(defTzEnabled, false);
129     }
130
131     @Test
132     public void defTzEnabledTrueConfigTest() throws Exception {
133         InstanceIdentifier<ItmConfig> iid = InstanceIdentifier.create(ItmConfig.class);
134         // set def-tz-enabled flag to true
135         ItmConfig itmConfigObj = new ItmConfigBuilder().setDefTzEnabled(true).build();
136
137         // write into config DS
138         //txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
139           //  tx -> tx.put(iid, itmConfigObj, true));
140
141         txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> tx.merge(LogicalDatastoreType.CONFIGURATION,
142                 iid,itmConfigObj)).get();
143
144         coordinatorEventsWaiter.awaitEventsConsumption();
145
146         // read from config DS
147         boolean defTzEnabled = SingleTransactionDataBroker.syncReadOptional(
148                 dataBroker, LogicalDatastoreType.CONFIGURATION, iid).get().isDefTzEnabled();
149         Assert.assertEquals(defTzEnabled, true);
150     }
151
152     @Test
153     public void defTzCreationTestWithDefTzEnabledTrueAndVxlanTunnelType() throws Exception {
154         // set def-tz-enabled flag to true
155         boolean defTzEnabledFlag = true;
156         // set def-tz-tunnel-type to VXLAN
157         String defTzTunnelType = ITMConstants.TUNNEL_TYPE_VXLAN;
158
159         InstanceIdentifier<TransportZone> tzonePath = processDefTzOnItmConfig(defTzEnabledFlag,
160                 defTzTunnelType);
161         Assert.assertNotNull(tzonePath);
162
163         assertEqualBeans(ExpectedDefTransportZoneObjects.newDefTzWithVxlanTunnelType(),
164                 dataBroker.newReadOnlyTransaction()
165                         .read(LogicalDatastoreType.CONFIGURATION, tzonePath).get().get());
166     }
167
168     @Test
169     public void defTzCreationTestWithDefTzEnabledTrueAndGreTunnelType() throws Exception {
170         // set def-tz-enabled flag to true
171         boolean defTzEnabledFlag = true;
172         // set def-tz-tunnel-type to GRE
173         String defTzTunnelType = ITMConstants.TUNNEL_TYPE_GRE;
174
175         InstanceIdentifier<TransportZone> tzPath =
176                 ItmUtils.getTZInstanceIdentifier(ITMConstants.DEFAULT_TRANSPORT_ZONE);
177         txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx ->
178                 tx.mergeParentStructureMerge(LogicalDatastoreType.CONFIGURATION, tzPath,
179                         new TransportZoneBuilder().setZoneName(ITMConstants.DEFAULT_TRANSPORT_ZONE)
180                         .setTunnelType(TunnelTypeGre.class)
181                         .withKey(new TransportZoneKey(ITMConstants.DEFAULT_TRANSPORT_ZONE))
182                         .build())).get();
183
184
185         InstanceIdentifier<TransportZone> tzonePath = processDefTzOnItmConfig(defTzEnabledFlag,
186                 defTzTunnelType);
187         Assert.assertNotNull(tzonePath);
188
189         assertEqualBeans(ExpectedDefTransportZoneObjects.newDefTzWithGreTunnelType(),
190                 dataBroker.newReadOnlyTransaction()
191                         .read(LogicalDatastoreType.CONFIGURATION, tzonePath).get().get());
192     }
193
194     @Test
195     public void defTzCreationFailedTestWithDefTzEnabledFalse() throws Exception {
196         // set def-tz-enabled flag to false
197         boolean defTzEnabledFlag = false;
198         // set def-tz-tunnel-type to GRE
199         String defTzTunnelType = null;
200
201         InstanceIdentifier<TransportZone> tzonePath = processDefTzOnItmConfig(defTzEnabledFlag,
202                 defTzTunnelType);
203         Assert.assertNotNull(tzonePath);
204
205         Assert.assertEquals(Optional.empty(), dataBroker.newReadOnlyTransaction()
206                 .read(LogicalDatastoreType.CONFIGURATION, tzonePath).get());
207     }
208
209     @Test
210     public void defTzRecreationTestOnTunnelTypeChange() throws Exception {
211         // set def-tz-enabled flag to true
212         boolean defTzEnabledFlag = true;
213         // set def-tz-tunnel-type to VXLAN
214         String defTzTunnelType = ITMConstants.TUNNEL_TYPE_VXLAN;
215
216         InstanceIdentifier<TransportZone> tzPath =
217                 ItmUtils.getTZInstanceIdentifier(ITMConstants.DEFAULT_TRANSPORT_ZONE);
218         txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx ->
219                 tx.mergeParentStructureMerge(LogicalDatastoreType.CONFIGURATION, tzPath,
220                         new TransportZoneBuilder().setZoneName(ITMConstants.DEFAULT_TRANSPORT_ZONE)
221                         .setTunnelType(TunnelTypeVxlan.class)
222                         .withKey(new TransportZoneKey(ITMConstants.DEFAULT_TRANSPORT_ZONE))
223                         .build())).get();
224
225         InstanceIdentifier<TransportZone> tzonePath = processDefTzOnItmConfig(defTzEnabledFlag,
226                 defTzTunnelType);
227         Assert.assertNotNull(tzonePath);
228
229         // check default-TZ is created with VXLAN tunnel type
230         assertEqualBeans(ExpectedDefTransportZoneObjects.newDefTzWithVxlanTunnelType().getTunnelType(),
231                 dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION, tzonePath)
232                         .get().get().getTunnelType());
233
234         // now, change def-tz-tunnel-type to GRE
235         defTzTunnelType = ITMConstants.TUNNEL_TYPE_GRE;
236
237
238         txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx ->
239                 tx.mergeParentStructureMerge(LogicalDatastoreType.CONFIGURATION, tzPath,
240                         new TransportZoneBuilder().setZoneName(ITMConstants.DEFAULT_TRANSPORT_ZONE)
241                         .setTunnelType(TunnelTypeGre.class)
242                         .withKey(new TransportZoneKey(ITMConstants.DEFAULT_TRANSPORT_ZONE))
243                         .build())).get();
244
245
246         tzonePath = processDefTzOnItmConfig(defTzEnabledFlag, defTzTunnelType);
247         Assert.assertNotNull(tzonePath);
248
249         // check default-TZ is re-created with GRE tunnel type
250         assertEqualBeans(ExpectedDefTransportZoneObjects.newDefTzWithGreTunnelType().getTunnelType(),
251                 dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION, tzonePath)
252                         .get().get().getTunnelType());
253     }
254
255     @Test
256     public void defTzDeletionTest() throws Exception {
257         // wait for start-up default-TZ creation task to get over
258         coordinatorEventsWaiter.awaitEventsConsumption();
259
260         // create default-TZ first by setting def-tz-enabled flag to true
261         ItmConfig itmConfigObj = new ItmConfigBuilder().setDefTzEnabled(true)
262                 .setDefTzTunnelType(ITMConstants.TUNNEL_TYPE_GRE).build();
263
264         // creates/deletes default-TZ based on def-tz-enabled flag
265         itmProvider.createDefaultTransportZone(itmConfigObj);
266         coordinatorEventsWaiter.awaitEventsConsumption();
267
268         InstanceIdentifier<TransportZone> tzPath =
269                 ItmUtils.getTZInstanceIdentifier(ITMConstants.DEFAULT_TRANSPORT_ZONE);
270         txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx ->
271                 tx.mergeParentStructureMerge(LogicalDatastoreType.CONFIGURATION, tzPath,
272                         new TransportZoneBuilder().setZoneName(ITMConstants.DEFAULT_TRANSPORT_ZONE)
273                         .setTunnelType(TunnelTypeGre.class)
274                         .withKey(new TransportZoneKey(ITMConstants.DEFAULT_TRANSPORT_ZONE))
275                         .build())).get();
276
277         InstanceIdentifier<TransportZone> tzonePath = ItmTepAutoConfigTestUtil.getTzIid(
278                 ITMConstants.DEFAULT_TRANSPORT_ZONE);
279         Assert.assertNotNull(tzonePath);
280
281         assertEqualBeans(ExpectedDefTransportZoneObjects.newDefTzWithGreTunnelType(),
282                 dataBroker.newReadOnlyTransaction()
283                         .read(LogicalDatastoreType.CONFIGURATION, tzonePath).get().get());
284
285         // now delete default-TZ first by setting def-tz-enabled flag to false
286         itmConfigObj = new ItmConfigBuilder().setDefTzEnabled(false).build();
287
288         // creates/deletes default-TZ based on def-tz-enabled flag
289         itmProvider.createDefaultTransportZone(itmConfigObj);
290         coordinatorEventsWaiter.awaitEventsConsumption();
291
292         Assert.assertEquals(Optional.empty(), dataBroker.newReadOnlyTransaction()
293                 .read(LogicalDatastoreType.CONFIGURATION, tzonePath).get());
294     }
295
296     @Test
297     public void testAddDeleteTepForDefTz() throws Exception {
298         // wait for start-up default-TZ creation task to get over
299         coordinatorEventsWaiter.awaitEventsConsumption();
300
301         InstanceIdentifier<TransportZone> tzPath =
302                 ItmUtils.getTZInstanceIdentifier(ITMConstants.DEFAULT_TRANSPORT_ZONE);
303         txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx ->
304                 tx.mergeParentStructureMerge(LogicalDatastoreType.CONFIGURATION, tzPath,
305                         new TransportZoneBuilder().setZoneName(ITMConstants.DEFAULT_TRANSPORT_ZONE)
306                         .setTunnelType(TunnelTypeVxlan.class)
307                         .withKey(new TransportZoneKey(ITMConstants.DEFAULT_TRANSPORT_ZONE))
308                         .build())).get();
309
310         InstanceIdentifier<TransportZone> tzonePath = ItmTepAutoConfigTestUtil.getTzIid(
311                 ITMConstants.DEFAULT_TRANSPORT_ZONE);
312         Assert.assertNotNull(tzonePath);
313
314         IpPrefix subnetMaskObj = ItmUtils.getDummySubnet();
315
316         // add TEP into default-TZ
317         ListenableFuture<Void> futures =
318                 ItmTepAutoConfigTestUtil.addTep(ItmTestConstants.DEF_TZ_TEP_IP,
319                         ItmTestConstants.DEF_BR_DPID,
320                         ITMConstants.DEFAULT_TRANSPORT_ZONE, false, dataBroker, txRunner);
321         futures.get();
322
323         InstanceIdentifier<Vteps> vtepPath = ItmTepAutoConfigTestUtil.getTepIid(subnetMaskObj,
324                 ITMConstants.DEFAULT_TRANSPORT_ZONE, ItmTestConstants.INT_DEF_BR_DPID,
325                 ITMConstants.DUMMY_PORT);
326         Assert.assertNotNull(vtepPath);
327
328         // check TEP is added into default-TZ
329         assertEqualBeans(ExpectedDefTransportZoneObjects.newDefTzWithTep(), dataBroker.newReadOnlyTransaction()
330                 .read(LogicalDatastoreType.CONFIGURATION, tzonePath).get().get());
331
332         // remove tep from default-TZ
333         futures = ItmTepAutoConfigTestUtil.deleteTep(ItmTestConstants.DEF_TZ_TEP_IP, ItmTestConstants.DEF_BR_DPID,
334                 ITMConstants.DEFAULT_TRANSPORT_ZONE, dataBroker, txRunner);
335         futures.get();
336
337         // check TEP is deleted from default-TZ
338         Assert.assertEquals(Optional.empty(), dataBroker.newReadOnlyTransaction()
339                 .read(LogicalDatastoreType.CONFIGURATION, vtepPath).get());
340     }
341
342     @Test
343     public void testAddDeleteTepForTz() throws Exception {
344         // wait for start-up default-TZ creation task to get over
345         coordinatorEventsWaiter.awaitEventsConsumption();
346
347         InstanceIdentifier<TransportZone> tzonePath = ItmTepAutoConfigTestUtil.getTzIid(
348                 ItmTestConstants.TZ_NAME);
349         Assert.assertNotNull(tzonePath);
350
351         // create TZA
352         InstanceIdentifier<TransportZone> tzPath = ItmUtils.getTZInstanceIdentifier(ItmTestConstants.TZ_NAME);
353         txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx ->
354                 tx.mergeParentStructureMerge(LogicalDatastoreType.CONFIGURATION, tzPath, transportZone)).get();
355
356         // check TZ is created with correct TZ name
357         Assert.assertEquals(ItmTestConstants.TZ_NAME, dataBroker.newReadOnlyTransaction()
358                 .read(LogicalDatastoreType.CONFIGURATION, tzonePath).get().get().getZoneName());
359
360         // add tep
361         ListenableFuture<Void> futures = ItmTepAutoConfigTestUtil.addTep(ItmTestConstants.NB_TZ_TEP_IP,
362                 ItmTestConstants.DEF_BR_DPID, ItmTestConstants.TZ_NAME, false, dataBroker, txRunner);
363         futures.get();
364
365         IpPrefix subnetMaskObj = ItmUtils.getDummySubnet();
366
367         InstanceIdentifier<Vteps> vtepPath = ItmTepAutoConfigTestUtil.getTepIid(subnetMaskObj,
368                 ItmTestConstants.TZ_NAME, ItmTestConstants.INT_DEF_BR_DPID, ITMConstants.DUMMY_PORT);
369         Assert.assertNotNull(vtepPath);
370
371         // check TEP is added into TZ that is already created.
372         assertEqualBeans(ExpectedTransportZoneObjects.newTransportZone(),
373                 dataBroker.newReadOnlyTransaction()
374                         .read(LogicalDatastoreType.CONFIGURATION, tzonePath).get().get());
375
376         // remove tep
377         futures = ItmTepAutoConfigTestUtil.deleteTep(ItmTestConstants.NB_TZ_TEP_IP, ItmTestConstants.DEF_BR_DPID,
378                 ItmTestConstants.TZ_NAME, dataBroker, txRunner);
379         futures.get();
380
381         // check TEP is deleted
382         Assert.assertEquals(Optional.empty(), dataBroker.newReadOnlyTransaction()
383                 .read(LogicalDatastoreType.CONFIGURATION, vtepPath).get());
384
385         // for safe side, check TZ is present
386         Assert.assertEquals(ItmTestConstants.TZ_NAME, dataBroker.newReadOnlyTransaction()
387                 .read(LogicalDatastoreType.CONFIGURATION, tzonePath).get().get().getZoneName());
388     }
389
390     @Test
391     public void tepAddDeleteFromDefTzViaSouthboundTest() throws Exception  {
392         // wait for start-up default-TZ creation task to get over
393         coordinatorEventsWaiter.awaitEventsConsumption();
394
395         String tepIp = ItmTestConstants.DEF_TZ_TEP_IP;
396         // create Network topology node with tep-ip set into ExternalIds list
397         // OvsdbNodeListener would be automatically listen on Node to add TEP
398         ConnectionInfo connInfo = OvsdbTestUtil.getConnectionInfo(ItmTestConstants.OVSDB_CONN_PORT,
399                 ItmTestConstants.LOCALHOST_IP);
400         FluentFuture<? extends @NonNull CommitInfo> future =
401                 OvsdbTestUtil.createNode(connInfo,tepIp,ITMConstants.DEFAULT_TRANSPORT_ZONE,dataBroker);
402         future.get();
403
404         InstanceIdentifier<TransportZone> tzPath =
405                 ItmUtils.getTZInstanceIdentifier(ITMConstants.DEFAULT_TRANSPORT_ZONE);
406         txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx ->
407                 tx.mergeParentStructureMerge(LogicalDatastoreType.CONFIGURATION, tzPath,
408                         new TransportZoneBuilder().setZoneName(ITMConstants.DEFAULT_TRANSPORT_ZONE)
409                         .setTunnelType(ItmTestConstants.TUNNEL_TYPE_VXLAN)
410                         .withKey(new TransportZoneKey(ITMConstants.DEFAULT_TRANSPORT_ZONE))
411                         .build())).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 ->
465                 tx.mergeParentStructureMerge(LogicalDatastoreType.CONFIGURATION, tzPath, transportZone)).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 ->
555                 tx.mergeParentStructureMerge(LogicalDatastoreType.CONFIGURATION, tzPath,
556                         new TransportZoneBuilder().setZoneName(ITMConstants.DEFAULT_TRANSPORT_ZONE)
557                         .setTunnelType(ItmTestConstants.TUNNEL_TYPE_VXLAN)
558                         .withKey(new TransportZoneKey(ITMConstants.DEFAULT_TRANSPORT_ZONE))
559                         .build())).get();
560
561         // add bridge into node
562         future = OvsdbTestUtil.addBridgeIntoNode(connInfo, ItmTestConstants.DEF_BR_NAME,
563                 ItmTestConstants.DEF_BR_DPID, dataBroker);
564         future.get();
565         // wait for OvsdbNodeListener to perform config DS update through transaction
566         coordinatorEventsWaiter.awaitEventsConsumption();
567         //Thread.sleep(5000);
568
569         // iid for default-TZ
570         InstanceIdentifier<TransportZone> defTzonePath = ItmTepAutoConfigTestUtil.getTzIid(
571                 ITMConstants.DEFAULT_TRANSPORT_ZONE);
572         Assert.assertNotNull(defTzonePath);
573
574
575         // check TEP is added into default-TZ
576
577         /*if (dataBroker != null)
578         {
579             String errMsg = "present db :" + dataBroker.newReadOnlyTransaction()
580             .read(LogicalDatastoreType.CONFIGURATION, defTzonePath);
581             throw new NullPointerException(errMsg);
582         }*/
583
584
585         assertEqualBeans(ExpectedDefTransportZoneObjects.newDefTzWithTep(),
586                 dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION, defTzonePath)
587                         .get().get());
588
589         // update OVSDB node with tep-ip in local_ip list
590         tepIp = ItmTestConstants.NB_TZ_TEP_IP;
591         future = OvsdbTestUtil.updateNode(connInfo, tepIp, ITMConstants.DEFAULT_TRANSPORT_ZONE, null, dataBroker);
592         future.get();
593         // wait for OvsdbNodeListener to perform config DS update through transaction
594         coordinatorEventsWaiter.awaitEventsConsumption();
595
596         // check TEP is updated and now it is added with updated tep-ip
597         // when local_ip is updated from southbound
598         assertEqualBeans(ExpectedDefTransportZoneObjects.defTzWithUpdatedTepIp(),
599                 dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION,defTzonePath)
600                         .get().get());
601     }
602
603     @Ignore
604     public void tepUpdateForTzTest() throws Exception {
605         // wait for start-up default-TZ creation task to get over
606         coordinatorEventsWaiter.awaitEventsConsumption();
607
608         String tepIp = ItmTestConstants.DEF_TZ_TEP_IP;
609         // create Network topology node with tep-ip set into ExternalIds list
610         // OvsdbNodeListener would be automatically listen on Node to add TEP
611         ConnectionInfo connInfo = OvsdbTestUtil.getConnectionInfo(ItmTestConstants.OVSDB_CONN_PORT,
612                 ItmTestConstants.LOCALHOST_IP);
613         FluentFuture<? extends @NonNull CommitInfo> future = OvsdbTestUtil.createNode(
614                 connInfo, tepIp, ITMConstants.DEFAULT_TRANSPORT_ZONE, dataBroker);
615         future.get();
616
617         // add bridge into node
618         future = OvsdbTestUtil.addBridgeIntoNode(connInfo, ItmTestConstants.DEF_BR_NAME,
619                 ItmTestConstants.DEF_BR_DPID, dataBroker);
620         future.get();
621         // wait for OvsdbNodeListener to perform config DS update through transaction
622         coordinatorEventsWaiter.awaitEventsConsumption();
623
624         // iid for default-TZ
625         InstanceIdentifier<TransportZone> defTzonePath = ItmTepAutoConfigTestUtil.getTzIid(
626                 ITMConstants.DEFAULT_TRANSPORT_ZONE);
627         Assert.assertNotNull(defTzonePath);
628
629         // check TEP is added into default-TZ
630         assertEqualBeans(ExpectedDefTransportZoneObjects.newDefTzWithTep(),
631                 dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION, defTzonePath)
632                         .get().get());
633
634         IpPrefix subnetMaskObj = ItmUtils.getDummySubnet();
635         InstanceIdentifier<Vteps> oldVTepPath = ItmTepAutoConfigTestUtil.getTepIid(subnetMaskObj,
636                 ITMConstants.DEFAULT_TRANSPORT_ZONE, ItmTestConstants.INT_DEF_BR_DPID,
637                 ITMConstants.DUMMY_PORT);
638         Assert.assertNotNull(oldVTepPath);
639
640         // create Transport-zone TZA
641         InstanceIdentifier<TransportZone> tzPath = ItmUtils.getTZInstanceIdentifier(ItmTestConstants.TZ_NAME);
642         txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx ->
643                 tx.mergeParentStructureMerge(LogicalDatastoreType.CONFIGURATION, tzPath, transportZone)).get();
644         // iid for TZA configured from NB
645         InstanceIdentifier<TransportZone> tzaTzonePath = ItmTepAutoConfigTestUtil.getTzIid(
646                 ItmTestConstants.TZ_NAME);
647         Assert.assertNotNull(tzaTzonePath);
648
649         // update OVSDB node with tzname=TZA in ExternalIds list
650         String tzName = ItmTestConstants.TZ_NAME;
651         future = OvsdbTestUtil.updateNode(connInfo, tepIp, tzName, null, dataBroker);
652         future.get();
653         // wait for OvsdbNodeListener to perform config DS update through transaction
654         coordinatorEventsWaiter.awaitEventsConsumption();
655
656         // check old TEP which was in default-TZ is deleted
657         Assert.assertEquals(Optional.empty(), dataBroker.newReadOnlyTransaction()
658                 .read(LogicalDatastoreType.CONFIGURATION, oldVTepPath).get());
659
660         // check TEP is updated and now it is added into TZA transport-zone when tzname is updated
661         // to TZA from southbound
662         assertEqualBeans(ExpectedTransportZoneObjects.updatedTransportZone(),
663                 dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION,tzaTzonePath)
664                         .get().get());
665     }
666
667     @Test
668     public void tepUpdateForBrNameTest() throws Exception {
669         String tepIp = ItmTestConstants.NB_TZ_TEP_IP;
670         // prepare OVSDB node with tep-ip set into ExternalIds list
671         // OvsdbNodeListener would be automatically listen on Node to add TEP
672         ConnectionInfo connInfo = OvsdbTestUtil.getConnectionInfo(ItmTestConstants.OVSDB_CONN_PORT,
673                 ItmTestConstants.LOCALHOST_IP);
674         FluentFuture<? extends @NonNull CommitInfo> future = OvsdbTestUtil.createNode(
675                 connInfo, tepIp, ItmTestConstants.TZ_NAME, dataBroker);
676         future.get();
677
678         // create Transport-zone in advance
679         InstanceIdentifier<TransportZone> tzPath = ItmUtils.getTZInstanceIdentifier(ItmTestConstants.TZ_NAME);
680         txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx ->
681                 tx.mergeParentStructureMerge(LogicalDatastoreType.CONFIGURATION, tzPath, transportZone)).get();
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,
802             tx -> tx.mergeParentStructurePut(ItmTepAutoConfigTestUtil.getTzIid(ItmTestConstants.NOT_HOSTED_TZ_NAME),
803                 transportZoneNorth)).get();
804
805         // wait for TransportZoneListener to perform config DS update
806         // for TEP movement through transaction
807         coordinatorEventsWaiter.awaitEventsConsumption();
808
809         InstanceIdentifier<TransportZone> tzPath = ItmTepAutoConfigTestUtil.getTzIid(
810                 ItmTestConstants.NOT_HOSTED_TZ_NAME);
811         Assert.assertNotNull(tzPath);
812
813         // check TZ is Moved
814         assertEqualBeans(ExpectedTepNotHostedTransportZoneObjects
815                         .newTepNotHostedTransportZone().getUnknownVteps().values().iterator().next().getIpAddress()
816                         .stringValue(), dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION,
817                 tzPath).get().get().getVteps().values().iterator().next().getIpAddress().stringValue());
818
819         assertEqualBeans(ExpectedTepNotHostedTransportZoneObjects.newTepNotHostedTransportZone().getUnknownVteps()
820                 .values().iterator().next().getDpnId(), dataBroker.newReadOnlyTransaction()
821                 .read(LogicalDatastoreType.CONFIGURATION, tzPath).get()
822                 .get().getVteps().values().iterator().next().getDpnId());
823
824         assertEqualBeans(ExpectedTepNotHostedTransportZoneObjects.newTepNotHostedTransportZone().getZoneName(),
825                 dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION, tzPath)
826                         .get().get().getZoneName());
827
828         // check TZ is removed
829         Assert.assertEquals(Optional.empty(), dataBroker.newReadOnlyTransaction()
830                 .read(LogicalDatastoreType.OPERATIONAL, notHostedPath).get());
831     }
832
833     @Test
834     public void tzDeletedAndReaddedTest() throws Exception {
835         // create TZ
836         InstanceIdentifier<TransportZone> tzPath = ItmUtils.getTZInstanceIdentifier(ItmTestConstants.TZ_NAME);
837         txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx ->
838                 tx.mergeParentStructureMerge(LogicalDatastoreType.CONFIGURATION, tzPath, transportZone)).get();
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 ->
886                 tx.mergeParentStructureMerge(LogicalDatastoreType.CONFIGURATION, tzPath, transportZone)).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 ->
905                 tx.mergeParentStructureMerge(LogicalDatastoreType.CONFIGURATION, tzPath, transportZone)).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         @Nullable Map<UnknownVtepsKey, UnknownVteps> unknownVtepsList = dataBroker.newReadOnlyTransaction()
958                 .read(LogicalDatastoreType.OPERATIONAL, notHostedPath).get().get().getUnknownVteps();
959
960         for (UnknownVteps unknownVtep:unknownVtepsList.values()) {
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 ->
972                 tx.mergeParentStructureMerge(LogicalDatastoreType.CONFIGURATION, tzPath, recreatedTransportZone)).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 ->
993                 tx.mergeParentStructureMerge(LogicalDatastoreType.CONFIGURATION, tzPath, transportZone)).get();
994
995         InstanceIdentifier<TransportZone> tzonePath = ItmTepAutoConfigTestUtil.getTzIid(
996                 ItmTestConstants.TZ_NAME);
997         Assert.assertNotNull(tzonePath);
998
999         // check TZ is created
1000         Assert.assertEquals(ItmTestConstants.TZ_NAME, dataBroker.newReadOnlyTransaction()
1001                 .read(LogicalDatastoreType.CONFIGURATION, tzonePath).get().get().getZoneName());
1002
1003         // add tep
1004         ListenableFuture<Void> futures =
1005                 ItmTepAutoConfigTestUtil.addTep(ItmTestConstants.NB_TZ_TEP_IP,
1006                         ItmTestConstants.DEF_BR_DPID, ItmTestConstants.TZ_NAME, false, dataBroker, txRunner);
1007         futures.get();
1008
1009         IpPrefix subnetMaskObj = ItmUtils.getDummySubnet();
1010
1011         InstanceIdentifier<Vteps> vtepPath = ItmTepAutoConfigTestUtil.getTepIid(subnetMaskObj,
1012                 ItmTestConstants.TZ_NAME, ItmTestConstants.INT_DEF_BR_DPID, ITMConstants.DUMMY_PORT);
1013         Assert.assertNotNull(vtepPath);
1014
1015         // check TEP is added into TZ that is already created.
1016         assertEqualBeans(ExpectedTransportZoneObjects.newTransportZone(),
1017                 dataBroker.newReadOnlyTransaction()
1018                         .read(LogicalDatastoreType.CONFIGURATION, tzonePath).get().get());
1019
1020         // remove Transport Zone
1021         txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> tx.delete(LogicalDatastoreType.CONFIGURATION,
1022                 tzPath));
1023
1024         // wait for TransportZoneListener to perform config DS update
1025         // for TEP movement through transaction
1026         coordinatorEventsWaiter.awaitEventsConsumption();
1027         //verify delete
1028
1029         Assert.assertEquals(Optional.empty(), dataBroker.newReadOnlyTransaction()
1030                 .read(LogicalDatastoreType.CONFIGURATION, tzonePath).get());
1031
1032         //check deleted tz moved to notHosted
1033         InstanceIdentifier<TepsInNotHostedTransportZone> notHostedPath =
1034                 ItmTepAutoConfigTestUtil.getTepNotHostedInTZIid(ItmTestConstants.TZ_NAME);
1035
1036         Assert.assertNotNull(notHostedPath);
1037         Assert.assertEquals(ItmTestConstants.TZ_NAME, dataBroker.newReadOnlyTransaction()
1038                 .read(LogicalDatastoreType.OPERATIONAL, notHostedPath).get().get().getZoneName());
1039
1040         //create vtepList form unknownVtepList
1041         List<Vteps> vtepsList = new ArrayList<>();
1042         @Nullable Map<UnknownVtepsKey, UnknownVteps> unknownVtepsList = dataBroker.newReadOnlyTransaction()
1043                 .read(LogicalDatastoreType.OPERATIONAL, notHostedPath).get().get().getUnknownVteps();
1044         //modifing the dpnid and keeping the ip same.
1045         for (UnknownVteps unknownVtep:unknownVtepsList.values()) {
1046             Vteps vteps = new VtepsBuilder().setDpnId(Uint64.valueOf(10))
1047                     .setIpAddress(unknownVtep.getIpAddress())
1048                     .withKey(new VtepsKey(Uint64.valueOf(10))).build();
1049             vtepsList.add(vteps);
1050         }
1051
1052         TransportZone recreatedTransportZone = new TransportZoneBuilder().setZoneName(ItmTestConstants.TZ_NAME)
1053                 .setTunnelType(ItmTestConstants.TUNNEL_TYPE_VXLAN).setVteps(vtepsList)
1054                 .withKey(new TransportZoneKey(ItmTestConstants.TZ_NAME)).build();
1055
1056         txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx ->
1057                 tx.mergeParentStructureMerge(LogicalDatastoreType.CONFIGURATION, tzPath, recreatedTransportZone)).get();
1058
1059         // wait for TransportZoneListener to perform config DS update
1060         // for TEP movement through transaction
1061         coordinatorEventsWaiter.awaitEventsConsumption();
1062
1063         // verify TZ is moved from notHosted to transport zone and their should be only one vtep in it.
1064
1065         Assert.assertEquals(Optional.empty(), dataBroker.newReadOnlyTransaction()
1066                 .read(LogicalDatastoreType.OPERATIONAL, notHostedPath).get());
1067
1068         assertEqualBeans(1, dataBroker.newReadOnlyTransaction()
1069                 .read(LogicalDatastoreType.CONFIGURATION, tzonePath).get().get()
1070                 .getVteps().size());
1071     }
1072 }
1073