9a922c12d27bffe1e45ad1872a00c646c16d6b53
[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                     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();
121                 }
122             } else {
123                 if (input.getBulk() == null) {
124                     throw new BadRequestException("Invalid requests");
125                 }
126                 for (T test : input.getBulk()) {
127                     test.initDefaults();
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();
134                     }
135                 }
136             }
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);
141         }
142     }
143
144     protected void updateDelta(String uuid, T delta, T original) {
145     }
146
147     private boolean checkRevisionNumber(T original, T delta) {
148         // If new update is null ignore the original revision number
149         if (delta.getRevisionNumber() == null) {
150             return false;
151         }
152         // If what is stored is null no need for comparison
153         if (original.getRevisionNumber() == null) {
154             return false;
155         }
156         if (original.getRevisionNumber() > delta.getRevisionNumber()) {
157             return true;
158         }
159         return false;
160     }
161
162     protected Response update(String uuid, final R input) throws DatastoreOperationFailedWebApplicationException {
163         if (!input.isSingleton()) {
164             throw new BadRequestException("Only singleton edit supported");
165         }
166         T delta = input.getSingleton();
167         try {
168             T original = neutronCRUD.get(uuid);
169             if (original == null) {
170                 throw new ResourceNotFoundException(uuidNoExist());
171             }
172             if (checkRevisionNumber(original, delta)) {
173                 return Response.status(HttpURLConnection.HTTP_OK).build();
174             }
175             updateDelta(uuid, delta, original);
176             /*
177              * update the object and return it
178              */
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();
185             }
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);
191         }
192     }
193
194     protected Response delete(String uuid) throws DatastoreOperationFailedWebApplicationException {
195         try {
196             // remove it and return 204 status
197             if (!neutronCRUD.remove(uuid)) {
198                 throw new ResourceNotFoundException(uuidNoExist());
199             } else {
200                 return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();
201             }
202         } catch (OperationFailedException e) {
203             LOG.warn("delete failed due to datastore problem; uuid: {}", uuid);
204             throw new DatastoreOperationFailedWebApplicationException(e);
205         }
206     }
207 }