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.opendaylight.neutron.spi.INeutronCRUD;
15 import org.opendaylight.neutron.spi.INeutronObject;
17 import javax.ws.rs.core.Response;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
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);
26 protected static final int HTTP_OK_BOTTOM = 200;
27 protected static final int HTTP_OK_TOP = 299;
29 private static final String INTERFACE_NAME_BASE = " CRUD Interface";
30 private static final String UUID_NO_EXIST_BASE = " UUID does not exist.";
32 protected final String serviceUnavailable() {
33 return getResourceName() + INTERFACE_NAME_BASE + RestMessages.SERVICEUNAVAILABLE.toString();
36 protected final String uuidNoExist() {
37 return getResourceName() + UUID_NO_EXIST_BASE;
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();
45 protected Response show(String uuid,
47 List<String> fields) {
48 I neutronCRUD = getNeutronCRUD();
49 T ans = neutronCRUD.get(uuid);
51 throw new ResourceNotFoundException(uuidNoExist());
54 if (fields.size() > 0) {
55 return Response.status(HttpURLConnection.HTTP_OK).entity(
56 newNeutronRequest(extractFields(ans, fields))).build();
58 return Response.status(HttpURLConnection.HTTP_OK).entity(newNeutronRequest(ans)).build();
62 protected Response create(final NeutronRequest input) {
63 I neutronCRUD = getNeutronCRUD();
64 if (input.isSingleton()) {
65 T singleton = input.getSingleton();
67 singleton.initDefaults();
68 neutronCRUD.add(singleton);
70 for (T test : input.getBulk()) {
72 neutronCRUD.add(test);
75 return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
78 protected void updateDelta(String uuid, T delta, T original) {
81 protected Response update(String uuid, final NeutronRequest input) {
82 I neutronCRUD = getNeutronCRUD();
83 if (!input.isSingleton()) {
84 throw new BadRequestException("Only singleton edit supported");
86 T delta = input.getSingleton();
87 T original = neutronCRUD.get(uuid);
88 if (original == null) {
89 throw new ResourceNotFoundException(uuidNoExist());
91 updateDelta(uuid, delta, original);
94 * update the object and return it
96 if (!neutronCRUD.update(uuid, delta)) {
97 throw new ResourceNotFoundException(uuidNoExist());
99 T updated = neutronCRUD.get(uuid);
100 return Response.status(HttpURLConnection.HTTP_OK).entity(newNeutronRequest(neutronCRUD.get(uuid))).build();
103 protected Response delete(String uuid) {
104 final I neutronCRUD = getNeutronCRUD();
106 T singleton = neutronCRUD.get(uuid);
109 * remove it and return 204 status
111 final String resourceName = getResourceName();
112 boolean exist = false;
114 exist = neutronCRUD.remove(uuid);
115 } catch (Exception e) {
116 LOGGER.debug("exception during remove {} {} {}",
117 resourceName, uuid, e);
118 throw new InternalServerErrorException("Could not delete " +
122 throw new ResourceNotFoundException(uuidNoExist());
125 return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();