pierventre | 37dcf4c | 2021-09-16 18:43:06 +0200 | [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 | |
| 17 | package org.onosproject.segmentrouting; |
| 18 | |
| 19 | import org.onosproject.cluster.LeadershipService; |
| 20 | import org.onosproject.cluster.NodeId; |
| 21 | import org.onosproject.cluster.PartitionId; |
| 22 | import org.onosproject.net.intent.WorkPartitionServiceAdapter; |
| 23 | |
| 24 | import java.util.function.Function; |
| 25 | |
| 26 | public class MockWorkPartitionService extends WorkPartitionServiceAdapter { |
| 27 | |
| 28 | LeadershipService leadershipService; |
| 29 | static final int NUM_PARTITIONS = 14; |
| 30 | |
| 31 | @Override |
| 32 | public <K> NodeId getLeader(K id, Function<K, Long> hasher) { |
| 33 | int partition = Math.abs(hasher.apply(id).intValue()) % NUM_PARTITIONS; |
| 34 | PartitionId partitionId = new PartitionId(partition); |
| 35 | return leadershipService.getLeadership(partitionId.toString()).leaderNodeId(); |
| 36 | } |
| 37 | |
| 38 | } |