2 * Copyright (c) 2015 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
9 package org.opendaylight.neutron.northbound.api;
11 import java.net.HttpURLConnection;
12 import java.util.List;
14 import org.codehaus.enunciate.jaxrs.ResponseCode;
15 import org.codehaus.enunciate.jaxrs.StatusCodes;
16 import org.opendaylight.neutron.spi.INeutronCRUD;
17 import org.opendaylight.neutron.spi.NeutronObject;
19 import javax.ws.rs.core.Response;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 public abstract class AbstractNeutronNorthbound<T extends NeutronObject, NeutronRequest extends INeutronRequest<T>, I extends INeutronCRUD<T>> {
25 private static final Logger LOGGER = LoggerFactory
26 .getLogger(AbstractNeutronNorthbound.class);
28 protected static final int HTTP_OK_BOTTOM = 200;
29 protected static final int HTTP_OK_TOP = 299;
31 private static final String INTERFACE_NAME_BASE = " CRUD Interface";
32 private static final String UUID_NO_EXIST_BASE = " UUID does not exist.";
34 protected final String serviceUnavailable() {
35 return getResourceName() + INTERFACE_NAME_BASE + RestMessages.SERVICEUNAVAILABLE.toString();
38 protected final String uuidNoExist() {
39 return getResourceName() + UUID_NO_EXIST_BASE;
42 protected abstract String getResourceName();
43 protected abstract T extractFields(T o, List<String> fields);
44 protected abstract NeutronRequest newNeutronRequest(T o);
45 protected abstract I getNeutronCRUD();
47 protected Response show(String uuid,
49 List<String> fields) {
50 I neutronCRUD = getNeutronCRUD();
51 if (!neutronCRUD.exists(uuid)) {
52 throw new ResourceNotFoundException(uuidNoExist());
54 if (fields.size() > 0) {
55 T ans = neutronCRUD.get(uuid);
56 return Response.status(HttpURLConnection.HTTP_OK).entity(
57 newNeutronRequest(extractFields(ans, fields))).build();
59 return Response.status(HttpURLConnection.HTTP_OK).entity(newNeutronRequest(neutronCRUD.get(uuid))).build();
63 protected Response create(final NeutronRequest input) {
64 I neutronCRUD = getNeutronCRUD();
65 if (input.isSingleton()) {
66 T singleton = input.getSingleton();
68 singleton.initDefaults();
69 neutronCRUD.add(singleton);
71 for (T test : input.getBulk()) {
73 neutronCRUD.add(test);
76 return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
79 protected void updateDelta(String uuid, T delta, T original) {
82 protected Response update(String uuid, final NeutronRequest input) {
83 I neutronCRUD = getNeutronCRUD();
84 if (!input.isSingleton()) {
85 throw new BadRequestException("Only singleton edit supported");
87 T delta = input.getSingleton();
88 T original = neutronCRUD.get(uuid);
89 if (original == null) {
90 throw new ResourceNotFoundException(getResourceName() + " doesn't Exist");
92 updateDelta(uuid, delta, original);
95 * update the object and return it
97 neutronCRUD.update(uuid, delta);
98 T updated = neutronCRUD.get(uuid);
99 return Response.status(HttpURLConnection.HTTP_OK).entity(newNeutronRequest(neutronCRUD.get(uuid))).build();
102 protected Response delete(String uuid) {
103 final I neutronCRUD = getNeutronCRUD();
105 T singleton = neutronCRUD.get(uuid);
108 * remove it and return 204 status
110 final String resourceName = getResourceName();
111 boolean exist = false;
113 exist = neutronCRUD.remove(uuid);
114 } catch (Exception e) {
115 LOGGER.debug("exception during remove {} {} {}",
116 resourceName, uuid, e);
117 throw new InternalServerErrorException("Could not delete " +
121 throw new ResourceNotFoundException(uuidNoExist());
124 return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();