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