blob: b6c63a7efa23588a1af7abb4055619dd8d8ef4fb [file] [log] [blame]
Yuta HIGUCHI3cc4d9b2014-11-29 18:17:17 -08001/*
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 */
16package org.onlab.onos.store.resource.impl;
17
18import java.util.HashSet;
19import java.util.Set;
20
21import org.junit.After;
22import org.junit.Before;
23import org.junit.Test;
24import org.onlab.onos.net.AnnotationKeys;
25import org.onlab.onos.net.Annotations;
26import org.onlab.onos.net.ConnectPoint;
27import org.onlab.onos.net.DefaultAnnotations;
28import org.onlab.onos.net.DefaultLink;
29import org.onlab.onos.net.Link;
30import org.onlab.onos.net.provider.ProviderId;
31import org.onlab.onos.net.resource.Bandwidth;
32import org.onlab.onos.net.resource.BandwidthResourceAllocation;
33import org.onlab.onos.net.resource.LambdaResourceAllocation;
34import org.onlab.onos.net.resource.LinkResourceAllocations;
35import org.onlab.onos.net.resource.LinkResourceStore;
36import org.onlab.onos.net.resource.ResourceAllocation;
37import org.onlab.onos.net.resource.ResourceType;
38import org.onlab.onos.store.hz.StoreService;
39import org.onlab.onos.store.hz.TestStoreManager;
40
41import com.hazelcast.config.Config;
42import com.hazelcast.core.Hazelcast;
43
44import static org.junit.Assert.assertEquals;
45import static org.junit.Assert.assertFalse;
46import static org.junit.Assert.assertNotNull;
47import static org.onlab.onos.net.DeviceId.deviceId;
48import static org.onlab.onos.net.Link.Type.DIRECT;
49import static org.onlab.onos.net.PortNumber.portNumber;
50
51/**
52 * Test of the simple LinkResourceStore implementation.
53 */
54public class HazelcastLinkResourceStoreTest {
55
56 private LinkResourceStore store;
57 private HazelcastLinkResourceStore storeImpl;
58 private Link link1;
59 private Link link2;
60 private Link link3;
61 private TestStoreManager storeMgr;
62
63 /**
64 * Returns {@link Link} object.
65 *
66 * @param dev1 source device
67 * @param port1 source port
68 * @param dev2 destination device
69 * @param port2 destination port
70 * @return created {@link Link} object
71 */
72 private Link newLink(String dev1, int port1, String dev2, int port2) {
73 Annotations annotations = DefaultAnnotations.builder()
74 .set(AnnotationKeys.OPTICAL_WAVES, "80")
75 .set(AnnotationKeys.BANDWIDTH, "1000000")
76 .build();
77 return new DefaultLink(
78 new ProviderId("of", "foo"),
79 new ConnectPoint(deviceId(dev1), portNumber(port1)),
80 new ConnectPoint(deviceId(dev2), portNumber(port2)),
81 DIRECT, annotations);
82 }
83
84 @Before
85 public void setUp() throws Exception {
86
87 Config config = TestStoreManager.getTestConfig();
88
89 storeMgr = new TestStoreManager(Hazelcast.newHazelcastInstance(config));
90 storeMgr.activate();
91
92
93 storeImpl = new TestHazelcastLinkResourceStore(storeMgr);
94 storeImpl.activate();
95 store = storeImpl;
96
97 link1 = newLink("of:1", 1, "of:2", 2);
98 link2 = newLink("of:2", 1, "of:3", 2);
99 link3 = newLink("of:3", 1, "of:4", 2);
100 }
101
102 @After
103 public void tearDown() throws Exception {
104 storeImpl.deactivate();
105
106 storeMgr.deactivate();
107 }
108
109 /**
110 * Tests constructor and activate method.
111 */
112 @Test
113 public void testConstructorAndActivate() {
114 final Iterable<LinkResourceAllocations> allAllocations = store.getAllocations();
115 assertNotNull(allAllocations);
116 assertFalse(allAllocations.iterator().hasNext());
117
118 final Iterable<LinkResourceAllocations> linkAllocations =
119 store.getAllocations(link1);
120 assertNotNull(linkAllocations);
121 assertFalse(linkAllocations.iterator().hasNext());
122
123 final Set<ResourceAllocation> res = store.getFreeResources(link2);
124 assertNotNull(res);
125 }
126
127 /**
128 * Picks up and returns one of bandwidth allocations from a given set.
129 *
130 * @param resources the set of {@link ResourceAllocation}s
131 * @return {@link BandwidthResourceAllocation} object if found, null
132 * otherwise
133 */
134 private BandwidthResourceAllocation getBandwidthObj(Set<ResourceAllocation> resources) {
135 for (ResourceAllocation res : resources) {
136 if (res.type() == ResourceType.BANDWIDTH) {
137 return ((BandwidthResourceAllocation) res);
138 }
139 }
140 return null;
141 }
142
143 /**
144 * Returns all lambda allocations from a given set.
145 *
146 * @param resources the set of {@link ResourceAllocation}s
147 * @return a set of {@link LambdaResourceAllocation} objects
148 */
149 private Set<LambdaResourceAllocation> getLambdaObjs(Set<ResourceAllocation> resources) {
150 Set<LambdaResourceAllocation> lambdaResources = new HashSet<>();
151 for (ResourceAllocation res : resources) {
152 if (res.type() == ResourceType.LAMBDA) {
153 lambdaResources.add((LambdaResourceAllocation) res);
154 }
155 }
156 return lambdaResources;
157 }
158
159 /**
160 * Tests initial free bandwidth for a link.
161 */
162 @Test
163 public void testInitialBandwidth() {
164 final Set<ResourceAllocation> freeRes = store.getFreeResources(link1);
165 assertNotNull(freeRes);
166
167 final BandwidthResourceAllocation alloc = getBandwidthObj(freeRes);
168 assertNotNull(alloc);
169
170 assertEquals(Bandwidth.valueOf(1000000.0), alloc.bandwidth());
171 }
172
173 /**
174 * Tests initial free lambda for a link.
175 */
176 @Test
177 public void testInitialLambdas() {
178 final Set<ResourceAllocation> freeRes = store.getFreeResources(link3);
179 assertNotNull(freeRes);
180
181 final Set<LambdaResourceAllocation> res = getLambdaObjs(freeRes);
182 assertNotNull(res);
183 assertEquals(80, res.size());
184 }
185
186 public static final class TestHazelcastLinkResourceStore
187 extends HazelcastLinkResourceStore {
188
189 public TestHazelcastLinkResourceStore(StoreService storeMgr) {
190 super.storeService = storeMgr;
191 }
192
193 }
194}