[AETHER-38] Moves T3 utils to ONOS utils

Change-Id: I8eebc7934df2018507742b187bb64ee9b425fca7
diff --git a/utils/misc/src/main/java/org/onlab/packet/MacAddress.java b/utils/misc/src/main/java/org/onlab/packet/MacAddress.java
index db74bf0..2a89a55 100644
--- a/utils/misc/src/main/java/org/onlab/packet/MacAddress.java
+++ b/utils/misc/src/main/java/org/onlab/packet/MacAddress.java
@@ -284,4 +284,35 @@
         Matcher matcher = MAC_PATTERN.matcher(mac);
         return matcher.matches();
     }
+
+    /**
+     * Checks if the Mac Address is inside a range defined by macAddr and mask.
+     *
+     * @param macAddr the mac address
+     * @param maskAddr the mask
+     * @return true if in range, false otherwise.
+     */
+    public boolean inRange(MacAddress macAddr, MacAddress maskAddr) {
+        byte[] min = macAddr.toBytes();
+        byte[] mask = maskAddr.toBytes();
+        boolean inRange = true;
+
+        int i = 0;
+
+        //if mask is 00 stop
+        while (inRange && i < mask.length && (mask[i] & 0xFF) != 0) {
+            int ibmac = this.address[i] & 0xFF;
+            int ibmin = min[i] & 0xFF;
+            int ibmask = mask[i] & 0xFF;
+            if (ibmask == 255) {
+                inRange = ibmac == ibmin;
+            } else if (ibmac < ibmin || ibmac >= ibmask) {
+                inRange = false;
+                break;
+            }
+            i++;
+        }
+
+        return inRange;
+    }
 }
diff --git a/utils/misc/src/main/java/org/onlab/util/Generator.java b/utils/misc/src/main/java/org/onlab/util/Generator.java
new file mode 100644
index 0000000..223de5d
--- /dev/null
+++ b/utils/misc/src/main/java/org/onlab/util/Generator.java
@@ -0,0 +1,154 @@
+/*
+ * Copyright 2020-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onlab.util;
+
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+/**
+ * Generator class that yields instances of T type objects as soon as they are ready.
+ *
+ * @param <T> type of the object.
+ */
+public abstract class Generator<T> implements Iterable<T> {
+
+    private class Condition {
+        private boolean isSet;
+
+        synchronized void set() {
+            isSet = true;
+            notifyAll();
+        }
+
+        synchronized void await() throws InterruptedException {
+            try {
+
+                if (isSet) {
+                    return;
+                }
+
+                while (!isSet) {
+                    wait();
+                }
+            } finally {
+                isSet = false;
+            }
+        }
+    }
+
+    private static ThreadGroup threadGroup;
+
+    private Thread producer;
+    private boolean hasFinished;
+    private final Condition itemAvailableOrHasFinished = new Condition();
+    private final Condition itemRequested = new Condition();
+    private T nextItem;
+    private boolean nextItemAvailable;
+    private RuntimeException exceptionRaisedByProducer;
+
+    @Override
+    public Iterator<T> iterator() {
+        return new Iterator<T>() {
+            @Override
+            public boolean hasNext() {
+                return waitForNext();
+            }
+
+            @Override
+            public T next() {
+                if (!waitForNext()) {
+                    throw new NoSuchElementException();
+                }
+                nextItemAvailable = false;
+                return nextItem;
+            }
+
+            @Override
+            public void remove() {
+                throw new UnsupportedOperationException();
+            }
+
+            private boolean waitForNext() {
+                if (nextItemAvailable) {
+                    return true;
+                }
+                if (hasFinished) {
+                    return false;
+                }
+                if (producer == null) {
+                    startProducer();
+                }
+                itemRequested.set();
+                try {
+                    itemAvailableOrHasFinished.await();
+                } catch (InterruptedException e) {
+                    hasFinished = true;
+                    producer.interrupt();
+                    try {
+                        producer.join();
+                    } catch (InterruptedException e1) {
+                        // Interrupting the broken thread
+                        Thread.currentThread().interrupt();
+                        throw new IllegalStateException(e1);
+                    }
+                }
+                if (exceptionRaisedByProducer != null) {
+                    throw exceptionRaisedByProducer;
+                }
+                return !hasFinished;
+            }
+        };
+    }
+
+    protected abstract void run() throws InterruptedException;
+
+    /**
+     * Makes available the next item.
+     *
+     * @param element the next item
+     * @throws InterruptedException if await fails
+     */
+    public void yield(T element) throws InterruptedException {
+        nextItem = element;
+        nextItemAvailable = true;
+        itemAvailableOrHasFinished.set();
+        itemRequested.await();
+    }
+
+    private void startProducer() {
+        assert producer == null;
+        synchronized (this) {
+            if (threadGroup == null) {
+                threadGroup = new ThreadGroup("onos-generator");
+            }
+        }
+        producer = new Thread(threadGroup, () -> {
+            try {
+                itemRequested.await();
+                Generator.this.run();
+            } catch (InterruptedException e) {
+                // Remaining steps in run() will shut down thread.
+            } catch (RuntimeException e) {
+                exceptionRaisedByProducer = e;
+            }
+            hasFinished = true;
+            itemAvailableOrHasFinished.set();
+        });
+        producer.setDaemon(true);
+        producer.start();
+    }
+}
diff --git a/utils/misc/src/test/java/org/onlab/packet/MacAddressTest.java b/utils/misc/src/test/java/org/onlab/packet/MacAddressTest.java
index 99b467d..f8ff8b4 100644
--- a/utils/misc/src/test/java/org/onlab/packet/MacAddressTest.java
+++ b/utils/misc/src/test/java/org/onlab/packet/MacAddressTest.java
@@ -33,6 +33,13 @@
     private static final MacAddress MAC_LLDP_3 = MacAddress.valueOf("01:80:c2:00:00:0e");
     private static final MacAddress MAC_ONOS = MacAddress.valueOf("a4:23:05:01:02:03");
     private static final MacAddress MAC_ONOS_EQUAL = MacAddress.valueOf("a4:23:05:01:02:03");
