Jonathan Hart
Committed by Thomas Vachuska

Add optional 'name' field in BGP speaker config

Change-Id: If6f4567cd1a7c29b0424cc84bef050efe392544c
...@@ -20,9 +20,10 @@ import com.fasterxml.jackson.databind.JsonNode; ...@@ -20,9 +20,10 @@ import com.fasterxml.jackson.databind.JsonNode;
20 import com.google.common.collect.Sets; 20 import com.google.common.collect.Sets;
21 import org.onlab.packet.IpAddress; 21 import org.onlab.packet.IpAddress;
22 import org.onosproject.core.ApplicationId; 22 import org.onosproject.core.ApplicationId;
23 -import org.onosproject.net.config.Config;
24 import org.onosproject.net.ConnectPoint; 23 import org.onosproject.net.ConnectPoint;
24 +import org.onosproject.net.config.Config;
25 25
26 +import java.util.Optional;
26 import java.util.Set; 27 import java.util.Set;
27 28
28 import static com.google.common.base.Preconditions.checkNotNull; 29 import static com.google.common.base.Preconditions.checkNotNull;
...@@ -34,6 +35,7 @@ public class BgpConfig extends Config<ApplicationId> { ...@@ -34,6 +35,7 @@ public class BgpConfig extends Config<ApplicationId> {
34 35
35 public static final String SPEAKERS = "bgpSpeakers"; 36 public static final String SPEAKERS = "bgpSpeakers";
36 public static final String CONNECT_POINT = "connectPoint"; 37 public static final String CONNECT_POINT = "connectPoint";
38 + public static final String NAME = "name";
37 public static final String PEERS = "peers"; 39 public static final String PEERS = "peers";
38 40
39 // TODO add methods for updating config 41 // TODO add methods for updating config
...@@ -52,7 +54,15 @@ public class BgpConfig extends Config<ApplicationId> { ...@@ -52,7 +54,15 @@ public class BgpConfig extends Config<ApplicationId> {
52 jsonNode.path(PEERS).forEach(addressNode -> 54 jsonNode.path(PEERS).forEach(addressNode ->
53 listenAddresses.add(IpAddress.valueOf(addressNode.asText())) 55 listenAddresses.add(IpAddress.valueOf(addressNode.asText()))
54 ); 56 );
55 - speakers.add(new BgpSpeakerConfig( 57 +
58 + Optional<String> name;
59 + if (jsonNode.get(NAME) == null) {
60 + name = Optional.empty();
61 + } else {
62 + name = Optional.of(jsonNode.get(NAME).asText());
63 + }
64 +
65 + speakers.add(new BgpSpeakerConfig(name,
56 ConnectPoint.deviceConnectPoint(jsonNode.path(CONNECT_POINT).asText()), 66 ConnectPoint.deviceConnectPoint(jsonNode.path(CONNECT_POINT).asText()),
57 listenAddresses)); 67 listenAddresses));
58 }); 68 });
...@@ -65,14 +75,21 @@ public class BgpConfig extends Config<ApplicationId> { ...@@ -65,14 +75,21 @@ public class BgpConfig extends Config<ApplicationId> {
65 */ 75 */
66 public static class BgpSpeakerConfig { 76 public static class BgpSpeakerConfig {
67 77
78 + private Optional<String> name;
68 private ConnectPoint connectPoint; 79 private ConnectPoint connectPoint;
69 private Set<IpAddress> peers; 80 private Set<IpAddress> peers;
70 81
71 - public BgpSpeakerConfig(ConnectPoint connectPoint, Set<IpAddress> peers) { 82 + public BgpSpeakerConfig(Optional<String> name, ConnectPoint connectPoint,
83 + Set<IpAddress> peers) {
84 + this.name = checkNotNull(name);
72 this.connectPoint = checkNotNull(connectPoint); 85 this.connectPoint = checkNotNull(connectPoint);
73 this.peers = checkNotNull(peers); 86 this.peers = checkNotNull(peers);
74 } 87 }
75 88
89 + public Optional<String> name() {
90 + return name;
91 + }
92 +
76 public ConnectPoint connectPoint() { 93 public ConnectPoint connectPoint() {
77 return connectPoint; 94 return connectPoint;
78 } 95 }
......
...@@ -16,14 +16,20 @@ ...@@ -16,14 +16,20 @@
16 16
17 package org.onosproject.routing.cli; 17 package org.onosproject.routing.cli;
18 18
19 +import com.google.common.collect.Lists;
19 import org.apache.karaf.shell.commands.Command; 20 import org.apache.karaf.shell.commands.Command;
20 import org.onosproject.cli.AbstractShellCommand; 21 import org.onosproject.cli.AbstractShellCommand;
22 +import org.onosproject.cli.Comparators;
21 import org.onosproject.core.ApplicationId; 23 import org.onosproject.core.ApplicationId;
22 import org.onosproject.core.CoreService; 24 import org.onosproject.core.CoreService;
23 import org.onosproject.net.config.NetworkConfigService; 25 import org.onosproject.net.config.NetworkConfigService;
24 import org.onosproject.routing.RoutingService; 26 import org.onosproject.routing.RoutingService;
25 import org.onosproject.routing.config.BgpConfig; 27 import org.onosproject.routing.config.BgpConfig;
26 28
29 +import java.util.Collections;
30 +import java.util.Comparator;
31 +import java.util.List;
32 +
27 /** 33 /**
28 * Lists the BGP speakers configured in the system. 34 * Lists the BGP speakers configured in the system.
29 */ 35 */
...@@ -31,7 +37,11 @@ import org.onosproject.routing.config.BgpConfig; ...@@ -31,7 +37,11 @@ import org.onosproject.routing.config.BgpConfig;
31 description = "Lists all BGP speakers") 37 description = "Lists all BGP speakers")
32 public class BgpSpeakersListCommand extends AbstractShellCommand { 38 public class BgpSpeakersListCommand extends AbstractShellCommand {
33 39
34 - private static final String FORMAT = "%s : %s"; 40 + private static final String FORMAT = "port=%s/%s, peers=%s";
41 + private static final String NAME_FORMAT = "%s: " + FORMAT;
42 +
43 + private static final Comparator<BgpConfig.BgpSpeakerConfig> SPEAKERS_COMPARATOR = (s1, s2) ->
44 + Comparators.CONNECT_POINT_COMPARATOR.compare(s1.connectPoint(), s2.connectPoint());
35 45
36 @Override 46 @Override
37 protected void execute() { 47 protected void execute() {
...@@ -39,15 +49,26 @@ public class BgpSpeakersListCommand extends AbstractShellCommand { ...@@ -39,15 +49,26 @@ public class BgpSpeakersListCommand extends AbstractShellCommand {
39 CoreService coreService = get(CoreService.class); 49 CoreService coreService = get(CoreService.class);
40 ApplicationId appId = coreService.getAppId(RoutingService.ROUTER_APP_ID); 50 ApplicationId appId = coreService.getAppId(RoutingService.ROUTER_APP_ID);
41 51
42 - print(appId.toString());
43 -
44 BgpConfig config = configService.getConfig(appId, BgpConfig.class); 52 BgpConfig config = configService.getConfig(appId, BgpConfig.class);
45 53
54 + List<BgpConfig.BgpSpeakerConfig> bgpSpeakers =
55 + Lists.newArrayList(config.bgpSpeakers());
56 +
57 + Collections.sort(bgpSpeakers, SPEAKERS_COMPARATOR);
58 +
46 if (config == null || config.bgpSpeakers().isEmpty()) { 59 if (config == null || config.bgpSpeakers().isEmpty()) {
47 print("No speakers configured"); 60 print("No speakers configured");
48 } else { 61 } else {
49 - config.bgpSpeakers().forEach( 62 + bgpSpeakers.forEach(
50 - s -> print(FORMAT, s.connectPoint(), s.peers())); 63 + s -> {
64 + if (s.name().isPresent()) {
65 + print(NAME_FORMAT, s.name().get(), s.connectPoint().deviceId(),
66 + s.connectPoint().port(), s.peers());
67 + } else {
68 + print(FORMAT, s.connectPoint().deviceId(),
69 + s.connectPoint().port(), s.peers());
70 + }
71 + });
51 } 72 }
52 } 73 }
53 } 74 }
......
...@@ -56,6 +56,7 @@ import java.util.HashMap; ...@@ -56,6 +56,7 @@ import java.util.HashMap;
56 import java.util.LinkedList; 56 import java.util.LinkedList;
57 import java.util.List; 57 import java.util.List;
58 import java.util.Map; 58 import java.util.Map;
59 +import java.util.Optional;
59 import java.util.Set; 60 import java.util.Set;
60 61
61 import static org.easymock.EasyMock.createMock; 62 import static org.easymock.EasyMock.createMock;
...@@ -149,9 +150,11 @@ public class PeerConnectivityManagerTest extends AbstractIntentTest { ...@@ -149,9 +150,11 @@ public class PeerConnectivityManagerTest extends AbstractIntentTest {
149 private Set<BgpConfig.BgpSpeakerConfig> setUpBgpSpeakers() { 150 private Set<BgpConfig.BgpSpeakerConfig> setUpBgpSpeakers() {
150 151
151 BgpConfig.BgpSpeakerConfig speaker1 = new BgpConfig.BgpSpeakerConfig( 152 BgpConfig.BgpSpeakerConfig speaker1 = new BgpConfig.BgpSpeakerConfig(
153 + Optional.empty(),
152 s1Eth100, Collections.singleton(IpAddress.valueOf("192.168.10.1"))); 154 s1Eth100, Collections.singleton(IpAddress.valueOf("192.168.10.1")));
153 155
154 BgpConfig.BgpSpeakerConfig speaker2 = new BgpConfig.BgpSpeakerConfig( 156 BgpConfig.BgpSpeakerConfig speaker2 = new BgpConfig.BgpSpeakerConfig(
157 + Optional.empty(),
155 s1Eth100, Sets.newHashSet(IpAddress.valueOf("192.168.20.1"), 158 s1Eth100, Sets.newHashSet(IpAddress.valueOf("192.168.20.1"),
156 IpAddress.valueOf("192.168.30.1"))); 159 IpAddress.valueOf("192.168.30.1")));
157 160
......
...@@ -96,7 +96,7 @@ public abstract class Config<S> { ...@@ -96,7 +96,7 @@ public abstract class Config<S> {
96 * @return JSON node backing the configuration 96 * @return JSON node backing the configuration
97 */ 97 */
98 public JsonNode node() { 98 public JsonNode node() {
99 - return object; 99 + return node;
100 } 100 }
101 101
102 /** 102 /**
......