Merge "Don't build OVSDB Distribution in Jenkins"
[ovsdb.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / AbstractHandler.java
1 /*
2  * Copyright (C) 2013 Red Hat, Inc.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Authors : Madhu Venugopal, Brent Salisbury
9  */
10 package org.opendaylight.ovsdb.openstack.netvirt;
11
12 import org.opendaylight.controller.sal.utils.Status;
13 import org.opendaylight.controller.sal.utils.StatusCode;
14 import org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher;
15
16 import com.google.common.base.Preconditions;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 import java.net.HttpURLConnection;
21
22 /**
23  * OpenStack related events originate from multiple north callbacks as well as south.
24  * This interface provides a layer of abstraction between the event dispatcher and the
25  * handlers.
26  */
27 public abstract class AbstractHandler {
28
29     /**
30      * Logger instance.
31      */
32     static final Logger logger = LoggerFactory.getLogger(AbstractHandler.class);
33
34     // The implementation for each of these services is resolved by the OSGi Service Manager
35     private volatile EventDispatcher eventDispatcher;
36
37     /**
38      * Convert failure status returned by the  manager into
39      * neutron API service errors.
40      *
41      * @param status  manager status
42      * @return  An error to be returned to neutron API service.
43      */
44     protected static final int getException(Status status) {
45         int result = HttpURLConnection.HTTP_INTERNAL_ERROR;
46
47         assert !status.isSuccess();
48
49         StatusCode code = status.getCode();
50         logger.debug(" Exception code - {}, description - {}",
51                 code, status.getDescription());
52
53         if (code == StatusCode.BADREQUEST) {
54             result = HttpURLConnection.HTTP_BAD_REQUEST;
55         } else if (code == StatusCode.CONFLICT) {
56             result = HttpURLConnection.HTTP_CONFLICT;
57         } else if (code == StatusCode.NOTACCEPTABLE) {
58             result = HttpURLConnection.HTTP_NOT_ACCEPTABLE;
59         } else if (code == StatusCode.NOTFOUND) {
60             result = HttpURLConnection.HTTP_NOT_FOUND;
61         } else {
62             result = HttpURLConnection.HTTP_INTERNAL_ERROR;
63         }
64
65         return result;
66     }
67
68     /**
69      * Enqueue the event.
70      *
71      * @param event the {@link org.opendaylight.ovsdb.openstack.netvirt.AbstractEvent} event to be handled.
72      * @see org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher
73      */
74     protected void enqueueEvent(AbstractEvent abstractEvent) {
75         Preconditions.checkNotNull(eventDispatcher);
76         eventDispatcher.enqueueEvent(abstractEvent);
77     }
78
79     /**
80      * Process the event.
81      *
82      * @param event the {@link org.opendaylight.ovsdb.openstack.netvirt.AbstractEvent} event to be handled.
83      * @see org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher
84      */
85     public abstract void processEvent(AbstractEvent abstractEvent);
86
87 }