Skip to content

Commit 5bae505

Browse files
William Daviespierventre
authored andcommitted
REST API for FPM Accept Routes
Change-Id: I2cd2f2ac5e7c7079ae87bfe32e76baeb97567e02
1 parent 2c5211d commit 5bae505

File tree

15 files changed

+607
-9
lines changed

15 files changed

+607
-9
lines changed

apps/routing/fpm/app/src/main/java/org/onosproject/routing/fpm/FpmConnectionInfo.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class FpmConnectionInfo {
2626
private final NodeId connectedTo;
2727
private final long connectTime;
2828
private final FpmPeer peer;
29+
private final boolean acceptRoutes;
2930

3031
/**
3132
* Creates a new connection info.
@@ -34,12 +35,30 @@ public class FpmConnectionInfo {
3435
* @param peer FPM peer
3536
* @param connectTime time the connection was made
3637
*/
38+
@Deprecated
3739
public FpmConnectionInfo(NodeId connectedTo, FpmPeer peer, long connectTime) {
3840
this.connectedTo = connectedTo;
3941
this.peer = peer;
4042
this.connectTime = connectTime;
43+
this.acceptRoutes = true;
4144
}
4245

46+
/**
47+
* Creates a new connection info.
48+
*
49+
* @param connectedTo ONOS node the FPM peer is connected to
50+
* @param peer FPM peer
51+
* @param connectTime time the connection was made
52+
* @param acceptRoutes flag to accept or discard routes
53+
*/
54+
FpmConnectionInfo(NodeId connectedTo, FpmPeer peer, long connectTime, boolean acceptRoutes) {
55+
this.connectedTo = connectedTo;
56+
this.peer = peer;
57+
this.connectTime = connectTime;
58+
this.acceptRoutes = acceptRoutes;
59+
}
60+
61+
4362
/**
4463
* Returns the node the FPM peers is connected to.
4564
*
@@ -66,4 +85,13 @@ public FpmPeer peer() {
6685
public long connectTime() {
6786
return connectTime;
6887
}
88+
89+
/**
90+
* Returns the acceptRoutes flag status of the peer.
91+
*
92+
* @return acceptRoutes flag
93+
*/
94+
public boolean isAcceptRoutes() {
95+
return acceptRoutes;
96+
}
6997
}

apps/routing/fpm/app/src/main/java/org/onosproject/routing/fpm/FpmInfoService.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616

1717
package org.onosproject.routing.fpm;
1818

19+
import java.util.Collection;
1920
import java.util.Map;
2021

22+
2123
/**
2224
* Provides information about the FPM route receiver module.
2325
*/
@@ -41,4 +43,12 @@ public interface FpmInfoService {
4143
* Pushes all local FPM routes to route store.
4244
*/
4345
void pushFpmRoutes();
46+
47+
/**
48+
* Updates the acceptRoute flag to either accept or discard routes for input peers address.
49+
*
50+
* @param peers peers for which flag is updated
51+
*/
52+
void updateAcceptRouteFlag(Collection<FpmPeerAcceptRoutes> peers);
53+
4454
}

apps/routing/fpm/app/src/main/java/org/onosproject/routing/fpm/FpmManager.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
import java.util.List;
8989
import java.util.Map;
9090
import java.util.Set;
91+
import java.util.Iterator;
9192
import java.util.concurrent.ConcurrentHashMap;
9293
import java.util.concurrent.ExecutorService;
9394
import java.util.concurrent.Executors;
@@ -688,6 +689,41 @@ public Map<FpmPeer, FpmPeerInfo> peers() {
688689
e -> toFpmInfo(e.getKey(), e.getValue())));
689690
}
690691

