blob: 946e86be3748401b25456acf36bd1bf84eefff77 [file] [log] [blame]
Yuta HIGUCHIa255bb42016-11-03 16:04:08 -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 */
16package org.onosproject.net.behaviour.protection;
17
18import static org.hamcrest.Matchers.is;
19import static org.junit.Assert.*;
20import static org.onosproject.net.PortNumber.portNumber;
21
22import java.io.IOException;
23import java.io.InputStream;
24import java.util.List;
25import org.junit.AfterClass;
26import org.junit.Before;
27import org.junit.BeforeClass;
28import org.junit.Test;
29import org.onlab.junit.TestUtils;
30import org.onlab.junit.TestUtils.TestUtilsException;
31import org.onlab.osgi.ServiceDirectory;
32import org.onlab.osgi.TestServiceDirectory;
33import org.onlab.packet.VlanId;
34import org.onosproject.codec.CodecService;
35import org.onosproject.codec.impl.CodecManager;
36import org.onosproject.net.ConnectPoint;
37import org.onosproject.net.DeviceId;
38import org.onosproject.net.FilteredConnectPoint;
39import org.onosproject.net.config.BaseConfig;
40import org.onosproject.net.config.ConfigApplyDelegate;
41import org.onosproject.net.flow.DefaultTrafficSelector;
42import org.onosproject.net.flow.TrafficSelector;
43import com.fasterxml.jackson.core.JsonProcessingException;
44import com.fasterxml.jackson.databind.DeserializationFeature;
45import com.fasterxml.jackson.databind.JsonNode;
46import com.fasterxml.jackson.databind.ObjectMapper;
47import com.fasterxml.jackson.databind.node.JsonNodeFactory;
48import com.fasterxml.jackson.databind.node.NumericNode;
49import com.google.common.collect.ImmutableList;
50
51public class ProtectionConfigTest {
52
53 private static TestServiceDirectory directory;
54 private static ServiceDirectory original;
55
56
57 // no-op
58 private ConfigApplyDelegate delegate = cfg -> { };
59
60
61 private final DeviceId did = DeviceId.deviceId("of:0000000000000001");
62 private final String fingerprint = "(IntentKey_XYZW)";
63 private final DeviceId peer = DeviceId.deviceId("of:0000000000000002");
64
65
66 private final ConnectPoint workingCp = new ConnectPoint(did, portNumber(1));
67 private final ConnectPoint backupCp = new ConnectPoint(did, portNumber(2));
68
69 private final TrafficSelector vlan100 = DefaultTrafficSelector.builder()
70 .matchVlanId(VlanId.vlanId((short) 100))
71 .build();
72
73
74 private final TransportEndpointDescription working
75 = TransportEndpointDescription.builder()
76 .withOutput(new FilteredConnectPoint(workingCp, vlan100))
77 .withEnabled(true)
78 .build();
79
80
81 private final TransportEndpointDescription backup
82 = TransportEndpointDescription.builder()
83 .withOutput(new FilteredConnectPoint(backupCp, vlan100))
84 .withEnabled(true)
85 .build();
86
87 private final List<TransportEndpointDescription> paths
88 = ImmutableList.of(working, backup);
89
90 private final ProtectedTransportEndpointDescription descr
91 = ProtectedTransportEndpointDescription.of(paths,
92 did,
93 fingerprint);
94
95
96 private ProtectionConfig sut;
97
98 private ObjectMapper mapper;
99
100 /**
101 * {@value #SAMPLE_JSON_PATH} after parsing.
102 */
103 private JsonNode node;
104
105 @BeforeClass
106 public static void setUpClass() throws TestUtilsException {
107 directory = new TestServiceDirectory();
108
109 CodecManager codecService = new CodecManager();
110 codecService.activate();
111 directory.add(CodecService.class, codecService);
112
113 // replace service directory used by BaseConfig
114 original = TestUtils.getField(BaseConfig.class, "services");
115 TestUtils.setField(BaseConfig.class, "services", directory);
116 }
117
118 @AfterClass
119 public static void tearDownClass() throws TestUtilsException {
120 TestUtils.setField(BaseConfig.class, "services", original);
121 }
122
123 @Before
124 public void setUp() throws JsonProcessingException, IOException, TestUtilsException {
125
126 mapper = new ObjectMapper();
127 // Jackson configuration for ease of Numeric node comparison
128 // - treat integral number node as long node
129 mapper.enable(DeserializationFeature.USE_LONG_FOR_INTS);
130 mapper.setNodeFactory(new JsonNodeFactory(false) {
131 @Override
132 public NumericNode numberNode(int v) {
133 return super.numberNode((long) v);
134 }
135 @Override
136 public NumericNode numberNode(short v) {
137 return super.numberNode((long) v);
138 }
139 });
140
141 InputStream stream = ProtectionConfigTest.class
142 .getResourceAsStream("protection_config.json");
143 JsonNode tree = mapper.readTree(stream);
144
145 node = tree.path("devices")
146 .path(did.toString())
147 .path(ProtectionConfig.CONFIG_KEY);
148 assertTrue(node.isObject());
149
150 }
151
152 @Test
153 public void readTest() {
154 sut = new ProtectionConfig();
155 sut.init(did, ProtectionConfig.CONFIG_KEY, node, mapper, delegate);
156
157 assertThat(sut.subject(), is(did));
158 assertThat(sut.paths().size(), is(2));
159
160 TransportEndpointDescription readWorking = sut.paths().get(0);
161 assertThat(readWorking.isEnabled(), is(true));
162 assertThat(readWorking.output().connectPoint(), is(workingCp));
163 assertThat(readWorking.output().trafficSelector(), is(vlan100));
164
165 TransportEndpointDescription readBackup = sut.paths().get(1);
166 assertThat(readBackup.isEnabled(), is(true));
167 assertThat(readBackup.output().connectPoint(), is(backupCp));
168 assertThat(readBackup.output().trafficSelector(), is(vlan100));
169
170 assertThat(sut.fingerprint(), is(fingerprint));
171 assertThat(sut.peer(), is(peer));
172 }
173
174 @Test
175 public void writeTest() throws JsonProcessingException, IOException {
176 ProtectionConfig w = new ProtectionConfig();
177 w.init(did, ProtectionConfig.CONFIG_KEY, mapper.createObjectNode(), mapper, delegate);
178
179 // write fields
180 w.paths(paths);
181 w.fingerprint(fingerprint);
182 w.peer(peer);
183
184 // reparse JSON
185 JsonNode r = mapper.readTree(w.node().toString());
186
187 sut = new ProtectionConfig();
188 sut.init(did, ProtectionConfig.CONFIG_KEY, r, mapper, delegate);
189
190 // verify equivalence
191 assertThat(sut.paths().size(), is(2));
192
193 TransportEndpointDescription readWorking = sut.paths().get(0);
194 assertThat(readWorking.isEnabled(), is(true));
195 assertThat(readWorking.output().connectPoint(), is(workingCp));
196 assertThat(readWorking.output().trafficSelector(), is(vlan100));
197
198 TransportEndpointDescription readBackup = sut.paths().get(1);
199 assertThat(readBackup.isEnabled(), is(true));
200 assertThat(readBackup.output().connectPoint(), is(backupCp));
201 assertThat(readBackup.output().trafficSelector(), is(vlan100));
202
203
204 assertThat(sut.fingerprint(), is(fingerprint));
205 assertThat(sut.peer(), is(peer));
206 }
207}