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