suppress warn log if replying 500 just for OptimisticLockFailedException
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / AbstractNeutronNorthbound.java
1 /*
2  * Copyright (c) 2018 Intel Corporation and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.neutron.northbound.api;
9
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;
13
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;
29
30 public abstract class AbstractNeutronNorthbound<T extends INeutronObject<T>, R extends INeutronRequest<T>,
31         I extends INeutronCRUD<T>> {
32
33     private static final Logger LOG = LoggerFactory.getLogger(AbstractNeutronNorthbound.class);
34
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;
39
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)
43
44     private static final String INTERFACE_NAME_BASE = " CRUD Interface";
45     private static final String UUID_NO_EXIST_BASE = " UUID does not exist.";
46
47     private final I neutronCRUD;
48
49     protected AbstractNeutronNorthbound(I neutronCRUD) {
50         this.neutronCRUD = Objects.requireNonNull(neutronCRUD, "neutronCRUD");
51     }
52
53     protected final String serviceUnavailable() {
54         return getResourceName() + INTERFACE_NAME_BASE + RestMessages.SERVICEUNAVAILABLE.toString();
55     }
56
57     protected final String uuidNoExist() {
58         return getResourceName() + UUID_NO_EXIST_BASE;
59     }
60
61     protected abstract String getResourceName();
62
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];
67         return cls;
68     }
69
70     private R newNeutronRequest(T neutronObject) {
71         // return new R(neutronObject)
72
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);
77         try {
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);
85         }
86     }
87
88     protected I getNeutronCRUD() {
89         return this.neutronCRUD;
90     }
91
92     protected Response show(String uuid, List<String> returnFields)
93             throws DatastoreOperationFailedWebApplicationException {
94         try {
95             T ans = neutronCRUD.get(uuid);
96             if (ans == null) {
97                 throw new ResourceNotFoundException(uuidNoExist());
98             }
99
100             if (returnFields.size() > 0) {
101                 return Response.status(HttpURLConnection.HTTP_OK)
102                         .entity(newNeutronRequest(ans.extractFields(returnFields))).build();
103             } else {
104                 return Response.status(HttpURLConnection.HTTP_OK).entity(newNeutronRequest(ans)).build();
105             }
106         } catch (ReadFailedException e) {
107             LOG.warn("get failed due to datastore problem; uuid: {}", uuid);
108             throw new DatastoreOperationFailedWebApplicationException(e);
109         }
110     }
111
112     protected Response create(final R input) throws DatastoreOperationFailedWebApplicationException {
113         try {
114             if (input.isSingleton()) {
115                 T singleton = input.getSingleton();
116
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();
124                 }
125             } else {
126                 if (input.getBulk() == null) {
127                     throw new BadRequestException("Invalid requests");
128                 }
129                 for (T test : input.getBulk()) {
130                     test.initDefaults();
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();
137                     }
138                 }
139             }
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);
147         }
148     }
149
150     protected void updateDelta(String uuid, T delta, T original) {
151     }
152
153     private boolean checkRevisionNumber(T original, T delta) {
154         // If new update is null ignore the original revision number
155         if (delta.getRevisionNumber() == null) {
156             return false;
157         }
158         // If what is stored is null no need for comparison
159         if (original.getRevisionNumber() == null) {
160             return false;
161         }
162         if (original.getRevisionNumber() > delta.getRevisionNumber()) {
163             return true;
164         }
165         return false;
166     }
167
168     protected Response update(String uuid, final R input) throws DatastoreOperationFailedWebApplicationException {
169         if (!input.isSingleton()) {
170             throw new BadRequestException("Only singleton edit supported");
171         }
172         T delta = input.getSingleton();
173         try {
174             T original = neutronCRUD.get(uuid);
175             if (original == null) {
176                 throw new ResourceNotFoundException(uuidNoExist());
177             }
178             if (checkRevisionNumber(original, delta)) {
179                 return Response.status(HttpURLConnection.HTTP_OK).build();
180             }
181             updateDelta(uuid, delta, original);
182             /*
183              * update the object and return it
184              */
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();
191             }
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);
200         }
201     }
202
203     protected Response delete(String uuid) throws DatastoreOperationFailedWebApplicationException {
204         try {
205             // remove it and return 204 status
206             if (!neutronCRUD.remove(uuid)) {
207                 throw new ResourceNotFoundException(uuidNoExist());
208             } else {
209                 return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();
210             }
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);
217         }
218     }
219 }