blob: 2cbea66e5577495461171661f479c4f26335f11f [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
2* Copyright 2012, Big Switch Networks, Inc.
3* Originally created by David Erickson, Stanford University
4*
5* Licensed under the Apache License, Version 2.0 (the "License"); you may
6* not use this file except in compliance with the License. You may obtain
7* a copy of the License at
8*
9* http://www.apache.org/licenses/LICENSE-2.0
10*
11* Unless required by applicable law or agreed to in writing, software
12* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14* License for the specific language governing permissions and limitations
15* under the License.
16**/
17
18package net.floodlightcontroller.devicemanager.internal;
19
20import java.util.Arrays;
21import java.util.Iterator;
22
23import net.floodlightcontroller.devicemanager.IEntityClass;
24import net.floodlightcontroller.devicemanager.SwitchPort;
25import net.floodlightcontroller.util.FilterIterator;
26
27/**
28 * An iterator for handling device queries
29 */
30public class DeviceIterator extends FilterIterator<Device> {
31 private IEntityClass[] entityClasses;
32
33 private Long macAddress;
34 private Short vlan;
35 private Integer ipv4Address;
36 private Long switchDPID;
37 private Integer switchPort;
38
39 /**
40 * Construct a new device iterator over the key fields
41 * @param subIterator an iterator over the full data structure to scan
42 * @param entityClasses the entity classes to search for
43 * @param macAddress The MAC address
44 * @param vlan the VLAN
45 * @param ipv4Address the ipv4 address
46 * @param switchDPID the switch DPID
47 * @param switchPort the switch port
48 */
49 public DeviceIterator(Iterator<Device> subIterator,
50 IEntityClass[] entityClasses,
51 Long macAddress,
52 Short vlan,
53 Integer ipv4Address,
54 Long switchDPID,
55 Integer switchPort) {
56 super(subIterator);
57 this.entityClasses = entityClasses;
58 this.subIterator = subIterator;
59 this.macAddress = macAddress;
60 this.vlan = vlan;
61 this.ipv4Address = ipv4Address;
62 this.switchDPID = switchDPID;
63 this.switchPort = switchPort;
64 }
65
66 @Override
67 protected boolean matches(Device value) {
68 boolean match;
69 if (entityClasses != null) {
70 IEntityClass clazz = value.getEntityClass();
71 if (clazz == null) return false;
72
73 match = false;
74 for (IEntityClass entityClass : entityClasses) {
75 if (clazz.equals(entityClass)) {
76 match = true;
77 break;
78 }
79 }
80 if (!match) return false;
81 }
82 if (macAddress != null) {
83 if (macAddress.longValue() != value.getMACAddress())
84 return false;
85 }
86 if (vlan != null) {
87 Short[] vlans = value.getVlanId();
88 if (Arrays.binarySearch(vlans, vlan) < 0)
89 return false;
90 }
91 if (ipv4Address != null) {
92 Integer[] ipv4Addresses = value.getIPv4Addresses();
93 if (Arrays.binarySearch(ipv4Addresses, ipv4Address) < 0)
94 return false;
95 }
96 if (switchDPID != null || switchPort != null) {
97 SwitchPort[] sps = value.getAttachmentPoints();
98 if (sps == null) return false;
99
100 match = false;
101 for (SwitchPort sp : sps) {
102 if (switchDPID != null) {
103 if (switchDPID.longValue() != sp.getSwitchDPID())
104 return false;
105 }
106 if (switchPort != null) {
107 if (switchPort.intValue() != sp.getPort())
108 return false;
109 }
110 match = true;
111 break;
112 }
113 if (!match) return false;
114 }
115 return true;
116 }
117}