blob: 8dedc82bd469cc057e645e92e7fbaa8301e48e8a [file] [log] [blame]
Andreas Papazoisc2c45012016-01-20 14:26:11 +02001/*
Andreas Papazoise6aebaa2016-05-26 15:25:51 +03002 * Copyright 2016-present Open Networking Laboratory
Andreas Papazoisc2c45012016-01-20 14:26:11 +02003 *
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
Andreas Papazoisf0ec25b2016-02-05 14:20:06 +020017package org.onosproject.sdxl3.config;
Andreas Papazoisc2c45012016-01-20 14:26:11 +020018
19import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.ObjectMapper;
21import com.google.common.collect.Sets;
22import org.junit.Before;
23import org.junit.Test;
24import org.onlab.packet.IpAddress;
25import org.onosproject.TestApplicationId;
26import org.onosproject.core.ApplicationId;
27import org.onosproject.net.ConnectPoint;
28import org.onosproject.net.config.Config;
29import org.onosproject.net.config.ConfigApplyDelegate;
Andreas Papazoisf0ec25b2016-02-05 14:20:06 +020030import org.onosproject.sdxl3.SdxL3;
Andreas Papazoisc2c45012016-01-20 14:26:11 +020031
32import java.util.HashSet;
33import java.util.Optional;
34import java.util.Set;
35
36import static junit.framework.TestCase.assertEquals;
37import static org.junit.Assert.assertFalse;
38
Andreas Papazoise6aebaa2016-05-26 15:25:51 +030039/**
40 * Unit tests for SdxParticipantsConfig class.
41 */
42public class SdxParticipantsConfigTest {
Andreas Papazoisc2c45012016-01-20 14:26:11 +020043 private static final ApplicationId APP_ID =
44 new TestApplicationId(SdxL3.SDX_L3_APP);
45 private static final String KEY = "key";
46
47 private static final String NAME1 = "peer1";
48 private static final String IP_STRING1 = "10.0.1.1";
49 private static final IpAddress IP1 = IpAddress.valueOf(IP_STRING1);
50 private static final String PORT_STRING1 = "of:00000000000000a1/1";
51 private static final ConnectPoint PORT1 =
52 ConnectPoint.deviceConnectPoint(PORT_STRING1);
53 private static final String INTF_NAME1 = "conn_point1";
54
55 private static final String IP_STRING2 = "10.0.2.1";
56 private static final IpAddress IP2 = IpAddress.valueOf(IP_STRING2);
57 private static final String PORT_STRING2 = "of:00000000000000a1/2";
58 private static final ConnectPoint PORT2 =
59 ConnectPoint.deviceConnectPoint(PORT_STRING2);
60 private static final String INTF_NAME2 = "conn_point2";
61
62 private static final String NAME3 = "new_peer";
63 private static final String IP_STRING3 = "10.0.3.1";
64 private static final String PORT_STRING3 = "of:00000000000000a1/3";
65 private static final ConnectPoint PORT3 =
66 ConnectPoint.deviceConnectPoint(PORT_STRING3);
67 private static final IpAddress IP3 = IpAddress.valueOf(IP_STRING3);
68 private static final String INTF_NAME3 = "conn_point3";
69
Andreas Papazoise6aebaa2016-05-26 15:25:51 +030070 private static final String JSON_TREE = "{\"" + SdxParticipantsConfig.PEERS + "\"" +
71 ": [{\"" + SdxParticipantsConfig.NAME + "\" : \"" + NAME1 + "\", " +
72 "\"" + SdxParticipantsConfig.IP + "\" : \"" + IP_STRING1 + "\", " +
73 "\"" + SdxParticipantsConfig.CONN_POINT + "\" : \"" + PORT_STRING1 + "\", " +
74 "\"" + SdxParticipantsConfig.INTF_NAME + "\" : \"" + INTF_NAME1 + "\"}, " +
75 "{ \"" + SdxParticipantsConfig.IP + "\" : \"" + IP_STRING2 + "\", " +
76 "\"" + SdxParticipantsConfig.CONN_POINT + "\" : \"" + PORT_STRING2 + "\", " +
77 "\"" + SdxParticipantsConfig.INTF_NAME + "\" : \"" + INTF_NAME2 + "\"}]}";
Andreas Papazoisc2c45012016-01-20 14:26:11 +020078
79 private static final String EMPTY_JSON_TREE = "{ }";
80
Andreas Papazoise6aebaa2016-05-26 15:25:51 +030081 private Set<SdxParticipantsConfig.PeerConfig> peers = new HashSet<>();
82 private SdxParticipantsConfig.PeerConfig peer1;
Andreas Papazoisc2c45012016-01-20 14:26:11 +020083
Andreas Papazoise6aebaa2016-05-26 15:25:51 +030084 private SdxParticipantsConfig peersConfig = new SdxParticipantsConfig();
85 private SdxParticipantsConfig emptyPeersConfig = new SdxParticipantsConfig();
Andreas Papazoisc2c45012016-01-20 14:26:11 +020086
87 private final ConfigApplyDelegate delegate = new MockCfgDelegate();
88 private final ObjectMapper mapper = new ObjectMapper();
89
90 @Before
91 public void setUp() throws Exception {
92 peers = createPeers();
93 JsonNode node = new ObjectMapper().readTree(JSON_TREE);
94 peersConfig.init(APP_ID, KEY, node, mapper, delegate);
95 JsonNode emptyTree = new ObjectMapper().readTree(EMPTY_JSON_TREE);
96 emptyPeersConfig.init(APP_ID, KEY, emptyTree, mapper, delegate);
97 }
98
99 /**
100 * Tests if peers can be retrieved from JSON.
101 */
102 @Test
103 public void testBgpPeers() throws Exception {
104 assertEquals(peers, peersConfig.bgpPeers());
105 }
106
107 /**
108 * Tests if interface name can be retrieved for given peer's IP.
109 */
110 @Test
111 public void testGetInterfaceNameForPeer() throws Exception {
112 assertEquals(INTF_NAME1, peersConfig.getInterfaceNameForPeer(IP1));
113 }
114
115 /**
116 * Tests addition of new peer.
117 */
118 @Test
119 public void testAddPeer() throws Exception {
120 int initialSize = peersConfig.bgpPeers().size();
Andreas Papazoise6aebaa2016-05-26 15:25:51 +0300121 SdxParticipantsConfig.PeerConfig newPeer = createNewPeer();
Andreas Papazoisc2c45012016-01-20 14:26:11 +0200122 peersConfig.addPeer(newPeer);
123 assertEquals(initialSize + 1, peersConfig.bgpPeers().size());
124 peers.add(newPeer);
125 assertEquals(peers, peersConfig.bgpPeers());
126 }
127
128 /**
129 * Tests addition of new peer to empty configuration.
130 */
131 @Test
132 public void testAddPeerToEmpty() throws Exception {
133 emptyPeersConfig.addPeer(createNewPeer());
134 assertFalse(emptyPeersConfig.bgpPeers().isEmpty());
135 }
136
137 /**
138 * Tests getting peer configuration based on given name.
139 */
140 @Test
141 public void testGetPeerForName() throws Exception {
142 assertEquals(peer1, peersConfig.getPeerForName(Optional.of(NAME1)));
143 }
144
145 /**
146 * Tests getting peer configuration based on given IP.
147 */
148 @Test
149 public void testGetPeerForIp() throws Exception {
150 assertEquals(peer1, peersConfig.getPeerForIp(IP1));
151 }
152
153 /**
154 * Tests removing peer details based on given IP.
155 */
156 @Test
157 public void testRemovePeer() throws Exception {
158 int initialSize = peersConfig.bgpPeers().size();
159 peersConfig.removePeer(IP1);
160 assertEquals(initialSize - 1, peersConfig.bgpPeers().size());
161 peers.remove(peer1);
162 assertEquals(peers, peersConfig.bgpPeers()); }
163
Andreas Papazoise6aebaa2016-05-26 15:25:51 +0300164 private Set<SdxParticipantsConfig.PeerConfig> createPeers() {
165 Set<SdxParticipantsConfig.PeerConfig> peers = Sets.newHashSet();
Andreas Papazoisc2c45012016-01-20 14:26:11 +0200166
Andreas Papazoise6aebaa2016-05-26 15:25:51 +0300167 peer1 = new SdxParticipantsConfig.
Andreas Papazoisc2c45012016-01-20 14:26:11 +0200168 PeerConfig(Optional.of(NAME1), IP1, PORT1, INTF_NAME1);
169 peers.add(peer1);
Andreas Papazoise6aebaa2016-05-26 15:25:51 +0300170 peers.add(new SdxParticipantsConfig.
Andreas Papazoisc2c45012016-01-20 14:26:11 +0200171 PeerConfig(Optional.empty(), IP2, PORT2, INTF_NAME2));
172
173 return peers;
174 }
175
Andreas Papazoise6aebaa2016-05-26 15:25:51 +0300176 private SdxParticipantsConfig.PeerConfig createNewPeer() {
177 return new SdxParticipantsConfig.
Andreas Papazoisc2c45012016-01-20 14:26:11 +0200178 PeerConfig(Optional.of(NAME3), IP3, PORT3, INTF_NAME3);
179 }
180
181 private class MockCfgDelegate implements ConfigApplyDelegate {
182
183 @Override
184 public void onApply(@SuppressWarnings("rawtypes") Config config) {
185 config.apply();
186 }
187
188 }
189}
190