Seyeon Jeong | 8d3cad2 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2020-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.t3.api; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableMap; |
| 20 | import org.onosproject.cluster.NodeId; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | |
| 23 | import java.util.Map; |
| 24 | |
| 25 | /** |
| 26 | * Represents Network Information Base (NIB) for mastership |
| 27 | * and supports alternative functions to |
| 28 | * {@link org.onosproject.mastership.MastershipService} for offline data. |
| 29 | */ |
| 30 | public class MastershipNib { |
| 31 | |
| 32 | private Map<DeviceId, NodeId> deviceMasterMap; |
| 33 | |
| 34 | // use the singleton helper to create the instance |
| 35 | protected MastershipNib() { |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Sets a map of device id : master node id. |
| 40 | * |
| 41 | * @param deviceMasterMap device-master map |
| 42 | */ |
| 43 | public void setDeviceMasterMap(Map<DeviceId, NodeId> deviceMasterMap) { |
| 44 | this.deviceMasterMap = deviceMasterMap; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Returns the device-master map. |
| 49 | * |
| 50 | * @return device-master map |
| 51 | */ |
| 52 | public Map<DeviceId, NodeId> getDeviceMasterMap() { |
| 53 | return ImmutableMap.copyOf(deviceMasterMap); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Returns the current master for a given device. |
| 58 | * |
| 59 | * @param deviceId the identifier of the device |
| 60 | * @return the ID of the master controller for the device |
| 61 | */ |
| 62 | public NodeId getMasterFor(DeviceId deviceId) { |
| 63 | return deviceMasterMap.get(deviceId); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Returns the singleton instance of mastership NIB. |
| 68 | * |
| 69 | * @return instance of mastership NIB |
| 70 | */ |
| 71 | public static MastershipNib getInstance() { |
| 72 | return MastershipNib.SingletonHelper.INSTANCE; |
| 73 | } |
| 74 | |
| 75 | private static class SingletonHelper { |
| 76 | private static final MastershipNib INSTANCE = new MastershipNib(); |
| 77 | } |
| 78 | |
| 79 | } |