Bug 4160 - null pointer exception in getDHCPServerPort() (re-do)
[netvirt.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / AbstractHandler.java
index 791c9f026740a0aebcc3656b810248be1a20708f..7566091e6193579e1ff9acdcd3a9a117df814abf 100644 (file)
@@ -1,31 +1,33 @@
 /*
- * Copyright (C) 2013 Red Hat, Inc.
+ * Copyright (c) 2013, 2015 Red Hat, Inc. and others. All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
- *
- * Authors : Madhu Venugopal, Brent Salisbury
  */
+
 package org.opendaylight.ovsdb.openstack.netvirt;
 
-import org.opendaylight.controller.sal.utils.Status;
-import org.opendaylight.controller.sal.utils.StatusCode;
+import org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher;
+import org.opendaylight.ovsdb.openstack.netvirt.api.Status;
+import org.opendaylight.ovsdb.openstack.netvirt.api.StatusCode;
 
+import com.google.common.base.Preconditions;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.net.HttpURLConnection;
 
 /**
- * Abstract class for utility functions used by neutron handlers.
+ * OpenStack related events originate from multiple north callbacks as well as south.
+ * This interface provides a layer of abstraction between the event dispatcher and the
+ * handlers.
  */
 public abstract class AbstractHandler {
+    private static final Logger LOG = LoggerFactory.getLogger(AbstractHandler.class);
 
-    /**
-     * Logger instance.
-     */
-    static final Logger logger = LoggerFactory.getLogger(AbstractHandler.class);
+    // The implementation for each of these services is resolved by the OSGi Service Manager
+    protected volatile EventDispatcher eventDispatcher;
 
     /**
      * Convert failure status returned by the  manager into
@@ -34,27 +36,45 @@ public abstract class AbstractHandler {
      * @param status  manager status
      * @return  An error to be returned to neutron API service.
      */
-    protected static final int getException(Status status) {
-        int result = HttpURLConnection.HTTP_INTERNAL_ERROR;
-
+    protected static int getException(Status status) {
         assert !status.isSuccess();
 
         StatusCode code = status.getCode();
-        logger.debug(" Exception code - {}, description - {}",
+        LOG.debug(" Exception code - {}, description - {}",
                 code, status.getDescription());
 
-        if (code == StatusCode.BADREQUEST) {
-            result = HttpURLConnection.HTTP_BAD_REQUEST;
-        } else if (code == StatusCode.CONFLICT) {
-            result = HttpURLConnection.HTTP_CONFLICT;
-        } else if (code == StatusCode.NOTACCEPTABLE) {
-            result = HttpURLConnection.HTTP_NOT_ACCEPTABLE;
-        } else if (code == StatusCode.NOTFOUND) {
-            result = HttpURLConnection.HTTP_NOT_FOUND;
-        } else {
-            result = HttpURLConnection.HTTP_INTERNAL_ERROR;
+        switch(code) {
+            case BADREQUEST:
+                return HttpURLConnection.HTTP_BAD_REQUEST;
+            case CONFLICT:
+                return HttpURLConnection.HTTP_CONFLICT;
+            case NOTACCEPTABLE:
+                return HttpURLConnection.HTTP_NOT_ACCEPTABLE;
+            case NOTFOUND:
+                return HttpURLConnection.HTTP_NOT_FOUND;
+            default:
+                return HttpURLConnection.HTTP_INTERNAL_ERROR;
         }
+    }
 
-        return result;
+    /**
+     * Enqueue the event.
+     *
+     * @param abstractEvent the {@link org.opendaylight.ovsdb.openstack.netvirt.AbstractEvent} event to be handled.
+     * @see org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher
+     */
+    protected void enqueueEvent(AbstractEvent abstractEvent) {
+        LOG.info("enqueueEvent: evenDispatcher: {} - {}", eventDispatcher, abstractEvent);
+        Preconditions.checkNotNull(eventDispatcher);
+        eventDispatcher.enqueueEvent(abstractEvent);
     }
+
+    /**
+     * Process the event.
+     *
+     * @param abstractEvent the {@link org.opendaylight.ovsdb.openstack.netvirt.AbstractEvent} event to be handled.
+     * @see org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher
+     */
+    public abstract void processEvent(AbstractEvent abstractEvent);
+
 }