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 java.util.Objects;
17 import javax.ws.rs.core.Response;
18 import org.opendaylight.neutron.spi.INeutronCRUD;
19 import org.opendaylight.neutron.spi.INeutronObject;
21 public abstract class AbstractNeutronNorthbound<T extends INeutronObject<T>, R extends INeutronRequest<T>,
22 I extends INeutronCRUD<T>> {
24 // T extends INeutronObject<T> as 0th type argument
25 private static final int NEUTRON_ARGUMENT_TYPE_INDEX = 0;
26 // NeutronRequest extends INeutronRequest<T> as 1st type argument
27 private static final int NEUTRON_REQUEST_TYPE_INDEX = 1;
29 protected static final int HTTP_OK_BOTTOM = 200;
30 protected static final int HTTP_OK_TOP = 299;
32 private static final String INTERFACE_NAME_BASE = " CRUD Interface";
33 private static final String UUID_NO_EXIST_BASE = " UUID does not exist.";
35 private final I neutronCRUD;
37 protected AbstractNeutronNorthbound(I neutronCRUD) {
38 this.neutronCRUD = Objects.requireNonNull(neutronCRUD, "neutronCRUD");
41 protected final String serviceUnavailable() {
42 return getResourceName() + INTERFACE_NAME_BASE + RestMessages.SERVICEUNAVAILABLE.toString();
45 protected final String uuidNoExist() {
46 return getResourceName() + UUID_NO_EXIST_BASE;
49 protected abstract String getResourceName();
51 private <K> Class<K> getActualTypeArgument(final int typeIndex) {
52 ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass();
53 @SuppressWarnings("unchecked")
54 Class<K> cls = (Class<K>) parameterizedType.getActualTypeArguments()[typeIndex];
58 private R newNeutronRequest(T neutronObject) {
59 // return new R(neutronObject)
61 // argumentClass = T.class
62 Class<T> argumentClass = getActualTypeArgument(NEUTRON_ARGUMENT_TYPE_INDEX);
63 // cls = NeturonRequest.class
64 Class<R> cls = getActualTypeArgument(NEUTRON_REQUEST_TYPE_INDEX);
66 // ctor = R constructor
67 Constructor<R> ctor = cls.getDeclaredConstructor(argumentClass);
68 return ctor.newInstance(neutronObject);
69 } catch (NoSuchMethodException | InstantiationException
70 | IllegalAccessException | InvocationTargetException e) {
71 // This case shouldn't happen
72 throw new IllegalArgumentException(e);
76 protected I getNeutronCRUD() {
77 return this.neutronCRUD;
80 protected Response show(String uuid,
82 List<String> fields) {
83 T ans = neutronCRUD.get(uuid);
85 throw new ResourceNotFoundException(uuidNoExist());
88 if (fields.size() > 0) {
89 return Response.status(HttpURLConnection.HTTP_OK).entity(newNeutronRequest(ans.extractFields(fields)))
92 return Response.status(HttpURLConnection.HTTP_OK).entity(newNeutronRequest(ans)).build();
96 protected Response create(final R input) {
97 if (input.isSingleton()) {
98 T singleton = input.getSingleton();
100 singleton.initDefaults();
101 neutronCRUD.add(singleton);
103 if (input.getBulk() == null) {
104 throw new BadRequestException("Invalid requests");
106 for (T test : input.getBulk()) {
108 neutronCRUD.add(test);
111 return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
114 protected void updateDelta(String uuid, T delta, T original) {
117 private boolean checkRevisionNumber(T original, T delta) {
118 // If new update is null ignore the original revision number
119 if (delta.getRevisionNumber() == null) {
122 // If what is stored is null no need for comparison
123 if (original.getRevisionNumber() == null) {
126 if (original.getRevisionNumber() > delta.getRevisionNumber()) {
132 protected Response update(String uuid, final R input) {
133 if (!input.isSingleton()) {
134 throw new BadRequestException("Only singleton edit supported");
136 T delta = input.getSingleton();
137 T original = neutronCRUD.get(uuid);
138 if (original == null) {
139 throw new ResourceNotFoundException(uuidNoExist());
141 if (checkRevisionNumber(original, delta)) {
142 return Response.status(HttpURLConnection.HTTP_OK).build();
144 updateDelta(uuid, delta, original);
146 * update the object and return it
148 if (!neutronCRUD.update(uuid, delta)) {
149 throw new ResourceNotFoundException(uuidNoExist());
151 T updated = neutronCRUD.get(uuid);
152 return Response.status(HttpURLConnection.HTTP_OK).entity(newNeutronRequest(updated)).build();
155 protected Response delete(String uuid) {
157 * remove it and return 204 status
159 if (!neutronCRUD.remove(uuid)) {
160 throw new ResourceNotFoundException(uuidNoExist());
163 return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();