Bug 1029: Remove dead code: samples/clustersession
[controller.git] / opendaylight / adsal / hosttracker_new / implementation / src / main / java / org / opendaylight / controller / hosttracker / internal / DeviceIterator.java
1 /*
2  * Copyright (c) 2012 Big Switch Networks, Inc.
3  *
4  * Licensed under the Eclipse Public License, Version 1.0 (the
5  * "License"); you may not use this file except in compliance with the
6  * License. You may obtain a copy of the License at
7  *
8  *      http://www.eclipse.org/legal/epl-v10.html
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
13  * implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  *
16  * This file incorporates work covered by the following copyright and
17  * permission notice:
18  *
19  *    Originally created by David Erickson, Stanford University
20  *
21  *    Licensed under the Apache License, Version 2.0 (the "License");
22  *    you may not use this file except in compliance with the
23  *    License. You may obtain a copy of the License at
24  *
25  *         http://www.apache.org/licenses/LICENSE-2.0
26  *
27  *    Unless required by applicable law or agreed to in writing,
28  *    software distributed under the License is distributed on an "AS
29  *    IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
30  *    express or implied. See the License for the specific language
31  *    governing permissions and limitations under the License.
32  */
33
34 package org.opendaylight.controller.hosttracker.internal;
35
36 import java.util.Arrays;
37 import java.util.Iterator;
38
39 import org.opendaylight.controller.hosttracker.IEntityClass;
40 import org.opendaylight.controller.hosttracker.SwitchPort;
41 import org.opendaylight.controller.sal.core.NodeConnector;
42 import org.opendaylight.controller.sal.utils.FilterIterator;
43
44 /**
45  * An iterator for handling device queries
46  */
47 public class DeviceIterator extends FilterIterator<Device> {
48     private IEntityClass[] entityClasses;
49
50     private Long macAddress;
51     private Short vlan;
52     private Integer ipv4Address;
53     private NodeConnector port;
54
55     /**
56      * Construct a new device iterator over the key fields
57      *
58      * @param subIterator
59      *            an iterator over the full data structure to scan
60      * @param entityClasses
61      *            the entity classes to search for
62      * @param macAddress
63      *            The MAC address
64      * @param vlan
65      *            the VLAN
66      * @param ipv4Address
67      *            the ipv4 address
68      * @param switchDPID
69      *            the switch DPID
70      * @param switchPort
71      *            the switch port
72      */
73     public DeviceIterator(Iterator<Device> subIterator,
74             IEntityClass[] entityClasses, Long macAddress, Short vlan,
75             Integer ipv4Address, NodeConnector port) {
76         super(subIterator);
77         this.entityClasses = entityClasses;
78         this.subIterator = subIterator;
79         this.macAddress = macAddress;
80         this.vlan = vlan;
81         this.ipv4Address = ipv4Address;
82         this.port = port;
83     }
84
85     @Override
86     protected boolean matches(Device value) {
87         boolean match;
88         if (entityClasses != null) {
89             IEntityClass clazz = value.getEntityClass();
90             if (clazz == null)
91                 return false;
92
93             match = false;
94             for (IEntityClass entityClass : entityClasses) {
95                 if (clazz.equals(entityClass)) {
96                     match = true;
97                     break;
98                 }
99             }
100             if (!match)
101                 return false;
102         }
103         if (macAddress != null) {
104             if (macAddress.longValue() != value.getMACAddress())
105                 return false;
106         }
107         if (vlan != null) {
108             Short[] vlans = value.getVlanId();
109             if (Arrays.binarySearch(vlans, vlan) < 0)
110                 return false;
111         }
112         if (ipv4Address != null) {
113             Integer[] ipv4Addresses = value.getIPv4Addresses();
114             if (Arrays.binarySearch(ipv4Addresses, ipv4Address) < 0)
115                 return false;
116         }
117         if (port != null) {
118             SwitchPort[] sps = value.getAttachmentPoints();
119             if (sps == null)
120                 return false;
121
122             match = false;
123             for (SwitchPort sp : sps) {
124                 if (sp.getPort().equals(sp.getPort())) {
125                     match = true;
126                     break;
127                 }
128             }
129             if (!match)
130                 return false;
131         }
132         return true;
133     }
134 }