Fix for Bug 3902 : Removal of the Unused imports from neutron.
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / AbstractNeutronNorthbound.java
1 /*
2  * Copyright (c) 2015 Intel Corporation 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.neutron.northbound.api;
10
11 import java.net.HttpURLConnection;
12 import java.util.List;
13
14 import org.opendaylight.neutron.spi.INeutronCRUD;
15 import org.opendaylight.neutron.spi.INeutronObject;
16
17 import javax.ws.rs.core.Response;
18
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 public abstract class AbstractNeutronNorthbound<T extends INeutronObject, NeutronRequest extends INeutronRequest<T>, I extends INeutronCRUD<T>> {
23     private static final Logger LOGGER = LoggerFactory
24         .getLogger(AbstractNeutronNorthbound.class);
25
26     protected static final int HTTP_OK_BOTTOM = 200;
27     protected static final int HTTP_OK_TOP = 299;
28
29     private static final String INTERFACE_NAME_BASE = " CRUD Interface";
30     private static final String UUID_NO_EXIST_BASE = " UUID does not exist.";
31
32     protected final String serviceUnavailable() {
33         return getResourceName() + INTERFACE_NAME_BASE + RestMessages.SERVICEUNAVAILABLE.toString();
34     }
35
36     protected final String uuidNoExist() {
37         return getResourceName() + UUID_NO_EXIST_BASE;
38     }
39
40     protected abstract String getResourceName();
41     protected abstract T extractFields(T o, List<String> fields);
42     protected abstract NeutronRequest newNeutronRequest(T o);
43     protected abstract I getNeutronCRUD();
44
45     protected Response show(String uuid,
46                             // return fields
47                             List<String> fields) {
48         I neutronCRUD = getNeutronCRUD();
49         T ans = neutronCRUD.get(uuid);
50         if (ans == null) {
51             throw new ResourceNotFoundException(uuidNoExist());
52         }
53
54         if (fields.size() > 0) {
55             return Response.status(HttpURLConnection.HTTP_OK).entity(
56                     newNeutronRequest(extractFields(ans, fields))).build();
57         } else {
58             return Response.status(HttpURLConnection.HTTP_OK).entity(newNeutronRequest(ans)).build();
59         }
60     }
61
62     protected Response create(final NeutronRequest input) {
63         I neutronCRUD = getNeutronCRUD();
64         if (input.isSingleton()) {
65             T singleton = input.getSingleton();
66
67             singleton.initDefaults();
68             neutronCRUD.add(singleton);
69         } else {
70             for (T test : input.getBulk()) {
71                 test.initDefaults();
72                 neutronCRUD.add(test);
73             }
74         }
75         return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
76     }
77
78     protected void updateDelta(String uuid, T delta, T original) {
79     }
80
81     protected Response update(String uuid, final NeutronRequest input) {
82         I neutronCRUD = getNeutronCRUD();
83         if (!input.isSingleton()) {
84             throw new BadRequestException("Only singleton edit supported");
85         }
86         T delta = input.getSingleton();
87         T original = neutronCRUD.get(uuid);
88         if (original == null) {
89             throw new ResourceNotFoundException(uuidNoExist());
90         }
91         updateDelta(uuid, delta, original);
92
93         /*
94          * update the object and return it
95          */
96         if (!neutronCRUD.update(uuid, delta)) {
97             throw new ResourceNotFoundException(uuidNoExist());
98         }
99         T updated = neutronCRUD.get(uuid);
100         return Response.status(HttpURLConnection.HTTP_OK).entity(newNeutronRequest(neutronCRUD.get(uuid))).build();
101     }
102
103     protected Response delete(String uuid) {
104         final I neutronCRUD = getNeutronCRUD();
105
106         T singleton = neutronCRUD.get(uuid);
107
108         /*
109          * remove it and return 204 status
110          */
111         final String resourceName = getResourceName();
112         boolean exist = false;
113         try {
114             exist = neutronCRUD.remove(uuid);
115         } catch (Exception e) {
116             LOGGER.debug("exception during remove {} {} {}",
117                          resourceName, uuid, e);
118             throw new InternalServerErrorException("Could not delete " +
119                                                    resourceName);
120         }
121         if (!exist) {
122             throw new ResourceNotFoundException(uuidNoExist());
123         }
124
125         return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();
126     }
127 }