blob: 6e170b0ffeef720e4757d6a69f3c66e55142e174 [file] [log] [blame]
Charles Chan1a3b02a2017-08-18 17:11:34 -07001/*
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
17package org.onosproject.segmentrouting;
18
19import com.google.common.collect.ImmutableSet;
pierf8e328d2019-06-28 22:17:31 +020020import org.onlab.packet.IpAddress;
21import org.onlab.packet.VlanId;
Charles Chan1a3b02a2017-08-18 17:11:34 -070022import org.onosproject.net.ConnectPoint;
23import org.onosproject.net.intf.Interface;
Charles Chan971d7ba2018-05-01 11:50:20 -070024import org.onosproject.net.intf.impl.InterfaceManager;
Charles Chan1a3b02a2017-08-18 17:11:34 -070025
26import java.util.Set;
27import java.util.stream.Collectors;
pierf8e328d2019-06-28 22:17:31 +020028import java.util.stream.Stream;
29
30import static java.util.stream.Collectors.toSet;
Charles Chan1a3b02a2017-08-18 17:11:34 -070031
32/**
33 * Mock Interface Service.
34 */
Charles Chan971d7ba2018-05-01 11:50:20 -070035public class MockInterfaceService extends InterfaceManager {
Charles Chan1a3b02a2017-08-18 17:11:34 -070036 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 Chan47933752017-11-30 15:37:50 -080047
48 @Override
49 public Set<Interface> getInterfaces() {
50 return interfaces;
51 }
pierf8e328d2019-06-28 22:17:31 +020052
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 Chan1a3b02a2017-08-18 17:11:34 -070085}