Jian Li | 6b40d39 | 2017-11-22 16:06:32 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.incubator.protobuf.services.nb; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.Maps; |
| 20 | import io.grpc.BindableService; |
| 21 | import io.grpc.ManagedChannel; |
| 22 | import io.grpc.inprocess.InProcessChannelBuilder; |
| 23 | import org.junit.After; |
| 24 | import org.junit.Before; |
| 25 | import org.junit.Test; |
| 26 | import org.onosproject.cluster.NodeId; |
| 27 | import org.onosproject.cluster.RoleInfo; |
| 28 | import org.onosproject.grpc.nb.mastership.MastershipServiceGrpc; |
| 29 | import org.onosproject.grpc.nb.mastership.MastershipServiceNb.getLocalRoleReply; |
| 30 | import org.onosproject.grpc.nb.mastership.MastershipServiceNb.getLocalRoleRequest; |
| 31 | import org.onosproject.grpc.nb.mastership.MastershipServiceNb.getMasterForReply; |
| 32 | import org.onosproject.grpc.nb.mastership.MastershipServiceNb.getMasterForRequest; |
| 33 | import org.onosproject.grpc.nb.mastership.MastershipServiceNb.getNodesForReply; |
| 34 | import org.onosproject.grpc.nb.mastership.MastershipServiceNb.getNodesForRequest; |
| 35 | import org.onosproject.grpc.nb.mastership.MastershipServiceNb.requestRoleForSyncReply; |
| 36 | import org.onosproject.grpc.nb.mastership.MastershipServiceNb.requestRoleForSyncRequest; |
| 37 | import org.onosproject.incubator.protobuf.models.cluster.NodeIdProtoTranslator; |
| 38 | import org.onosproject.incubator.protobuf.models.net.MastershipRoleProtoTranslator; |
| 39 | import org.onosproject.mastership.MastershipListener; |
| 40 | import org.onosproject.mastership.MastershipService; |
| 41 | import org.onosproject.net.DeviceId; |
| 42 | import org.onosproject.net.MastershipRole; |
| 43 | |
| 44 | import java.io.IOException; |
| 45 | import java.util.Map; |
| 46 | import java.util.Optional; |
| 47 | import java.util.Set; |
| 48 | import java.util.concurrent.CompletableFuture; |
| 49 | |
| 50 | import static org.junit.Assert.assertEquals; |
| 51 | |
| 52 | /** |
| 53 | * Unit tests for mastership gRPC NB service. |
| 54 | */ |
| 55 | public class GrpcNbMastershipServiceTest { |
| 56 | |
| 57 | private static InProcessServer<BindableService> inprocessServer; |
| 58 | private static MastershipServiceGrpc.MastershipServiceBlockingStub blockingStub; |
| 59 | private static ManagedChannel channel; |
| 60 | |
| 61 | private final MastershipService mastershipService = new MockMastershipService(); |
| 62 | |
| 63 | private Map<DeviceId, MastershipRole> mastershipMap = Maps.newHashMap(); |
| 64 | private Map<DeviceId, NodeId> nodeIdMap = Maps.newHashMap(); |
| 65 | private Map<DeviceId, RoleInfo> roleInfoMap = Maps.newHashMap(); |
| 66 | |
| 67 | private static final String DEVICE_ID_1 = "1"; |
| 68 | private static final String DEVICE_ID_2 = "2"; |
| 69 | private static final String DEVICE_ID_3 = "3"; |
| 70 | |
| 71 | private DeviceId did1 = DeviceId.deviceId(DEVICE_ID_1); |
| 72 | private DeviceId did2 = DeviceId.deviceId(DEVICE_ID_2); |
| 73 | private DeviceId did3 = DeviceId.deviceId(DEVICE_ID_3); |
| 74 | |
| 75 | private NodeId nid1 = NodeId.nodeId("1"); |
| 76 | private NodeId nid2 = NodeId.nodeId("2"); |
| 77 | private NodeId nid3 = NodeId.nodeId("3"); |
| 78 | |
| 79 | /** |
| 80 | * Initializes the test environment. |
| 81 | */ |
| 82 | @Before |
| 83 | public void setUp() throws IllegalAccessException, IOException, InstantiationException { |
| 84 | GrpcNbMastershipService grpcMastershipService = new GrpcNbMastershipService(); |
| 85 | grpcMastershipService.mastershipService = mastershipService; |
| 86 | inprocessServer = grpcMastershipService.registerInProcessServer(); |
| 87 | inprocessServer.start(); |
| 88 | |
| 89 | channel = InProcessChannelBuilder.forName("test").directExecutor() |
| 90 | .usePlaintext(true).build(); |
| 91 | |
| 92 | blockingStub = MastershipServiceGrpc.newBlockingStub(channel); |
| 93 | |
| 94 | initMastershipMap(); |
| 95 | initNodeIdMap(); |
| 96 | initRoleInfoMap(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Finalizes the test setup. |
| 101 | */ |
| 102 | @After |
| 103 | public void tearDown() { |
| 104 | channel.shutdownNow(); |
| 105 | inprocessServer.stop(); |
| 106 | } |
| 107 | |
| 108 | private void initMastershipMap() { |
| 109 | mastershipMap.put(did1, MastershipRole.MASTER); |
| 110 | mastershipMap.put(did2, MastershipRole.STANDBY); |
| 111 | mastershipMap.put(did3, MastershipRole.NONE); |
| 112 | } |
| 113 | |
| 114 | private void initNodeIdMap() { |
| 115 | nodeIdMap.put(did1, nid1); |
| 116 | nodeIdMap.put(did2, nid2); |
| 117 | nodeIdMap.put(did3, nid3); |
| 118 | } |
| 119 | |
| 120 | private void initRoleInfoMap() { |
| 121 | RoleInfo roleInfo1 = new RoleInfo(nid1, ImmutableList.of(nid2, nid3)); |
| 122 | RoleInfo roleInfo2 = new RoleInfo(nid2, ImmutableList.of(nid1, nid3)); |
| 123 | RoleInfo roleInfo3 = new RoleInfo(nid3, ImmutableList.of(nid2, nid3)); |
| 124 | roleInfoMap.put(did1, roleInfo1); |
| 125 | roleInfoMap.put(did2, roleInfo2); |
| 126 | roleInfoMap.put(did3, roleInfo3); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Tests the invocation result of getLocalRole method. |
| 131 | * |
| 132 | * @throws InterruptedException |
| 133 | */ |
| 134 | @Test |
| 135 | public void testGetLocalRole() throws InterruptedException { |
| 136 | getLocalRoleRequest request = getLocalRoleRequest.newBuilder() |
| 137 | .setDeviceId(DEVICE_ID_1) |
| 138 | .build(); |
| 139 | getLocalRoleReply reply; |
| 140 | |
| 141 | reply = blockingStub.getLocalRole(request); |
| 142 | assertEquals(Optional.of(MastershipRole.MASTER), |
| 143 | MastershipRoleProtoTranslator.translate(reply.getMastershipRole())); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Tests the invocation result of requestRoleForSync method. |
| 148 | * |
| 149 | * @throws InterruptedException |
| 150 | */ |
| 151 | @Test |
| 152 | public void testRequestRoleForSync() throws InterruptedException { |
| 153 | requestRoleForSyncRequest request = requestRoleForSyncRequest.newBuilder() |
| 154 | .setDeviceId(DEVICE_ID_2) |
| 155 | .build(); |
| 156 | requestRoleForSyncReply reply; |
| 157 | |
| 158 | reply = blockingStub.requestRoleForSync(request); |
| 159 | assertEquals(Optional.of(MastershipRole.STANDBY), |
| 160 | MastershipRoleProtoTranslator.translate(reply.getMastershipRole())); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Tests the invocation result of getMasterFor method. |
| 165 | * |
| 166 | * @throws InterruptedException |
| 167 | */ |
| 168 | @Test |
| 169 | public void testGetMasterFor() { |
| 170 | getMasterForRequest request = getMasterForRequest.newBuilder() |
| 171 | .setDeviceId(DEVICE_ID_1) |
| 172 | .build(); |
| 173 | getMasterForReply reply; |
| 174 | |
| 175 | reply = blockingStub.getMasterFor(request); |
| 176 | assertEquals(nid1, NodeIdProtoTranslator.translate(reply.getNodeId())); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Tests the invocation result of getNodesFor method. |
| 181 | * |
| 182 | * @throws InterruptedException |
| 183 | */ |
| 184 | @Test |
| 185 | public void testGetNodesFor() { |
| 186 | getNodesForRequest request = getNodesForRequest.newBuilder() |
| 187 | .setDeviceId(DEVICE_ID_3) |
| 188 | .build(); |
| 189 | getNodesForReply reply; |
| 190 | |
| 191 | reply = blockingStub.getNodesFor(request); |
| 192 | assertEquals(nid3, NodeIdProtoTranslator.translate(reply.getRoleInfo().getMaster())); |
| 193 | } |
| 194 | |
| 195 | private class MockMastershipService implements MastershipService { |
| 196 | |
| 197 | @Override |
| 198 | public MastershipRole getLocalRole(DeviceId deviceId) { |
| 199 | return mastershipMap.get(deviceId); |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public CompletableFuture<MastershipRole> requestRoleFor(DeviceId deviceId) { |
| 204 | return null; |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public MastershipRole requestRoleForSync(DeviceId deviceId) { |
| 209 | return mastershipMap.get(deviceId); |
| 210 | } |
| 211 | |
| 212 | @Override |
| 213 | public CompletableFuture<Void> relinquishMastership(DeviceId deviceId) { |
| 214 | return null; |
| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public void relinquishMastershipSync(DeviceId deviceId) { |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public NodeId getMasterFor(DeviceId deviceId) { |
| 223 | return nodeIdMap.get(deviceId); |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public RoleInfo getNodesFor(DeviceId deviceId) { |
| 228 | return roleInfoMap.get(deviceId); |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public Set<DeviceId> getDevicesOf(NodeId nodeId) { |
| 233 | return null; |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public void addListener(MastershipListener listener) { |
| 238 | |
| 239 | } |
| 240 | |
| 241 | @Override |
| 242 | public void removeListener(MastershipListener listener) { |
| 243 | |
| 244 | } |
| 245 | } |
| 246 | } |