2 * Copyright (c) 2018 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
8 package org.opendaylight.neutron.northbound.api;
10 import static org.opendaylight.neutron.spi.INeutronCRUD.Result.AlreadyExists;
11 import static org.opendaylight.neutron.spi.INeutronCRUD.Result.DependencyMissing;
12 import static org.opendaylight.neutron.spi.INeutronCRUD.Result.DoesNotExist;
14 import java.lang.reflect.Constructor;
15 import java.lang.reflect.InvocationTargetException;
16 import java.lang.reflect.ParameterizedType;
17 import java.net.HttpURLConnection;
18 import java.util.List;
19 import java.util.Objects;
20 import javax.ws.rs.core.Response;
21 import org.opendaylight.neutron.spi.INeutronCRUD;
22 import org.opendaylight.neutron.spi.INeutronCRUD.Result;
23 import org.opendaylight.neutron.spi.INeutronObject;
24 import org.opendaylight.yangtools.yang.common.OperationFailedException;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 public abstract class AbstractNeutronNorthbound<T extends INeutronObject<T>, R extends INeutronRequest<T>,
29 I extends INeutronCRUD<T>> {
31 private static final Logger LOG = LoggerFactory.getLogger(AbstractNeutronNorthbound.class);
33 // T extends INeutronObject<T> as 0th type argument
34 private static final int NEUTRON_ARGUMENT_TYPE_INDEX = 0;
35 // NeutronRequest extends INeutronRequest<T> as 1st type argument
36 private static final int NEUTRON_REQUEST_TYPE_INDEX = 1;
38 protected static final int HTTP_OK_BOTTOM = 200;
39 protected static final int HTTP_OK_TOP = 299;
40 private static final int HTTP_MISSING_DEPENDENCY = 442; // see NEUTRON-158 (also in neutron.e2etest.HttpUtils)
42 private static final String INTERFACE_NAME_BASE = " CRUD Interface";
43 private static final String UUID_NO_EXIST_BASE = " UUID does not exist.";
45 private final I neutronCRUD;
47 protected AbstractNeutronNorthbound(I neutronCRUD) {
48 this.neutronCRUD = Objects.requireNonNull(neutronCRUD, "neutronCRUD");
51 protected final String serviceUnavailable() {
52 return getResourceName() + INTERFACE_NAME_BASE + RestMessages.SERVICEUNAVAILABLE.toString();
55 protected final String uuidNoExist() {
56 return getResourceName() + UUID_NO_EXIST_BASE;
59 protected abstract String getResourceName();
61 private <K> Class<K> getActualTypeArgument(final int typeIndex) {
62 ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass();
63 @SuppressWarnings("unchecked")
64 Class<K> cls = (Class<K>) parameterizedType.getActualTypeArguments()[typeIndex];
68 private R newNeutronRequest(T neutronObject) {
69 // return new R(neutronObject)
71 // argumentClass = T.class
72 Class<T> argumentClass = getActualTypeArgument(NEUTRON_ARGUMENT_TYPE_INDEX);
73 // cls = NeturonRequest.class
74 Class<R> cls = getActualTypeArgument(NEUTRON_REQUEST_TYPE_INDEX);
76 // ctor = R constructor
77 Constructor<R> ctor = cls.getDeclaredConstructor(argumentClass);
78 return ctor.newInstance(neutronObject);
79 } catch (NoSuchMethodException | InstantiationException
80 | IllegalAccessException | InvocationTargetException e) {
81 // This case shouldn't happen
82 throw new IllegalArgumentException(e);
86 protected I getNeutronCRUD() {
87 return this.neutronCRUD;
90 protected Response show(String uuid, List<String> returnFields)
91 throws DatastoreOperationFailedWebApplicationException {
93 T ans = neutronCRUD.get(uuid);
95 throw new ResourceNotFoundException(uuidNoExist());
98 if (returnFields.size() > 0) {
99 return Response.status(HttpURLConnection.HTTP_OK)
100 .entity(newNeutronRequest(ans.extractFields(returnFields))).build();
102 return Response.status(HttpURLConnection.HTTP_OK).entity(newNeutronRequest(ans)).build();
104 } catch (OperationFailedException e) {
105 LOG.warn("get failed due to datastore problem; uuid: {}", uuid);
106 throw new DatastoreOperationFailedWebApplicationException(e);
110 protected Response create(final R input) throws DatastoreOperationFailedWebApplicationException {
112 if (input.isSingleton()) {
113 T singleton = input.getSingleton();
115 singleton.initDefaults();
116 Result result = neutronCRUD.add(singleton);
117 if (result.equals(DependencyMissing)) {
118 LOG.warn("create failed due to input missing dependencies: {}", input);
119 return Response.status(HTTP_MISSING_DEPENDENCY).entity(input).build();
120 } else if (result.equals(AlreadyExists)) {
121 return Response.status(HttpURLConnection.HTTP_CONFLICT).entity(input).build();
124 if (input.getBulk() == null) {
125 throw new BadRequestException("Invalid requests");
127 for (T test : input.getBulk()) {
129 Result result = neutronCRUD.add(test);
130 if (result.equals(DependencyMissing)) {
131 LOG.warn("create failed due to input missing dependencies: {}", input);
132 return Response.status(HTTP_MISSING_DEPENDENCY).entity(input).build();
133 } else if (result.equals(AlreadyExists)) {
134 return Response.status(HttpURLConnection.HTTP_CONFLICT).entity(input).build();
138 return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
139 } catch (OperationFailedException e) {
140 LOG.warn("create failed due to datastore problem (possibly missing required fields); input: {}", input);
141 throw new DatastoreOperationFailedWebApplicationException(e);
145 protected void updateDelta(String uuid, T delta, T original) {
148 private boolean checkRevisionNumber(T original, T delta) {
149 // If new update is null ignore the original revision number
150 if (delta.getRevisionNumber() == null) {
153 // If what is stored is null no need for comparison
154 if (original.getRevisionNumber() == null) {
157 if (original.getRevisionNumber() > delta.getRevisionNumber()) {
163 protected Response update(String uuid, final R input) throws DatastoreOperationFailedWebApplicationException {
164 if (!input.isSingleton()) {
165 throw new BadRequestException("Only singleton edit supported");
167 T delta = input.getSingleton();
169 T original = neutronCRUD.get(uuid);
170 if (original == null) {
171 throw new ResourceNotFoundException(uuidNoExist());
173 if (checkRevisionNumber(original, delta)) {
174 return Response.status(HttpURLConnection.HTTP_OK).build();
176 updateDelta(uuid, delta, original);
178 * update the object and return it
180 Result updateResult = neutronCRUD.update(uuid, delta);
181 if (updateResult.equals(DoesNotExist)) {
182 throw new ResourceNotFoundException(uuidNoExist());
183 } else if (updateResult.equals(DependencyMissing)) {
184 LOG.warn("update failed due to missing dependencies; input: {}", input);
185 return Response.status(HTTP_MISSING_DEPENDENCY).entity(input).build();
187 T updated = neutronCRUD.get(uuid);
188 return Response.status(HttpURLConnection.HTTP_OK).entity(newNeutronRequest(updated)).build();
189 } catch (OperationFailedException e) {
190 LOG.warn("update failed due to datastore problem (possibly missing required fields); input: {}", input);
191 throw new DatastoreOperationFailedWebApplicationException(e);
195 protected Response delete(String uuid) throws DatastoreOperationFailedWebApplicationException {
197 // remove it and return 204 status
198 if (!neutronCRUD.remove(uuid)) {
199 throw new ResourceNotFoundException(uuidNoExist());
201 return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();
203 } catch (OperationFailedException e) {
204 LOG.warn("delete failed due to datastore problem; uuid: {}", uuid);
205 throw new DatastoreOperationFailedWebApplicationException(e);