translator: remove dependency of neutron.spi
[netvirt.git] / openstack / net-virt / src / main / java / org / opendaylight / netvirt / openstack / netvirt / AbstractHandler.java
1 /*
2  * Copyright (c) 2013, 2015 Red Hat, Inc. and others. All rights reserved.
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
9 package org.opendaylight.netvirt.openstack.netvirt;
10
11 import org.opendaylight.netvirt.openstack.netvirt.api.Status;
12 import org.opendaylight.netvirt.openstack.netvirt.api.StatusCode;
13 import org.opendaylight.netvirt.openstack.netvirt.api.EventDispatcher;
14
15 import com.google.common.base.Preconditions;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 import java.net.HttpURLConnection;
20
21 /**
22  * OpenStack related events originate from multiple north callbacks as well as south.
23  * This interface provides a layer of abstraction between the event dispatcher and the
24  * handlers.
25  */
26 public abstract class AbstractHandler {
27     private static final Logger LOG = LoggerFactory.getLogger(AbstractHandler.class);
28
29     // The implementation for each of these services is resolved by the OSGi Service Manager
30     protected volatile EventDispatcher eventDispatcher;
31
32     /**
33      * Convert failure status returned by the  manager into
34      * neutron API service errors.
35      *
36      * @param status  manager status
37      * @return  An error to be returned to neutron API service.
38      */
39     protected static int getException(Status status) {
40         assert !status.isSuccess();
41
42         StatusCode code = status.getCode();
43         LOG.debug(" Exception code - {}, description - {}",
44                 code, status.getDescription());
45
46         switch(code) {
47             case BADREQUEST:
48                 return HttpURLConnection.HTTP_BAD_REQUEST;
49             case CONFLICT:
50                 return HttpURLConnection.HTTP_CONFLICT;
51             case NOTACCEPTABLE:
52                 return HttpURLConnection.HTTP_NOT_ACCEPTABLE;
53             case NOTFOUND:
54                 return HttpURLConnection.HTTP_NOT_FOUND;
55             default:
56                 return HttpURLConnection.HTTP_INTERNAL_ERROR;
57         }
58     }
59
60     /**
61      * Enqueue the event.
62      *
63      * @param abstractEvent the {@link AbstractEvent} event to be handled.
64      * @see EventDispatcher
65      */
66     protected void enqueueEvent(AbstractEvent abstractEvent) {
67         LOG.info("enqueueEvent: evenDispatcher: {} - {}", eventDispatcher, abstractEvent);
68         Preconditions.checkNotNull(eventDispatcher);
69         eventDispatcher.enqueueEvent(abstractEvent);
70     }
71
72     /**
73      * Process the event.
74      *
75      * @param abstractEvent the {@link AbstractEvent} event to be handled.
76      * @see EventDispatcher
77      */
78     public abstract void processEvent(AbstractEvent abstractEvent);
79
80 }