blob: cea0036849d132b33a10b0002bc9fa915ae6f08f [file] [log] [blame]
Sho SHIMIZU68470162016-07-05 11:54:57 -07001/*
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
17package org.onosproject.store.resource.impl;
18
19import com.google.common.collect.ImmutableList;
20import org.junit.Test;
21import org.onlab.util.Bandwidth;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.PortNumber;
24import org.onosproject.net.intent.IntentId;
25import org.onosproject.net.resource.ContinuousResource;
26import org.onosproject.net.resource.ResourceAllocation;
27import org.onosproject.net.resource.ResourceConsumer;
28import org.onosproject.net.resource.Resources;
29
30import static org.hamcrest.Matchers.is;
31import static org.junit.Assert.assertThat;
32
33/**
34 * Unit tests for ContinuousResourceAllocation.
35 */
36public class ContinuousResourceAllocationTest {
37
38 private static final DeviceId DID = DeviceId.deviceId("a");
39 private static final PortNumber PN1 = PortNumber.portNumber(1);
40
41 @Test
42 public void testReleaseWhenAllocatedResourceIsRequested() {
43 ContinuousResource original =
44 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.gbps(1).bps());
45 ContinuousResource allocated =
46 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(500).bps());
47 ResourceConsumer consumer = IntentId.valueOf(1);
48
49 ContinuousResourceAllocation sut = new ContinuousResourceAllocation(original,
50 ImmutableList.of(new ResourceAllocation(allocated, consumer)));
51
52 ContinuousResource request =
53 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(500).bps());
54
55 ContinuousResourceAllocation released = sut.release(request, consumer.consumerId());
56
57 assertThat(released.allocations().isEmpty(), is(true));
58 }
59
60 @Test
61 public void testReleaseWhenDifferentConsumerIsSpecified() {
62 ContinuousResource original =
63 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.gbps(1).bps());
64 ContinuousResource allocated =
65 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(500).bps());
66 ResourceConsumer consumer = IntentId.valueOf(1);
67 ResourceConsumer otherConsumer = IntentId.valueOf(2);
68
69 ContinuousResourceAllocation sut = new ContinuousResourceAllocation(original,
70 ImmutableList.of(new ResourceAllocation(allocated, consumer)));
71
72 ContinuousResource request =
73 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(500).bps());
74
75 ImmutableList<ResourceAllocation> allocations = sut.release(request, otherConsumer.consumerId()).allocations();
76
77 assertThat(allocations.size(), is(1));
78 assertThat(allocations.get(0).resource().equals(allocated), is(true));
79 }
80}