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