Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 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 | */ |
Ray Milkey | d1c34da | 2018-06-22 18:10:53 -0700 | [diff] [blame] | 16 | package org.onosproject.incubator.net.resource.label; |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 17 | |
Ray Milkey | d1c34da | 2018-06-22 18:10:53 -0700 | [diff] [blame] | 18 | import com.google.common.collect.Multimap; |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 19 | import org.onosproject.net.Device; |
| 20 | import org.onosproject.net.DeviceId; |
| 21 | import org.onosproject.net.device.DeviceEvent; |
| 22 | import org.onosproject.net.device.DeviceEvent.Type; |
| 23 | import org.onosproject.net.device.DeviceListener; |
| 24 | import org.onosproject.net.provider.AbstractListenerProviderRegistry; |
| 25 | import org.onosproject.net.provider.AbstractProviderService; |
| 26 | |
Ray Milkey | d1c34da | 2018-06-22 18:10:53 -0700 | [diff] [blame] | 27 | import java.util.Collection; |
| 28 | import java.util.LinkedList; |
| 29 | import java.util.Random; |
| 30 | import java.util.Set; |
| 31 | |
| 32 | import static com.google.common.base.Preconditions.checkNotNull; |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Provides test implementation of class LabelResourceService. |
| 36 | */ |
| 37 | public class LabelResourceAdapter |
| 38 | extends AbstractListenerProviderRegistry<LabelResourceEvent, LabelResourceListener, |
Ray Milkey | d1c34da | 2018-06-22 18:10:53 -0700 | [diff] [blame] | 39 | LabelResourceProvider, LabelResourceProviderService> |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 40 | implements LabelResourceService, LabelResourceAdminService, LabelResourceProviderRegistry { |
| 41 | public static final long GLOBAL_LABEL_SPACE_MIN = 4097; |
| 42 | public static final long GLOBAL_LABEL_SPACE_MAX = 5121; |
| 43 | public static final long LOCAL_LABEL_SPACE_MIN = 5122; |
| 44 | public static final long LOCAL_LABEL_SPACE_MAX = 9217; |
| 45 | |
| 46 | private Random random = new Random(); |
| 47 | |
| 48 | @Override |
| 49 | public boolean createDevicePool(DeviceId deviceId, |
| 50 | LabelResourceId beginLabel, |
| 51 | LabelResourceId endLabel) { |
Ray Milkey | d1c34da | 2018-06-22 18:10:53 -0700 | [diff] [blame] | 52 | return true; |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public boolean createGlobalPool(LabelResourceId beginLabel, |
| 57 | LabelResourceId endLabel) { |
Ray Milkey | d1c34da | 2018-06-22 18:10:53 -0700 | [diff] [blame] | 58 | return true; |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public boolean destroyDevicePool(DeviceId deviceId) { |
Ray Milkey | d1c34da | 2018-06-22 18:10:53 -0700 | [diff] [blame] | 63 | return true; |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public boolean destroyGlobalPool() { |
Ray Milkey | d1c34da | 2018-06-22 18:10:53 -0700 | [diff] [blame] | 68 | return true; |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | public long getLabelId(long min, long max) { |
Ray Milkey | d1c34da | 2018-06-22 18:10:53 -0700 | [diff] [blame] | 72 | return random.nextInt((int) max - (int) min + 1) + (int) min; |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public Collection<LabelResource> applyFromDevicePool(DeviceId deviceId, |
Ray Milkey | d1c34da | 2018-06-22 18:10:53 -0700 | [diff] [blame] | 77 | long applyNum) { |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 78 | Collection<LabelResource> labelList = new LinkedList<>(); |
| 79 | LabelResource label = new DefaultLabelResource(deviceId, |
Ray Milkey | d1c34da | 2018-06-22 18:10:53 -0700 | [diff] [blame] | 80 | LabelResourceId.labelResourceId( |
| 81 | getLabelId(LOCAL_LABEL_SPACE_MIN, LOCAL_LABEL_SPACE_MAX))); |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 82 | labelList.add(label); |
| 83 | return labelList; |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public Collection<LabelResource> applyFromGlobalPool(long applyNum) { |
| 88 | Collection<LabelResource> labelList = new LinkedList<>(); |
| 89 | LabelResource label = new DefaultLabelResource(DeviceId.deviceId("foo"), |
Ray Milkey | d1c34da | 2018-06-22 18:10:53 -0700 | [diff] [blame] | 90 | LabelResourceId.labelResourceId( |
| 91 | getLabelId(GLOBAL_LABEL_SPACE_MIN, GLOBAL_LABEL_SPACE_MAX))); |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 92 | labelList.add(label); |
| 93 | return labelList; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public boolean releaseToDevicePool(Multimap<DeviceId, LabelResource> release) { |
Ray Milkey | d1c34da | 2018-06-22 18:10:53 -0700 | [diff] [blame] | 98 | return true; |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public boolean releaseToGlobalPool(Set<LabelResourceId> release) { |
Ray Milkey | d1c34da | 2018-06-22 18:10:53 -0700 | [diff] [blame] | 103 | return true; |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public boolean isDevicePoolFull(DeviceId deviceId) { |
Ray Milkey | d1c34da | 2018-06-22 18:10:53 -0700 | [diff] [blame] | 108 | return false; |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public boolean isGlobalPoolFull() { |
Ray Milkey | d1c34da | 2018-06-22 18:10:53 -0700 | [diff] [blame] | 113 | return false; |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public long getFreeNumOfDevicePool(DeviceId deviceId) { |
Ray Milkey | d1c34da | 2018-06-22 18:10:53 -0700 | [diff] [blame] | 118 | return 4; |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public long getFreeNumOfGlobalPool() { |
Ray Milkey | d1c34da | 2018-06-22 18:10:53 -0700 | [diff] [blame] | 123 | return 4; |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public LabelResourcePool getDeviceLabelResourcePool(DeviceId deviceId) { |
Ray Milkey | d1c34da | 2018-06-22 18:10:53 -0700 | [diff] [blame] | 128 | return null; |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public LabelResourcePool getGlobalLabelResourcePool() { |
Ray Milkey | d1c34da | 2018-06-22 18:10:53 -0700 | [diff] [blame] | 133 | return null; |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | private class InternalLabelResourceDelegate implements LabelResourceDelegate { |
| 137 | @Override |
| 138 | public void notify(LabelResourceEvent event) { |
| 139 | post(event); |
| 140 | } |
| 141 | |
| 142 | } |
| 143 | |
| 144 | private class InternalDeviceListener implements DeviceListener { |
| 145 | @Override |
| 146 | public void event(DeviceEvent event) { |
| 147 | Device device = event.subject(); |
| 148 | if (Type.DEVICE_REMOVED.equals(event.type())) { |
| 149 | destroyDevicePool(device.id()); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | private class InternalLabelResourceProviderService |
| 155 | extends AbstractProviderService<LabelResourceProvider> |
| 156 | implements LabelResourceProviderService { |
| 157 | |
| 158 | protected InternalLabelResourceProviderService(LabelResourceProvider provider) { |
| 159 | super(provider); |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public void deviceLabelResourcePoolDetected(DeviceId deviceId, |
| 164 | LabelResourceId beginLabel, |
| 165 | LabelResourceId endLabel) { |
| 166 | checkNotNull(deviceId, "deviceId is not null"); |
| 167 | checkNotNull(beginLabel, "beginLabel is not null"); |
| 168 | checkNotNull(endLabel, "endLabel is not null"); |
| 169 | createDevicePool(deviceId, beginLabel, endLabel); |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public void deviceLabelResourcePoolDestroyed(DeviceId deviceId) { |
| 174 | checkNotNull(deviceId, "deviceId is not null"); |
| 175 | destroyDevicePool(deviceId); |
| 176 | } |
| 177 | |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | protected LabelResourceProviderService createProviderService(LabelResourceProvider provider) { |
| 182 | return null; |
| 183 | } |
| 184 | } |