From 03974824abae35128f53f5a4af9a4ed8f573601a Mon Sep 17 00:00:00 2001 From: Ashlee Young Date: Thu, 12 Nov 2015 12:46:25 -0800 Subject: Updating onos src up to commit id fdb426e7ca9232692b0b6c335c79fc9c5a4e341f Change-Id: I78f29f15b39f965b61154d5f8d576ed1a9481b9b Signed-off-by: Ashlee Young --- .../org/onosproject/routing/config/BgpConfig.java | 167 +++++++++++++++++++++ 1 file changed, 167 insertions(+) (limited to 'framework/src/onos/apps/routing-api/src/main') 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 { Set speakers = Sets.newHashSet(); JsonNode speakersNode = object.get(SPEAKERS); + + if (speakersNode == null) { + return speakers; + } + speakersNode.forEach(jsonNode -> { Set listenAddresses = Sets.newHashSet(); jsonNode.path(PEERS).forEach(addressNode -> @@ -70,6 +79,130 @@ public class BgpConfig extends Config { return speakers; } + /** + * 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. */ @@ -97,5 +230,39 @@ public class BgpConfig extends Config { public Set 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); + } } } -- cgit 1.2.3-korg