Migrate from adsal Node to mdsal Node
[ovsdb.git] / northbound / src / main / java / org / opendaylight / ovsdb / northbound / RowResource.java
1 package org.opendaylight.ovsdb.northbound;
2
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;
8 import java.util.Map;
9
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;
20
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;
32
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;
37
38 /**
39  * Northbound interface for OVSDB Rows
40  */
41 public class RowResource {
42
43     String nodeId;
44     String databaseName;
45     String tableName;
46     ObjectMapper objectMapper;
47
48     public RowResource(String nodeId, String databaseName, String tableName) {
49         this.nodeId = nodeId;
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);
55     }
56
57     private OvsdbRow getOvsdbRow (InputStream stream) throws IOException {
58         StringBuilder rowNodeStrBuilder = new StringBuilder();
59         BufferedReader in = new BufferedReader(new InputStreamReader(stream));
60         String line = null;
61         while ((line = in.readLine()) != null) {
62             rowNodeStrBuilder.append(line);
63         }
64         JsonNode jsonNode = objectMapper.readTree(rowNodeStrBuilder.toString());
65         OvsdbClient client = NodeResource.getOvsdbClient(nodeId, this);
66         return OvsdbRow.fromJsonNode(client, OvsVswitchdSchemaConstants.DATABASE_NAME, jsonNode);
67     }
68
69     @GET
70     @Produces(MediaType.APPLICATION_JSON)
71     public Response getRows() throws JsonProcessingException {
72         OvsdbConfigurationService
73                 ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
74                                                                                             this);
75         if (ovsdbTable == null) {
76             throw new ServiceUnavailableException("Ovsdb ConfigurationService " + RestMessages.SERVICEUNAVAILABLE.toString());
77         }
78
79         Node node = NodeResource.getOvsdbNode(nodeId, this);
80         Map<UUID,Row<GenericTableSchema>> rows = null;
81         try {
82             rows = ovsdbTable.getRows(node, databaseName, tableName);
83         } catch (Exception e) {
84             throw new BadRequestException(e.getMessage());
85         }
86         String response = objectMapper.writeValueAsString(rows);
87         return Response.status(Response.Status.OK)
88                 .entity(response)
89                 .build();
90     }
91
92     @POST
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();
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         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(),
110                 localRow.getRow());
111         String response = objectMapper.writeValueAsString(row);
112         return Response.status(Response.Status.CREATED)
113                 .entity(response)
114                 .build();
115     }
116
117     @GET
118     @Path("{id}")
119     @Produces(MediaType.APPLICATION_JSON)
120     public Response getRowDetails(@PathParam("id") String id) throws JsonProcessingException {
121         OvsdbConfigurationService
122         ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
123                                                                                     this);
124         if (ovsdbTable == null) {
125             throw new ServiceUnavailableException("Ovsdb ConfigurationService " + RestMessages.SERVICEUNAVAILABLE.toString());
126         }
127
128         Node node = NodeResource.getOvsdbNode(nodeId, this);
129         Row<GenericTableSchema> row = null;
130         try {
131             row = ovsdbTable.getRow(node, databaseName, tableName, new UUID(id));
132         } catch (Exception e) {
133             throw new BadRequestException(e.getMessage());
134         }
135         String response = objectMapper.writeValueAsString(row);
136         return Response.status(Response.Status.OK)
137                 .entity(response)
138                 .build();
139     }
140
141     @PUT
142     @Path("{id}")
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();
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         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)
160                 .entity(response)
161                 .build();
162     }
163
164     @DELETE
165     @Path("{id}")
166     @Consumes(MediaType.APPLICATION_JSON)
167     public Response deleteRow(@PathParam("id") String id){
168         OvsdbConfigurationService
169         ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
170                                                                                     this);
171         if (ovsdbTable == null) {
172             throw new ServiceUnavailableException("Ovsdb ConfigurationService " + RestMessages.SERVICEUNAVAILABLE.toString());
173         }
174
175         Node node = NodeResource.getOvsdbNode(nodeId, this);
176         try {
177             ovsdbTable.deleteRow(node, databaseName, tableName, new UUID(id));
178         } catch (Exception e) {
179             throw new BadRequestException(e.getMessage());
180         }
181         return Response.status(Response.Status.OK)
182                 .build();
183
184     }
185 }