Sho SHIMIZU
Committed by Gerrit Code Review

Remove LinkResourceStore and its implementations

They were deprecated in Emu

Change-Id: I4272c7788bcd74ad7f272392a67b07bde6b09df4
1 -/*
2 - * Copyright 2014 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.net.resource.link;
17 -
18 -import java.util.Set;
19 -
20 -import org.onosproject.net.Link;
21 -import org.onosproject.net.intent.IntentId;
22 -import org.onosproject.net.resource.ResourceAllocation;
23 -
24 -/**
25 - * Manages link resources.
26 - *
27 - * @deprecated in Emu Release.
28 - */
29 -@Deprecated
30 -public interface LinkResourceStore {
31 - /**
32 - * Returns free resources for given link.
33 - *
34 - * @param link a target link
35 - * @return free resources for given link
36 - */
37 - Set<ResourceAllocation> getFreeResources(Link link);
38 -
39 - /**
40 - * Allocates resources.
41 - *
42 - * @param allocations resources to be allocated
43 - */
44 - void allocateResources(LinkResourceAllocations allocations);
45 -
46 - /**
47 - * Releases resources.
48 - *
49 - * @param allocations resources to be released
50 - * @return the link resource event
51 - */
52 - LinkResourceEvent releaseResources(LinkResourceAllocations allocations);
53 -
54 - /**
55 - * Returns resources allocated for an Intent.
56 - *
57 - * @param intentId the target Intent's ID
58 - * @return allocated resources or null if no resource is allocated
59 - */
60 - LinkResourceAllocations getAllocations(IntentId intentId);
61 -
62 - /**
63 - * Returns resources allocated for a link.
64 - *
65 - * @param link the target link
66 - * @return allocated resources
67 - */
68 - Iterable<LinkResourceAllocations> getAllocations(Link link);
69 -
70 - /**
71 - * Returns all allocated resources.
72 - *
73 - * @return allocated resources
74 - */
75 - Iterable<LinkResourceAllocations> getAllocations();
76 -}
1 -/*
2 - * Copyright 2014-2015 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.store.trivial;
17 -
18 -import java.util.Collections;
19 -import java.util.HashMap;
20 -import java.util.HashSet;
21 -import java.util.List;
22 -import java.util.Map;
23 -import java.util.Set;
24 -
25 -import org.apache.felix.scr.annotations.Activate;
26 -import org.apache.felix.scr.annotations.Component;
27 -import org.apache.felix.scr.annotations.Deactivate;
28 -import org.apache.felix.scr.annotations.Service;
29 -import org.onlab.util.Bandwidth;
30 -import org.onlab.util.PositionalParameterStringFormatter;
31 -import org.onosproject.net.AnnotationKeys;
32 -import org.onosproject.net.Annotations;
33 -import org.onosproject.net.Link;
34 -import org.onosproject.net.intent.IntentId;
35 -import org.onosproject.net.resource.link.BandwidthResource;
36 -import org.onosproject.net.resource.link.BandwidthResourceAllocation;
37 -import org.onosproject.net.resource.link.LambdaResource;
38 -import org.onosproject.net.resource.link.LambdaResourceAllocation;
39 -import org.onosproject.net.resource.link.LinkResourceAllocations;
40 -import org.onosproject.net.resource.link.LinkResourceEvent;
41 -import org.onosproject.net.resource.link.LinkResourceStore;
42 -import org.onosproject.net.resource.ResourceAllocation;
43 -import org.onosproject.net.resource.ResourceAllocationException;
44 -import org.onosproject.net.resource.ResourceType;
45 -import org.slf4j.Logger;
46 -
47 -import com.google.common.collect.ImmutableList;
48 -
49 -import static com.google.common.base.Preconditions.checkNotNull;
50 -import static com.google.common.base.Preconditions.checkState;
51 -import static org.slf4j.LoggerFactory.getLogger;
52 -
53 -/**
54 - * Manages link resources using trivial in-memory structures implementation.
55 - *
56 - * @deprecated in Emu Release
57 - */
58 -@Deprecated
59 -@Component(immediate = true)
60 -@Service
61 -public class SimpleLinkResourceStore implements LinkResourceStore {
62 - private static final BandwidthResource DEFAULT_BANDWIDTH = new BandwidthResource(Bandwidth.mbps(1_000));
63 - private final Logger log = getLogger(getClass());
64 -
65 - private Map<IntentId, LinkResourceAllocations> linkResourceAllocationsMap;
66 - private Map<Link, Set<LinkResourceAllocations>> allocatedResources;
67 - private Map<Link, Set<ResourceAllocation>> freeResources;
68 -
69 - @Activate
70 - public void activate() {
71 - linkResourceAllocationsMap = new HashMap<>();
72 - allocatedResources = new HashMap<>();
73 - freeResources = new HashMap<>();
74 -
75 - log.info("Started");
76 - }
77 -
78 - @Deactivate
79 - public void deactivate() {
80 - log.info("Stopped");
81 - }
82 -
83 - /**
84 - * Returns free resources for a given link obtaining from topology
85 - * information.
86 - *
87 - * @param link the target link
88 - * @return free resources
89 - */
90 - private synchronized Set<ResourceAllocation> readOriginalFreeResources(Link link) {
91 - Annotations annotations = link.annotations();
92 - Set<ResourceAllocation> allocations = new HashSet<>();
93 -
94 - try {
95 - int waves = Integer.parseInt(annotations.value(AnnotationKeys.OPTICAL_WAVES));
96 - for (int i = 1; i <= waves; i++) {
97 - allocations.add(new LambdaResourceAllocation(LambdaResource.valueOf(i)));
98 - }
99 - } catch (NumberFormatException e) {
100 - log.debug("No optical.wave annotation on link %s", link);
101 - }
102 -
103 - BandwidthResource bandwidth = DEFAULT_BANDWIDTH;
104 - try {
105 - bandwidth = new BandwidthResource(
106 - Bandwidth.mbps((Double.parseDouble(annotations.value(AnnotationKeys.BANDWIDTH)))));
107 - } catch (NumberFormatException e) {
108 - log.debug("No bandwidth annotation on link %s", link);
109 - }
110 - allocations.add(
111 - new BandwidthResourceAllocation(bandwidth));
112 - return allocations;
113 - }
114 -
115 - /**
116 - * Finds and returns {@link BandwidthResourceAllocation} object from a given
117 - * set.
118 - *
119 - * @param freeRes a set of ResourceAllocation object.
120 - * @return {@link BandwidthResourceAllocation} object if found, otherwise
121 - * {@link BandwidthResourceAllocation} object with 0 bandwidth
122 - *
123 - */
124 - private synchronized BandwidthResourceAllocation getBandwidth(
125 - Set<ResourceAllocation> freeRes) {
126 - for (ResourceAllocation res : freeRes) {
127 - if (res.type() == ResourceType.BANDWIDTH) {
128 - return (BandwidthResourceAllocation) res;
129 - }
130 - }
131 - return new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.bps(0)));
132 - }
133 -
134 - /**
135 - * Subtracts given resources from free resources for given link.
136 - *
137 - * @param link the target link
138 - * @param allocations the resources to be subtracted
139 - */
140 - private synchronized void subtractFreeResources(Link link,
141 - LinkResourceAllocations allocations) {
142 - // TODO Use lock or version for updating freeResources.
143 - checkNotNull(link);
144 - Set<ResourceAllocation> freeRes = new HashSet<>(getFreeResources(link));
145 - Set<ResourceAllocation> subRes = allocations.getResourceAllocation(link);
146 - for (ResourceAllocation res : subRes) {
147 - switch (res.type()) {
148 - case BANDWIDTH:
149 - BandwidthResourceAllocation ba = getBandwidth(freeRes);
150 - double requestedBandwidth =
151 - ((BandwidthResourceAllocation) res).bandwidth().toDouble();
152 - double newBandwidth = ba.bandwidth().toDouble() - requestedBandwidth;
153 - if (newBandwidth < 0.0) {
154 - throw new ResourceAllocationException(
155 - PositionalParameterStringFormatter.format(
156 - "Unable to allocate bandwidth for link {} "
157 - + "requested amount is {} current allocation is {}",
158 - link,
159 - requestedBandwidth,
160 - ba));
161 - }
162 - freeRes.remove(ba);
163 - freeRes.add(new BandwidthResourceAllocation(
164 - new BandwidthResource(Bandwidth.bps(newBandwidth))));
165 - break;
166 - case LAMBDA:
167 - final boolean lambdaAvailable = freeRes.remove(res);
168 - if (!lambdaAvailable) {
169 - int requestedLambda =
170 - ((LambdaResourceAllocation) res).lambda().toInt();
171 - throw new ResourceAllocationException(
172 - PositionalParameterStringFormatter.format(
173 - "Unable to allocate lambda for link {} lambda is {}",
174 - link,
175 - requestedLambda));
176 - }
177 - break;
178 - default:
179 - break;
180 - }
181 - }
182 - freeResources.put(link, freeRes);
183 -
184 - }
185 -
186 - /**
187 - * Adds given resources to free resources for given link.
188 - *
189 - * @param link the target link
190 - * @param allocations the resources to be added
191 - */
192 - private synchronized void addFreeResources(Link link,
193 - LinkResourceAllocations allocations) {
194 - // TODO Use lock or version for updating freeResources.
195 - Set<ResourceAllocation> freeRes = new HashSet<>(getFreeResources(link));
196 - Set<ResourceAllocation> addRes = allocations.getResourceAllocation(link);
197 - for (ResourceAllocation res : addRes) {
198 - switch (res.type()) {
199 - case BANDWIDTH:
200 - BandwidthResourceAllocation ba = getBandwidth(freeRes);
201 - double requestedBandwidth =
202 - ((BandwidthResourceAllocation) res).bandwidth().toDouble();
203 - double newBandwidth = ba.bandwidth().toDouble() + requestedBandwidth;
204 - freeRes.remove(ba);
205 - freeRes.add(new BandwidthResourceAllocation(
206 - new BandwidthResource(Bandwidth.bps(newBandwidth))));
207 - break;
208 - case LAMBDA:
209 - checkState(freeRes.add(res));
210 - break;
211 - default:
212 - break;
213 - }
214 - }
215 - freeResources.put(link, freeRes);
216 - }
217 -
218 - @Override
219 - public synchronized Set<ResourceAllocation> getFreeResources(Link link) {
220 - checkNotNull(link);
221 - Set<ResourceAllocation> freeRes = freeResources.get(link);
222 - if (freeRes == null) {
223 - freeRes = readOriginalFreeResources(link);
224 - }
225 -
226 - return freeRes;
227 - }
228 -
229 - @Override
230 - public synchronized void allocateResources(LinkResourceAllocations allocations) {
231 - checkNotNull(allocations);
232 - linkResourceAllocationsMap.put(allocations.intentId(), allocations);
233 - for (Link link : allocations.links()) {
234 - subtractFreeResources(link, allocations);
235 - Set<LinkResourceAllocations> linkAllocs = allocatedResources.get(link);
236 - if (linkAllocs == null) {
237 - linkAllocs = new HashSet<>();
238 - }
239 - linkAllocs.add(allocations);
240 - allocatedResources.put(link, linkAllocs);
241 - }
242 - }
243 -
244 - @Override
245 - public synchronized LinkResourceEvent releaseResources(LinkResourceAllocations allocations) {
246 - checkNotNull(allocations);
247 - linkResourceAllocationsMap.remove(allocations.intentId());
248 - for (Link link : allocations.links()) {
249 - addFreeResources(link, allocations);
250 - Set<LinkResourceAllocations> linkAllocs = allocatedResources.get(link);
251 - if (linkAllocs == null) {
252 - log.error("Missing resource allocation.");
253 - } else {
254 - linkAllocs.remove(allocations);
255 - }
256 - allocatedResources.put(link, linkAllocs);
257 - }
258 -
259 - final List<LinkResourceAllocations> releasedResources =
260 - ImmutableList.of(allocations);
261 -
262 - return new LinkResourceEvent(
263 - LinkResourceEvent.Type.ADDITIONAL_RESOURCES_AVAILABLE,
264 - releasedResources);
265 - }
266 -
267 - @Override
268 - public synchronized LinkResourceAllocations getAllocations(IntentId intentId) {
269 - checkNotNull(intentId);
270 - return linkResourceAllocationsMap.get(intentId);
271 - }
272 -
273 - @Override
274 - public synchronized Iterable<LinkResourceAllocations> getAllocations(Link link) {
275 - checkNotNull(link);
276 - Set<LinkResourceAllocations> result = allocatedResources.get(link);
277 - if (result == null) {
278 - result = Collections.emptySet();
279 - }
280 - return Collections.unmodifiableSet(result);
281 - }
282 -
283 - @Override
284 - public synchronized Iterable<LinkResourceAllocations> getAllocations() {
285 - return Collections.unmodifiableCollection(linkResourceAllocationsMap.values());
286 - }
287 -
288 -
289 -}
1 -/*
2 - * Copyright 2014-2015 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.store.trivial;
17 -
18 -import java.util.Collection;
19 -import java.util.HashSet;
20 -import java.util.Set;
21 -
22 -import org.junit.After;
23 -import org.junit.Before;
24 -import org.junit.Test;
25 -import org.onlab.util.Bandwidth;
26 -import org.onosproject.net.AnnotationKeys;
27 -import org.onosproject.net.Annotations;
28 -import org.onosproject.net.ConnectPoint;
29 -import org.onosproject.net.DefaultAnnotations;
30 -import org.onosproject.net.DefaultLink;
31 -import org.onosproject.net.Link;
32 -import org.onosproject.net.intent.IntentId;
33 -import org.onosproject.net.provider.ProviderId;
34 -import org.onosproject.net.resource.link.BandwidthResource;
35 -import org.onosproject.net.resource.link.BandwidthResourceAllocation;
36 -import org.onosproject.net.resource.link.LambdaResource;
37 -import org.onosproject.net.resource.link.LambdaResourceAllocation;
38 -import org.onosproject.net.resource.link.LinkResourceAllocations;
39 -import org.onosproject.net.resource.link.LinkResourceStore;
40 -import org.onosproject.net.resource.ResourceAllocation;
41 -import org.onosproject.net.resource.ResourceAllocationException;
42 -import org.onosproject.net.resource.ResourceRequest;
43 -import org.onosproject.net.resource.ResourceType;
44 -
45 -import com.google.common.collect.ImmutableSet;
46 -
47 -import static org.junit.Assert.assertEquals;
48 -import static org.junit.Assert.assertFalse;
49 -import static org.junit.Assert.assertNotNull;
50 -import static org.onosproject.net.DeviceId.deviceId;
51 -import static org.onosproject.net.Link.Type.DIRECT;
52 -import static org.onosproject.net.PortNumber.portNumber;
53 -
54 -/**
55 - * Test of the simple LinkResourceStore implementation.
56 - */
57 -public class SimpleLinkResourceStoreTest {
58 -
59 - private LinkResourceStore store;
60 - private SimpleLinkResourceStore simpleStore;
61 - private Link link1;
62 - private Link link2;
63 - private Link link3;
64 -
65 - /**
66 - * Returns {@link Link} object.
67 - *
68 - * @param dev1 source device
69 - * @param port1 source port
70 - * @param dev2 destination device
71 - * @param port2 destination port
72 - * @return created {@link Link} object
73 - */
74 - private static Link newLink(String dev1, int port1, String dev2, int port2) {
75 - Annotations annotations = DefaultAnnotations.builder()
76 - .set(AnnotationKeys.OPTICAL_WAVES, "80")
77 - .set(AnnotationKeys.BANDWIDTH, "1000")
78 - .build();
79 - return DefaultLink.builder()
80 - .providerId(new ProviderId("of", "foo"))
81 - .src(new ConnectPoint(deviceId(dev1), portNumber(port1)))
82 - .dst(new ConnectPoint(deviceId(dev2), portNumber(port2)))
83 - .type(DIRECT)
84 - .annotations(annotations)
85 - .build();
86 - }
87 -
88 - @Before
89 - public void setUp() throws Exception {
90 - simpleStore = new SimpleLinkResourceStore();
91 - simpleStore.activate();
92 - store = simpleStore;
93 -
94 - link1 = newLink("of:1", 1, "of:2", 2);
95 - link2 = newLink("of:2", 1, "of:3", 2);
96 - link3 = newLink("of:3", 1, "of:4", 2);
97 - }
98 -
99 - @After
100 - public void tearDown() throws Exception {
101 - simpleStore.deactivate();
102 - }
103 -
104 - /**
105 - * Tests constructor and activate method.
106 - */
107 - @Test
108 - public void testConstructorAndActivate() {
109 - final Iterable<LinkResourceAllocations> allAllocations = store.getAllocations();
110 - assertNotNull(allAllocations);
111 - assertFalse(allAllocations.iterator().hasNext());
112 -
113 - final Iterable<LinkResourceAllocations> linkAllocations =
114 - store.getAllocations(link1);
115 - assertNotNull(linkAllocations);
116 - assertFalse(linkAllocations.iterator().hasNext());
117 -
118 - final Set<ResourceAllocation> res = store.getFreeResources(link2);
119 - assertNotNull(res);
120 - }
121 -
122 - /**
123 - * Picks up and returns one of bandwidth allocations from a given set.
124 - *
125 - * @param resources the set of {@link ResourceAllocation}s
126 - * @return {@link BandwidthResourceAllocation} object if found, null
127 - * otherwise
128 - */
129 - private BandwidthResourceAllocation getBandwidthObj(Set<ResourceAllocation> resources) {
130 - for (ResourceAllocation res : resources) {
131 - if (res.type() == ResourceType.BANDWIDTH) {
132 - return ((BandwidthResourceAllocation) res);
133 - }
134 - }
135 - return null;
136 - }
137 -
138 - /**
139 - * Returns all lambda allocations from a given set.
140 - *
141 - * @param resources the set of {@link ResourceAllocation}s
142 - * @return a set of {@link LambdaResourceAllocation} objects
143 - */
144 - private Set<LambdaResourceAllocation> getLambdaObjs(Set<ResourceAllocation> resources) {
145 - Set<LambdaResourceAllocation> lambdaResources = new HashSet<>();
146 - for (ResourceAllocation res : resources) {
147 - if (res.type() == ResourceType.LAMBDA) {
148 - lambdaResources.add((LambdaResourceAllocation) res);
149 - }
150 - }
151 - return lambdaResources;
152 - }
153 -
154 - /**
155 - * Tests initial free bandwidth for a link.
156 - */
157 - @Test
158 - public void testInitialBandwidth() {
159 - final Set<ResourceAllocation> freeRes = store.getFreeResources(link1);
160 - assertNotNull(freeRes);
161 -
162 - final BandwidthResourceAllocation alloc = getBandwidthObj(freeRes);
163 - assertNotNull(alloc);
164 -
165 - assertEquals(new BandwidthResource(Bandwidth.mbps(1000.0)), alloc.bandwidth());
166 - }
167 -
168 - /**
169 - * Tests initial free lambda for a link.
170 - */
171 - @Test
172 - public void testInitialLambdas() {
173 - final Set<ResourceAllocation> freeRes = store.getFreeResources(link3);
174 - assertNotNull(freeRes);
175 -
176 - final Set<LambdaResourceAllocation> res = getLambdaObjs(freeRes);
177 - assertNotNull(res);
178 - assertEquals(80, res.size());
179 - }
180 -
181 - public static class MockLinkResourceBandwidthAllocations implements LinkResourceAllocations {
182 - final double allocationAmount;
183 -
184 - MockLinkResourceBandwidthAllocations(Double allocationAmount) {
185 - this.allocationAmount = allocationAmount;
186 - }
187 - @Override
188 - public Set<ResourceAllocation> getResourceAllocation(Link link) {
189 - final ResourceAllocation allocation =
190 - new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.bps(allocationAmount)));
191 - final Set<ResourceAllocation> allocations = new HashSet<>();
192 - allocations.add(allocation);
193 - return allocations;
194 - }
195 -
196 - @Override
197 - public IntentId intentId() {
198 - return null;
199 - }
200 -
201 - @Override
202 - public Collection<Link> links() {
203 - return ImmutableSet.of(newLink("of:1", 1, "of:2", 2));
204 - }
205 -
206 - @Override
207 - public Set<ResourceRequest> resources() {
208 - return null;
209 - }
210 -
211 - @Override
212 - public ResourceType type() {
213 - return null;
214 - }
215 - }
216 -
217 - public static class MockLinkResourceLambdaAllocations implements LinkResourceAllocations {
218 - final int allocatedLambda;
219 -
220 - MockLinkResourceLambdaAllocations(int allocatedLambda) {
221 - this.allocatedLambda = allocatedLambda;
222 - }
223 - @Override
224 - public Set<ResourceAllocation> getResourceAllocation(Link link) {
225 - final ResourceAllocation allocation =
226 - new LambdaResourceAllocation(LambdaResource.valueOf(allocatedLambda));
227 - final Set<ResourceAllocation> allocations = new HashSet<>();
228 - allocations.add(allocation);
229 - return allocations;
230 - }
231 -
232 - @Override
233 - public IntentId intentId() {
234 - return null;
235 - }
236 -
237 - @Override
238 - public Collection<Link> links() {
239 - return ImmutableSet.of(newLink("of:1", 1, "of:2", 2));
240 - }
241 -
242 - @Override
243 - public Set<ResourceRequest> resources() {
244 - return null;
245 - }
246 -
247 - @Override
248 - public ResourceType type() {
249 - return null;
250 - }
251 - }
252 -
253 - /**
254 - * Tests a successful bandwidth allocation.
255 - */
256 - @Test
257 - public void testSuccessfulBandwidthAllocation() {
258 - final LinkResourceAllocations allocations =
259 - new MockLinkResourceBandwidthAllocations(900.0);
260 - store.allocateResources(allocations);
261 - }
262 -
263 - /**
264 - * Tests an unsuccessful bandwidth allocation.
265 - */
266 - @Test
267 - public void testUnsuccessfulBandwidthAllocation() {
268 - final LinkResourceAllocations allocations =
269 - new MockLinkResourceBandwidthAllocations(2000000000.0);
270 - boolean gotException = false;
271 - try {
272 - store.allocateResources(allocations);
273 - } catch (ResourceAllocationException rae) {
274 - assertEquals(true, rae.getMessage().contains("Unable to allocate bandwidth for link"));
275 - gotException = true;
276 - }
277 - assertEquals(true, gotException);
278 - }
279 -
280 - /**
281 - * Tests a successful lambda allocation.
282 - */
283 - @Test
284 - public void testSuccessfulLambdaAllocation() {
285 - final LinkResourceAllocations allocations =
286 - new MockLinkResourceLambdaAllocations(1);
287 - store.allocateResources(allocations);
288 - }
289 -
290 - /**
291 - * Tests an unsuccessful lambda allocation.
292 - */
293 - @Test
294 - public void testUnsuccessfulLambdaAllocation() {
295 - final LinkResourceAllocations allocations =
296 - new MockLinkResourceLambdaAllocations(1);
297 - store.allocateResources(allocations);
298 -
299 - boolean gotException = false;
300 -
301 - try {
302 - store.allocateResources(allocations);
303 - } catch (ResourceAllocationException rae) {
304 - assertEquals(true, rae.getMessage().contains("Unable to allocate lambda for link"));
305 - gotException = true;
306 - }
307 - assertEquals(true, gotException);
308 - }
309 -}
1 -/*
2 - * Copyright 2015 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.store.resource.impl;
17 -
18 -import java.util.ArrayList;
19 -import java.util.Collection;
20 -import java.util.Collections;
21 -import java.util.HashMap;
22 -import java.util.HashSet;
23 -import java.util.List;
24 -import java.util.Map;
25 -import java.util.Set;
26 -import java.util.stream.Collectors;
27 -
28 -import org.apache.felix.scr.annotations.Component;
29 -import org.apache.felix.scr.annotations.Reference;
30 -import org.apache.felix.scr.annotations.ReferenceCardinality;
31 -import org.apache.felix.scr.annotations.Service;
32 -import org.apache.felix.scr.annotations.Activate;
33 -import org.apache.felix.scr.annotations.Deactivate;
34 -import org.onlab.util.Bandwidth;
35 -import org.onosproject.net.OmsPort;
36 -import org.onosproject.net.device.DeviceService;
37 -import org.slf4j.Logger;
38 -import org.onlab.util.PositionalParameterStringFormatter;
39 -import org.onosproject.net.Link;
40 -import org.onosproject.net.LinkKey;
41 -import org.onosproject.net.Port;
42 -import org.onosproject.net.intent.IntentId;
43 -import org.onosproject.net.resource.link.BandwidthResource;
44 -import org.onosproject.net.resource.link.BandwidthResourceAllocation;
45 -import org.onosproject.net.resource.link.LambdaResource;
46 -import org.onosproject.net.resource.link.LambdaResourceAllocation;
47 -import org.onosproject.net.resource.link.LinkResourceAllocations;
48 -import org.onosproject.net.resource.link.LinkResourceEvent;
49 -import org.onosproject.net.resource.link.LinkResourceStore;
50 -import org.onosproject.net.resource.link.LinkResourceStoreDelegate;
51 -import org.onosproject.net.resource.link.MplsLabel;
52 -import org.onosproject.net.resource.link.MplsLabelResourceAllocation;
53 -import org.onosproject.net.resource.ResourceAllocation;
54 -import org.onosproject.net.resource.ResourceAllocationException;
55 -import org.onosproject.net.resource.ResourceType;
56 -import org.onosproject.store.AbstractStore;
57 -import org.onosproject.store.serializers.KryoNamespaces;
58 -import org.onosproject.store.service.ConsistentMap;
59 -import org.onosproject.store.service.Serializer;
60 -import org.onosproject.store.service.StorageService;
61 -import org.onosproject.store.service.TransactionContext;
62 -import org.onosproject.store.service.TransactionalMap;
63 -import org.onosproject.store.service.Versioned;
64 -
65 -import com.google.common.collect.ImmutableList;
66 -import com.google.common.collect.ImmutableSet;
67 -import com.google.common.collect.Sets;
68 -
69 -import static com.google.common.base.Preconditions.checkNotNull;
70 -import static org.slf4j.LoggerFactory.getLogger;
71 -import static org.onosproject.net.AnnotationKeys.BANDWIDTH;
72 -
73 -/**
74 - * Store that manages link resources using Copycat-backed TransactionalMaps.
75 - *
76 - * @deprecated in Emu Release
77 - */
78 -@Deprecated
79 -@Component(immediate = true, enabled = true)
80 -@Service
81 -public class ConsistentLinkResourceStore extends
82 - AbstractStore<LinkResourceEvent, LinkResourceStoreDelegate> implements
83 - LinkResourceStore {
84 -
85 - private final Logger log = getLogger(getClass());
86 -
87 - private static final BandwidthResource DEFAULT_BANDWIDTH = new BandwidthResource(Bandwidth.mbps(1_000));
88 - private static final BandwidthResource EMPTY_BW = new BandwidthResource(Bandwidth.bps(0));
89 -
90 - // Smallest non-reserved MPLS label
91 - private static final int MIN_UNRESERVED_LABEL = 0x10;
92 - // Max non-reserved MPLS label = 239
93 - private static final int MAX_UNRESERVED_LABEL = 0xEF;
94 -
95 - // table to store current allocations
96 - /** LinkKey -> List<LinkResourceAllocations>. */
97 - private static final String LINK_RESOURCE_ALLOCATIONS = "LinkAllocations";
98 -
99 - /** IntentId -> LinkResourceAllocations. */
100 - private static final String INTENT_ALLOCATIONS = "LinkIntentAllocations";
101 -
102 - private static final Serializer SERIALIZER = Serializer.using(KryoNamespaces.API);
103 -
104 - // for reading committed values.
105 - private ConsistentMap<IntentId, LinkResourceAllocations> intentAllocMap;
106 -
107 - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
108 - protected StorageService storageService;
109 -
110 - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
111 - protected DeviceService deviceService;
112 -
113 - @Activate
114 - public void activate() {
115 - intentAllocMap = storageService.<IntentId, LinkResourceAllocations>consistentMapBuilder()
116 - .withName(INTENT_ALLOCATIONS)
117 - .withSerializer(SERIALIZER)
118 - .build();
119 - log.info("Started");
120 - }
121 -
122 - @Deactivate
123 - public void deactivate() {
124 - log.info("Stopped");
125 - }
126 -
127 - private TransactionalMap<IntentId, LinkResourceAllocations> getIntentAllocs(TransactionContext tx) {
128 - return tx.getTransactionalMap(INTENT_ALLOCATIONS, SERIALIZER);
129 - }
130 -
131 - private TransactionalMap<LinkKey, List<LinkResourceAllocations>> getLinkAllocs(TransactionContext tx) {
132 - return tx.getTransactionalMap(LINK_RESOURCE_ALLOCATIONS, SERIALIZER);
133 - }
134 -
135 - private TransactionContext getTxContext() {
136 - return storageService.transactionContextBuilder().build();
137 - }
138 -
139 - private Set<ResourceAllocation> getResourceCapacity(ResourceType type, Link link) {
140 - switch (type) {
141 - case BANDWIDTH:
142 - return ImmutableSet.of(getBandwidthResourceCapacity(link));
143 - case LAMBDA:
144 - return getLambdaResourceCapacity(link);
145 - case MPLS_LABEL:
146 - return getMplsResourceCapacity();
147 - default:
148 - return ImmutableSet.of();
149 - }
150 - }
151 -
152 - private Set<ResourceAllocation> getLambdaResourceCapacity(Link link) {
153 - Port port = deviceService.getPort(link.src().deviceId(), link.src().port());
154 - if (!(port instanceof OmsPort)) {
155 - return Collections.emptySet();
156 - }
157 -
158 - OmsPort omsPort = (OmsPort) port;
159 - Set<ResourceAllocation> allocations = new HashSet<>();
160 - // Assume fixed grid for now
161 - for (int i = 0; i < omsPort.totalChannels(); i++) {
162 - allocations.add(new LambdaResourceAllocation(LambdaResource.valueOf(i)));
163 - }
164 - return allocations;
165 - }
166 -
167 - private BandwidthResourceAllocation getBandwidthResourceCapacity(Link link) {
168 -
169 - // if Link annotation exist, use them
170 - // if all fails, use DEFAULT_BANDWIDTH
171 - BandwidthResource bandwidth = DEFAULT_BANDWIDTH;
172 - String strBw = link.annotations().value(BANDWIDTH);
173 - if (strBw == null) {
174 - return new BandwidthResourceAllocation(bandwidth);
175 - }
176 -
177 - try {
178 - bandwidth = new BandwidthResource(Bandwidth.mbps(Double.parseDouble(strBw)));
179 - } catch (NumberFormatException e) {
180 - // do nothings, use default bandwidth
181 - bandwidth = DEFAULT_BANDWIDTH;
182 - }
183 - return new BandwidthResourceAllocation(bandwidth);
184 - }
185 -
186 - private Set<ResourceAllocation> getMplsResourceCapacity() {
187 - Set<ResourceAllocation> allocations = new HashSet<>();
188 - //Ignoring reserved labels of 0 through 15
189 - for (int i = MIN_UNRESERVED_LABEL; i <= MAX_UNRESERVED_LABEL; i++) {
190 - allocations.add(new MplsLabelResourceAllocation(MplsLabel
191 - .valueOf(i)));
192 -
193 - }
194 - return allocations;
195 - }
196 -
197 - private Map<ResourceType, Set<ResourceAllocation>> getResourceCapacity(Link link) {
198 - Map<ResourceType, Set<ResourceAllocation>> caps = new HashMap<>();
199 - for (ResourceType type : ResourceType.values()) {
200 - Set<ResourceAllocation> cap = getResourceCapacity(type, link);
201 - caps.put(type, cap);
202 - }
203 - return caps;
204 - }
205 -
206 - @Override
207 - public Set<ResourceAllocation> getFreeResources(Link link) {
208 - TransactionContext tx = getTxContext();
209 -
210 - tx.begin();
211 - try {
212 - Map<ResourceType, Set<ResourceAllocation>> freeResources = getFreeResourcesEx(tx, link);
213 - return freeResources.values().stream()
214 - .flatMap(Collection::stream)
215 - .collect(Collectors.toSet());
216 - } finally {
217 - tx.abort();
218 - }
219 - }
220 -
221 - private Map<ResourceType, Set<ResourceAllocation>> getFreeResourcesEx(TransactionContext tx, Link link) {
222 - checkNotNull(tx);
223 - checkNotNull(link);
224 -
225 - Map<ResourceType, Set<ResourceAllocation>> free = new HashMap<>();
226 - final Map<ResourceType, Set<ResourceAllocation>> caps = getResourceCapacity(link);
227 - final List<LinkResourceAllocations> allocations = ImmutableList.copyOf(getAllocations(tx, link));
228 -
229 - Set<ResourceAllocation> bw = caps.get(ResourceType.BANDWIDTH);
230 - Set<ResourceAllocation> value = getFreeBandwidthResources(link, bw, allocations);
231 - free.put(ResourceType.BANDWIDTH, value);
232 -
233 - Set<ResourceAllocation> lmd = caps.get(ResourceType.LAMBDA);
234 - Set<ResourceAllocation> freeL = getFreeResources(link, lmd, allocations,
235 - LambdaResourceAllocation.class);
236 - free.put(ResourceType.LAMBDA, freeL);
237 -
238 - Set<ResourceAllocation> mpls = caps.get(ResourceType.MPLS_LABEL);
239 - Set<ResourceAllocation> freeLabel = getFreeResources(link, mpls, allocations,
240 - MplsLabelResourceAllocation.class);
241 - free.put(ResourceType.MPLS_LABEL, freeLabel);
242 -
243 - return free;
244 - }
245 -
246 - private Set<ResourceAllocation> getFreeBandwidthResources(Link link, Set<ResourceAllocation> bw,
247 - List<LinkResourceAllocations> allocations) {
248 - if (bw == null || bw.isEmpty()) {
249 - bw = Sets.newHashSet(new BandwidthResourceAllocation(EMPTY_BW));
250 - }
251 -
252 - BandwidthResourceAllocation cap = (BandwidthResourceAllocation) bw.iterator().next();
253 - double freeBw = cap.bandwidth().toDouble();
254 -
255 - // enumerate current allocations, subtracting resources
256 - double allocatedBw = allocations.stream()
257 - .flatMap(x -> x.getResourceAllocation(link).stream())
258 - .filter(x -> x instanceof BandwidthResourceAllocation)
259 - .map(x -> (BandwidthResourceAllocation) x)
260 - .mapToDouble(x -> x.bandwidth().toDouble())
261 - .sum();
262 - freeBw -= allocatedBw;
263 - return Sets.newHashSet(
264 - new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.bps(freeBw))));
265 - }
266 -
267 - private Set<ResourceAllocation> getFreeResources(Link link,
268 - Set<ResourceAllocation> resources,
269 - List<LinkResourceAllocations> allocations,
270 - Class<? extends ResourceAllocation> cls) {
271 - if (resources == null || resources.isEmpty()) {
272 - // nothing left
273 - return Collections.emptySet();
274 - }
275 - Set<ResourceAllocation> freeL = resources.stream()
276 - .filter(cls::isInstance)
277 - .collect(Collectors.toSet());
278 -
279 - // enumerate current allocations, removing resources
280 - List<ResourceAllocation> allocated = allocations.stream()
281 - .flatMap(x -> x.getResourceAllocation(link).stream())
282 - .filter(cls::isInstance)
283 - .collect(Collectors.toList());
284 - freeL.removeAll(allocated);
285 - return freeL;
286 - }
287 -
288 - @Override
289 - public void allocateResources(LinkResourceAllocations allocations) {
290 - checkNotNull(allocations);
291 - TransactionContext tx = getTxContext();
292 -
293 - tx.begin();
294 - try {
295 - TransactionalMap<IntentId, LinkResourceAllocations> intentAllocs = getIntentAllocs(tx);
296 - intentAllocs.put(allocations.intentId(), allocations);
297 - allocations.links().forEach(link -> allocateLinkResource(tx, link, allocations));
298 - tx.commit();
299 - } catch (ResourceAllocationException e) {
300 - log.error("Exception thrown, rolling back", e);
301 - tx.abort();
302 - } catch (Exception e) {
303 - log.error("Exception thrown, rolling back", e);
304 - tx.abort();
305 - throw e;
306 - }
307 - }
308 -
309 - private void allocateLinkResource(TransactionContext tx, Link link,
310 - LinkResourceAllocations allocations) {
311 - // requested resources
312 - Set<ResourceAllocation> reqs = allocations.getResourceAllocation(link);
313 - Map<ResourceType, Set<ResourceAllocation>> available = getFreeResourcesEx(tx, link);
314 - for (ResourceAllocation req : reqs) {
315 - Set<ResourceAllocation> avail = available.get(req.type());
316 - if (req instanceof BandwidthResourceAllocation) {
317 - // check if allocation should be accepted
318 - if (avail.isEmpty()) {
319 - throw new ResourceAllocationException(String.format("There's no Bandwidth resource on %s?", link));
320 - }
321 - BandwidthResourceAllocation bw = (BandwidthResourceAllocation) avail.iterator().next();
322 - double bwLeft = bw.bandwidth().toDouble();
323 - BandwidthResourceAllocation bwReq = ((BandwidthResourceAllocation) req);
324 - bwLeft -= bwReq.bandwidth().toDouble();
325 - if (bwLeft < 0) {
326 - throw new ResourceAllocationException(
327 - PositionalParameterStringFormatter.format(
328 - "Unable to allocate bandwidth for link {} "
329 - + " requested amount is {} current allocation is {}",
330 - link,
331 - bwReq.bandwidth().toDouble(),
332 - bw));
333 - }
334 - } else if (req instanceof LambdaResourceAllocation) {
335 - LambdaResourceAllocation lambdaAllocation = (LambdaResourceAllocation) req;
336 - // check if allocation should be accepted
337 - if (!avail.contains(req)) {
338 - // requested lambda was not available
339 - throw new ResourceAllocationException(
340 - PositionalParameterStringFormatter.format(
341 - "Unable to allocate lambda for link {} lambda is {}",
342 - link,
343 - lambdaAllocation.lambda().toInt()));
344 - }
345 - } else if (req instanceof MplsLabelResourceAllocation) {
346 - MplsLabelResourceAllocation mplsAllocation = (MplsLabelResourceAllocation) req;
347 - if (!avail.contains(req)) {
348 - throw new ResourceAllocationException(
349 - PositionalParameterStringFormatter
350 - .format("Unable to allocate MPLS label for link "
351 - + "{} MPLS label is {}",
352 - link,
353 - mplsAllocation
354 - .mplsLabel()
355 - .toString()));
356 - }
357 - }
358 - }
359 - // all requests allocatable => add allocation
360 - final LinkKey linkKey = LinkKey.linkKey(link);
361 - TransactionalMap<LinkKey, List<LinkResourceAllocations>> linkAllocs = getLinkAllocs(tx);
362 - List<LinkResourceAllocations> before = linkAllocs.get(linkKey);
363 - if (before == null) {
364 - List<LinkResourceAllocations> after = new ArrayList<>();
365 - after.add(allocations);
366 - linkAllocs.putIfAbsent(linkKey, after);
367 - } else {
368 - boolean overlapped = before.stream()
369 - .flatMap(x -> x.getResourceAllocation(link).stream())
370 - .anyMatch(x -> allocations.getResourceAllocation(link).contains(x));
371 - if (overlapped) {
372 - throw new ResourceAllocationException(
373 - String.format("Resource allocations are overlapped between %s and %s",
374 - before, allocations)
375 - );
376 - }
377 - List<LinkResourceAllocations> after = new ArrayList<>(before.size() + 1);
378 - after.addAll(before);
379 - after.add(allocations);
380 - linkAllocs.replace(linkKey, before, after);
381 - }
382 - }
383 -
384 - @Override
385 - public LinkResourceEvent releaseResources(LinkResourceAllocations allocations) {
386 - checkNotNull(allocations);
387 -
388 - final IntentId intentId = allocations.intentId();
389 - final Collection<Link> links = allocations.links();
390 - boolean success = false;
391 - do {
392 - TransactionContext tx = getTxContext();
393 - tx.begin();
394 - try {
395 - TransactionalMap<IntentId, LinkResourceAllocations> intentAllocs = getIntentAllocs(tx);
396 - intentAllocs.remove(intentId);
397 -
398 - TransactionalMap<LinkKey, List<LinkResourceAllocations>> linkAllocs = getLinkAllocs(tx);
399 - links.forEach(link -> {
400 - final LinkKey linkId = LinkKey.linkKey(link);
401 -
402 - List<LinkResourceAllocations> before = linkAllocs.get(linkId);
403 - if (before == null || before.isEmpty()) {
404 - // something is wrong, but it is already freed
405 - log.warn("There was no resource left to release on {}", linkId);
406 - return;
407 - }
408 - List<LinkResourceAllocations> after = new ArrayList<>(before);
409 - after.remove(allocations);
410 - linkAllocs.replace(linkId, before, after);
411 - });
412 - success = tx.commit();
413 - } catch (Exception e) {
414 - log.error("Exception thrown during releaseResource {}", allocations, e);
415 - tx.abort();
416 - throw e;
417 - }
418 - } while (!success);
419 -
420 - // Issue events to force recompilation of intents.
421 - final List<LinkResourceAllocations> releasedResources = ImmutableList.of(allocations);
422 - return new LinkResourceEvent(
423 - LinkResourceEvent.Type.ADDITIONAL_RESOURCES_AVAILABLE,
424 - releasedResources);
425 -
426 - }
427 -
428 - @Override
429 - public LinkResourceAllocations getAllocations(IntentId intentId) {
430 - checkNotNull(intentId);
431 - Versioned<LinkResourceAllocations> alloc = null;
432 - try {
433 - alloc = intentAllocMap.get(intentId);
434 - } catch (Exception e) {
435 - log.warn("Could not read resource allocation information", e);
436 - }
437 - return alloc == null ? null : alloc.value();
438 - }
439 -
440 - @Override
441 - public Iterable<LinkResourceAllocations> getAllocations(Link link) {
442 - checkNotNull(link);
443 - TransactionContext tx = getTxContext();
444 - Iterable<LinkResourceAllocations> res = null;
445 - tx.begin();
446 - try {
447 - res = getAllocations(tx, link);
448 - } finally {
449 - tx.abort();
450 - }
451 - return res == null ? Collections.emptyList() : res;
452 - }
453 -
454 - @Override
455 - public Iterable<LinkResourceAllocations> getAllocations() {
456 - try {
457 - Set<LinkResourceAllocations> allocs =
458 - intentAllocMap.values().stream().map(Versioned::value).collect(Collectors.toSet());
459 - return ImmutableSet.copyOf(allocs);
460 - } catch (Exception e) {
461 - log.warn("Could not read resource allocation information", e);
462 - }
463 - return ImmutableSet.of();
464 - }
465 -
466 - private Iterable<LinkResourceAllocations> getAllocations(TransactionContext tx, Link link) {
467 - checkNotNull(tx);
468 - checkNotNull(link);
469 - final LinkKey key = LinkKey.linkKey(link);
470 - TransactionalMap<LinkKey, List<LinkResourceAllocations>> linkAllocs = getLinkAllocs(tx);
471 -
472 - List<LinkResourceAllocations> res = linkAllocs.get(key);
473 - if (res != null) {
474 - return res;
475 - }
476 -
477 - res = linkAllocs.putIfAbsent(key, new ArrayList<>());
478 - if (res == null) {
479 - return Collections.emptyList();
480 - } else {
481 - return res;
482 - }
483 - }
484 -
485 -}