+    // For range testing
+    private static final MacAddress MAC_IPV4_MCAST = MacAddress.valueOf("01:00:5e:00:00:00");
+    private static final MacAddress MAC_IPV4_MCAST_ADDR = MacAddress.valueOf("01:00:5e:00:01:01");
+    private static final MacAddress MAC_IPV4_MCAST_MASK = MacAddress.valueOf("ff:ff:ff:80:00:00");
+    private static final MacAddress MAC_IPV6_MCAST = MacAddress.valueOf("33:33:00:00:00:00");
+    private static final MacAddress MAC_IPV6_MCAST_ADDR = MacAddress.valueOf("33:33:00:00:00:01");
+    private static final MacAddress MAC_IPV6_MCAST_MASK = MacAddress.valueOf("ff:ff:00:00:00:00");
 
     private static final byte[] OUI_ONOS = {(byte) 0xa4, (byte) 0x23, (byte) 0x05};
     private static final byte[] MAC_ONOS_BYTE = {
@@ -161,6 +168,18 @@
     }
 
     @Test
+    public void testContained() throws Exception {
+        assertFalse(MAC_NORMAL.inRange(MAC_IPV4_MCAST, MAC_IPV4_MCAST_MASK));
+        assertFalse(MAC_NORMAL.inRange(MAC_IPV6_MCAST, MAC_IPV6_MCAST_MASK));
+        // Contained in itself
+        assertTrue(MAC_IPV4_MCAST.inRange(MAC_IPV4_MCAST, MAC_IPV4_MCAST_MASK));
+        assertTrue(MAC_IPV6_MCAST.inRange(MAC_IPV6_MCAST, MAC_IPV6_MCAST_MASK));
+        // Verify the mcast addresses
+        assertTrue(MAC_IPV4_MCAST_ADDR.inRange(MAC_IPV4_MCAST, MAC_IPV4_MCAST_MASK));
+        assertTrue(MAC_IPV6_MCAST_ADDR.inRange(MAC_IPV6_MCAST, MAC_IPV6_MCAST_MASK));
+    }
+
+    @Test
     public void testEquals() throws Exception {
         assertTrue(MAC_ONOS.equals(MAC_ONOS));
         assertFalse(MAC_ONOS.equals(MAC_ONOS_STR));