2 * Copyright (c) 2014, 2015 Red Hat, Inc. and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.northbound;
11 import com.fasterxml.jackson.databind.SerializationFeature;
12 import java.io.InputStream;
13 import java.util.Collections;
14 import java.util.List;
16 import javax.ws.rs.Consumes;
17 import javax.ws.rs.DELETE;
18 import javax.ws.rs.GET;
19 import javax.ws.rs.POST;
20 import javax.ws.rs.PUT;
21 import javax.ws.rs.Path;
22 import javax.ws.rs.PathParam;
23 import javax.ws.rs.Produces;
24 import javax.ws.rs.core.MediaType;
25 import javax.ws.rs.core.Response;
27 import org.opendaylight.controller.northbound.commons.RestMessages;
28 import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException;
29 import org.opendaylight.controller.northbound.commons.exception.ServiceUnavailableException;
30 import org.opendaylight.ovsdb.lib.OvsdbClient;
31 import org.opendaylight.ovsdb.plugin.api.Connection;
32 import org.opendaylight.ovsdb.plugin.api.OvsdbConnectionService;
33 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
36 import com.fasterxml.jackson.core.JsonProcessingException;
37 import com.fasterxml.jackson.databind.DeserializationFeature;
38 import com.fasterxml.jackson.databind.ObjectMapper;
39 import com.google.common.collect.Lists;
42 * Northbound Interface for OVSDB Nodes
44 public class NodeResource {
45 ObjectMapper objectMapper;
46 public NodeResource () {
47 objectMapper = new ObjectMapper();
48 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
49 objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
52 public static Node getOvsdbNode(String nodeId, Object bundleClassRef) {
53 OvsdbConnectionService connectionService =
54 (OvsdbConnectionService)ServiceHelper.
55 getGlobalInstance(OvsdbConnectionService.class, bundleClassRef);
56 if (connectionService == null) {
57 throw new ServiceUnavailableException("Ovsdb ConnectionService "
58 + RestMessages.SERVICEUNAVAILABLE.toString());
61 Node node = connectionService.getNode(nodeId);
63 throw new ResourceNotFoundException("Node "+nodeId+" not found");
69 public static Connection getOvsdbConnection(String nodeId, Object bundleClassRef) {
70 OvsdbConnectionService connectionService =
71 (OvsdbConnectionService)ServiceHelper.
72 getGlobalInstance(OvsdbConnectionService.class, bundleClassRef);
73 if (connectionService == null) {
74 throw new ServiceUnavailableException("Ovsdb ConnectionService "
75 + RestMessages.SERVICEUNAVAILABLE.toString());
78 Node node = connectionService.getNode(nodeId);
80 throw new ResourceNotFoundException("Node "+nodeId+" not found");
83 Connection connection = connectionService.getConnection(node);
84 if (connection == null) {
85 throw new ResourceNotFoundException("Connection for "+nodeId+" not available");
91 public static OvsdbClient getOvsdbClient(String nodeId, Object bundleClassRef) {
92 Connection connection = NodeResource.getOvsdbConnection(nodeId, bundleClassRef);
93 OvsdbClient client = connection.getClient();
95 throw new ResourceNotFoundException("No Ovsdb Client to handle Node "+nodeId);
101 @Produces(MediaType.APPLICATION_JSON)
102 public Response getNodes() throws JsonProcessingException {
103 OvsdbConnectionService connectionService = (OvsdbConnectionService)ServiceHelper.getGlobalInstance(OvsdbConnectionService.class, this);
104 List<Node> nodes = connectionService.getNodes();
106 return Response.noContent().build();
109 List<String> nodeIds = Lists.newArrayList();
110 for (Node node : nodes) {
111 nodeIds.add(node.getId().getValue());
113 Collections.sort(nodeIds);
115 String response = objectMapper.writeValueAsString(nodeIds);
116 return Response.status(Response.Status.OK)
122 @Consumes(MediaType.APPLICATION_JSON)
123 @Produces(MediaType.APPLICATION_JSON)
124 public Response createNode(InputStream is){
125 return Response.noContent().build();
130 @Produces(MediaType.APPLICATION_JSON)
131 public Response getNode(@PathParam("id") String id) throws JsonProcessingException {
132 OvsdbClient client = NodeResource.getOvsdbClient(id, this);
133 String response = objectMapper.writeValueAsString(client.getConnectionInfo());
134 return Response.status(Response.Status.OK)
141 @Consumes(MediaType.APPLICATION_JSON)
142 @Produces(MediaType.APPLICATION_JSON)
143 public Response updateNode(@PathParam("id") String id, InputStream is){
144 return Response.noContent().build();
149 public Response deleteNode(@PathParam("id") String id){
150 return Response.noContent().build();
153 @Path("{id}/database")
154 public DatabaseResource getNodeDatabase(@PathParam("id") String nodeId){
155 return new DatabaseResource(nodeId);