+/*
+ * Copyright (c) 2014, 2015 Red Hat, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
package org.opendaylight.ovsdb.northbound;
+import com.fasterxml.jackson.databind.SerializationFeature;
import java.io.InputStream;
+import java.util.Collections;
+import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
+import org.opendaylight.controller.northbound.commons.RestMessages;
+import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException;
+import org.opendaylight.controller.northbound.commons.exception.ServiceUnavailableException;
+import org.opendaylight.ovsdb.lib.OvsdbClient;
+import org.opendaylight.ovsdb.plugin.api.Connection;
+import org.opendaylight.ovsdb.plugin.api.OvsdbConnectionService;
+import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.DeserializationFeature;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.collect.Lists;
+
/**
* Northbound Interface for OVSDB Nodes
*/
public class NodeResource {
+ ObjectMapper objectMapper;
+ public NodeResource () {
+ objectMapper = new ObjectMapper();
+ objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
+ objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
+ }
+
+ public static Node getOvsdbNode(String nodeId, Object bundleClassRef) {
+ OvsdbConnectionService connectionService =
+ (OvsdbConnectionService)ServiceHelper.
+ getGlobalInstance(OvsdbConnectionService.class, bundleClassRef);
+ if (connectionService == null) {
+ throw new ServiceUnavailableException("Ovsdb ConnectionService "
+ + RestMessages.SERVICEUNAVAILABLE.toString());
+ }
+
+ Node node = connectionService.getNode(nodeId);
+ if (node == null) {
+ throw new ResourceNotFoundException("Node "+nodeId+" not found");
+ }
+
+ return node;
+ }
+
+ public static Connection getOvsdbConnection(String nodeId, Object bundleClassRef) {
+ OvsdbConnectionService connectionService =
+ (OvsdbConnectionService)ServiceHelper.
+ getGlobalInstance(OvsdbConnectionService.class, bundleClassRef);
+ if (connectionService == null) {
+ throw new ServiceUnavailableException("Ovsdb ConnectionService "
+ + RestMessages.SERVICEUNAVAILABLE.toString());
+ }
+
+ Node node = connectionService.getNode(nodeId);
+ if (node == null) {
+ throw new ResourceNotFoundException("Node "+nodeId+" not found");
+ }
+
+ Connection connection = connectionService.getConnection(node);
+ if (connection == null) {
+ throw new ResourceNotFoundException("Connection for "+nodeId+" not available");
+ }
+
+ return connection;
+ }
+
+ public static OvsdbClient getOvsdbClient(String nodeId, Object bundleClassRef) {
+ Connection connection = NodeResource.getOvsdbConnection(nodeId, bundleClassRef);
+ OvsdbClient client = connection.getClient();
+ if (client == null) {
+ throw new ResourceNotFoundException("No Ovsdb Client to handle Node "+nodeId);
+ }
+ return client;
+ }
@GET
@Produces(MediaType.APPLICATION_JSON)
- public Response getNodes(){
- return Response.noContent().build();
+ public Response getNodes() throws JsonProcessingException {
+ OvsdbConnectionService connectionService = (OvsdbConnectionService)ServiceHelper.getGlobalInstance(OvsdbConnectionService.class, this);
+ List<Node> nodes = connectionService.getNodes();
+ if (nodes == null) {
+ return Response.noContent().build();
+ }
+
+ List<String> nodeIds = Lists.newArrayList();
+ for (Node node : nodes) {
+ nodeIds.add(node.getId().getValue());
+ }
+ Collections.sort(nodeIds);
+
+ String response = objectMapper.writeValueAsString(nodeIds);
+ return Response.status(Response.Status.OK)
+ .entity(response)
+ .build();
}
@POST
@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
- public Response getNode(@PathParam("id") String id){
- return Response.noContent().build();
+ public Response getNode(@PathParam("id") String id) throws JsonProcessingException {
+ OvsdbClient client = NodeResource.getOvsdbClient(id, this);
+ String response = objectMapper.writeValueAsString(client.getConnectionInfo());
+ return Response.status(Response.Status.OK)
+ .entity(response)
+ .build();
}
@PUT