Added events for the Interface subsytem

Change-Id: I9adcc2caa2a98625173c9b9c6d15bbd1a0706eaa
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/config/basics/InterfaceConfig.java b/incubator/api/src/main/java/org/onosproject/incubator/net/config/basics/InterfaceConfig.java
index 5246f31..3c41291 100644
--- a/incubator/api/src/main/java/org/onosproject/incubator/net/config/basics/InterfaceConfig.java
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/config/basics/InterfaceConfig.java
@@ -31,6 +31,9 @@
 import java.util.Iterator;
 import java.util.Set;
 
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+
 /**
  * Configuration for interfaces.
  */
@@ -41,7 +44,9 @@
     public static final String MAC = "mac";
     public static final String VLAN = "vlan";
 
-    public static final String CONFIG_VALUE_ERROR = "Error parsing config value";
+    private static final String CONFIG_VALUE_ERROR = "Error parsing config value";
+    private static final String INTF_NULL_ERROR = "Interface cannot be null";
+    private static final String INTF_NAME_ERROR = "Interface must have a valid name";
 
     /**
      * Retrieves all interfaces configured on this port.
@@ -78,6 +83,12 @@
      * @param intf interface to add
      */
     public void addInterface(Interface intf) {
+        checkNotNull(intf, INTF_NULL_ERROR);
+        checkArgument(!intf.name().equals(Interface.NO_INTERFACE_NAME), INTF_NAME_ERROR);
+
+        // Remove old interface with this name if it exists
+        removeInterface(intf.name());
+
         ObjectNode intfNode = array.addObject();
 
         intfNode.put(NAME, intf.name());
@@ -101,6 +112,9 @@
      * @param name name of the interface to remove
      */
     public void removeInterface(String name) {
+        checkNotNull(name, INTF_NULL_ERROR);
+        checkArgument(!name.equals(Interface.NO_INTERFACE_NAME), INTF_NAME_ERROR);
+
         Iterator<JsonNode> it = array.iterator();
         while (it.hasNext()) {
             JsonNode node = it.next();
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/intf/InterfaceEvent.java b/incubator/api/src/main/java/org/onosproject/incubator/net/intf/InterfaceEvent.java
new file mode 100644
index 0000000..1bbf665
--- /dev/null
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/intf/InterfaceEvent.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * 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.onosproject.incubator.net.intf;
+
+import org.onosproject.event.AbstractEvent;
+
+/**
+ * Describes an interface event.
+ */
+public class InterfaceEvent extends AbstractEvent<InterfaceEvent.Type, Interface> {
+
+    public enum Type {
+        /**
+         * Indicates a new interface has been added.
+         */
+        INTERFACE_ADDED,
+
+        /**
+         * Indicates an interface has been updated.
+         */
+        INTERFACE_UPDATED,
+
+        /**
+         * Indicates an interface has been removed.
+         */
+        INTERFACE_REMOVED
+    }
+
+    /**
+     * Creates an interface event with type and subject.
+     *
+     * @param type event type
+     * @param subject subject interface
+     */
+    public InterfaceEvent(Type type, Interface subject) {
+        super(type, subject);
+    }
+
+    /**
+     * Creates an interface event with type, subject and time.
+     *
+     * @param type event type
+     * @param subject subject interface
+     * @param time time of event
+     */
+    public InterfaceEvent(Type type, Interface subject, long time) {
+        super(type, subject, time);
+    }
+
+}
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/intf/InterfaceListener.java b/incubator/api/src/main/java/org/onosproject/incubator/net/intf/InterfaceListener.java
new file mode 100644
index 0000000..2fd045d
--- /dev/null
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/intf/InterfaceListener.java
@@ -0,0 +1,25 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * 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.onosproject.incubator.net.intf;
+
+import org.onosproject.event.EventListener;
+
+/**
+ * Listener for interface events.
+ */
+public interface InterfaceListener extends EventListener<InterfaceEvent> {
+}
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/intf/InterfaceService.java b/incubator/api/src/main/java/org/onosproject/incubator/net/intf/InterfaceService.java
index b164df9..38cfdc5 100644
--- a/incubator/api/src/main/java/org/onosproject/incubator/net/intf/InterfaceService.java
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/intf/InterfaceService.java
@@ -19,6 +19,7 @@
 import com.google.common.annotations.Beta;
 import org.onlab.packet.IpAddress;
 import org.onlab.packet.VlanId;
+import org.onosproject.event.ListenerService;
 import org.onosproject.net.ConnectPoint;
 
 import java.util.Set;
@@ -27,7 +28,8 @@
  * Service for interacting with interfaces.
  */
 @Beta
-public interface InterfaceService {
+public interface InterfaceService
+        extends ListenerService<InterfaceEvent, InterfaceListener> {
 
     /**
      * Returns the set of all interfaces in the system.
diff --git a/incubator/net/src/main/java/org/onosproject/incubator/net/intf/impl/InterfaceManager.java b/incubator/net/src/main/java/org/onosproject/incubator/net/intf/impl/InterfaceManager.java
index bbd96de..7edbf25 100644
--- a/incubator/net/src/main/java/org/onosproject/incubator/net/intf/impl/InterfaceManager.java
+++ b/incubator/net/src/main/java/org/onosproject/incubator/net/intf/impl/InterfaceManager.java
@@ -27,10 +27,13 @@
 import org.apache.felix.scr.annotations.Service;
 import org.onlab.packet.IpAddress;
 import org.onlab.packet.VlanId;
+import org.onosproject.event.ListenerRegistry;
 import org.onosproject.incubator.net.config.basics.ConfigException;
 import org.onosproject.incubator.net.config.basics.InterfaceConfig;
 import org.onosproject.incubator.net.intf.Interface;
 import org.onosproject.incubator.net.intf.InterfaceAdminService;
+import org.onosproject.incubator.net.intf.InterfaceEvent;
+import org.onosproject.incubator.net.intf.InterfaceListener;
 import org.onosproject.incubator.net.intf.InterfaceService;
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.config.NetworkConfigEvent;
@@ -40,11 +43,9 @@
 import org.slf4j.LoggerFactory;
 
 import java.util.Collections;
-import java.util.Iterator;
 import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
-import java.util.concurrent.atomic.AtomicBoolean;
 
 import static java.util.stream.Collectors.collectingAndThen;
 import static java.util.stream.Collectors.toSet;
@@ -54,8 +55,8 @@
  */
 @Service
 @Component(immediate = true)
-public class InterfaceManager implements InterfaceService,
-        InterfaceAdminService {
+public class InterfaceManager extends ListenerRegistry<InterfaceEvent, InterfaceListener>
+        implements InterfaceService, InterfaceAdminService {
 
     private final Logger log = LoggerFactory.getLogger(getClass());
 
@@ -148,20 +149,53 @@
 
     private void updateInterfaces(InterfaceConfig intfConfig) {
         try {
-            interfaces.put(intfConfig.subject(), Sets.newHashSet(intfConfig.getInterfaces()));
+            Set<Interface> old = interfaces.put(intfConfig.subject(),
+                    Sets.newHashSet(intfConfig.getInterfaces()));
+
+            if (old == null) {
+                old = Collections.emptySet();
+            }
+
+            for (Interface intf : intfConfig.getInterfaces()) {
+                if (intf.name().equals(Interface.NO_INTERFACE_NAME)) {
+                    process(new InterfaceEvent(InterfaceEvent.Type.INTERFACE_ADDED, intf));
+                } else {
+                    Optional<Interface> oldIntf = findInterface(intf, old);
+                    if (oldIntf.isPresent()) {
+                        old.remove(oldIntf.get());
+                        if (!oldIntf.get().equals(intf)) {
+                            process(new InterfaceEvent(InterfaceEvent.Type.INTERFACE_UPDATED, intf));
+                        }
+                    } else {
+                        process(new InterfaceEvent(InterfaceEvent.Type.INTERFACE_ADDED, intf));
+                    }
+                }
+            }
+
+            for (Interface intf : old) {
+                if (!intf.name().equals(Interface.NO_INTERFACE_NAME)) {
+                    process(new InterfaceEvent(InterfaceEvent.Type.INTERFACE_REMOVED, intf));
+                }
+            }
         } catch (ConfigException e) {
             log.error("Error in interface config", e);
         }
     }
 
+    private Optional<Interface> findInterface(Interface intf, Set<Interface> set) {
+        return set.stream().filter(i -> i.name().equals(intf.name())).findAny();
+    }
+
     private void removeInterfaces(ConnectPoint port) {
-        interfaces.remove(port);
+        Set<Interface> old = interfaces.remove(port);
+
+        old.stream()
+                .filter(i -> !i.name().equals(Interface.NO_INTERFACE_NAME))
+                .forEach(i -> process(new InterfaceEvent(InterfaceEvent.Type.INTERFACE_REMOVED, i)));
     }
 
     @Override
     public void add(Interface intf) {
-        addInternal(intf);
-
         InterfaceConfig config =
                 configService.addConfig(intf.connectPoint(), CONFIG_CLASS);
 
@@ -170,30 +204,8 @@
         configService.applyConfig(intf.connectPoint(), CONFIG_CLASS, config.node());
     }
 
-    private void addInternal(Interface intf) {
-        interfaces.compute(intf.connectPoint(), (cp, current) -> {
-            if (current == null) {
-                return Sets.newHashSet(intf);
-            }
-
-            Iterator<Interface> it = current.iterator();
-            while (it.hasNext()) {
-                Interface i = it.next();
-                if (i.name().equals(intf.name())) {
-                    it.remove();
-                    break;
-                }
-            }
-
-            current.add(intf);
-            return current;
-        });
-    }
-
     @Override
     public boolean remove(ConnectPoint connectPoint, String name) {
-        boolean success = removeInternal(name, connectPoint);
-
         InterfaceConfig config = configService.addConfig(connectPoint, CONFIG_CLASS);
         config.removeInterface(name);
 
@@ -205,37 +217,10 @@
             }
         } catch (ConfigException e) {
             log.error("Error reading interfaces JSON", e);
+            return false;
         }
 
-        return success;
-    }
-
-    public boolean removeInternal(String name, ConnectPoint connectPoint) {
-        AtomicBoolean removed = new AtomicBoolean(false);
-
-        interfaces.compute(connectPoint, (cp, current) -> {
-            if (current == null) {
-                return null;
-            }
-
-            Iterator<Interface> it = current.iterator();
-            while (it.hasNext()) {
-                Interface i = it.next();
-                if (i.name().equals(name)) {
-                    it.remove();
-                    removed.set(true);
-                    break;
-                }
-            }
-
-            if (current.isEmpty()) {
-                return null;
-            } else {
-                return current;
-            }
-        });
-
-        return removed.get();
+        return true;
     }
 
     /**
@@ -245,24 +230,22 @@
 
         @Override
         public void event(NetworkConfigEvent event) {
-            switch (event.type()) {
-            case CONFIG_ADDED:
-            case CONFIG_UPDATED:
-                if (event.configClass() == InterfaceConfig.class) {
+            if (event.configClass() == CONFIG_CLASS) {
+                switch (event.type()) {
+                case CONFIG_ADDED:
+                case CONFIG_UPDATED:
                     InterfaceConfig config =
                             configService.getConfig((ConnectPoint) event.subject(), InterfaceConfig.class);
                     updateInterfaces(config);
-                }
-                break;
-            case CONFIG_REMOVED:
-                if (event.configClass() == InterfaceConfig.class) {
+                    break;
+                case CONFIG_REMOVED:
                     removeInterfaces((ConnectPoint) event.subject());
+                    break;
+                case CONFIG_REGISTERED:
+                case CONFIG_UNREGISTERED:
+                default:
+                    break;
                 }
-                break;
-            case CONFIG_REGISTERED:
-            case CONFIG_UNREGISTERED:
-            default:
-                break;
             }
         }
     }