Squashed commit of the following:
[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.ovsdb.openstack.netvirt.api.EventDispatcher;
13 import org.opendaylight.ovsdb.openstack.netvirt.api.Status;
14 import org.opendaylight.ovsdb.openstack.netvirt.api.StatusCode;
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     void init() {
38         logger.info(">>>>> init {}", this.getClass());
39     }
40
41     /**
42      * Convert failure status returned by the  manager into
43      * neutron API service errors.
44      *
45      * @param status  manager status
46      * @return  An error to be returned to neutron API service.
47      */
48     protected static final int getException(Status status) {
49         int result = HttpURLConnection.HTTP_INTERNAL_ERROR;
50
51         assert !status.isSuccess();
52
53         StatusCode code = status.getCode();
54         logger.debug(" Exception code - {}, description - {}",
55                 code, status.getDescription());
56
57         if (code == StatusCode.BADREQUEST) {
58             result = HttpURLConnection.HTTP_BAD_REQUEST;
59         } else if (code == StatusCode.CONFLICT) {
60             result = HttpURLConnection.HTTP_CONFLICT;
61         } else if (code == StatusCode.NOTACCEPTABLE) {
62             result = HttpURLConnection.HTTP_NOT_ACCEPTABLE;
63         } else if (code == StatusCode.NOTFOUND) {
64             result = HttpURLConnection.HTTP_NOT_FOUND;
65         } else {
66             result = HttpURLConnection.HTTP_INTERNAL_ERROR;
67         }
68
69         return result;
70     }
71
72     /**
73      * Enqueue the event.
74      *
75      * @param abstractEvent the {@link org.opendaylight.ovsdb.openstack.netvirt.AbstractEvent} event to be handled.
76      * @see org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher
77      */
78     protected void enqueueEvent(AbstractEvent abstractEvent) {
79         Preconditions.checkNotNull(eventDispatcher);
80         eventDispatcher.enqueueEvent(abstractEvent);
81     }
82
83     /**
84      * Process the event.
85      *
86      * @param abstractEvent the {@link org.opendaylight.ovsdb.openstack.netvirt.AbstractEvent} event to be handled.
87      * @see org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher
88      */
89     public abstract void processEvent(AbstractEvent abstractEvent);
90
91 }