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; |
Jian Li | 6b40d39 | 2017-11-22 16:06:32 +0900 | [diff] [blame] | 39 | import org.onosproject.mastership.MastershipService; |
Ray Milkey | eb13070 | 2018-06-21 15:19:33 -0700 | [diff] [blame] | 40 | import org.onosproject.mastership.MastershipServiceAdapter; |
Jian Li | 6b40d39 | 2017-11-22 16:06:32 +0900 | [diff] [blame] | 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; |
Jian Li | 6b40d39 | 2017-11-22 16:06:32 +0900 | [diff] [blame] | 47 | |
| 48 | import static org.junit.Assert.assertEquals; |
| 49 | |
| 50 | /** |
| 51 | * Unit tests for mastership gRPC NB service. |
| 52 | */ |
| 53 | public class GrpcNbMastershipServiceTest { |
| 54 | |
| 55 | private static InProcessServer<BindableService> inprocessServer; |
| 56 | private static MastershipServiceGrpc.MastershipServiceBlockingStub blockingStub; |
| 57 | private static ManagedChannel channel; |
| 58 | |
| 59 | private final MastershipService mastershipService = new MockMastershipService(); |
| 60 | |
| 61 | private Map<DeviceId, MastershipRole> mastershipMap = Maps.newHashMap(); |
| 62 | private Map<DeviceId, NodeId> nodeIdMap = Maps.newHashMap(); |
| 63 | private Map<DeviceId, RoleInfo> roleInfoMap = Maps.newHashMap(); |
| 64 | |
| 65 | private static final String DEVICE_ID_1 = "1"; |
| 66 | private static final String DEVICE_ID_2 = "2"; |
| 67 | private static final String DEVICE_ID_3 = "3"; |
| 68 | |
| 69 | private DeviceId did1 = DeviceId.deviceId(DEVICE_ID_1); |
| 70 | private DeviceId did2 = DeviceId.deviceId(DEVICE_ID_2); |
| 71 | private DeviceId did3 = DeviceId.deviceId(DEVICE_ID_3); |
| 72 | |
| 73 | private NodeId nid1 = NodeId.nodeId("1"); |
| 74 | private NodeId nid2 = NodeId.nodeId("2"); |
| 75 | private NodeId nid3 = NodeId.nodeId("3"); |
| 76 | |
| 77 | /** |
| 78 | * Initializes the test environment. |
| 79 | */ |
| 80 | @Before |
| 81 | public void setUp() throws IllegalAccessException, IOException, InstantiationException { |
| 82 | GrpcNbMastershipService grpcMastershipService = new GrpcNbMastershipService(); |
| 83 | grpcMastershipService.mastershipService = mastershipService; |
| 84 | inprocessServer = grpcMastershipService.registerInProcessServer(); |
| 85 | inprocessServer.start(); |
| 86 | |
| 87 | channel = InProcessChannelBuilder.forName("test").directExecutor() |
| 88 | .usePlaintext(true).build(); |
| 89 | |
| 90 | blockingStub = MastershipServiceGrpc.newBlockingStub(channel); |
| 91 | |
| 92 | initMastershipMap(); |
| 93 | initNodeIdMap(); |
| 94 | initRoleInfoMap(); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Finalizes the test setup. |
| 99 | */ |
| 100 | @After |
| 101 | public void tearDown() { |
| 102 | channel.shutdownNow(); |
| 103 | inprocessServer.stop(); |
| 104 | } |
| 105 | |
| 106 | private void initMastershipMap() { |
| 107 | mastershipMap.put(did1, MastershipRole.MASTER); |
| 108 | mastershipMap.put(did2, MastershipRole.STANDBY); |
| 109 | mastershipMap.put(did3, MastershipRole.NONE); |
| 110 | } |
| 111 | |
| 112 | private void initNodeIdMap() { |
| 113 | nodeIdMap.put(did1, nid1); |
| 114 | nodeIdMap.put(did2, nid2); |
| 115 | nodeIdMap.put(did3, nid3); |
| 116 | } |
| 117 | |
| 118 | private void initRoleInfoMap() { |
| 119 | RoleInfo roleInfo1 = new RoleInfo(nid1, ImmutableList.of(nid2, nid3)); |
| 120 | RoleInfo roleInfo2 = new RoleInfo(nid2, ImmutableList.of(nid1, nid3)); |
| 121 | RoleInfo roleInfo3 = new RoleInfo(nid3, ImmutableList.of(nid2, nid3)); |
| 122 | roleInfoMap.put(did1, roleInfo1); |
| 123 | roleInfoMap.put(did2, roleInfo2); |
| 124 | roleInfoMap.put(did3, roleInfo3); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Tests the invocation result of getLocalRole method. |
| 129 | * |
| 130 | * @throws InterruptedException |
| 131 | */ |
| 132 | @Test |
| 133 | public void testGetLocalRole() throws InterruptedException { |
| 134 | getLocalRoleRequest request = getLocalRoleRequest.newBuilder() |
| 135 | .setDeviceId(DEVICE_ID_1) |
| 136 | .build(); |
| 137 | getLocalRoleReply reply; |
| 138 | |
| 139 | reply = blockingStub.getLocalRole(request); |
| 140 | assertEquals(Optional.of(MastershipRole.MASTER), |
| 141 | MastershipRoleProtoTranslator.translate(reply.getMastershipRole())); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Tests the invocation result of requestRoleForSync method. |
| 146 | * |
| 147 | * @throws InterruptedException |
| 148 | */ |
| 149 | @Test |
| 150 | public void testRequestRoleForSync() throws InterruptedException { |
| 151 | requestRoleForSyncRequest request = requestRoleForSyncRequest.newBuilder() |
| 152 | .setDeviceId(DEVICE_ID_2) |
| 153 | .build(); |
| 154 | requestRoleForSyncReply reply; |
| 155 | |
| 156 | reply = blockingStub.requestRoleForSync(request); |
| 157 | assertEquals(Optional.of(MastershipRole.STANDBY), |
| 158 | MastershipRoleProtoTranslator.translate(reply.getMastershipRole())); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Tests the invocation result of getMasterFor method. |
| 163 | * |
| 164 | * @throws InterruptedException |
| 165 | */ |
| 166 | @Test |
| 167 | public void testGetMasterFor() { |
| 168 | getMasterForRequest request = getMasterForRequest.newBuilder() |
| 169 | .setDeviceId(DEVICE_ID_1) |
| 170 | .build(); |
| 171 | getMasterForReply reply; |
| 172 | |
| 173 | reply = blockingStub.getMasterFor(request); |
| 174 | assertEquals(nid1, NodeIdProtoTranslator.translate(reply.getNodeId())); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Tests the invocation result of getNodesFor method. |
| 179 | * |
| 180 | * @throws InterruptedException |
| 181 | */ |
| 182 | @Test |
| 183 | public void testGetNodesFor() { |
| 184 | getNodesForRequest request = getNodesForRequest.newBuilder() |
| 185 | .setDeviceId(DEVICE_ID_3) |
| 186 | .build(); |
| 187 | getNodesForReply reply; |
| 188 | |
| 189 | reply = blockingStub.getNodesFor(request); |
| 190 | assertEquals(nid3, NodeIdProtoTranslator.translate(reply.getRoleInfo().getMaster())); |
| 191 | } |
| 192 | |
Ray Milkey | eb13070 | 2018-06-21 15:19:33 -0700 | [diff] [blame] | 193 | private class MockMastershipService extends MastershipServiceAdapter { |
Jian Li | 6b40d39 | 2017-11-22 16:06:32 +0900 | [diff] [blame] | 194 | |
| 195 | @Override |
| 196 | public MastershipRole getLocalRole(DeviceId deviceId) { |
| 197 | return mastershipMap.get(deviceId); |
| 198 | } |
| 199 | |
| 200 | @Override |
Jian Li | 6b40d39 | 2017-11-22 16:06:32 +0900 | [diff] [blame] | 201 | public MastershipRole requestRoleForSync(DeviceId deviceId) { |
| 202 | return mastershipMap.get(deviceId); |
| 203 | } |
| 204 | |
| 205 | @Override |
Jian Li | 6b40d39 | 2017-11-22 16:06:32 +0900 | [diff] [blame] | 206 | public NodeId getMasterFor(DeviceId deviceId) { |
| 207 | return nodeIdMap.get(deviceId); |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public RoleInfo getNodesFor(DeviceId deviceId) { |
| 212 | return roleInfoMap.get(deviceId); |
| 213 | } |
Jian Li | 6b40d39 | 2017-11-22 16:06:32 +0900 | [diff] [blame] | 214 | } |
| 215 | } |