1 package org.opendaylight.ovsdb.northbound;
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.InputStreamReader;
9 import javax.ws.rs.Consumes;
10 import javax.ws.rs.DELETE;
11 import javax.ws.rs.GET;
12 import javax.ws.rs.POST;
13 import javax.ws.rs.PUT;
14 import javax.ws.rs.Path;
15 import javax.ws.rs.PathParam;
16 import javax.ws.rs.Produces;
17 import javax.ws.rs.core.MediaType;
18 import javax.ws.rs.core.Response;
20 import org.opendaylight.controller.northbound.commons.RestMessages;
21 import org.opendaylight.controller.northbound.commons.exception.BadRequestException;
22 import org.opendaylight.controller.northbound.commons.exception.ServiceUnavailableException;
23 import org.opendaylight.controller.sal.core.Node;
24 import org.opendaylight.controller.sal.utils.ServiceHelper;
25 import org.opendaylight.ovsdb.lib.OvsdbClient;
26 import org.opendaylight.ovsdb.lib.notation.Row;
27 import org.opendaylight.ovsdb.lib.notation.UUID;
28 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
29 import org.opendaylight.ovsdb.plugin.api.OvsVswitchdSchemaConstants;
30 import org.opendaylight.ovsdb.plugin.api.OvsdbConfigurationService;
32 import com.fasterxml.jackson.core.JsonProcessingException;
33 import com.fasterxml.jackson.databind.DeserializationFeature;
34 import com.fasterxml.jackson.databind.JsonNode;
35 import com.fasterxml.jackson.databind.ObjectMapper;
38 * Northbound interface for OVSDB Rows
40 public class RowResource {
45 ObjectMapper objectMapper;
47 public RowResource(String nodeId, String databaseName, String tableName) {
49 this.databaseName = databaseName;
50 this.tableName = tableName;
51 objectMapper = new ObjectMapper();
52 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
56 private OvsdbRow getOvsdbRow (InputStream stream) throws IOException {
57 StringBuilder rowNodeStrBuilder = new StringBuilder();
58 BufferedReader in = new BufferedReader(new InputStreamReader(stream));
60 while ((line = in.readLine()) != null) {
61 rowNodeStrBuilder.append(line);
63 JsonNode jsonNode = objectMapper.readTree(rowNodeStrBuilder.toString());
64 OvsdbClient client = NodeResource.getOvsdbClient(nodeId, this);
65 return OvsdbRow.fromJsonNode(client, OvsVswitchdSchemaConstants.DATABASE_NAME, jsonNode);
69 @Produces(MediaType.APPLICATION_JSON)
70 public Response getRows() throws JsonProcessingException {
71 OvsdbConfigurationService
72 ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
74 if (ovsdbTable == null) {
75 throw new ServiceUnavailableException("Ovsdb ConfigurationService " + RestMessages.SERVICEUNAVAILABLE.toString());
78 Node node = NodeResource.getOvsdbNode(nodeId, this);
79 Map<UUID,Row<GenericTableSchema>> rows = null;
81 rows = ovsdbTable.getRows(node, databaseName, tableName);
82 } catch (Exception e) {
83 throw new BadRequestException(e.getMessage());
85 String response = objectMapper.writeValueAsString(rows);
86 return Response.status(Response.Status.OK)
92 @Consumes(MediaType.APPLICATION_JSON)
93 @Produces(MediaType.APPLICATION_JSON)
94 public Response createRow(InputStream stream) throws IOException {
95 OvsdbRow localRow = this.getOvsdbRow(stream);
96 if (localRow == null) {
97 return Response.status(Response.Status.BAD_REQUEST).build();
99 OvsdbConfigurationService
100 ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
102 if (ovsdbTable == null) {
103 throw new ServiceUnavailableException("OVS Configuration Service " + RestMessages.SERVICEUNAVAILABLE.toString());
106 Node node = NodeResource.getOvsdbNode(nodeId, this);
107 Row row = ovsdbTable.insertTree(node, OvsVswitchdSchemaConstants.DATABASE_NAME, tableName,
108 localRow.getParentTable(), new UUID(localRow.getParentUuid()), localRow.getParentColumn(),
110 String response = objectMapper.writeValueAsString(row);
111 return Response.status(Response.Status.CREATED)
118 @Produces(MediaType.APPLICATION_JSON)
119 public Response getRowDetails(@PathParam("id") String id) throws JsonProcessingException {
120 OvsdbConfigurationService
121 ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
123 if (ovsdbTable == null) {
124 throw new ServiceUnavailableException("Ovsdb ConfigurationService " + RestMessages.SERVICEUNAVAILABLE.toString());
127 Node node = NodeResource.getOvsdbNode(nodeId, this);
128 Row<GenericTableSchema> row = null;
130 row = ovsdbTable.getRow(node, databaseName, tableName, new UUID(id));
131 } catch (Exception e) {
132 throw new BadRequestException(e.getMessage());
134 String response = objectMapper.writeValueAsString(row);
135 return Response.status(Response.Status.OK)
142 @Consumes(MediaType.APPLICATION_JSON)
143 @Produces(MediaType.APPLICATION_JSON)
144 public Response updateRow(@PathParam("id") String id, InputStream stream) throws IOException{
145 OvsdbRow localRow = this.getOvsdbRow(stream);
146 if (localRow == null) {
147 return Response.status(Response.Status.BAD_REQUEST).build();
149 OvsdbConfigurationService ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
151 if (ovsdbTable == null) {
152 throw new ServiceUnavailableException("OVS Configuration Service " + RestMessages.SERVICEUNAVAILABLE.toString());
155 Node node = NodeResource.getOvsdbNode(nodeId, this);
156 Row<GenericTableSchema> row = ovsdbTable.updateRow(node, databaseName, tableName, new UUID(id), localRow.getRow(), true);
157 String response = objectMapper.writeValueAsString(row);
158 return Response.status(Response.Status.OK)
165 @Consumes(MediaType.APPLICATION_JSON)
166 public Response deleteRow(@PathParam("id") String id){
167 OvsdbConfigurationService
168 ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
170 if (ovsdbTable == null) {
171 throw new ServiceUnavailableException("Ovsdb ConfigurationService " + RestMessages.SERVICEUNAVAILABLE.toString());
174 Node node = NodeResource.getOvsdbNode(nodeId, this);
176 ovsdbTable.deleteRow(node, databaseName, tableName, new UUID(id));
177 } catch (Exception e) {
178 throw new BadRequestException(e.getMessage());
180 return Response.status(Response.Status.OK)