blob: d09a1dea99780901cc717c7f26a3736c1631e033 [file] [log] [blame]
Avantika-Huawei9e848e82016-09-01 12:12:42 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Avantika-Huawei9e848e82016-09-01 12:12:42 +05303 *
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 Milkeyd1c34da2018-06-22 18:10:53 -070016package org.onosproject.incubator.net.resource.label;
Avantika-Huawei9e848e82016-09-01 12:12:42 +053017
Ray Milkeyd1c34da2018-06-22 18:10:53 -070018import com.google.common.collect.Multimap;
Avantika-Huawei9e848e82016-09-01 12:12:42 +053019import org.onosproject.net.Device;
20import org.onosproject.net.DeviceId;
21import org.onosproject.net.device.DeviceEvent;
22import org.onosproject.net.device.DeviceEvent.Type;
23import org.onosproject.net.device.DeviceListener;
24import org.onosproject.net.provider.AbstractListenerProviderRegistry;
25import org.onosproject.net.provider.AbstractProviderService;
26
Ray Milkeyd1c34da2018-06-22 18:10:53 -070027import java.util.Collection;
28import java.util.LinkedList;
29import java.util.Random;
30import java.util.Set;
31
32import static com.google.common.base.Preconditions.checkNotNull;
Avantika-Huawei9e848e82016-09-01 12:12:42 +053033
34/**
35 * Provides test implementation of class LabelResourceService.
36 */
37public class LabelResourceAdapter
38 extends AbstractListenerProviderRegistry<LabelResourceEvent, LabelResourceListener,
Ray Milkeyd1c34da2018-06-22 18:10:53 -070039 LabelResourceProvider, LabelResourceProviderService>
Avantika-Huawei9e848e82016-09-01 12:12:42 +053040 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 Milkeyd1c34da2018-06-22 18:10:53 -070052 return true;
Avantika-Huawei9e848e82016-09-01 12:12:42 +053053 }
54
55 @Override
56 public boolean createGlobalPool(LabelResourceId beginLabel,
57 LabelResourceId endLabel) {
Ray Milkeyd1c34da2018-06-22 18:10:53 -070058 return true;
Avantika-Huawei9e848e82016-09-01 12:12:42 +053059 }
60
61 @Override
62 public boolean destroyDevicePool(DeviceId deviceId) {
Ray Milkeyd1c34da2018-06-22 18:10:53 -070063 return true;
Avantika-Huawei9e848e82016-09-01 12:12:42 +053064 }
65
66 @Override
67 public boolean destroyGlobalPool() {
Ray Milkeyd1c34da2018-06-22 18:10:53 -070068 return true;
Avantika-Huawei9e848e82016-09-01 12:12:42 +053069 }
70
71 public long getLabelId(long min, long max) {
Ray Milkeyd1c34da2018-06-22 18:10:53 -070072 return random.nextInt((int) max - (int) min + 1) + (int) min;
Avantika-Huawei9e848e82016-09-01 12:12:42 +053073 }
74
75 @Override
76 public Collection<LabelResource> applyFromDevicePool(DeviceId deviceId,
Ray Milkeyd1c34da2018-06-22 18:10:53 -070077 long applyNum) {
Avantika-Huawei9e848e82016-09-01 12:12:42 +053078 Collection<LabelResource> labelList = new LinkedList<>();
79 LabelResource label = new DefaultLabelResource(deviceId,
Ray Milkeyd1c34da2018-06-22 18:10:53 -070080 LabelResourceId.labelResourceId(
81 getLabelId(LOCAL_LABEL_SPACE_MIN, LOCAL_LABEL_SPACE_MAX)));
Avantika-Huawei9e848e82016-09-01 12:12:42 +053082 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 Milkeyd1c34da2018-06-22 18:10:53 -070090 LabelResourceId.labelResourceId(
91 getLabelId(GLOBAL_LABEL_SPACE_MIN, GLOBAL_LABEL_SPACE_MAX)));
Avantika-Huawei9e848e82016-09-01 12:12:42 +053092 labelList.add(label);
93 return labelList;
94 }
95
96 @Override
97 public boolean releaseToDevicePool(Multimap<DeviceId, LabelResource> release) {
Ray Milkeyd1c34da2018-06-22 18:10:53 -070098 return true;
Avantika-Huawei9e848e82016-09-01 12:12:42 +053099 }
100
101 @Override
102 public boolean releaseToGlobalPool(Set<LabelResourceId> release) {
Ray Milkeyd1c34da2018-06-22 18:10:53 -0700103 return true;
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530104 }
105
106 @Override
107 public boolean isDevicePoolFull(DeviceId deviceId) {
Ray Milkeyd1c34da2018-06-22 18:10:53 -0700108 return false;
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530109 }
110
111 @Override
112 public boolean isGlobalPoolFull() {
Ray Milkeyd1c34da2018-06-22 18:10:53 -0700113 return false;
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530114 }
115
116 @Override
117 public long getFreeNumOfDevicePool(DeviceId deviceId) {
Ray Milkeyd1c34da2018-06-22 18:10:53 -0700118 return 4;
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530119 }
120
121 @Override
122 public long getFreeNumOfGlobalPool() {
Ray Milkeyd1c34da2018-06-22 18:10:53 -0700123 return 4;
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530124 }
125
126 @Override
127 public LabelResourcePool getDeviceLabelResourcePool(DeviceId deviceId) {
Ray Milkeyd1c34da2018-06-22 18:10:53 -0700128 return null;
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530129 }
130
131 @Override
132 public LabelResourcePool getGlobalLabelResourcePool() {
Ray Milkeyd1c34da2018-06-22 18:10:53 -0700133 return null;
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530134 }
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}