2 * Copyright (c) 2015 Intel Corporation and others. All rights reserved.
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
9 package org.opendaylight.neutron.northbound.api;
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;
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;
32 private static final Logger LOGGER = LoggerFactory.getLogger(AbstractNeutronNorthbound.class);
34 protected static final int HTTP_OK_BOTTOM = 200;
35 protected static final int HTTP_OK_TOP = 299;
37 private static final String INTERFACE_NAME_BASE = " CRUD Interface";
38 private static final String UUID_NO_EXIST_BASE = " UUID does not exist.";
40 protected final String serviceUnavailable() {
41 return getResourceName() + INTERFACE_NAME_BASE + RestMessages.SERVICEUNAVAILABLE.toString();
44 protected final String uuidNoExist() {
45 return getResourceName() + UUID_NO_EXIST_BASE;
48 protected abstract String getResourceName();
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];
57 private R newNeutronRequest(T neutronObject) {
58 // return new R(neutronObject);
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);
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);
75 protected I getNeutronCRUD() {
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());
85 protected Response show(String uuid,
87 List<String> fields) {
88 I neutronCRUD = getNeutronCRUD();
89 T ans = neutronCRUD.get(uuid);
91 throw new ResourceNotFoundException(uuidNoExist());
94 if (fields.size() > 0) {
95 return Response.status(HttpURLConnection.HTTP_OK).entity(newNeutronRequest(ans.extractFields(fields)))
98 return Response.status(HttpURLConnection.HTTP_OK).entity(newNeutronRequest(ans)).build();
102 protected Response create(final R input) {
103 I neutronCRUD = getNeutronCRUD();
104 if (input.isSingleton()) {
105 T singleton = input.getSingleton();
107 singleton.initDefaults();
108 neutronCRUD.add(singleton);
110 if (input.getBulk() == null) {
111 throw new BadRequestException("Invalid requests");
113 for (T test : input.getBulk()) {
115 neutronCRUD.add(test);
118 return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
121 protected void updateDelta(String uuid, T delta, T original) {
124 private boolean checkRevisionNumber(T original, T delta) {
125 // If new update is null ignore the original revision number
126 if (delta.getRevisionNumber() == null) {
129 // If what is stored is null no need for comparison
130 if (original.getRevisionNumber() == null) {
133 if (original.getRevisionNumber() > delta.getRevisionNumber()) {
139 protected Response update(String uuid, final R input) {
140 I neutronCRUD = getNeutronCRUD();
141 if (!input.isSingleton()) {
142 throw new BadRequestException("Only singleton edit supported");
144 T delta = input.getSingleton();
145 T original = neutronCRUD.get(uuid);
146 if (original == null) {
147 throw new ResourceNotFoundException(uuidNoExist());
149 if (checkRevisionNumber(original, delta)) {
150 return Response.status(HttpURLConnection.HTTP_OK).build();
152 updateDelta(uuid, delta, original);
154 * update the object and return it
156 if (!neutronCRUD.update(uuid, delta)) {
157 throw new ResourceNotFoundException(uuidNoExist());
159 T updated = neutronCRUD.get(uuid);
160 return Response.status(HttpURLConnection.HTTP_OK).entity(newNeutronRequest(updated)).build();
163 protected Response delete(String uuid) {
164 final I neutronCRUD = getNeutronCRUD();
167 * remove it and return 204 status
169 if (!neutronCRUD.remove(uuid)) {
170 throw new ResourceNotFoundException(uuidNoExist());
173 return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();