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 return Response.status(HTTP_MISSING_DEPENDENCY).entity(input).build();
119 } else if (result.equals(AlreadyExists)) {
120 return Response.status(HttpURLConnection.HTTP_CONFLICT).entity(input).build();
123 if (input.getBulk() == null) {
124 throw new BadRequestException("Invalid requests");
126 for (T test : input.getBulk()) {
128 Result result = neutronCRUD.add(test);
129 if (result.equals(DependencyMissing)) {
130 LOG.warn("create failed due to input missing dependencies: {}", input);
131 return Response.status(HTTP_MISSING_DEPENDENCY).entity(input).build();
132 } else if (result.equals(AlreadyExists)) {
133 return Response.status(HttpURLConnection.HTTP_CONFLICT).entity(input).build();
137 return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
138 } catch (OperationFailedException e) {
139 LOG.warn("create failed due to datastore problem (possibly missing required fields); input: {}", input);
140 throw new DatastoreOperationFailedWebApplicationException(e);
144 protected void updateDelta(String uuid, T delta, T original) {
147 private boolean checkRevisionNumber(T original, T delta) {
148 // If new update is null ignore the original revision number
149 if (delta.getRevisionNumber() == null) {
152 // If what is stored is null no need for comparison
153 if (original.getRevisionNumber() == null) {
156 if (original.getRevisionNumber() > delta.getRevisionNumber()) {
162 protected Response update(String uuid, final R input) throws DatastoreOperationFailedWebApplicationException {
163 if (!input.isSingleton()) {
164 throw new BadRequestException("Only singleton edit supported");
166 T delta = input.getSingleton();
168 T original = neutronCRUD.get(uuid);
169 if (original == null) {
170 throw new ResourceNotFoundException(uuidNoExist());
172 if (checkRevisionNumber(original, delta)) {
173 return Response.status(HttpURLConnection.HTTP_OK).build();
175 updateDelta(uuid, delta, original);
177 * update the object and return it
179 Result updateResult = neutronCRUD.update(uuid, delta);
180 if (updateResult.equals(DoesNotExist)) {
181 throw new ResourceNotFoundException(uuidNoExist());
182 } else if (updateResult.equals(DependencyMissing)) {
183 LOG.warn("update failed due to missing dependencies; input: {}", input);
184 return Response.status(HTTP_MISSING_DEPENDENCY).entity(input).build();
186 T updated = neutronCRUD.get(uuid);
187 return Response.status(HttpURLConnection.HTTP_OK).entity(newNeutronRequest(updated)).build();
188 } catch (OperationFailedException e) {
189 LOG.warn("update failed due to datastore problem (possibly missing required fields); input: {}", input);
190 throw new DatastoreOperationFailedWebApplicationException(e);
194 protected Response delete(String uuid) throws DatastoreOperationFailedWebApplicationException {
196 // remove it and return 204 status
197 if (!neutronCRUD.remove(uuid)) {
198 throw new ResourceNotFoundException(uuidNoExist());
200 return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();
202 } catch (OperationFailedException e) {
203 LOG.warn("delete failed due to datastore problem; uuid: {}", uuid);
204 throw new DatastoreOperationFailedWebApplicationException(e);