blob: f12e5b8c3b45774399cd7d87495585ebd6b60338 [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
Sho SHIMIZU709a1792016-07-05 11:55:53 -070042 public void testNoAllocationHasEnoughResource() {
43 ContinuousResource original =
44 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.gbps(1).bps());
45
46 ContinuousResourceAllocation sut = ContinuousResourceAllocation.empty(original);
47
48 ContinuousResource request =
49 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(100).bps());
50
51 assertThat(sut.hasEnoughResource(request), is(true));
52 }
53
54 @Test
55 public void testHasEnoughResourceWhenSmallResourceIsRequested() {
56 ContinuousResource original =
57 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.gbps(1).bps());
58 ContinuousResource allocated =
59 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(500).bps());
60 ResourceConsumer consumer = IntentId.valueOf(1);
61
62 ContinuousResourceAllocation sut = new ContinuousResourceAllocation(original,
63 ImmutableList.of(new ResourceAllocation(allocated, consumer)));
64
65 ContinuousResource request =
66 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(200).bps());
67 assertThat(sut.hasEnoughResource(request), is(true));
68 }
69
70 @Test
71 public void testHasEnoughResourceWhenLargeResourceIsRequested() {
72 ContinuousResource original =
73 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.gbps(1).bps());
74 ContinuousResource allocated =
75 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(500).bps());
76 ResourceConsumer consumer = IntentId.valueOf(1);
77
78 ContinuousResourceAllocation sut = new ContinuousResourceAllocation(original,
79 ImmutableList.of(new ResourceAllocation(allocated, consumer)));
80
81 ContinuousResource request =
82 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(600).bps());
83 assertThat(sut.hasEnoughResource(request), is(false));
84 }
85
86 @Test
87 public void testHasEnoughResourceWhenExactResourceIsRequested() {
88 ContinuousResource original =
89 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.gbps(1).bps());
90 ContinuousResource allocated =
91 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(500).bps());
92 ResourceConsumer consumer = IntentId.valueOf(1);
93
94 ContinuousResourceAllocation sut = new ContinuousResourceAllocation(original,
95 ImmutableList.of(new ResourceAllocation(allocated, consumer)));
96
97 ContinuousResource request =
98 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(500).bps());
99 assertThat(sut.hasEnoughResource(request), is(true));
100 }
101
102 @Test
Sho SHIMIZU68470162016-07-05 11:54:57 -0700103 public void testReleaseWhenAllocatedResourceIsRequested() {
104 ContinuousResource original =
105 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.gbps(1).bps());
106 ContinuousResource allocated =
107 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(500).bps());
108 ResourceConsumer consumer = IntentId.valueOf(1);
109
110 ContinuousResourceAllocation sut = new ContinuousResourceAllocation(original,
111 ImmutableList.of(new ResourceAllocation(allocated, consumer)));
112
113 ContinuousResource request =
114 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(500).bps());
115
116 ContinuousResourceAllocation released = sut.release(request, consumer.consumerId());
117
118 assertThat(released.allocations().isEmpty(), is(true));
119 }
120
121 @Test
122 public void testReleaseWhenDifferentConsumerIsSpecified() {
123 ContinuousResource original =
124 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.gbps(1).bps());
125 ContinuousResource allocated =
126 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(500).bps());
127 ResourceConsumer consumer = IntentId.valueOf(1);
128 ResourceConsumer otherConsumer = IntentId.valueOf(2);
129
130 ContinuousResourceAllocation sut = new ContinuousResourceAllocation(original,
131 ImmutableList.of(new ResourceAllocation(allocated, consumer)));
132
133 ContinuousResource request =
134 Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(500).bps());
135
136 ImmutableList<ResourceAllocation> allocations = sut.release(request, otherConsumer.consumerId()).allocations();
137
138 assertThat(allocations.size(), is(1));
139 assertThat(allocations.get(0).resource().equals(allocated), is(true));
140 }
141}