AbstractNeutronNorthbound: remove unused local variable
[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.lang.reflect.Constructor;
12 import java.lang.reflect.InvocationTargetException;
13 import java.lang.reflect.ParameterizedType;
14 import java.net.HttpURLConnection;
15 import java.util.List;
16 import javax.ws.rs.core.Response;
17 import org.opendaylight.neutron.spi.INeutronCRUD;
18 import org.opendaylight.neutron.spi.INeutronObject;
19 import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public abstract class AbstractNeutronNorthbound<T extends INeutronObject<T>, R extends INeutronRequest<T>,
24         I extends INeutronCRUD<T>> {
25     // T extends INeutronObject<T> as 0th type argument
26     private static final int NEUTRON_ARGUMENT_TYPE_INDEX = 0;
27     // NeutronRequest extends INeutronRequest<T> as 1st type argument
28     private static final int NEUTRON_REQUEST_TYPE_INDEX = 1;
29     // I extends INeutronCRUD<T> as 2nd type argument
30     private static final int NEUTRON_CRUD_TYPE_INDEX = 2;
31
32     private static final Logger LOGGER = LoggerFactory.getLogger(AbstractNeutronNorthbound.class);
33
34     protected static final int HTTP_OK_BOTTOM = 200;
35     protected static final int HTTP_OK_TOP = 299;
36
37     private static final String INTERFACE_NAME_BASE = " CRUD Interface";
38     private static final String UUID_NO_EXIST_BASE = " UUID does not exist.";
39
40     protected final String serviceUnavailable() {
41         return getResourceName() + INTERFACE_NAME_BASE + RestMessages.SERVICEUNAVAILABLE.toString();
42     }
43
44     protected final String uuidNoExist() {
45         return getResourceName() + UUID_NO_EXIST_BASE;
46     }
47
48     protected abstract String getResourceName();
49
50     private <K> Class<K> getActualTypeArgument(final int typeIndex) {
51         ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass();
52         @SuppressWarnings("unchecked")
53         Class<K> cls = (Class<K>) parameterizedType.getActualTypeArguments()[typeIndex];
54         return cls;
55     }
56
57     private R newNeutronRequest(T neutronObject) {
58         // return new R(neutronObject);
59
60         // argumentClass = T.class
61         Class<T> argumentClass = getActualTypeArgument(NEUTRON_ARGUMENT_TYPE_INDEX);
62         // cls = NeturonRequest.class
63         Class<R> cls = getActualTypeArgument(NEUTRON_REQUEST_TYPE_INDEX);
64         try {
65             // ctor = R constructor
66             Constructor<R> ctor = cls.getDeclaredConstructor(argumentClass);
67             return ctor.newInstance(neutronObject);
68         } catch (NoSuchMethodException | InstantiationException
69                  | IllegalAccessException | InvocationTargetException e) {
70             // This case shouldn't happen
71             throw new IllegalArgumentException(e);
72         }
73     }
74
75     protected I getNeutronCRUD() {
76         // cls = I.class
77         Class<I> cls = getActualTypeArgument(NEUTRON_CRUD_TYPE_INDEX);
78         I neutronCrud = NeutronCRUDInterfaces.fetchINeutronCRUD(cls, (Object) this);
79         if (neutronCrud == null) {
80             throw new ServiceUnavailableException(serviceUnavailable());
81         }
82         return neutronCrud;
83     }
84
85     protected Response show(String uuid,
86             // return fields
87             List<String> fields) {
88         I neutronCRUD = getNeutronCRUD();
89         T ans = neutronCRUD.get(uuid);
90         if (ans == null) {
91             throw new ResourceNotFoundException(uuidNoExist());
92         }
93
94         if (fields.size() > 0) {
95             return Response.status(HttpURLConnection.HTTP_OK).entity(newNeutronRequest(ans.extractFields(fields)))
96                     .build();
97         } else {
98             return Response.status(HttpURLConnection.HTTP_OK).entity(newNeutronRequest(ans)).build();
99         }
100     }
101
102     protected Response create(final R input) {
103         I neutronCRUD = getNeutronCRUD();
104         if (input.isSingleton()) {
105             T singleton = input.getSingleton();
106
107             singleton.initDefaults();
108             neutronCRUD.add(singleton);
109         } else {
110             if (input.getBulk() == null) {
111                 throw new BadRequestException("Invalid requests");
112             }
113             for (T test : input.getBulk()) {
114                 test.initDefaults();
115                 neutronCRUD.add(test);
116             }
117         }
118         return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
119     }
120
121     protected void updateDelta(String uuid, T delta, T original) {
122     }
123
124     protected Response update(String uuid, final R input) {
125         I neutronCRUD = getNeutronCRUD();
126         if (!input.isSingleton()) {
127             throw new BadRequestException("Only singleton edit supported");
128         }
129         T delta = input.getSingleton();
130         T original = neutronCRUD.get(uuid);
131         if (original == null) {
132             throw new ResourceNotFoundException(uuidNoExist());
133         }
134         updateDelta(uuid, delta, original);
135
136         /*
137          * update the object and return it
138          */
139         if (!neutronCRUD.update(uuid, delta)) {
140             throw new ResourceNotFoundException(uuidNoExist());
141         }
142         T updated = neutronCRUD.get(uuid);
143         return Response.status(HttpURLConnection.HTTP_OK).entity(newNeutronRequest(updated)).build();
144     }
145
146     protected Response delete(String uuid) {
147         final I neutronCRUD = getNeutronCRUD();
148
149         /*
150          * remove it and return 204 status
151          */
152         if (!neutronCRUD.remove(uuid)) {
153             throw new ResourceNotFoundException(uuidNoExist());
154         }
155
156         return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();
157     }
158 }