e4927ca67b7e123c501161f77dd0baca8e38ce0f
[controller.git] / opendaylight / md-sal / eos-dom-akka / src / test / java / org / opendaylight / controller / eos / akka / DataCentersTest.java
1 /*
2  * Copyright (c) 2021 PANTHEON.tech, s.r.o. 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.controller.eos.akka;
9
10 import akka.actor.testkit.typed.javadsl.ActorTestKit;
11 import akka.cluster.Member;
12 import akka.cluster.MemberStatus;
13 import akka.cluster.typed.Cluster;
14 import java.time.Duration;
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.List;
18 import org.awaitility.Awaitility;
19 import org.junit.After;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.mdsal.eos.dom.api.DOMEntity;
23
24 public class DataCentersTest extends AbstractNativeEosTest {
25
26     private ClusterNode node1 = null;
27     private ClusterNode node2 = null;
28     private ClusterNode node3 = null;
29     private ClusterNode node4 = null;
30     public static final DOMEntity ENTITY_1 = new DOMEntity("test-type", "entity-1");
31     public static final DOMEntity ENTITY_2 = new DOMEntity("test-type-2", "entity-2");
32
33     @Before
34     public void setUp() throws Exception {
35         node1 = startupWithDatacenter(2550, Collections.singletonList("member-1"), DATACENTER_SEED_NODES, "dc-primary");
36         node2 = startupWithDatacenter(2551, Collections.singletonList("member-2"), DATACENTER_SEED_NODES, "dc-primary");
37         node3 = startupWithDatacenter(2552, Collections.singletonList("member-3"), DATACENTER_SEED_NODES, "dc-backup");
38         node4 = startupWithDatacenter(2553, Collections.singletonList("member-4"), DATACENTER_SEED_NODES, "dc-backup");
39
40         // need to wait until all nodes are ready
41         final Cluster cluster = Cluster.get(node4.getActorSystem());
42         Awaitility.await().atMost(Duration.ofSeconds(20)).until(() -> {
43             final List<Member> members = new ArrayList<>();
44             cluster.state().getMembers().forEach(members::add);
45             if (members.size() != 4) {
46                 return false;
47             }
48
49             for (final Member member : members) {
50                 if (!member.status().equals(MemberStatus.up())) {
51                     return false;
52                 }
53             }
54
55             return true;
56         });
57     }
58
59     @Test
60     public void testDatacenterActivation() throws Exception {
61         registerCandidates(node1, ENTITY_1, "member-1");
62         registerCandidates(node3, ENTITY_1, "member-3");
63
64         activateDatacenter(node1).get();
65
66         waitUntillOwnerPresent(node1, ENTITY_1);
67         final MockEntityOwnershipListener listener1 = registerListener(node1, ENTITY_1);
68         verifyListenerState(listener1, ENTITY_1, true, true, false);
69
70         final MockEntityOwnershipListener listener2 = registerListener(node3, ENTITY_1);
71         verifyListenerState(listener2, ENTITY_1, true, false, false);
72
73         unregisterCandidates(node1, ENTITY_1, "member-1");
74
75         verifyListenerState(listener1, ENTITY_1, false, false, true);
76         verifyListenerState(listener2, ENTITY_1, false, false, false);
77
78         deactivateDatacenter(node1).get();
79         activateDatacenter(node4).get();
80
81         verifyListenerState(listener1, ENTITY_1, true, false, false);
82         verifyListenerState(listener2, ENTITY_1, true, true, false);
83
84         registerCandidates(node4, ENTITY_1, "member-4");
85         unregisterCandidates(node3, ENTITY_1, "member-3");
86
87         // checking index after notif so current + 1
88         verifyListenerState(listener1, ENTITY_1, true, false, false);
89         verifyListenerState(listener2, ENTITY_1, true, false, false);
90
91         deactivateDatacenter(node3).get();
92         activateDatacenter(node2).get();
93     }
94
95     @Test
96     public void testDataCenterShutdown() throws Exception {
97         registerCandidates(node1, ENTITY_1, "member-1");
98         registerCandidates(node3, ENTITY_1, "member-3");
99         registerCandidates(node4, ENTITY_1, "member-4");
100
101         waitUntillCandidatePresent(node1, ENTITY_1, "member-1");
102         waitUntillCandidatePresent(node1, ENTITY_1, "member-3");
103         waitUntillCandidatePresent(node1, ENTITY_1, "member-4");
104
105         activateDatacenter(node1).get();
106
107         waitUntillOwnerPresent(node4, ENTITY_1);
108         final MockEntityOwnershipListener listener1 = registerListener(node1, ENTITY_1);
109         verifyListenerState(listener1, ENTITY_1, true, true, false);
110
111         final MockEntityOwnershipListener listener2 = registerListener(node3, ENTITY_1);
112         verifyListenerState(listener2, ENTITY_1, true, false, false);
113
114         unregisterCandidates(node1, ENTITY_1, "member-1");
115
116         verifyListenerState(listener1, ENTITY_1, false, false, true);
117         verifyListenerState(listener2, ENTITY_1, false, false, false);
118
119         ActorTestKit.shutdown(node1.getActorSystem(), Duration.ofSeconds(20));
120         ActorTestKit.shutdown(node2.getActorSystem(), Duration.ofSeconds(20));
121
122         activateDatacenter(node3).get();
123         verifyListenerState(listener2, ENTITY_1, true, true, false);
124
125         waitUntillOwnerPresent(node3, ENTITY_1);
126         unregisterCandidates(node3, ENTITY_1, "member-3");
127         verifyListenerState(listener2, ENTITY_1, true, false, true);
128     }
129
130     @After
131     public void tearDown() {
132         ActorTestKit.shutdown(node1.getActorSystem(), Duration.ofSeconds(20));
133         ActorTestKit.shutdown(node2.getActorSystem(), Duration.ofSeconds(20));
134         ActorTestKit.shutdown(node3.getActorSystem(), Duration.ofSeconds(20));
135         ActorTestKit.shutdown(node4.getActorSystem(), Duration.ofSeconds(20));
136     }
137
138 }