add missing warn log to AbstractNeutronNorthbound for 442
[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.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;
27
28 public abstract class AbstractNeutronNorthbound<T extends INeutronObject<T>, R extends INeutronRequest<T>,
29         I extends INeutronCRUD<T>> {
30
31     private static final Logger LOG = LoggerFactory.getLogger(AbstractNeutronNorthbound.class);
32
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;
37
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)
41
42     private static final String INTERFACE_NAME_BASE = " CRUD Interface";
43     private static final String UUID_NO_EXIST_BASE = " UUID does not exist.";
44
45     private final I neutronCRUD;
46
47     protected AbstractNeutronNorthbound(I neutronCRUD) {
48         this.neutronCRUD = Objects.requireNonNull(neutronCRUD, "neutronCRUD");
49     }
50
51     protected final String serviceUnavailable() {
52         return getResourceName() + INTERFACE_NAME_BASE + RestMessages.SERVICEUNAVAILABLE.toString();
53     }
54
55     protected final String uuidNoExist() {
56         return getResourceName() + UUID_NO_EXIST_BASE;
57     }
58
59     protected abstract String getResourceName();
60
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];
65         return cls;
66     }
67
68     private R newNeutronRequest(T neutronObject) {
69         // return new R(neutronObject)
70
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);
75         try {
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);
83         }
84     }
85
86     protected I getNeutronCRUD() {
87         return this.neutronCRUD;
88     }
89
90     protected Response show(String uuid, List<String> returnFields)
91             throws DatastoreOperationFailedWebApplicationException {
92         try {
93             T ans = neutronCRUD.get(uuid);
94             if (ans == null) {
95                 throw new ResourceNotFoundException(uuidNoExist());
96             }
97
98             if (returnFields.size() > 0) {
99                 return Response.status(HttpURLConnection.HTTP_OK)
100                         .entity(newNeutronRequest(ans.extractFields(returnFields))).build();
101             } else {
102                 return Response.status(HttpURLConnection.HTTP_OK).entity(newNeutronRequest(ans)).build();
103             }
104         } catch (OperationFailedException e) {
105             LOG.warn("get failed due to datastore problem; uuid: {}", uuid);
106             throw new DatastoreOperationFailedWebApplicationException(e);
107         }
108     }
109
110     protected Response create(final R input) throws DatastoreOperationFailedWebApplicationException {
111         try {
112             if (input.isSingleton()) {
113                 T singleton = input.getSingleton();
114
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();
122                 }
123             } else {
124                 if (input.getBulk() == null) {
125                     throw new BadRequestException("Invalid requests");
126                 }
127                 for (T test : input.getBulk()) {
128                     test.initDefaults();
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();
135                     }
136                 }
137             }
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);
142         }
143     }
144
145     protected void updateDelta(String uuid, T delta, T original) {
146     }
147
148     private boolean checkRevisionNumber(T original, T delta) {
149         // If new update is null ignore the original revision number
150         if (delta.getRevisionNumber() == null) {
151             return false;
152         }
153         // If what is stored is null no need for comparison
154         if (original.getRevisionNumber() == null) {
155             return false;
156         }
157         if (original.getRevisionNumber() > delta.getRevisionNumber()) {
158             return true;
159         }
160         return false;
161     }
162
163     protected Response update(String uuid, final R input) throws DatastoreOperationFailedWebApplicationException {
164         if (!input.isSingleton()) {
165             throw new BadRequestException("Only singleton edit supported");
166         }
167         T delta = input.getSingleton();
168         try {
169             T original = neutronCRUD.get(uuid);
170             if (original == null) {
171                 throw new ResourceNotFoundException(uuidNoExist());
172             }
173             if (checkRevisionNumber(original, delta)) {
174                 return Response.status(HttpURLConnection.HTTP_OK).build();
175             }
176             updateDelta(uuid, delta, original);
177             /*
178              * update the object and return it
179              */
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();
186             }
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);
192         }
193     }
194
195     protected Response delete(String uuid) throws DatastoreOperationFailedWebApplicationException {
196         try {
197             // remove it and return 204 status
198             if (!neutronCRUD.remove(uuid)) {
199                 throw new ResourceNotFoundException(uuidNoExist());
200             } else {
201                 return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();
202             }
203         } catch (OperationFailedException e) {
204             LOG.warn("delete failed due to datastore problem; uuid: {}", uuid);
205             throw new DatastoreOperationFailedWebApplicationException(e);
206         }
207     }
208 }