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 com.google.common.collect.ImmutableSet; |
pier | 6a2052b | 2019-06-28 22:17:31 +0200 | [diff] [blame] | 20 | import org.onlab.packet.IpAddress; |
| 21 | import org.onlab.packet.VlanId; |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 22 | import org.onosproject.net.ConnectPoint; |
| 23 | import org.onosproject.net.intf.Interface; |
Charles Chan | 098ca20 | 2018-05-01 11:50:20 -0700 | [diff] [blame] | 24 | import org.onosproject.net.intf.impl.InterfaceManager; |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 25 | |
| 26 | import java.util.Set; |
| 27 | import java.util.stream.Collectors; |
pier | 6a2052b | 2019-06-28 22:17:31 +0200 | [diff] [blame] | 28 | import java.util.stream.Stream; |
| 29 | |
| 30 | import static java.util.stream.Collectors.toSet; |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Mock Interface Service. |
| 34 | */ |
Charles Chan | 098ca20 | 2018-05-01 11:50:20 -0700 | [diff] [blame] | 35 | public class MockInterfaceService extends InterfaceManager { |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 36 | private Set<Interface> interfaces; |
| 37 | |
| 38 | MockInterfaceService(Set<Interface> interfaces) { |
| 39 | this.interfaces = ImmutableSet.copyOf(interfaces); |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public Set<Interface> getInterfacesByPort(ConnectPoint cp) { |
| 44 | return interfaces.stream().filter(intf -> cp.equals(intf.connectPoint())) |
| 45 | .collect(Collectors.toSet()); |
| 46 | } |
Charles Chan | 873661e | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 47 | |
| 48 | @Override |
| 49 | public Set<Interface> getInterfaces() { |
| 50 | return interfaces; |
| 51 | } |
pier | 6a2052b | 2019-06-28 22:17:31 +0200 | [diff] [blame] | 52 | |
| 53 | @Override |
| 54 | public Interface getMatchingInterface(IpAddress ip) { |
| 55 | return getMatchingInterfacesStream(ip).findFirst().orElse(null); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public Set<Interface> getMatchingInterfaces(IpAddress ip) { |
| 60 | return getMatchingInterfacesStream(ip).collect(toSet()); |
| 61 | } |
| 62 | |
| 63 | private Stream<Interface> getMatchingInterfacesStream(IpAddress ip) { |
| 64 | return interfaces.stream() |
| 65 | .filter(intf -> intf.ipAddressesList().stream() |
| 66 | .anyMatch(intfIp -> intfIp.subnetAddress().contains(ip))); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public boolean isConfigured(ConnectPoint connectPoint) { |
| 71 | Set<Interface> intfs = getInterfacesByPort(connectPoint); |
| 72 | if (intfs == null) { |
| 73 | return false; |
| 74 | } |
| 75 | for (Interface intf : intfs) { |
| 76 | if (!intf.ipAddressesList().isEmpty() || intf.vlan() != VlanId.NONE |
| 77 | || intf.vlanNative() != VlanId.NONE |
| 78 | || intf.vlanUntagged() != VlanId.NONE |
| 79 | || !intf.vlanTagged().isEmpty()) { |
| 80 | return true; |
| 81 | } |
| 82 | } |
| 83 | return false; |
| 84 | } |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 85 | } |