Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 1 | /* |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Laboratory |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 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 | |
Andreas Papazois | f0ec25b | 2016-02-05 14:20:06 +0200 | [diff] [blame] | 17 | package org.onosproject.sdxl3.config; |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | import com.google.common.collect.Sets; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onosproject.TestApplicationId; |
| 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.net.ConnectPoint; |
| 28 | import org.onosproject.net.config.Config; |
| 29 | import org.onosproject.net.config.ConfigApplyDelegate; |
Andreas Papazois | f0ec25b | 2016-02-05 14:20:06 +0200 | [diff] [blame] | 30 | import org.onosproject.sdxl3.SdxL3; |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 31 | |
| 32 | import java.util.HashSet; |
| 33 | import java.util.Optional; |
| 34 | import java.util.Set; |
| 35 | |
| 36 | import static junit.framework.TestCase.assertEquals; |
| 37 | import static org.junit.Assert.assertFalse; |
| 38 | |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame^] | 39 | /** |
| 40 | * Unit tests for SdxParticipantsConfig class. |
| 41 | */ |
| 42 | public class SdxParticipantsConfigTest { |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 43 | 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 Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame^] | 70 | 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 Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 78 | |
| 79 | private static final String EMPTY_JSON_TREE = "{ }"; |
| 80 | |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame^] | 81 | private Set<SdxParticipantsConfig.PeerConfig> peers = new HashSet<>(); |
| 82 | private SdxParticipantsConfig.PeerConfig peer1; |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 83 | |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame^] | 84 | private SdxParticipantsConfig peersConfig = new SdxParticipantsConfig(); |
| 85 | private SdxParticipantsConfig emptyPeersConfig = new SdxParticipantsConfig(); |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 86 | |
| 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 Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame^] | 121 | SdxParticipantsConfig.PeerConfig newPeer = createNewPeer(); |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 122 | 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 Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame^] | 164 | private Set<SdxParticipantsConfig.PeerConfig> createPeers() { |
| 165 | Set<SdxParticipantsConfig.PeerConfig> peers = Sets.newHashSet(); |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 166 | |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame^] | 167 | peer1 = new SdxParticipantsConfig. |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 168 | PeerConfig(Optional.of(NAME1), IP1, PORT1, INTF_NAME1); |
| 169 | peers.add(peer1); |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame^] | 170 | peers.add(new SdxParticipantsConfig. |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 171 | PeerConfig(Optional.empty(), IP2, PORT2, INTF_NAME2)); |
| 172 | |
| 173 | return peers; |
| 174 | } |
| 175 | |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame^] | 176 | private SdxParticipantsConfig.PeerConfig createNewPeer() { |
| 177 | return new SdxParticipantsConfig. |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 178 | 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 | |