Merge "Initial pass at netvirt model"
[netvirt.git] / northbound / src / main / java / org / opendaylight / ovsdb / northbound / RowResource.java
1 /*
2  * Copyright (c) 2014, 2015 Red Hat, Inc. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.ovsdb.northbound;
10
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;
16 import java.util.Map;
17
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;
28
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;
40
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;
45
46 /**
47  * Northbound interface for OVSDB Rows
48  */
49 public class RowResource {
50
51     String nodeId;
52     String databaseName;
53     String tableName;
54     ObjectMapper objectMapper;
55
56     public RowResource(String nodeId, String databaseName, String tableName) {
57         this.nodeId = nodeId;
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);
63     }
64
65     private OvsdbRow getOvsdbRow (InputStream stream) throws IOException {
66         StringBuilder rowNodeStrBuilder = new StringBuilder();
67         BufferedReader in = new BufferedReader(new InputStreamReader(stream));
68         String line = null;
69         while ((line = in.readLine()) != null) {
70             rowNodeStrBuilder.append(line);
71         }
72         JsonNode jsonNode = objectMapper.readTree(rowNodeStrBuilder.toString());
73         OvsdbClient client = NodeResource.getOvsdbClient(nodeId, this);
74         return OvsdbRow.fromJsonNode(client, OvsVswitchdSchemaConstants.DATABASE_NAME, jsonNode);
75     }
76
77     @GET
78     @Produces(MediaType.APPLICATION_JSON)
79     public Response getRows() throws JsonProcessingException {
80         OvsdbConfigurationService
81                 ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
82                                                                                             this);
83         if (ovsdbTable == null) {
84             throw new ServiceUnavailableException("Ovsdb ConfigurationService " + RestMessages.SERVICEUNAVAILABLE.toString());
85         }
86
87         Node node = NodeResource.getOvsdbNode(nodeId, this);
88         Map<UUID,Row<GenericTableSchema>> rows = null;
89         try {
90             rows = ovsdbTable.getRows(node, databaseName, tableName);
91         } catch (Exception e) {
92             throw new BadRequestException(e.getMessage());
93         }
94         String response = objectMapper.writeValueAsString(rows);
95         return Response.status(Response.Status.OK)
96                 .entity(response)
97                 .build();
98     }
99
100     @POST
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();
107         }
108         OvsdbConfigurationService
109         ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
110                                                                                     this);
111         if (ovsdbTable == null) {
112             throw new ServiceUnavailableException("OVS Configuration Service " + RestMessages.SERVICEUNAVAILABLE.toString());
113         }
114
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(),
118                 localRow.getRow());
119         String response = objectMapper.writeValueAsString(row);
120         return Response.status(Response.Status.CREATED)
121                 .entity(response)
122                 .build();
123     }
124
125     @GET
126     @Path("{id}")
127     @Produces(MediaType.APPLICATION_JSON)
128     public Response getRowDetails(@PathParam("id") String id) throws JsonProcessingException {
129         OvsdbConfigurationService
130         ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
131                                                                                     this);
132         if (ovsdbTable == null) {
133             throw new ServiceUnavailableException("Ovsdb ConfigurationService " + RestMessages.SERVICEUNAVAILABLE.toString());
134         }
135
136         Node node = NodeResource.getOvsdbNode(nodeId, this);
137         Row<GenericTableSchema> row = null;
138         try {
139             row = ovsdbTable.getRow(node, databaseName, tableName, new UUID(id));
140         } catch (Exception e) {
141             throw new BadRequestException(e.getMessage());
142         }
143         String response = objectMapper.writeValueAsString(row);
144         return Response.status(Response.Status.OK)
145                 .entity(response)
146                 .build();
147     }
148
149     @PUT
150     @Path("{id}")
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();
157         }
158         OvsdbConfigurationService ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
159                                                                                     this);
160         if (ovsdbTable == null) {
161             throw new ServiceUnavailableException("OVS Configuration Service " + RestMessages.SERVICEUNAVAILABLE.toString());
162         }
163
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)
168                 .entity(response)
169                 .build();
170     }
171
172     @DELETE
173     @Path("{id}")
174     @Consumes(MediaType.APPLICATION_JSON)
175     public Response deleteRow(@PathParam("id") String id){
176         OvsdbConfigurationService
177         ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
178                                                                                     this);
179         if (ovsdbTable == null) {
180             throw new ServiceUnavailableException("Ovsdb ConfigurationService " + RestMessages.SERVICEUNAVAILABLE.toString());
181         }
182
183         Node node = NodeResource.getOvsdbNode(nodeId, this);
184         try {
185             ovsdbTable.deleteRow(node, databaseName, tableName, new UUID(id));
186         } catch (Exception e) {
187             throw new BadRequestException(e.getMessage());
188         }
189         return Response.status(Response.Status.OK)
190                 .build();
191
192     }
193 }