Toshio Koide

Initial implementation of unit tests for SimpleLinkResourceStore.

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.onlab.onos.store.trivial.impl;
17 +
18 +import static org.junit.Assert.assertEquals;
19 +import static org.junit.Assert.assertFalse;
20 +import static org.junit.Assert.assertNotNull;
21 +import static org.onlab.onos.net.DeviceId.deviceId;
22 +import static org.onlab.onos.net.Link.Type.DIRECT;
23 +import static org.onlab.onos.net.PortNumber.portNumber;
24 +
25 +import java.util.HashSet;
26 +import java.util.Set;
27 +
28 +import org.junit.After;
29 +import org.junit.Before;
30 +import org.junit.Test;
31 +import org.onlab.onos.net.ConnectPoint;
32 +import org.onlab.onos.net.DefaultLink;
33 +import org.onlab.onos.net.Link;
34 +import org.onlab.onos.net.provider.ProviderId;
35 +import org.onlab.onos.net.resource.Bandwidth;
36 +import org.onlab.onos.net.resource.BandwidthResourceAllocation;
37 +import org.onlab.onos.net.resource.LambdaResourceAllocation;
38 +import org.onlab.onos.net.resource.LinkResourceAllocations;
39 +import org.onlab.onos.net.resource.LinkResourceStore;
40 +import org.onlab.onos.net.resource.ResourceAllocation;
41 +import org.onlab.onos.net.resource.ResourceType;
42 +
43 +/**
44 + * Test of the simple LinkResourceStore implementation.
45 + */
46 +public class SimpleLinkResourceStoreTest {
47 +
48 + private LinkResourceStore store;
49 + private SimpleLinkResourceStore simpleStore;
50 + private Link link1;
51 + private Link link2;
52 + private Link link3;
53 +
54 + /**
55 + * Returns {@link Link} object.
56 + *
57 + * @param dev1 source device
58 + * @param port1 source port
59 + * @param dev2 destination device
60 + * @param port2 destination port
61 + * @return created {@link Link} object
62 + */
63 + private Link newLink(String dev1, int port1, String dev2, int port2) {
64 + return new DefaultLink(
65 + new ProviderId("of", "foo"),
66 + new ConnectPoint(deviceId(dev1), portNumber(port1)),
67 + new ConnectPoint(deviceId(dev2), portNumber(port2)),
68 + DIRECT);
69 + }
70 +
71 + @Before
72 + public void setUp() throws Exception {
73 + simpleStore = new SimpleLinkResourceStore();
74 + simpleStore.activate();
75 + store = simpleStore;
76 +
77 + link1 = newLink("of:1", 1, "of:2", 2);
78 + link2 = newLink("of:2", 1, "of:3", 2);
79 + link3 = newLink("of:3", 1, "of:4", 2);
80 + }
81 +
82 + @After
83 + public void tearDown() throws Exception {
84 + simpleStore.deactivate();
85 + }
86 +
87 + /**
88 + * Tests constructor and activate method.
89 + */
90 + @Test
91 + public void testConstructorAndActivate() {
92 + final Iterable<LinkResourceAllocations> allAllocations = store.getAllocations();
93 + assertNotNull(allAllocations);
94 + assertFalse(allAllocations.iterator().hasNext());
95 +
96 + final Iterable<LinkResourceAllocations> linkAllocations =
97 + store.getAllocations(link1);
98 + assertNotNull(linkAllocations);
99 + assertFalse(linkAllocations.iterator().hasNext());
100 +
101 + final Set<ResourceAllocation> res = store.getFreeResources(link2);
102 + assertNotNull(res);
103 + }
104 +
105 + /**
106 + * Picks up and returns one of bandwidth allocations from a given set.
107 + *
108 + * @param resources the set of {@link ResourceAllocation}s
109 + * @return {@link BandwidthResourceAllocation} object if found, null
110 + * otherwise
111 + */
112 + private BandwidthResourceAllocation getBandwidthObj(Set<ResourceAllocation> resources) {
113 + for (ResourceAllocation res : resources) {
114 + if (res.type() == ResourceType.BANDWIDTH) {
115 + return ((BandwidthResourceAllocation) res);
116 + }
117 + }
118 + return null;
119 + }
120 +
121 + /**
122 + * Returns all lambda allocations from a given set.
123 + *
124 + * @param resources the set of {@link ResourceAllocation}s
125 + * @return a set of {@link LambdaResourceAllocation} objects
126 + */
127 + private Set<LambdaResourceAllocation> getLambdaObjs(Set<ResourceAllocation> resources) {
128 + Set<LambdaResourceAllocation> lambdaResources = new HashSet<>();
129 + for (ResourceAllocation res : resources) {
130 + if (res.type() == ResourceType.LAMBDA) {
131 + lambdaResources.add((LambdaResourceAllocation) res);
132 + }
133 + }
134 + return lambdaResources;
135 + }
136 +
137 + /**
138 + * Tests initial free bandwidth for a link.
139 + */
140 + @Test
141 + public void testInitialBandwidth() {
142 + final Set<ResourceAllocation> freeRes = store.getFreeResources(link1);
143 + assertNotNull(freeRes);
144 +
145 + final BandwidthResourceAllocation alloc = getBandwidthObj(freeRes);
146 + assertNotNull(alloc);
147 +
148 + assertEquals(Bandwidth.valueOf(1000000.0), alloc.bandwidth());
149 + }
150 +
151 + /**
152 + * Tests initial free lambda for a link.
153 + */
154 + @Test
155 + public void testInitialLambdas() {
156 + final Set<ResourceAllocation> freeRes = store.getFreeResources(link3);
157 + assertNotNull(freeRes);
158 +
159 + final Set<LambdaResourceAllocation> res = getLambdaObjs(freeRes);
160 + assertNotNull(res);
161 + assertEquals(100, res.size());
162 + }
163 +}