NorthboundV3 APIs for RowResource
[ovsdb.git] / northbound / src / main / java / org / opendaylight / ovsdb / northbound / RowResource.java
1 package org.opendaylight.ovsdb.northbound;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.InputStreamReader;
7 import java.util.Map;
8
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;
19
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;
31
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;
36
37 /**
38  * Northbound interface for OVSDB Rows
39  */
40 public class RowResource {
41
42     String nodeId;
43     String databaseName;
44     String tableName;
45     ObjectMapper objectMapper;
46
47     public RowResource(String nodeId, String databaseName, String tableName) {
48         this.nodeId = nodeId;
49         this.databaseName = databaseName;
50         this.tableName = tableName;
51         objectMapper = new ObjectMapper();
52         objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
53
54     }
55
56     private OvsdbRow getOvsdbRow (InputStream stream) throws IOException {
57         StringBuilder rowNodeStrBuilder = new StringBuilder();
58         BufferedReader in = new BufferedReader(new InputStreamReader(stream));
59         String line = null;
60         while ((line = in.readLine()) != null) {
61             rowNodeStrBuilder.append(line);
62         }
63         JsonNode jsonNode = objectMapper.readTree(rowNodeStrBuilder.toString());
64         OvsdbClient client = NodeResource.getOvsdbConnection(nodeId, this);
65         return OvsdbRow.fromJsonNode(client, OvsVswitchdSchemaConstants.DATABASE_NAME, jsonNode);
66     }
67
68     @GET
69     @Produces(MediaType.APPLICATION_JSON)
70     public Response getRows() throws JsonProcessingException {
71         OvsdbConfigurationService
72                 ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
73                                                                                             this);
74         if (ovsdbTable == null) {
75             throw new ServiceUnavailableException("Ovsdb ConfigurationService " + RestMessages.SERVICEUNAVAILABLE.toString());
76         }
77
78         Node node = Node.fromString(nodeId);
79         Map<UUID,Row<GenericTableSchema>> rows = null;
80         try {
81             rows = ovsdbTable.getRows(node, databaseName, tableName);
82         } catch (Exception e) {
83             throw new BadRequestException(e.getMessage());
84         }
85         String response = objectMapper.writeValueAsString(rows);
86         return Response.status(Response.Status.OK)
87                 .entity(response)
88                 .build();
89     }
90
91     @POST
92     @Consumes(MediaType.APPLICATION_JSON)
93     @Produces(MediaType.APPLICATION_JSON)
94     public Response createRow(InputStream stream) throws IOException {
95         Node node = Node.fromString(nodeId);
96         OvsdbRow localRow = this.getOvsdbRow(stream);
97         if (localRow == null) {
98             return Response.status(Response.Status.BAD_REQUEST).build();
99         }
100         OvsdbConfigurationService
101         ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
102                                                                                     this);
103         if (ovsdbTable == null) {
104             throw new ServiceUnavailableException("OVS Configuration Service " + RestMessages.SERVICEUNAVAILABLE.toString());
105         }
106
107         Row row = ovsdbTable.insertTree(node, OvsVswitchdSchemaConstants.DATABASE_NAME, tableName,
108                 localRow.getParentTable(), new UUID(localRow.getParentUuid()), localRow.getParentColumn(),
109                 localRow.getRow());
110         String response = objectMapper.writeValueAsString(row);
111         return Response.status(Response.Status.CREATED)
112                 .entity(response)
113                 .build();
114     }
115
116     @GET
117     @Path("{id}")
118     @Produces(MediaType.APPLICATION_JSON)
119     public Response getRowDetails(@PathParam("id") String id) throws JsonProcessingException {
120         OvsdbConfigurationService
121         ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
122                                                                                     this);
123         if (ovsdbTable == null) {
124             throw new ServiceUnavailableException("Ovsdb ConfigurationService " + RestMessages.SERVICEUNAVAILABLE.toString());
125         }
126
127         Node node = Node.fromString(nodeId);
128         Row<GenericTableSchema> row = null;
129         try {
130             row = ovsdbTable.getRow(node, databaseName, tableName, new UUID(id));
131         } catch (Exception e) {
132             throw new BadRequestException(e.getMessage());
133         }
134         String response = objectMapper.writeValueAsString(row);
135         return Response.status(Response.Status.OK)
136                 .entity(response)
137                 .build();
138     }
139
140     @PUT
141     @Path("{id}")
142     @Consumes(MediaType.APPLICATION_JSON)
143     @Produces(MediaType.APPLICATION_JSON)
144     public Response updateRow(@PathParam("id") String id, InputStream stream) throws IOException{
145         Node node = Node.fromString(nodeId);
146         OvsdbRow localRow = this.getOvsdbRow(stream);
147         if (localRow == null) {
148             return Response.status(Response.Status.BAD_REQUEST).build();
149         }
150         OvsdbConfigurationService ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
151                                                                                     this);
152         if (ovsdbTable == null) {
153             throw new ServiceUnavailableException("OVS Configuration Service " + RestMessages.SERVICEUNAVAILABLE.toString());
154         }
155
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)
159                 .entity(response)
160                 .build();
161     }
162
163     @DELETE
164     @Path("{id}")
165     @Consumes(MediaType.APPLICATION_JSON)
166     public Response deleteRow(@PathParam("id") String id){
167         OvsdbConfigurationService
168         ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
169                                                                                     this);
170         if (ovsdbTable == null) {
171             throw new ServiceUnavailableException("Ovsdb ConfigurationService " + RestMessages.SERVICEUNAVAILABLE.toString());
172         }
173
174         Node node = Node.fromString(nodeId);
175         try {
176             ovsdbTable.deleteRow(node, databaseName, tableName, new UUID(id));
177         } catch (Exception e) {
178             throw new BadRequestException(e.getMessage());
179         }
180         return Response.status(Response.Status.OK)
181                 .build();
182
183     }
184 }