692+
@Override
693+
public void updateAcceptRouteFlag(Collection<FpmPeerAcceptRoutes> modifiedPeers) {
694+
modifiedPeers.forEach(modifiedPeer -> {
695+
log.debug("FPM connection to {} is disabled", modifiedPeer);
696+
NodeId localNode = clusterService.getLocalNode().id();
697+
log.debug("Peer Flag {}", modifiedPeer.isAcceptRoutes());
698+
peers.compute(modifiedPeer.peer(), (p, infos) -> {
699+
if (infos == null) {
700+
return null;
701+
}
702+
Iterator<FpmConnectionInfo> iterator = infos.iterator();
703+
if (iterator.hasNext()) {
704+
FpmConnectionInfo connectionInfo = iterator.next();
705+
if (connectionInfo.isAcceptRoutes() == modifiedPeer.isAcceptRoutes()) {
706+
return null;
707+
}
708+
localPeers.remove(modifiedPeer.peer());
709+
infos.remove(connectionInfo);
710+
infos.add(new FpmConnectionInfo(localNode, modifiedPeer.peer(),
711+
System.currentTimeMillis(), modifiedPeer.isAcceptRoutes()));
712+
localPeers.put(modifiedPeer.peer(), infos);
713+
}
714+
Map<IpPrefix, Route> routes = fpmRoutes.get(modifiedPeer.peer());
715+
if (routes != null && !modifiedPeer.isAcceptRoutes()) {
716+
updateRouteStore(Lists.newArrayList(), routes.values());
717+
} else {
718+
updateRouteStore(routes.values(), Lists.newArrayList());
719+
}
720+
721+
return infos;
722+
});
723+
});
724+
725+
}
726+
691727
private class InternalFpmListener implements FpmListener {
692728
@Override
693729
public void fpmMessage(FpmPeer peer, FpmHeader fpmMessage) {
@@ -707,7 +743,7 @@ public boolean peerConnected(FpmPeer peer) {
707743
infos = new HashSet<>();
708744
}
709745

710-
infos.add(new FpmConnectionInfo(localNode, peer, System.currentTimeMillis()));
746+
infos.add(new FpmConnectionInfo(localNode, peer, System.currentTimeMillis(), true));
711747
localPeers.put(peer, infos);
712748
return infos;
713749
});
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 2017-present Open Networking Foundation
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+
17+
package org.onosproject.routing.fpm;
18+
19+
import java.util.Objects;
20+
21+
import static com.google.common.base.MoreObjects.toStringHelper;
22+
23+
/**
24+
* Represents an FPM peer with accept routes flag.
25+
*/
26+
public class FpmPeerAcceptRoutes {
27+
28+
private final boolean isAcceptRoutes;
29+
private final FpmPeer peer;
30+
31+
32+
33+
/**
34+
* Creates a new FPM peer.
35+
*
36+
* @param peer Fpm Peer
37+
* @param isAcceptRoutes is route accepted on peer
38+
*
39+
*/
40+
public FpmPeerAcceptRoutes(FpmPeer peer, boolean isAcceptRoutes) {
41+
this.peer = peer;
42+
this.isAcceptRoutes = isAcceptRoutes;
43+
}
44+
45+
/**
46+
* Returns isAcceptRoutes flag status.
47+
*
48+
* @return isAcceptRoutes
49+
*/
50+
public boolean isAcceptRoutes() {
51+
return isAcceptRoutes;
52+
}
53+
54+
/**
55+
* Returns the FPM peer.
56+
*
57+
* @return FPM peer
58+
*/
59+
public FpmPeer peer() {
60+
return peer;
61+
}
62+
63+
@Override
64+
public int hashCode() {
65+
return Objects.hash(isAcceptRoutes, peer);
66+
}
67+
68+
@Override
69+
public boolean equals(Object other) {
70+
if (this == other) {
71+
return true;
72+
}
73+
74+
if (!(other instanceof FpmPeerAcceptRoutes)) {
75+
return false;
76+
}
77+
78+
FpmPeerAcceptRoutes that = (FpmPeerAcceptRoutes) other;
79+
80+
return Objects.equals(this.isAcceptRoutes, that.isAcceptRoutes) &&
81+
Objects.equals(this.peer, that.peer);
82+
}
83+
84+
@Override
85+
public String toString() {
86+
return toStringHelper(this)
87+
.add("peer", peer)
88+
.add("isAcceptRoutes", isAcceptRoutes)
89+
.toString();
90+
}
91+
92+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2017-present Open Networking Foundation
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+
17+
package org.onosproject.routing.fpm.cli;
18+
19+
import org.apache.karaf.shell.api.action.Argument;
20+
import org.apache.karaf.shell.api.action.Command;
21+
import org.apache.karaf.shell.api.action.lifecycle.Service;
22+
import org.onlab.packet.IpAddress;
23+
import org.onosproject.cli.AbstractShellCommand;
24+
import org.onosproject.routing.fpm.FpmInfoService;
25+
import org.onosproject.routing.fpm.FpmPeer;
26+
import org.onosproject.routing.fpm.FpmPeerInfo;
27+
28+
import java.util.Comparator;
29+
import java.util.Map;
30+
31+
/**
32+
* Displays the acceptRoute flag value for given peer.
33+
*/
34+
@Service
35+
@Command(scope = "onos", name = "fpm-get-accept-route",
36+
description = "Displays the acceptRoute flag value for given peer")
37+
public class FpmAcceptRoutesInfoCommand extends AbstractShellCommand {
38+
39+
private static final String FORMAT = "peer %s port %s acceptRoutes %s";
40+
41+
@Argument(index = 0, name = "peerAddress", description = "Peer Ip address",
42+
required = false, multiValued = false)
43+
String peerAddress = null;
44+
45+
46+
47+
@Override
48+
protected void doExecute() {
49+
FpmInfoService fpmInfo = get(FpmInfoService.class);
50+
if (peerAddress != null) {
51+
IpAddress address = IpAddress.valueOf(peerAddress);
52+
fpmInfo.peers().entrySet().stream()
53+
.filter(peer -> peer.getKey().address().equals(address))
54+
.map(Map.Entry::getValue)
55+
.forEach(this::print);
56+
} else {
57+
fpmInfo.peers().entrySet().stream()
58+
.sorted(Comparator.<Map.Entry<FpmPeer, FpmPeerInfo>, IpAddress>comparing(e -> e.getKey().address())
59+
.thenComparing(e -> e.getKey().port()))
60+
.map(Map.Entry::getValue)
61+
.forEach(this::print);
62+
}
63+
}
64+
65+
private void print(FpmPeerInfo info) {
66+
info.connections().forEach(cinfo ->
67+
print(FORMAT, cinfo.peer().address(),
68+
cinfo.peer().port(),
69+
cinfo.isAcceptRoutes())
70+
);
71+
}
72+
}

apps/routing/fpm/app/src/main/java/org/onosproject/routing/fpm/cli/FpmConnectionsList.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.onosproject.routing.fpm.cli;
1818

1919
import org.apache.karaf.shell.api.action.Command;
20+
import org.apache.karaf.shell.api.action.Argument;
2021
import org.apache.karaf.shell.api.action.lifecycle.Service;
2122
import org.onlab.packet.IpAddress;
2223
import org.onlab.util.Tools;
@@ -37,19 +38,32 @@
3738
description = "Displays the current FPM connections")
3839
public class FpmConnectionsList extends AbstractShellCommand {
3940

40-
private static final String FORMAT = "peer %s:%s connected to %s since %s %s (%d routes locally)";
41+
private static final String FORMAT = "peer %s:%s connected to %s since %s %s (%d routes locally) acceptRoutes %s";
42+
43+
@Argument(index = 0, name = "peerAddress", description = "Peer Ip address",
44+
required = false, multiValued = false)
45+
String peerAddress = null;
4146

4247
@Override
4348
protected void doExecute() {
4449
FpmInfoService fpmInfo = get(FpmInfoService.class);
4550

4651
print(String.format("PD Pushing is %s.", fpmInfo.isPdPushEnabled() ? "enabled" : "disabled"));
52+
if (peerAddress != null) {
53+
IpAddress address = IpAddress.valueOf(peerAddress);
54+
fpmInfo.peers().entrySet().stream()
55+
.filter(peer -> peer.getKey().address().equals(address))
56+
.map(Map.Entry::getValue)
57+
.forEach(this::print);
58+
} else {
59+
fpmInfo.peers().entrySet().stream()
60+
.sorted(Comparator.<Map.Entry<FpmPeer, FpmPeerInfo>, IpAddress>comparing(e -> e.getKey().address())
61+
.thenComparing(e -> e.getKey().port()))
62+
.map(Map.Entry::getValue)
63+
.forEach(this::print);
64+
}
65+
4766

48-
fpmInfo.peers().entrySet().stream()
49-
.sorted(Comparator.<Map.Entry<FpmPeer, FpmPeerInfo>, IpAddress>comparing(e -> e.getKey().address())
50-
.thenComparing(e -> e.getKey().port()))
51-
.map(Map.Entry::getValue)
52-
.forEach(this::print);
5367
}
5468

5569
private void print(FpmPeerInfo info) {
@@ -59,7 +73,8 @@ private void print(FpmPeerInfo info) {
5973
print(FORMAT, cinfo.peer().address(), cinfo.peer().port(),
6074
cinfo.connectedTo(), Tools.timeAgo(cinfo.connectTime()),
6175
cinfo.connectedTo().equals(clusterService.getLocalNode().id()) ? "*" : "",
62-
info.routes())
76+
info.routes(),
77+
cinfo.isAcceptRoutes())
6378
);
6479
}
6580
}

0 commit comments

Comments
 (0)