blob: 9246f6d8d48a3b33d75a2c39d8b79dda005bcb3a [file] [log] [blame]
Jian Li2c52e562017-03-02 00:15:45 +09001/*
2 * Copyright 2017-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 */
16package org.onosproject.mapping.impl;
17
18import com.google.common.collect.Lists;
19import org.junit.After;
20import org.junit.Before;
21import org.junit.Test;
22import org.onlab.packet.ChassisId;
23import org.onlab.packet.IpPrefix;
24import org.onosproject.mapping.DefaultMapping;
25import org.onosproject.mapping.DefaultMappingKey;
26import org.onosproject.mapping.DefaultMappingTreatment;
27import org.onosproject.mapping.DefaultMappingValue;
28import org.onosproject.mapping.Mapping;
29import org.onosproject.mapping.MappingKey;
30import org.onosproject.mapping.MappingTreatment;
31import org.onosproject.mapping.MappingValue;
32import org.onosproject.mapping.actions.MappingAction;
33import org.onosproject.mapping.actions.MappingActions;
34import org.onosproject.mapping.addresses.MappingAddress;
35import org.onosproject.mapping.addresses.MappingAddresses;
36import org.onosproject.net.Annotations;
37import org.onosproject.net.DefaultAnnotations;
38import org.onosproject.net.DefaultDevice;
39import org.onosproject.net.Device;
40import org.onosproject.net.DeviceId;
41import org.onosproject.net.device.DeviceServiceAdapter;
42import org.onosproject.net.provider.ProviderId;
43import org.onosproject.store.service.TestStorageService;
44
45import java.util.List;
46
47import static org.junit.Assert.assertFalse;
48import static org.junit.Assert.assertThat;
49import static org.junit.Assert.assertTrue;
50import static org.hamcrest.Matchers.is;
51import static org.onosproject.mapping.MappingStore.Type.MAP_DATABASE;
52
53/**
54 * Unit tests for DistributedMappingStore.
55 */
56public class DistributedMappingStoreTest {
57 private static final DeviceId DEVICE_ID_1 = DeviceId.deviceId("foo");
58 private static final DeviceId DEVICE_ID_2 = DeviceId.deviceId("bar");
59 private static final String IP_ADDRESS = "1.2.3.4/24";
60
61 private DistributedMappingStore mappingStore;
62 private Mapping mapping1;
63 private Mapping mapping2;
64
65 private Device device1;
66 private Device device2;
67
68 /**
69 * Sets up the storage service test harness.
70 */
71 @Before
72 public void setUp() {
73 mappingStore = new DistributedMappingStore();
74 mappingStore.storageService = new TestStorageService();
75 mappingStore.deviceService = new InternalDeviceServiceAdapter();
76 mappingStore.setDelegate(event -> {
77 });
78
79 IpPrefix ipPrefix = IpPrefix.valueOf(IP_ADDRESS);
80 MappingAddress address = MappingAddresses.ipv4MappingAddress(ipPrefix);
81
82 MappingKey key = DefaultMappingKey.builder()
83 .withAddress(address)
84 .build();
85
86 MappingAction action = MappingActions.noAction();
87 MappingTreatment treatment = DefaultMappingTreatment.builder()
88 .withAddress(address)
89 .setUnicastPriority(10)
90 .setUnicastWeight(10)
91 .build();
92
93 MappingValue value = DefaultMappingValue.builder()
94 .withAction(action)
95 .add(treatment)
96 .build();
97
98 device1 = new MockDevice(ProviderId.NONE, DEVICE_ID_1, Device.Type.OTHER,
99 "foo.inc", "0", "0", "0", null,
100 DefaultAnnotations.builder().build());
101
102 device2 = new MockDevice(ProviderId.NONE, DEVICE_ID_2, Device.Type.OTHER,
103 "foo.inc", "0", "0", "0", null,
104 DefaultAnnotations.builder().build());
105
106 mapping1 = DefaultMapping.builder()
107 .forDevice(DEVICE_ID_1)
108 .withId(1000L)
109 .withKey(key)
110 .withValue(value)
111 .build();
112
113 mapping2 = DefaultMapping.builder()
114 .forDevice(DEVICE_ID_2)
115 .withId(2000L)
116 .withKey(key)
117 .withValue(value)
118 .build();
119
120 mappingStore.activate();
121 }
122
123 private class InternalDeviceServiceAdapter extends DeviceServiceAdapter {
124
125 List<Device> devices = Lists.newArrayList();
126
127 @Override
128 public Iterable<Device> getDevices() {
129 devices.add(device1);
130 devices.add(device2);
131 return devices;
132 }
133 }
134
135 private class MockDevice extends DefaultDevice {
136 public MockDevice(ProviderId providerId, DeviceId id, Type type,
137 String manufacturer, String hwVersion, String swVersion,
138 String serialNumber, ChassisId chassisId, Annotations... annotations) {
139 super(providerId, id, type, manufacturer, hwVersion, swVersion, serialNumber,
140 chassisId, annotations);
141 }
142 }
143
144 /**
145 * Tears down the mapping1 store.
146 */
147 @After
148 public void tearDown() {
149 mappingStore.deactivate();
150 }
151
152 /**
153 * Tests adding, removing and getting.
154 */
155 @Test
156 public void basics() {
157 mappingStore.storeMapping(MAP_DATABASE, mapping1);
158 mappingStore.storeMapping(MAP_DATABASE, mapping2);
159
160 assertThat("There should be one mapping1 in the map database.",
161 mappingStore.getMappingCount(MAP_DATABASE), is(2));
162 assertTrue("There should be one mapping1 in the map database.",
163 mapping1.equals(mappingStore.getMappingEntries(MAP_DATABASE, DEVICE_ID_1)
164 .iterator().next()));
165 assertTrue("The mapping1 should be identical.",
166 mappingStore.getMappingEntry(MAP_DATABASE, mapping1).equals(mapping1));
Jian Li2dc9f002017-03-03 04:26:31 +0900167 mappingStore.removeMapping(MAP_DATABASE, mapping1);
Jian Li2c52e562017-03-02 00:15:45 +0900168 assertFalse("There should not be any mapping1 in the map database.",
169 mappingStore.getMappingEntries(MAP_DATABASE, DEVICE_ID_1).iterator().hasNext());
170 }
171}