aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/apps/routing-api/src
diff options
context:
space:
mode:
Diffstat (limited to 'framework/src/onos/apps/routing-api/src')
-rw-r--r--framework/src/onos/apps/routing-api/src/main/java/org/onosproject/routing/config/BgpConfig.java167
-rw-r--r--framework/src/onos/apps/routing-api/src/test/java/org/onosproject/routing/config/BgpConfigTest.java244
2 files changed, 411 insertions, 0 deletions
diff --git a/framework/src/onos/apps/routing-api/src/main/java/org/onosproject/routing/config/BgpConfig.java b/framework/src/onos/apps/routing-api/src/main/java/org/onosproject/routing/config/BgpConfig.java
index 2f1ede79..6085c60a 100644
--- a/framework/src/onos/apps/routing-api/src/main/java/org/onosproject/routing/config/BgpConfig.java
+++ b/framework/src/onos/apps/routing-api/src/main/java/org/onosproject/routing/config/BgpConfig.java
@@ -17,12 +17,16 @@
package org.onosproject.routing.config;
import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.JsonNodeFactory;
+import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Sets;
import org.onlab.packet.IpAddress;
import org.onosproject.core.ApplicationId;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.config.Config;
+import java.util.Objects;
import java.util.Optional;
import java.util.Set;
@@ -49,6 +53,11 @@ public class BgpConfig extends Config<ApplicationId> {
Set<BgpSpeakerConfig> speakers = Sets.newHashSet();
JsonNode speakersNode = object.get(SPEAKERS);
+
+ if (speakersNode == null) {
+ return speakers;
+ }
+
speakersNode.forEach(jsonNode -> {
Set<IpAddress> listenAddresses = Sets.newHashSet();
jsonNode.path(PEERS).forEach(addressNode ->
@@ -71,6 +80,130 @@ public class BgpConfig extends Config<ApplicationId> {
}
/**
+ * Examines whether a name of BGP speaker exists in configuration.
+ *
+ * @param name name of BGP speaker being search
+ * @return speaker
+ */
+ public BgpSpeakerConfig getSpeakerWithName(String name) {
+ for (BgpConfig.BgpSpeakerConfig speaker : bgpSpeakers()) {
+ if (speaker.name().isPresent() && speaker.name().get().equals(name)) {
+ return speaker;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Adds BGP speaker to configuration.
+ *
+ * @param speaker BGP speaker configuration entry
+ */
+ public void addSpeaker(BgpSpeakerConfig speaker) {
+ ObjectNode speakerNode = JsonNodeFactory.instance.objectNode();
+
+ speakerNode.put(NAME, speaker.name().get());
+
+ speakerNode.put(CONNECT_POINT, speaker.connectPoint().elementId().toString()
+ + "/" + speaker.connectPoint().port().toString());
+
+ ArrayNode peersNode = speakerNode.putArray(PEERS);
+ for (IpAddress peerAddress: speaker.peers()) {
+ peersNode.add(peerAddress.toString());
+ }
+
+ ArrayNode speakersArray = bgpSpeakers().isEmpty() ?
+ initBgpConfiguration() : (ArrayNode) object.get(SPEAKERS);
+ speakersArray.add(speakerNode);
+ }
+
+ /**
+ * Removes BGP speaker from configuration.
+ *
+ * @param speakerName BGP speaker name
+ */
+ public void removeSpeaker(String speakerName) {
+ ArrayNode speakersArray = (ArrayNode) object.get(SPEAKERS);
+
+ for (int i = 0; i < speakersArray.size(); i++) {
+ if (speakersArray.get(i).hasNonNull(NAME) &&
+ speakersArray.get(i).get(NAME).asText().equals(speakerName)) {
+ speakersArray.remove(i);
+ return;
+ }
+ }
+ }
+
+ /**
+ * Adds peering address to BGP speaker.
+ *
+ * @param speakerName name of BGP speaker
+ * @param peerAddress peering address to be added
+ */
+ public void addPeerToSpeaker(String speakerName, IpAddress peerAddress) {
+ JsonNode speakersNode = object.get(SPEAKERS);
+ speakersNode.forEach(jsonNode -> {
+ if (jsonNode.hasNonNull(NAME) &&
+ jsonNode.get(NAME).asText().equals(speakerName)) {
+ ArrayNode peersNode = (ArrayNode) jsonNode.get(PEERS);
+ for (int i = 0; i < peersNode.size(); i++) {
+ if (peersNode.get(i).asText().equals(peerAddress.toString())) {
+ return; // Peer already exists.
+ }
+ }
+ peersNode.add(peerAddress.toString());
+ }
+ });
+ }
+
+ /**
+ * Finds BGP speaker peering with a given external peer.
+ *
+ * @param peerAddress peering address to be removed
+ * @return speaker
+ */
+ public BgpSpeakerConfig getSpeakerFromPeer(IpAddress peerAddress) {
+ for (BgpConfig.BgpSpeakerConfig speaker : bgpSpeakers()) {
+ if (speaker.peers().contains(peerAddress)) {
+ return speaker;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Removes peering address from BGP speaker.
+ *
+ * @param speaker BGP speaker configuration entries
+ * @param peerAddress peering address to be removed
+ */
+ public void removePeerFromSpeaker(BgpSpeakerConfig speaker, IpAddress peerAddress) {
+ JsonNode speakersNode = object.get(SPEAKERS);
+ speakersNode.forEach(jsonNode -> {
+ if (jsonNode.hasNonNull(NAME) &&
+ jsonNode.get(NAME).asText().equals(speaker.name().get())) {
+ ArrayNode peersNode = (ArrayNode) jsonNode.get(PEERS);
+ for (int i = 0; i < peersNode.size(); i++) {
+ if (peersNode.get(i).asText().equals(peerAddress.toString())) {
+ peersNode.remove(i);
+ return;
+ }
+ }
+ }
+ });
+ }
+
+ /**
+ * Creates empty configuration for BGP speakers.
+ *
+ * @return empty array of BGP speakers
+ */
+ private ArrayNode initBgpConfiguration() {
+ return object.putArray(SPEAKERS);
+ }
+
+
+ /**
* Configuration for a BGP speaker.
*/
public static class BgpSpeakerConfig {
@@ -97,5 +230,39 @@ public class BgpConfig extends Config<ApplicationId> {
public Set<IpAddress> peers() {
return peers;
}
+
+ /**
+ * Examines if BGP peer is connected.
+ *
+ * @param peer IP address of peer
+ * @return result of search
+ */
+ public boolean isConnectedToPeer(IpAddress peer) {
+ for (final IpAddress entry : peers()) {
+ if (entry.equals(peer)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj instanceof BgpSpeakerConfig) {
+ final BgpSpeakerConfig that = (BgpSpeakerConfig) obj;
+ return Objects.equals(this.name, that.name) &&
+ Objects.equals(this.connectPoint, that.connectPoint) &&
+ Objects.equals(this.peers, that.peers);
+ }
+ return false;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(name, connectPoint, peers);
+ }
}
}
diff --git a/framework/src/onos/apps/routing-api/src/test/java/org/onosproject/routing/config/BgpConfigTest.java b/framework/src/onos/apps/routing-api/src/test/java/org/onosproject/routing/config/BgpConfigTest.java
new file mode 100644
index 00000000..28f52b79
--- /dev/null
+++ b/framework/src/onos/apps/routing-api/src/test/java/org/onosproject/routing/config/BgpConfigTest.java
@@ -0,0 +1,244 @@
+/*
+ * Copyright 2014-2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.routing.config;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.IpAddress;
+import org.onosproject.TestApplicationId;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.config.Config;
+import org.onosproject.net.config.ConfigApplyDelegate;
+import org.onosproject.routing.RoutingService;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Optional;
+import java.util.Set;
+
+import static junit.framework.Assert.assertNotNull;
+import static junit.framework.Assert.assertNull;
+import static junit.framework.TestCase.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class BgpConfigTest {
+
+ private static final ApplicationId APP_ID =
+ new TestApplicationId(RoutingService.ROUTER_APP_ID);
+
+ private static final IpAddress IP1 = IpAddress.valueOf("10.0.1.1");
+ private static final IpAddress IP2 = IpAddress.valueOf("10.0.2.1");
+ private static final IpAddress IP3 = IpAddress.valueOf("10.0.3.1");
+ private static final IpAddress IP4 = IpAddress.valueOf("10.0.101.1");
+ private static final IpAddress IP5 = IpAddress.valueOf("10.0.201.1");
+ public static final IpAddress IP_NON_EXIST = IpAddress.valueOf("10.101.1.1");
+
+ public static final ConnectPoint CONNECT_POINT1 = ConnectPoint.
+ deviceConnectPoint("of:0000000000000001/1");
+ public static final ConnectPoint CONNECT_POINT2 = ConnectPoint.
+ deviceConnectPoint("of:00000000000000a3/1");
+
+ private static final String JSON_TREE = "{\"" + BgpConfig.SPEAKERS +
+ "\" : [{\"" + BgpConfig.NAME + "\" : \"bgp1\"," +
+ "\"" + BgpConfig.CONNECT_POINT +
+ "\" : \"of:0000000000000001/1\"," +
+ "\"" + BgpConfig.PEERS + "\" : [" +
+ "\"10.0.1.1\",\"10.0.2.1\",\"10.0.3.1\"]}]}";
+ private static final String EMPTY_JSON_TREE = "{}";
+
+ private final ObjectMapper mapper = new ObjectMapper();
+ private final ConfigApplyDelegate delegate = new MockCfgDelegate();
+ private final BgpConfig.BgpSpeakerConfig initialSpeaker = createInitialSpeaker();
+
+ private Set<BgpConfig.BgpSpeakerConfig> speakers = new HashSet<>();
+ private BgpConfig bgpConfig = new BgpConfig();
+ private BgpConfig emptyBgpConfig = new BgpConfig();
+
+ @Before
+ public void setUp() throws Exception {
+ JsonNode tree = new ObjectMapper().readTree(JSON_TREE);
+ bgpConfig.init(APP_ID, "bgp-test", tree, mapper, delegate);
+ JsonNode emptyTree = new ObjectMapper().readTree(EMPTY_JSON_TREE);
+ emptyBgpConfig.init(APP_ID, "bgp-test", emptyTree, mapper, delegate);
+ speakers.add(initialSpeaker);
+ }
+
+ /**
+ * Tests if speakers can be retrieved from JSON.
+ */
+ @Test
+ public void testBgpSpeakers() throws Exception {
+ assertEquals(speakers, bgpConfig.bgpSpeakers());
+ }
+
+ /**
+ * Tests if speakers can be retrieved from empty JSON.
+ */
+ @Test
+ public void testEmptyBgpSpeakers() throws Exception {
+ assertTrue(emptyBgpConfig.bgpSpeakers().isEmpty());
+ }
+
+ /**
+ * Tests if speaker can be found by name.
+ */
+ @Test
+ public void testGetSpeakerWithName() throws Exception {
+ assertNotNull(bgpConfig.getSpeakerWithName("bgp1"));
+ assertNull(bgpConfig.getSpeakerWithName("bgp2"));
+ }
+
+ /**
+ * Tests addition of new speaker.
+ */
+ @Test
+ public void testAddSpeaker() throws Exception {
+ int initialSize = bgpConfig.bgpSpeakers().size();
+ BgpConfig.BgpSpeakerConfig newSpeaker = createNewSpeaker();
+ bgpConfig.addSpeaker(newSpeaker);
+ assertEquals(initialSize + 1, bgpConfig.bgpSpeakers().size());
+ speakers.add(newSpeaker);
+ assertEquals(speakers, bgpConfig.bgpSpeakers());
+ }
+
+ /**
+ * Tests addition of new speaker to empty configuration.
+ */
+ @Test
+ public void testAddSpeakerToEmpty() throws Exception {
+ BgpConfig.BgpSpeakerConfig newSpeaker = createNewSpeaker();
+ emptyBgpConfig.addSpeaker(newSpeaker);
+
+ assertFalse(emptyBgpConfig.bgpSpeakers().isEmpty());
+ }
+
+ /**
+ * Tests removal of existing speaker.
+ */
+ @Test
+ public void testRemoveExistingSpeaker() throws Exception {
+ int initialSize = bgpConfig.bgpSpeakers().size();
+ bgpConfig.removeSpeaker("bgp1");
+
+ assertEquals(initialSize - 1, bgpConfig.bgpSpeakers().size());
+ }
+
+ /**
+ * Tests removal of non-existing speaker.
+ */
+ @Test
+ public void testRemoveInexistingSpeaker() throws Exception {
+ int initialSize = bgpConfig.bgpSpeakers().size();
+ bgpConfig.removeSpeaker("bgp2");
+
+ assertEquals(initialSize, bgpConfig.bgpSpeakers().size());
+ }
+
+ /**
+ * Tests addition of new speaker.
+ */
+ @Test
+ public void testAddPeerToSpeaker() throws Exception {
+ int initialSize = bgpConfig.getSpeakerWithName("bgp1").peers().size();
+ bgpConfig.addPeerToSpeaker("bgp1", IP4);
+
+ assertEquals(initialSize + 1, bgpConfig.getSpeakerWithName("bgp1").peers().size());
+ }
+
+ /**
+ * Tests addition of new speaker when peer already exists.
+ */
+ @Test
+ public void testAddExistingPeerToSpeaker() throws Exception {
+ int initialSize = bgpConfig.getSpeakerWithName("bgp1").peers().size();
+ bgpConfig.addPeerToSpeaker("bgp1", IP1);
+
+ assertEquals(initialSize, bgpConfig.getSpeakerWithName("bgp1").peers().size());
+ }
+
+ /**
+ * Tests retrieval of speaker based on peering address.
+ */
+ @Test
+ public void testGetSpeakerFromPeer() throws Exception {
+ assertNotNull(bgpConfig.getSpeakerFromPeer(IP1));
+ assertNull(bgpConfig.getSpeakerFromPeer(IP_NON_EXIST));
+ }
+
+ /**
+ * Tests removal of peer.
+ */
+ @Test
+ public void testRemoveExistingPeerFromSpeaker() throws Exception {
+ int initialSize = bgpConfig.getSpeakerWithName("bgp1").peers().size();
+ bgpConfig.removePeerFromSpeaker(initialSpeaker, IP1);
+
+ assertEquals(initialSize - 1, bgpConfig.getSpeakerWithName("bgp1").peers().size());
+ }
+
+ /**
+ * Tests peer removal when peer does not exist.
+ */
+ @Test
+ public void testRemoveNonExistingPeerFromSpeaker() throws Exception {
+ int initialSize = bgpConfig.getSpeakerWithName("bgp1").peers().size();
+ bgpConfig.removePeerFromSpeaker(initialSpeaker, IP_NON_EXIST);
+
+ assertEquals(initialSize, bgpConfig.getSpeakerWithName("bgp1").peers().size());
+ }
+
+ /**
+ * Tests if connections to peers are found.
+ */
+ @Test
+ public void testIsConnectedToPeer() {
+ BgpConfig.BgpSpeakerConfig speaker = createNewSpeaker();
+
+ assertTrue(speaker.isConnectedToPeer(IP4));
+ assertFalse(speaker.isConnectedToPeer(IP_NON_EXIST));
+ }
+
+ private class MockCfgDelegate implements ConfigApplyDelegate {
+
+ @Override
+ public void onApply(@SuppressWarnings("rawtypes") Config config) {
+ config.apply();
+ }
+
+ }
+
+ private BgpConfig.BgpSpeakerConfig createInitialSpeaker() {
+ Optional<String> speakerName = Optional.of("bgp1");
+ ConnectPoint connectPoint = CONNECT_POINT1;
+ Set<IpAddress> connectedPeers = new HashSet<>(Arrays.asList(IP1, IP2, IP3));
+
+ return new BgpConfig.BgpSpeakerConfig(speakerName, connectPoint, connectedPeers);
+ }
+
+ private BgpConfig.BgpSpeakerConfig createNewSpeaker() {
+ Optional<String> speakerName = Optional.of("newSpeaker");
+ ConnectPoint connectPoint = CONNECT_POINT2;
+ Set<IpAddress> connectedPeers = new HashSet<>(
+ Arrays.asList(IP4, IP5));
+
+ return new BgpConfig.BgpSpeakerConfig(speakerName, connectPoint, connectedPeers);
+ }
+}