1 package org.opendaylight.ovsdb.northbound;
3 import com.fasterxml.jackson.databind.SerializationFeature;
4 import java.io.BufferedReader;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.InputStreamReader;
10 import javax.ws.rs.Consumes;
11 import javax.ws.rs.DELETE;
12 import javax.ws.rs.GET;
13 import javax.ws.rs.POST;
14 import javax.ws.rs.PUT;
15 import javax.ws.rs.Path;
16 import javax.ws.rs.PathParam;
17 import javax.ws.rs.Produces;
18 import javax.ws.rs.core.MediaType;
19 import javax.ws.rs.core.Response;
21 import org.opendaylight.controller.northbound.commons.RestMessages;
22 import org.opendaylight.controller.northbound.commons.exception.BadRequestException;
23 import org.opendaylight.controller.northbound.commons.exception.ServiceUnavailableException;
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;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
33 import com.fasterxml.jackson.core.JsonProcessingException;
34 import com.fasterxml.jackson.databind.DeserializationFeature;
35 import com.fasterxml.jackson.databind.JsonNode;
36 import com.fasterxml.jackson.databind.ObjectMapper;
39 * Northbound interface for OVSDB Rows
41 public class RowResource {
46 ObjectMapper objectMapper;
48 public RowResource(String nodeId, String databaseName, String tableName) {
50 this.databaseName = databaseName;
51 this.tableName = tableName;
52 objectMapper = new ObjectMapper();
53 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
54 objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
57 private OvsdbRow getOvsdbRow (InputStream stream) throws IOException {
58 StringBuilder rowNodeStrBuilder = new StringBuilder();
59 BufferedReader in = new BufferedReader(new InputStreamReader(stream));
61 while ((line = in.readLine()) != null) {
62 rowNodeStrBuilder.append(line);
64 JsonNode jsonNode = objectMapper.readTree(rowNodeStrBuilder.toString());
65 OvsdbClient client = NodeResource.getOvsdbClient(nodeId, this);
66 return OvsdbRow.fromJsonNode(client, OvsVswitchdSchemaConstants.DATABASE_NAME, jsonNode);
70 @Produces(MediaType.APPLICATION_JSON)
71 public Response getRows() throws JsonProcessingException {
72 OvsdbConfigurationService
73 ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
75 if (ovsdbTable == null) {
76 throw new ServiceUnavailableException("Ovsdb ConfigurationService " + RestMessages.SERVICEUNAVAILABLE.toString());
79 Node node = NodeResource.getOvsdbNode(nodeId, this);
80 Map<UUID,Row<GenericTableSchema>> rows = null;
82 rows = ovsdbTable.getRows(node, databaseName, tableName);
83 } catch (Exception e) {
84 throw new BadRequestException(e.getMessage());
86 String response = objectMapper.writeValueAsString(rows);
87 return Response.status(Response.Status.OK)
93 @Consumes(MediaType.APPLICATION_JSON)
94 @Produces(MediaType.APPLICATION_JSON)
95 public Response createRow(InputStream stream) throws IOException {
96 OvsdbRow localRow = this.getOvsdbRow(stream);
97 if (localRow == null) {
98 return Response.status(Response.Status.BAD_REQUEST).build();
100 OvsdbConfigurationService
101 ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
103 if (ovsdbTable == null) {
104 throw new ServiceUnavailableException("OVS Configuration Service " + RestMessages.SERVICEUNAVAILABLE.toString());
107 Node node = NodeResource.getOvsdbNode(nodeId, this);
108 Row row = ovsdbTable.insertTree(node, OvsVswitchdSchemaConstants.DATABASE_NAME, tableName,
109 localRow.getParentTable(), new UUID(localRow.getParentUuid()), localRow.getParentColumn(),
111 String response = objectMapper.writeValueAsString(row);
112 return Response.status(Response.Status.CREATED)
119 @Produces(MediaType.APPLICATION_JSON)
120 public Response getRowDetails(@PathParam("id") String id) throws JsonProcessingException {
121 OvsdbConfigurationService
122 ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
124 if (ovsdbTable == null) {
125 throw new ServiceUnavailableException("Ovsdb ConfigurationService " + RestMessages.SERVICEUNAVAILABLE.toString());
128 Node node = NodeResource.getOvsdbNode(nodeId, this);
129 Row<GenericTableSchema> row = null;
131 row = ovsdbTable.getRow(node, databaseName, tableName, new UUID(id));
132 } catch (Exception e) {
133 throw new BadRequestException(e.getMessage());
135 String response = objectMapper.writeValueAsString(row);
136 return Response.status(Response.Status.OK)
143 @Consumes(MediaType.APPLICATION_JSON)
144 @Produces(MediaType.APPLICATION_JSON)
145 public Response updateRow(@PathParam("id") String id, InputStream stream) throws IOException{
146 OvsdbRow localRow = this.getOvsdbRow(stream);
147 if (localRow == null) {
148 return Response.status(Response.Status.BAD_REQUEST).build();
150 OvsdbConfigurationService ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
152 if (ovsdbTable == null) {
153 throw new ServiceUnavailableException("OVS Configuration Service " + RestMessages.SERVICEUNAVAILABLE.toString());
156 Node node = NodeResource.getOvsdbNode(nodeId, this);
157 Row<GenericTableSchema> row = ovsdbTable.updateRow(node, databaseName, tableName, new UUID(id), localRow.getRow(), true);
158 String response = objectMapper.writeValueAsString(row);
159 return Response.status(Response.Status.OK)
166 @Consumes(MediaType.APPLICATION_JSON)
167 public Response deleteRow(@PathParam("id") String id){
168 OvsdbConfigurationService
169 ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
171 if (ovsdbTable == null) {
172 throw new ServiceUnavailableException("Ovsdb ConfigurationService " + RestMessages.SERVICEUNAVAILABLE.toString());
175 Node node = NodeResource.getOvsdbNode(nodeId, this);
177 ovsdbTable.deleteRow(node, databaseName, tableName, new UUID(id));
178 } catch (Exception e) {
179 throw new BadRequestException(e.getMessage());
181 return Response.status(Response.Status.OK)