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.controller.md.sal.common.api.data.OptimisticLockFailedException;
22 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
23 import org.opendaylight.neutron.spi.INeutronCRUD;
24 import org.opendaylight.neutron.spi.INeutronCRUD.Result;
25 import org.opendaylight.neutron.spi.INeutronObject;
26 import org.opendaylight.yangtools.yang.common.OperationFailedException;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public abstract class AbstractNeutronNorthbound<T extends INeutronObject<T>, R extends INeutronRequest<T>,
31 I extends INeutronCRUD<T>> {
33 private static final Logger LOG = LoggerFactory.getLogger(AbstractNeutronNorthbound.class);
35 // T extends INeutronObject<T> as 0th type argument
36 private static final int NEUTRON_ARGUMENT_TYPE_INDEX = 0;
37 // NeutronRequest extends INeutronRequest<T> as 1st type argument
38 private static final int NEUTRON_REQUEST_TYPE_INDEX = 1;
40 protected static final int HTTP_OK_BOTTOM = 200;
41 protected static final int HTTP_OK_TOP = 299;
42 private static final int HTTP_MISSING_DEPENDENCY = 442; // see NEUTRON-158 (also in neutron.e2etest.HttpUtils)
44 private static final String INTERFACE_NAME_BASE = " CRUD Interface";
45 private static final String UUID_NO_EXIST_BASE = " UUID does not exist.";
47 private final I neutronCRUD;
49 protected AbstractNeutronNorthbound(I neutronCRUD) {
50 this.neutronCRUD = Objects.requireNonNull(neutronCRUD, "neutronCRUD");
53 protected final String serviceUnavailable() {
54 return getResourceName() + INTERFACE_NAME_BASE + RestMessages.SERVICEUNAVAILABLE.toString();
57 protected final String uuidNoExist() {
58 return getResourceName() + UUID_NO_EXIST_BASE;
61 protected abstract String getResourceName();
63 private <K> Class<K> getActualTypeArgument(final int typeIndex) {
64 ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass();
65 @SuppressWarnings("unchecked")
66 Class<K> cls = (Class<K>) parameterizedType.getActualTypeArguments()[typeIndex];
70 private R newNeutronRequest(T neutronObject) {
71 // return new R(neutronObject)
73 // argumentClass = T.class
74 Class<T> argumentClass = getActualTypeArgument(NEUTRON_ARGUMENT_TYPE_INDEX);
75 // cls = NeturonRequest.class
76 Class<R> cls = getActualTypeArgument(NEUTRON_REQUEST_TYPE_INDEX);
78 // ctor = R constructor
79 Constructor<R> ctor = cls.getDeclaredConstructor(argumentClass);
80 return ctor.newInstance(neutronObject);
81 } catch (NoSuchMethodException | InstantiationException
82 | IllegalAccessException | InvocationTargetException e) {
83 // This case shouldn't happen
84 throw new IllegalArgumentException(e);
88 protected I getNeutronCRUD() {
89 return this.neutronCRUD;
92 protected Response show(String uuid, List<String> returnFields)
93 throws DatastoreOperationFailedWebApplicationException {
95 T ans = neutronCRUD.get(uuid);
97 throw new ResourceNotFoundException(uuidNoExist());
100 if (returnFields.size() > 0) {
101 return Response.status(HttpURLConnection.HTTP_OK)
102 .entity(newNeutronRequest(ans.extractFields(returnFields))).build();
104 return Response.status(HttpURLConnection.HTTP_OK).entity(newNeutronRequest(ans)).build();
106 } catch (ReadFailedException e) {
107 LOG.warn("get failed due to datastore problem; uuid: {}", uuid);
108 throw new DatastoreOperationFailedWebApplicationException(e);
112 protected Response create(final R input) throws DatastoreOperationFailedWebApplicationException {
114 if (input.isSingleton()) {
115 T singleton = input.getSingleton();
117 singleton.initDefaults();
118 Result result = neutronCRUD.add(singleton);
119 if (result.equals(DependencyMissing)) {
120 LOG.warn("create failed due to input missing dependencies: {}", input);
121 return Response.status(HTTP_MISSING_DEPENDENCY).entity(input).build();
122 } else if (result.equals(AlreadyExists)) {
123 return Response.status(HttpURLConnection.HTTP_CONFLICT).entity(input).build();
126 if (input.getBulk() == null) {
127 throw new BadRequestException("Invalid requests");
129 for (T test : input.getBulk()) {
131 Result result = neutronCRUD.add(test);
132 if (result.equals(DependencyMissing)) {
133 LOG.warn("create failed due to input missing dependencies: {}", input);
134 return Response.status(HTTP_MISSING_DEPENDENCY).entity(input).build();
135 } else if (result.equals(AlreadyExists)) {
136 return Response.status(HttpURLConnection.HTTP_CONFLICT).entity(input).build();
140 return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
141 } catch (OptimisticLockFailedException e) {
142 // Do not log this, it's "normal" - the driver will retry
143 throw new DatastoreOperationFailedWebApplicationException(e);
144 } catch (OperationFailedException e) {
145 LOG.warn("create failed due to datastore problem (possibly missing required fields); input: {}", input);
146 throw new DatastoreOperationFailedWebApplicationException(e);
150 protected void updateDelta(String uuid, T delta, T original) {
153 private boolean checkRevisionNumber(T original, T delta) {
154 // If new update is null ignore the original revision number
155 if (delta.getRevisionNumber() == null) {
158 // If what is stored is null no need for comparison
159 if (original.getRevisionNumber() == null) {
162 if (original.getRevisionNumber() > delta.getRevisionNumber()) {
168 protected Response update(String uuid, final R input) throws DatastoreOperationFailedWebApplicationException {
169 if (!input.isSingleton()) {
170 throw new BadRequestException("Only singleton edit supported");
172 T delta = input.getSingleton();
174 T original = neutronCRUD.get(uuid);
175 if (original == null) {
176 throw new ResourceNotFoundException(uuidNoExist());
178 if (checkRevisionNumber(original, delta)) {
179 return Response.status(HttpURLConnection.HTTP_OK).build();
181 updateDelta(uuid, delta, original);
183 * update the object and return it
185 Result updateResult = neutronCRUD.update(uuid, delta);
186 if (updateResult.equals(DoesNotExist)) {
187 throw new ResourceNotFoundException(uuidNoExist());
188 } else if (updateResult.equals(DependencyMissing)) {
189 LOG.warn("update failed due to missing dependencies; input: {}", input);
190 return Response.status(HTTP_MISSING_DEPENDENCY).entity(input).build();
192 T updated = neutronCRUD.get(uuid);
193 return Response.status(HttpURLConnection.HTTP_OK).entity(newNeutronRequest(updated)).build();
194 } catch (OptimisticLockFailedException e) {
195 // Do not log this, it's "normal" - the driver will retry
196 throw new DatastoreOperationFailedWebApplicationException(e);
197 } catch (OperationFailedException e) {
198 LOG.warn("update failed due to datastore problem (possibly missing required fields); input: {}", input);
199 throw new DatastoreOperationFailedWebApplicationException(e);
203 protected Response delete(String uuid) throws DatastoreOperationFailedWebApplicationException {
205 // remove it and return 204 status
206 if (!neutronCRUD.remove(uuid)) {
207 throw new ResourceNotFoundException(uuidNoExist());
209 return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();
211 } catch (OptimisticLockFailedException e) {
212 // Do not log this, it's "normal" - the driver will retry
213 throw new DatastoreOperationFailedWebApplicationException(e);
214 } catch (OperationFailedException e) {
215 LOG.warn("delete failed due to datastore problem; uuid: {}", uuid);
216 throw new DatastoreOperationFailedWebApplicationException(e);