[ONOS-4287] Persistent and distributed alarm store

Change-Id: I2fb0f5d84e563a53f036be012a8190d7df5869dc
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/faultmanagement/alarm/AlarmEvent.java b/incubator/api/src/main/java/org/onosproject/incubator/net/faultmanagement/alarm/AlarmEvent.java
index 000bfc9..f93d76f 100644
--- a/incubator/api/src/main/java/org/onosproject/incubator/net/faultmanagement/alarm/AlarmEvent.java
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/faultmanagement/alarm/AlarmEvent.java
@@ -15,50 +15,13 @@
  */
 package org.onosproject.incubator.net.faultmanagement.alarm;
 
-import java.util.Set;
 import org.onosproject.event.AbstractEvent;
-import org.onosproject.net.DeviceId;
 
 /**
- * Entity that represents Alarm events. Note: although the event will itself have a time, consumers may be more
- * interested in the times embedded in the alarms themselves.
- *
+ * Entity that represents Alarm events. Note: although the event will itself have a time,
+ * consumers may be more interested in the times embedded in the alarms themselves.
  */
-public class AlarmEvent extends AbstractEvent<AlarmEvent.Type, Set<Alarm>> {
-
-    private final DeviceId deviceRefreshed;
-
-    /**
-     * Creates an event due to one or more notification.
-     *
-     * @param alarms the set one or more of alarms.
-     */
-    public AlarmEvent(Set<Alarm> alarms) {
-        super(Type.NOTIFICATION, alarms);
-        deviceRefreshed = null;
-    }
-
-    /**
-     * Creates an event due to alarm discovery for a device.
-     *
-     * @param alarms the set of alarms.
-     * @param deviceRefreshed if of refreshed device, populated after a de-discovery
-     */
-    public AlarmEvent(Set<Alarm> alarms,
-            DeviceId deviceRefreshed) {
-        super(Type.DEVICE_DISCOVERY, alarms);
-        this.deviceRefreshed = deviceRefreshed;
-
-    }
-
-    /**
-     * Gets which device was refreshed.
-     *
-     * @return the refreshed device, or null if event related to a asynchronous notification(s)
-     */
-    public DeviceId getDeviceRefreshed() {
-        return deviceRefreshed;
-    }
+public class AlarmEvent extends AbstractEvent<AlarmEvent.Type, Alarm> {
 
     /**
      * Type of alarm event.
@@ -66,13 +29,22 @@
     public enum Type {
 
         /**
-         * Individual alarm(s) updated.
+         * Individual alarm updated.
          */
-        NOTIFICATION,
+        CREATED,
         /**
          * Alarm set updated for a given device.
          */
-        DEVICE_DISCOVERY,
+        REMOVED,
+    }
+
+    /**
+     * Creates an event due to one alarm.
+     *
+     * @param alarm the alarm related to the event.
+     */
+    public AlarmEvent(AlarmEvent.Type type, Alarm alarm) {
+        super(type, alarm);
     }
 
 }
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/faultmanagement/alarm/AlarmListener.java b/incubator/api/src/main/java/org/onosproject/incubator/net/faultmanagement/alarm/AlarmListener.java
new file mode 100644
index 0000000..3388963
--- /dev/null
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/faultmanagement/alarm/AlarmListener.java
@@ -0,0 +1,25 @@
+/*
+ * Copyright 2016-present 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.faultmanagement.alarm;
+
+import org.onosproject.event.EventListener;
+
+/**
+ * Entity capable of receiving alarm related events.
+ */
+public interface AlarmListener extends EventListener<AlarmEvent> {
+}
\ No newline at end of file
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/faultmanagement/alarm/AlarmProviderService.java b/incubator/api/src/main/java/org/onosproject/incubator/net/faultmanagement/alarm/AlarmProviderService.java
index cd36931..dc53d7b 100644
--- a/incubator/api/src/main/java/org/onosproject/incubator/net/faultmanagement/alarm/AlarmProviderService.java
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/faultmanagement/alarm/AlarmProviderService.java
@@ -16,7 +16,6 @@
 package org.onosproject.incubator.net.faultmanagement.alarm;
 
 
-import com.google.common.annotations.Beta;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.provider.ProviderService;
 
@@ -25,7 +24,7 @@
 /**
  * The interface Alarm provider service.
  */
