Mahesh Poojary S
Committed by Mahesh Poojary Huawei

[ONOS-4164] Basic PCECC and SR-TE Handlers

Change-Id: I7fbf86d845ec6b5d780f63518776fa841e9fb57c
(cherry picked from commit a4df1aa0)
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 +
17 +package org.onosproject.pce.pceservice;
18 +
19 +/**
20 + * Describes about Label type.
21 + */
22 +public enum LabelType {
23 + /**
24 + * Signifies in label id of a device.
25 + */
26 + OUT_LABEL(0),
27 +
28 + /**
29 + * Signifies out label id of a device.
30 + */
31 + IN_LABEL(1);
32 +
33 + int value;
34 +
35 + /**
36 + * Assign val with the value as the Label type.
37 + *
38 + * @param val Label type
39 + */
40 + LabelType(int val) {
41 + value = val;
42 + }
43 +
44 + /**
45 + * Returns value of Label type.
46 + *
47 + * @return label type
48 + */
49 + public byte type() {
50 + return (byte) value;
51 + }
52 +}
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.pce.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 + public Collection<LabelResource> applyFromDevicePool(DeviceId deviceId,
89 + long applyNum) {
90 + Collection<LabelResource> labelList = new LinkedList<>();
91 + LabelResource label = new DefaultLabelResource(deviceId,
92 + LabelResourceId.labelResourceId(
93 + getLabelId(LOCAL_LABEL_SPACE_MIN, LOCAL_LABEL_SPACE_MAX)));
94 + labelList.add(label);
95 + return labelList;
96 + }
97 +
98 + public Collection<LabelResource> applyFromGlobalPool(long applyNum) {
99 + Collection<LabelResource> labelList = new LinkedList<>();
100 + LabelResource label = new DefaultLabelResource(DeviceId.deviceId("foo"),
101 + LabelResourceId.labelResourceId(
102 + getLabelId(GLOBAL_LABEL_SPACE_MIN, GLOBAL_LABEL_SPACE_MAX)));
103 + labelList.add(label);
104 + return labelList;
105 + }
106 +
107 + public boolean releaseToDevicePool(Multimap<DeviceId, LabelResource> release) {
108 + return true;
109 + }
110 +
111 + public boolean releaseToGlobalPool(Set<LabelResourceId> release) {
112 + return true;
113 + }
114 +
115 + public boolean isDevicePoolFull(DeviceId deviceId) {
116 + return false;
117 + }
118 +
119 + public boolean isGlobalPoolFull() {
120 + return false;
121 + }
122 +
123 + public long getFreeNumOfDevicePool(DeviceId deviceId) {
124 + return 4;
125 + }
126 +
127 + public long getFreeNumOfGlobalPool() {
128 + return 4;
129 + }
130 +
131 + @Override
132 + public LabelResourcePool getDeviceLabelResourcePool(DeviceId deviceId) {
133 + return null;
134 + }
135 +
136 + @Override
137 + public LabelResourcePool getGlobalLabelResourcePool() {
138 + return null;
139 + }
140 +
141 + private class InternalLabelResourceDelegate implements LabelResourceDelegate {
142 + @Override
143 + public void notify(LabelResourceEvent event) {
144 + post(event);
145 + }
146 +
147 + }
148 +
149 + private class InternalDeviceListener implements DeviceListener {
150 + @Override
151 + public void event(DeviceEvent event) {
152 + Device device = event.subject();
153 + if (Type.DEVICE_REMOVED.equals(event.type())) {
154 + destroyDevicePool(device.id());
155 + }
156 + }
157 + }
158 +
159 + private class InternalLabelResourceProviderService
160 + extends AbstractProviderService<LabelResourceProvider>
161 + implements LabelResourceProviderService {
162 +
163 + protected InternalLabelResourceProviderService(LabelResourceProvider provider) {
164 + super(provider);
165 + }
166 +
167 + @Override
168 + public void deviceLabelResourcePoolDetected(DeviceId deviceId,
169 + LabelResourceId beginLabel,
170 + LabelResourceId endLabel) {
171 + checkNotNull(deviceId, "deviceId is not null");
172 + checkNotNull(beginLabel, "beginLabel is not null");
173 + checkNotNull(endLabel, "endLabel is not null");
174 + createDevicePool(deviceId, beginLabel, endLabel);
175 + }
176 +
177 + @Override
178 + public void deviceLabelResourcePoolDestroyed(DeviceId deviceId) {
179 + checkNotNull(deviceId, "deviceId is not null");
180 + destroyDevicePool(deviceId);
181 + }
182 +
183 + }
184 +
185 + @Override
186 + protected LabelResourceProviderService createProviderService(LabelResourceProvider provider) {
187 + return null;
188 + }
189 +}