Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 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.segmentrouting; |
| 18 | |
| 19 | import org.onlab.packet.IpPrefix; |
| 20 | import org.onosproject.net.ConnectPoint; |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 21 | import org.onosproject.net.DeviceId; |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 22 | |
| 23 | import java.util.Map; |
| 24 | import java.util.Set; |
| 25 | |
| 26 | /** |
| 27 | * Mock Default Routing Handler. |
| 28 | */ |
| 29 | public class MockDefaultRoutingHandler extends DefaultRoutingHandler { |
| 30 | private Map<ConnectPoint, Set<IpPrefix>> subnetTable; |
Charles Chan | c4d6888 | 2018-03-15 16:41:10 -0700 | [diff] [blame] | 31 | private Map<MockRoutingTableKey, MockRoutingTableValue> routingTable; |
pierventre | 37dcf4c | 2021-09-16 18:43:06 +0200 | [diff] [blame] | 32 | private Set<DeviceId> ledDevices; |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 33 | |
| 34 | MockDefaultRoutingHandler(SegmentRoutingManager srManager, |
Charles Chan | c4d6888 | 2018-03-15 16:41:10 -0700 | [diff] [blame] | 35 | Map<ConnectPoint, Set<IpPrefix>> subnetTable, |
pierventre | 37dcf4c | 2021-09-16 18:43:06 +0200 | [diff] [blame] | 36 | Map<MockRoutingTableKey, MockRoutingTableValue> routingTable, |
| 37 | Set<DeviceId> ledDevices) { |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 38 | super(srManager); |
| 39 | this.subnetTable = subnetTable; |
Charles Chan | c4d6888 | 2018-03-15 16:41:10 -0700 | [diff] [blame] | 40 | this.routingTable = routingTable; |
pierventre | 37dcf4c | 2021-09-16 18:43:06 +0200 | [diff] [blame] | 41 | this.ledDevices = ledDevices; |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | @Override |
| 45 | protected void populateSubnet(Set<ConnectPoint> cpts, Set<IpPrefix> subnets) { |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 46 | subnetTable.forEach((k, v) -> { |
| 47 | if (!cpts.contains(k)) { |
| 48 | subnetTable.get(k).removeAll(subnets); |
| 49 | if (subnetTable.get(k).isEmpty()) { |
| 50 | subnetTable.remove(k); |
| 51 | } |
| 52 | } |
| 53 | }); |
| 54 | |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 55 | cpts.forEach(cpt -> subnetTable.put(cpt, subnets)); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | protected boolean revokeSubnet(Set<IpPrefix> subnets) { |
| 60 | for (Map.Entry<ConnectPoint, Set<IpPrefix>> entry : subnetTable.entrySet()) { |
| 61 | entry.getValue().removeAll(subnets); |
| 62 | if (entry.getValue().isEmpty()) { |
| 63 | subnetTable.remove(entry.getKey()); |
| 64 | } |
| 65 | } |
Charles Chan | c4d6888 | 2018-03-15 16:41:10 -0700 | [diff] [blame] | 66 | routingTable.entrySet().removeIf(e -> subnets.contains(e.getKey().ipPrefix)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 67 | return true; |
| 68 | } |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 69 | |
| 70 | @Override |
pierventre | 37dcf4c | 2021-09-16 18:43:06 +0200 | [diff] [blame] | 71 | public boolean shouldProgram(DeviceId deviceId) { |
| 72 | return ledDevices.contains(deviceId); |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 73 | } |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 74 | } |