2 * Copyright (c) 2014, 2015 Red Hat, Inc. 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.ovsdb.northbound;
11 import com.fasterxml.jackson.databind.SerializationFeature;
12 import java.io.BufferedReader;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.io.InputStreamReader;
18 import javax.ws.rs.Consumes;
19 import javax.ws.rs.DELETE;
20 import javax.ws.rs.GET;
21 import javax.ws.rs.POST;
22 import javax.ws.rs.PUT;
23 import javax.ws.rs.Path;
24 import javax.ws.rs.PathParam;
25 import javax.ws.rs.Produces;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
29 import org.opendaylight.controller.northbound.commons.RestMessages;
30 import org.opendaylight.controller.northbound.commons.exception.BadRequestException;
31 import org.opendaylight.controller.northbound.commons.exception.ServiceUnavailableException;
32 import org.opendaylight.ovsdb.lib.OvsdbClient;
33 import org.opendaylight.ovsdb.lib.notation.Row;
34 import org.opendaylight.ovsdb.lib.notation.UUID;
35 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
36 import org.opendaylight.ovsdb.plugin.api.OvsVswitchdSchemaConstants;
37 import org.opendaylight.ovsdb.plugin.api.OvsdbConfigurationService;
38 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
41 import com.fasterxml.jackson.core.JsonProcessingException;
42 import com.fasterxml.jackson.databind.DeserializationFeature;
43 import com.fasterxml.jackson.databind.JsonNode;
44 import com.fasterxml.jackson.databind.ObjectMapper;
47 * Northbound interface for OVSDB Rows
49 public class RowResource {
54 ObjectMapper objectMapper;
56 public RowResource(String nodeId, String databaseName, String tableName) {
58 this.databaseName = databaseName;
59 this.tableName = tableName;
60 objectMapper = new ObjectMapper();
61 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
62 objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
65 private OvsdbRow getOvsdbRow (InputStream stream) throws IOException {
66 StringBuilder rowNodeStrBuilder = new StringBuilder();
67 BufferedReader in = new BufferedReader(new InputStreamReader(stream));
69 while ((line = in.readLine()) != null) {
70 rowNodeStrBuilder.append(line);
72 JsonNode jsonNode = objectMapper.readTree(rowNodeStrBuilder.toString());
73 OvsdbClient client = NodeResource.getOvsdbClient(nodeId, this);
74 return OvsdbRow.fromJsonNode(client, OvsVswitchdSchemaConstants.DATABASE_NAME, jsonNode);
78 @Produces(MediaType.APPLICATION_JSON)
79 public Response getRows() throws JsonProcessingException {
80 OvsdbConfigurationService
81 ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
83 if (ovsdbTable == null) {
84 throw new ServiceUnavailableException("Ovsdb ConfigurationService " + RestMessages.SERVICEUNAVAILABLE.toString());
87 Node node = NodeResource.getOvsdbNode(nodeId, this);
88 Map<UUID,Row<GenericTableSchema>> rows = null;
90 rows = ovsdbTable.getRows(node, databaseName, tableName);
91 } catch (Exception e) {
92 throw new BadRequestException(e.getMessage());
94 String response = objectMapper.writeValueAsString(rows);
95 return Response.status(Response.Status.OK)
101 @Consumes(MediaType.APPLICATION_JSON)
102 @Produces(MediaType.APPLICATION_JSON)
103 public Response createRow(InputStream stream) throws IOException {
104 OvsdbRow localRow = this.getOvsdbRow(stream);
105 if (localRow == null) {
106 return Response.status(Response.Status.BAD_REQUEST).build();
108 OvsdbConfigurationService
109 ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
111 if (ovsdbTable == null) {
112 throw new ServiceUnavailableException("OVS Configuration Service " + RestMessages.SERVICEUNAVAILABLE.toString());
115 Node node = NodeResource.getOvsdbNode(nodeId, this);
116 Row row = ovsdbTable.insertTree(node, OvsVswitchdSchemaConstants.DATABASE_NAME, tableName,
117 localRow.getParentTable(), new UUID(localRow.getParentUuid()), localRow.getParentColumn(),
119 String response = objectMapper.writeValueAsString(row);
120 return Response.status(Response.Status.CREATED)
127 @Produces(MediaType.APPLICATION_JSON)
128 public Response getRowDetails(@PathParam("id") String id) throws JsonProcessingException {
129 OvsdbConfigurationService
130 ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
132 if (ovsdbTable == null) {
133 throw new ServiceUnavailableException("Ovsdb ConfigurationService " + RestMessages.SERVICEUNAVAILABLE.toString());
136 Node node = NodeResource.getOvsdbNode(nodeId, this);
137 Row<GenericTableSchema> row = null;
139 row = ovsdbTable.getRow(node, databaseName, tableName, new UUID(id));
140 } catch (Exception e) {
141 throw new BadRequestException(e.getMessage());
143 String response = objectMapper.writeValueAsString(row);
144 return Response.status(Response.Status.OK)
151 @Consumes(MediaType.APPLICATION_JSON)
152 @Produces(MediaType.APPLICATION_JSON)
153 public Response updateRow(@PathParam("id") String id, InputStream stream) throws IOException{
154 OvsdbRow localRow = this.getOvsdbRow(stream);
155 if (localRow == null) {
156 return Response.status(Response.Status.BAD_REQUEST).build();
158 OvsdbConfigurationService ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
160 if (ovsdbTable == null) {
161 throw new ServiceUnavailableException("OVS Configuration Service " + RestMessages.SERVICEUNAVAILABLE.toString());
164 Node node = NodeResource.getOvsdbNode(nodeId, this);
165 Row<GenericTableSchema> row = ovsdbTable.updateRow(node, databaseName, tableName, new UUID(id), localRow.getRow(), true);
166 String response = objectMapper.writeValueAsString(row);
167 return Response.status(Response.Status.OK)
174 @Consumes(MediaType.APPLICATION_JSON)
175 public Response deleteRow(@PathParam("id") String id){
176 OvsdbConfigurationService
177 ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
179 if (ovsdbTable == null) {
180 throw new ServiceUnavailableException("Ovsdb ConfigurationService " + RestMessages.SERVICEUNAVAILABLE.toString());
183 Node node = NodeResource.getOvsdbNode(nodeId, this);
185 ovsdbTable.deleteRow(node, databaseName, tableName, new UUID(id));
186 } catch (Exception e) {
187 throw new BadRequestException(e.getMessage());
189 return Response.status(Response.Status.OK)