-@Beta
+
 public interface AlarmProviderService extends ProviderService<AlarmProvider> {
 
     /**
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/faultmanagement/alarm/AlarmService.java b/incubator/api/src/main/java/org/onosproject/incubator/net/faultmanagement/alarm/AlarmService.java
index 121835e..d233e34 100644
--- a/incubator/api/src/main/java/org/onosproject/incubator/net/faultmanagement/alarm/AlarmService.java
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/faultmanagement/alarm/AlarmService.java
@@ -15,28 +15,26 @@
  */
 package org.onosproject.incubator.net.faultmanagement.alarm;
 
-import com.google.common.annotations.Beta;
-import java.util.Map;
-
-import java.util.Set;
+import org.onosproject.event.ListenerService;
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.DeviceId;
 
+import java.util.Map;
+import java.util.Set;
+
 /**
  * Service for interacting with the alarm handling of devices. Unless stated otherwise, getter methods
  * return active AND recently-cleared alarms.
  */
-@Beta
-public interface AlarmService {
+public interface AlarmService extends ListenerService<AlarmEvent, AlarmListener> {
 
     /**
      * Update book-keeping (ie administrative) fields for the alarm matching the specified identifier.
      *
-     * @param id alarm identifier
+     * @param id             alarm identifier
      * @param isAcknowledged new acknowledged state
-     * @param assignedUser new assigned user, null clear
+     * @param assignedUser   new assigned user, null clear
      * @return updated alarm (including any recent device-derived changes)
-     *
      */
     Alarm updateBookkeepingFields(AlarmId id, boolean isAcknowledged, String assignedUser);
 
@@ -98,7 +96,7 @@
      * Returns the alarm for a given device and source.
      *
      * @param deviceId the device
-     * @param source the source within the device
+     * @param source   the source within the device
      * @return set of alarms; empty set if no alarms
      */
     Set<Alarm> getAlarms(DeviceId deviceId, AlarmEntityId source);
@@ -116,7 +114,7 @@
      * Returns the alarm affecting a given flow.
      *
      * @param deviceId the device
-     * @param flowId the flow
+     * @param flowId   the flow
      * @return set of alarms; empty set if no alarms
      */
     Set<Alarm> getAlarmsForFlow(DeviceId deviceId, long flowId);
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/faultmanagement/alarm/DefaultAlarm.java b/incubator/api/src/main/java/org/onosproject/incubator/net/faultmanagement/alarm/DefaultAlarm.java
index 8de205c..162b3df 100644
--- a/incubator/api/src/main/java/org/onosproject/incubator/net/faultmanagement/alarm/DefaultAlarm.java
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/faultmanagement/alarm/DefaultAlarm.java
@@ -25,6 +25,7 @@
 /**
  * Default implementation of an alarm.
  */
+//TODO simpler creation and updating.
 public final class DefaultAlarm implements Alarm {
 
     private final AlarmId id;
@@ -41,34 +42,50 @@
     private final boolean isManuallyClearable;
     private final String assignedUser;
 
+    //Only for Kryo
+    DefaultAlarm() {
+        id = null;
+        deviceId = null;
+        description = null;
+        source = null;
+        timeRaised = -1;
+        timeUpdated = -1;
+        timeCleared = null;
+        severity = null;
+        isServiceAffecting = false;
+        isAcknowledged = false;
+        isManuallyClearable = false;
+        assignedUser = null;
+    }
+
     /**
      * Instantiates a new Default alarm.
      *
-     * @param id the id
-     * @param deviceId the device id
-     * @param description the description
-     * @param source the source, null indicates none.
-     * @param timeRaised the time raised.
-     * @param timeUpdated the time last updated.
-     * @param timeCleared the time cleared, null indicates uncleared.
-     * @param severity the severity
-     * @param isServiceAffecting the service affecting
-     * @param isAcknowledged the acknowledged
+     * @param id                  the id
+     * @param deviceId            the device id
+     * @param description         the description
+     * @param source              the source, null indicates none.
+     * @param timeRaised          the time raised.
+     * @param timeUpdated         the time last updated.
+     * @param timeCleared         the time cleared, null indicates uncleared.
+     * @param severity            the severity
+     * @param isServiceAffecting  the service affecting
+     * @param isAcknowledged      the acknowledged
      * @param isManuallyClearable the manually clearable
-     * @param assignedUser the assigned user, `null` indicates none.
+     * @param assignedUser        the assigned user, `null` indicates none.
      */
     private DefaultAlarm(final AlarmId id,
-            final DeviceId deviceId,
-            final String description,
-            final AlarmEntityId source,
-            final long timeRaised,
-            final long timeUpdated,
-            final Long timeCleared,
-            final SeverityLevel severity,
-            final boolean isServiceAffecting,
-            final boolean isAcknowledged,
-            final boolean isManuallyClearable,
-            final String assignedUser) {
+                         final DeviceId deviceId,
+                         final String description,
+                         final AlarmEntityId source,
+                         final long timeRaised,
+                         final long timeUpdated,
+                         final Long timeCleared,
+                         final SeverityLevel severity,
+                         final boolean isServiceAffecting,
+                         final boolean isAcknowledged,
+                         final boolean isManuallyClearable,
+                         final String assignedUser) {
         this.id = id;
         this.deviceId = deviceId;
         this.description = description;
@@ -147,9 +164,9 @@
     public int hashCode() {
         // id or timeRaised or timeUpdated may differ
         return Objects.hash(deviceId, description,
-                source, timeCleared, severity,
-                isServiceAffecting, isAcknowledged,
-                isManuallyClearable, assignedUser);
+                            source, timeCleared, severity,
+                            isServiceAffecting, isAcknowledged,
+                            isManuallyClearable, assignedUser);
     }
 
     @Override
@@ -244,7 +261,7 @@
         }
 
         public Builder(final DeviceId deviceId,
-                final String description, final SeverityLevel severity, final long timeRaised) {
+                       final String description, final SeverityLevel severity, final long timeRaised) {
             super();
             this.id = AlarmId.NONE;
             this.deviceId = deviceId;
@@ -310,7 +327,7 @@
             checkNotNull(severity, "Must specify a severity");
 
             return new DefaultAlarm(id, deviceId, description, source, timeRaised, timeUpdated, timeCleared,
-                    severity, isServiceAffecting, isAcknowledged, isManuallyClearable, assignedUser);
+                                    severity, isServiceAffecting, isAcknowledged, isManuallyClearable, assignedUser);
         }
     }
 }