blob: 3e5daa5b4ae508a7bba92cd67b63edfc707c7cf8 [file] [log] [blame]
Charles Chan531a78b2015-12-01 10:00:51 -08001/*
2 * Copyright 2014-2015 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
17package org.onosproject.segmentrouting.config;
18
19import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.ObjectMapper;
21import org.junit.Before;
22import org.junit.Test;
23import org.onlab.packet.IpAddress;
24import org.onlab.packet.MacAddress;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.config.Config;
27import org.onosproject.net.config.ConfigApplyDelegate;
28
29import java.util.HashMap;
30import java.util.HashSet;
31import java.util.Map;
32import java.util.Set;
33
34import static org.junit.Assert.assertThat;
35import static org.hamcrest.Matchers.is;
36import static org.junit.Assert.assertTrue;
37
38/**
39 * Tests for class {@link SegmentRoutingConfig}.
40 */
41public class SegmentRoutingConfigTest {
42 private SegmentRoutingConfig config;
43 private Map<Integer, Set<Integer>> adjacencySids1;
44 private Map<Integer, Set<Integer>> adjacencySids2;
45
46 @Before
47 public void setUp() throws Exception {
48 String jsonString = "{" +
49 "\"name\" : \"Leaf-R1\"," +
50 "\"nodeSid\" : 101," +
51 "\"routerIp\" : \"10.0.1.254\"," +
52 "\"routerMac\" : \"00:00:00:00:01:80\"," +
53 "\"isEdgeRouter\" : true," +
54 "\"adjacencySids\" : [" +
55 " { \"adjSid\" : 100, \"ports\" : [2, 3] }," +
56 " { \"adjSid\" : 200, \"ports\" : [4, 5] }" +
57 "]}";
58
59 adjacencySids1 = new HashMap<>();
60 Set<Integer> ports1 = new HashSet<>();
61 ports1.add(2);
62 ports1.add(3);
63 adjacencySids1.put(100, ports1);
64 Set<Integer> ports2 = new HashSet<>();
65 ports2.add(4);
66 ports2.add(5);
67 adjacencySids1.put(200, ports2);
68
69 adjacencySids2 = new HashMap<>();
70 Set<Integer> ports3 = new HashSet<>();
71 ports3.add(6);
72 adjacencySids2.put(300, ports3);
73
74 DeviceId subject = DeviceId.deviceId("of:0000000000000001");
75 String key = "org.onosproject.segmentrouting";
76 ObjectMapper mapper = new ObjectMapper();
77 JsonNode jsonNode = mapper.readTree(jsonString);
78 ConfigApplyDelegate delegate = new MockDelegate();
79
80 config = new SegmentRoutingConfig();
81 config.init(subject, key, jsonNode, mapper, delegate);
82 }
83
84 @Test
85 public void testName() throws Exception {
86 assertTrue(config.name().isPresent());
87 assertThat(config.name().get(), is("Leaf-R1"));
88 }
89
90 @Test
91 public void testSetName() throws Exception {
92 config.setName("Spine-R1");
93 assertTrue(config.name().isPresent());
94 assertThat(config.name().get(), is("Spine-R1"));
95 }
96
97 @Test
98 public void testRouterIp() throws Exception {
99 assertThat(config.routerIp(), is(IpAddress.valueOf("10.0.1.254")));
100 }
101
102 @Test
103 public void testSetRouterIp() throws Exception {
104 config.setRouterIp("10.0.2.254");
105 assertThat(config.routerIp(), is(IpAddress.valueOf("10.0.2.254")));
106 }
107
108 @Test
109 public void testRouterMac() throws Exception {
110 assertThat(config.routerMac(), is(MacAddress.valueOf("00:00:00:00:01:80")));
111 }
112
113 @Test
114 public void testSetRouterMac() throws Exception {
115 config.setRouterMac("00:00:00:00:02:80");
116 assertThat(config.routerMac(), is(MacAddress.valueOf("00:00:00:00:02:80")));
117 }
118
119 @Test
120 public void testNodeSid() throws Exception {
121 assertThat(config.nodeSid(), is(101));
122 }
123
124 @Test
125 public void testSetNodeSid() throws Exception {
126 config.setNodeSid(200);
127 assertThat(config.nodeSid(), is(200));
128 }
129
130 @Test
131 public void testIsEdgeRouter() throws Exception {
132 assertThat(config.isEdgeRouter(), is(true));
133 }
134
135 @Test
136 public void testSetIsEdgeRouter() throws Exception {
137 config.setIsEdgeRouter(false);
138 assertThat(config.isEdgeRouter(), is(false));
139 }
140
141 @Test
142 public void testAdjacencySids() throws Exception {
143 assertThat(config.adjacencySids(), is(adjacencySids1));
144 }
145
146 @Test
147 public void testSetAdjacencySids() throws Exception {
148 config.setAdjacencySids(adjacencySids2);
149 assertThat(config.adjacencySids(), is(adjacencySids2));
150 }
151
152 private class MockDelegate implements ConfigApplyDelegate {
153 @Override
154 public void onApply(Config config) {
155 }
156 }
157}