Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
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 | |
| 39 | public class SdxProvidersConfigTest { |
| 40 | private static final ApplicationId APP_ID = |
| 41 | new TestApplicationId(SdxL3.SDX_L3_APP); |
| 42 | private static final String KEY = "key"; |
| 43 | |
| 44 | private static final String NAME1 = "peer1"; |
| 45 | private static final String IP_STRING1 = "10.0.1.1"; |
| 46 | private static final IpAddress IP1 = IpAddress.valueOf(IP_STRING1); |
| 47 | private static final String PORT_STRING1 = "of:00000000000000a1/1"; |
| 48 | private static final ConnectPoint PORT1 = |
| 49 | ConnectPoint.deviceConnectPoint(PORT_STRING1); |
| 50 | private static final String INTF_NAME1 = "conn_point1"; |
| 51 | |
| 52 | private static final String IP_STRING2 = "10.0.2.1"; |
| 53 | private static final IpAddress IP2 = IpAddress.valueOf(IP_STRING2); |
| 54 | private static final String PORT_STRING2 = "of:00000000000000a1/2"; |
| 55 | private static final ConnectPoint PORT2 = |
| 56 | ConnectPoint.deviceConnectPoint(PORT_STRING2); |
| 57 | private static final String INTF_NAME2 = "conn_point2"; |
| 58 | |
| 59 | private static final String NAME3 = "new_peer"; |
| 60 | private static final String IP_STRING3 = "10.0.3.1"; |
| 61 | private static final String PORT_STRING3 = "of:00000000000000a1/3"; |
| 62 | private static final ConnectPoint PORT3 = |
| 63 | ConnectPoint.deviceConnectPoint(PORT_STRING3); |
| 64 | private static final IpAddress IP3 = IpAddress.valueOf(IP_STRING3); |
| 65 | private static final String INTF_NAME3 = "conn_point3"; |
| 66 | |
| 67 | private static final String JSON_TREE = "{\"" + SdxProvidersConfig.PEERS + "\"" + |
| 68 | ": [{\"" + SdxProvidersConfig.NAME + "\" : \"" + NAME1 + "\", " + |
| 69 | "\"" + SdxProvidersConfig.IP + "\" : \"" + IP_STRING1 + "\", " + |
| 70 | "\"" + SdxProvidersConfig.CONN_POINT + "\" : \"" + PORT_STRING1 + "\", " + |
| 71 | "\"" + SdxProvidersConfig.INTF_NAME + "\" : \"" + INTF_NAME1 + "\"}, " + |
| 72 | "{ \"" + SdxProvidersConfig.IP + "\" : \"" + IP_STRING2 + "\", " + |
| 73 | "\"" + SdxProvidersConfig.CONN_POINT + "\" : \"" + PORT_STRING2 + "\", " + |
| 74 | "\"" + SdxProvidersConfig.INTF_NAME + "\" : \"" + INTF_NAME2 + "\"}]}"; |
| 75 | |
| 76 | private static final String EMPTY_JSON_TREE = "{ }"; |
| 77 | |
| 78 | private Set<SdxProvidersConfig.PeerConfig> peers = new HashSet<>(); |
| 79 | private SdxProvidersConfig.PeerConfig peer1; |
| 80 | |
| 81 | private SdxProvidersConfig peersConfig = new SdxProvidersConfig(); |
| 82 | private SdxProvidersConfig emptyPeersConfig = new SdxProvidersConfig(); |
| 83 | |
| 84 | private final ConfigApplyDelegate delegate = new MockCfgDelegate(); |
| 85 | private final ObjectMapper mapper = new ObjectMapper(); |
| 86 | |
| 87 | @Before |
| 88 | public void setUp() throws Exception { |
| 89 | peers = createPeers(); |
| 90 | JsonNode node = new ObjectMapper().readTree(JSON_TREE); |
| 91 | peersConfig.init(APP_ID, KEY, node, mapper, delegate); |
| 92 | JsonNode emptyTree = new ObjectMapper().readTree(EMPTY_JSON_TREE); |
| 93 | emptyPeersConfig.init(APP_ID, KEY, emptyTree, mapper, delegate); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Tests if peers can be retrieved from JSON. |
| 98 | */ |
| 99 | @Test |
| 100 | public void testBgpPeers() throws Exception { |
| 101 | assertEquals(peers, peersConfig.bgpPeers()); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Tests if interface name can be retrieved for given peer's IP. |
| 106 | */ |
| 107 | @Test |
| 108 | public void testGetInterfaceNameForPeer() throws Exception { |
| 109 | assertEquals(INTF_NAME1, peersConfig.getInterfaceNameForPeer(IP1)); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Tests addition of new peer. |
| 114 | */ |
| 115 | @Test |
| 116 | public void testAddPeer() throws Exception { |
| 117 | int initialSize = peersConfig.bgpPeers().size(); |
| 118 | SdxProvidersConfig.PeerConfig newPeer = createNewPeer(); |
| 119 | peersConfig.addPeer(newPeer); |
| 120 | assertEquals(initialSize + 1, peersConfig.bgpPeers().size()); |
| 121 | peers.add(newPeer); |
| 122 | assertEquals(peers, peersConfig.bgpPeers()); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Tests addition of new peer to empty configuration. |
| 127 | */ |
| 128 | @Test |
| 129 | public void testAddPeerToEmpty() throws Exception { |
| 130 | emptyPeersConfig.addPeer(createNewPeer()); |
| 131 | assertFalse(emptyPeersConfig.bgpPeers().isEmpty()); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Tests getting peer configuration based on given name. |
| 136 | */ |
| 137 | @Test |
| 138 | public void testGetPeerForName() throws Exception { |
| 139 | assertEquals(peer1, peersConfig.getPeerForName(Optional.of(NAME1))); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Tests getting peer configuration based on given IP. |
| 144 | */ |
| 145 | @Test |
| 146 | public void testGetPeerForIp() throws Exception { |
| 147 | assertEquals(peer1, peersConfig.getPeerForIp(IP1)); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Tests removing peer details based on given IP. |
| 152 | */ |
| 153 | @Test |
| 154 | public void testRemovePeer() throws Exception { |
| 155 | int initialSize = peersConfig.bgpPeers().size(); |
| 156 | peersConfig.removePeer(IP1); |
| 157 | assertEquals(initialSize - 1, peersConfig.bgpPeers().size()); |
| 158 | peers.remove(peer1); |
| 159 | assertEquals(peers, peersConfig.bgpPeers()); } |
| 160 | |
| 161 | private Set<SdxProvidersConfig.PeerConfig> createPeers() { |
| 162 | Set<SdxProvidersConfig.PeerConfig> peers = Sets.newHashSet(); |
| 163 | |
| 164 | peer1 = new SdxProvidersConfig. |
| 165 | PeerConfig(Optional.of(NAME1), IP1, PORT1, INTF_NAME1); |
| 166 | peers.add(peer1); |
| 167 | peers.add(new SdxProvidersConfig. |
| 168 | PeerConfig(Optional.empty(), IP2, PORT2, INTF_NAME2)); |
| 169 | |
| 170 | return peers; |
| 171 | } |
| 172 | |
| 173 | private SdxProvidersConfig.PeerConfig createNewPeer() { |
| 174 | return new SdxProvidersConfig. |
| 175 | PeerConfig(Optional.of(NAME3), IP3, PORT3, INTF_NAME3); |
| 176 | } |
| 177 | |
| 178 | private class MockCfgDelegate implements ConfigApplyDelegate { |
| 179 | |
| 180 | @Override |
| 181 | public void onApply(@SuppressWarnings("rawtypes") Config config) { |
| 182 | config.apply(); |
| 183 | } |
| 184 | |
| 185 | } |
| 186 | } |
| 187 | |