Merge "Fix for multiple external fixedIPs under router-ext-gw-info"
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / AbstractNeutronNorthbound.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.neutron.northbound.api;
10
11 import java.net.HttpURLConnection;
12 import java.util.List;
13
14 import org.opendaylight.neutron.spi.INeutronCRUD;
15 import org.opendaylight.neutron.spi.INeutronObject;
16
17 import javax.ws.rs.core.Response;
18
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 public abstract class AbstractNeutronNorthbound<T extends INeutronObject, NeutronRequest extends INeutronRequest<T>, I extends INeutronCRUD<T>> {
23     private static final Logger LOGGER = LoggerFactory
24         .getLogger(AbstractNeutronNorthbound.class);
25
26     protected static final int HTTP_OK_BOTTOM = 200;
27     protected static final int HTTP_OK_TOP = 299;
28
29     private static final String INTERFACE_NAME_BASE = " CRUD Interface";
30     private static final String UUID_NO_EXIST_BASE = " UUID does not exist.";
31
32     protected final String serviceUnavailable() {
33         return getResourceName() + INTERFACE_NAME_BASE + RestMessages.SERVICEUNAVAILABLE.toString();
34     }
35
36     protected final String uuidNoExist() {
37         return getResourceName() + UUID_NO_EXIST_BASE;
38     }
39
40     protected abstract String getResourceName();
41     protected abstract T extractFields(T o, List<String> fields);
42     protected abstract NeutronRequest newNeutronRequest(T o);
43     protected abstract I getNeutronCRUD();
44
45     protected Response show(String uuid,
46                             // return fields
47                             List<String> fields) {
48         I neutronCRUD = getNeutronCRUD();
49         T ans = neutronCRUD.get(uuid);
50         if (ans == null) {
51             throw new ResourceNotFoundException(uuidNoExist());
52         }
53
54         if (fields.size() > 0) {
55             return Response.status(HttpURLConnection.HTTP_OK).entity(
56                     newNeutronRequest(extractFields(ans, fields))).build();
57         } else {
58             return Response.status(HttpURLConnection.HTTP_OK).entity(newNeutronRequest(ans)).build();
59         }
60     }
61
62     protected Response create(final NeutronRequest input) {
63         I neutronCRUD = getNeutronCRUD();
64         if (input.isSingleton()) {
65             T singleton = input.getSingleton();
66
67             singleton.initDefaults();
68             neutronCRUD.add(singleton);
69         } else {
70             if (input.getBulk() == null) {
71                 throw new BadRequestException("Invalid requests");
72             }
73             for (T test : input.getBulk()) {
74                 test.initDefaults();
75                 neutronCRUD.add(test);
76             }
77         }
78         return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
79     }
80
81     protected void updateDelta(String uuid, T delta, T original) {
82     }
83
84     protected Response update(String uuid, final NeutronRequest input) {
85         I neutronCRUD = getNeutronCRUD();
86         if (!input.isSingleton()) {
87             throw new BadRequestException("Only singleton edit supported");
88         }
89         T delta = input.getSingleton();
90         T original = neutronCRUD.get(uuid);
91         if (original == null) {
92             throw new ResourceNotFoundException(uuidNoExist());
93         }
94         updateDelta(uuid, delta, original);
95
96         /*
97          * update the object and return it
98          */
99         if (!neutronCRUD.update(uuid, delta)) {
100             throw new ResourceNotFoundException(uuidNoExist());
101         }
102         T updated = neutronCRUD.get(uuid);
103         return Response.status(HttpURLConnection.HTTP_OK).entity(newNeutronRequest(updated)).build();
104     }
105
106     protected Response delete(String uuid) {
107         final I neutronCRUD = getNeutronCRUD();
108
109         /*
110          * remove it and return 204 status
111          */
112         final String resourceName = getResourceName();
113         boolean exist = false;
114         try {
115             exist = neutronCRUD.remove(uuid);
116         } catch (Exception e) {
117             LOGGER.debug("exception during remove {} {} {}",
118                          resourceName, uuid, e);
119             throw new InternalServerErrorException("Could not delete " +
120                                                    resourceName);
121         }
122         if (!exist) {
123             throw new ResourceNotFoundException(uuidNoExist());
124         }
125
126         return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();
127     }
128 }