musonous
Committed by Gerrit Code Review

[ONOS-3947] Implement Openstack FloatingIP REST call json parser

 - Implement Openstack FloatingIP REST call json parser

Change-Id: I6123bc13eea00a973bdec3ff5503949b25ebceca
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.openstackinterface.web;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.onlab.packet.Ip4Address;
import org.onosproject.codec.CodecContext;
import org.onosproject.codec.JsonCodec;
import org.onosproject.openstackinterface.OpenstackFloatingIP;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Implementation of the OpenstackFloatingIP Codec.
*/
public class OpenstackFloatingIpCodec extends JsonCodec<OpenstackFloatingIP> {
private final Logger log = LoggerFactory.getLogger(getClass());
private static final String FLOATINGIP = "floatingip";
private static final String FLOATING_NETWORK_ID = "floating_network_id";
private static final String ROUTER_ID = "router_id";
private static final String FIXED_IP_ADDRESS = "fixed_ip_address";
private static final String FLOATING_IP_ADDRESS = "floating_ip_address";
private static final String TENANT_ID = "tenant_id";
private static final String STATUS = "status";
private static final String PORT_ID = "port_id";
private static final String ID = "id";
/**
* Decodes the OpenstackFloatingIP.
*
* @param json JSON to decode
* @param context decoding context
* @return OpenstackFloatingIP
*/
@Override
public OpenstackFloatingIP decode(ObjectNode json, CodecContext context) {
if (json == null || !json.isObject()) {
return null;
}
JsonNode floatingIpInfo = json.get(FLOATINGIP);
if (floatingIpInfo == null) {
floatingIpInfo = json;
}
String networkId = floatingIpInfo.path(FLOATING_NETWORK_ID).asText();
String routerId = floatingIpInfo.path(ROUTER_ID).asText();
String fixedIpAddressStr = floatingIpInfo.path(FIXED_IP_ADDRESS).asText();
String floatingIpAddressStr = floatingIpInfo.path(FLOATING_IP_ADDRESS).asText();
String tenantId = floatingIpInfo.path(TENANT_ID).asText();
String statusStr = floatingIpInfo.path(STATUS).asText();
String portId = floatingIpInfo.path(PORT_ID).asText();
String id = floatingIpInfo.path(ID).asText();
checkNotNull(networkId);
checkNotNull(floatingIpAddressStr);
checkNotNull(tenantId);
checkNotNull(statusStr);
checkNotNull(id);
if (routerId != null && routerId.equals("null")) {
routerId = null;
}
Ip4Address fixedIpAddress = null;
if (fixedIpAddressStr != null && !fixedIpAddressStr.equals("null")) {
fixedIpAddress = Ip4Address.valueOf(fixedIpAddressStr);
}
Ip4Address floatingIpAddress = Ip4Address.valueOf(floatingIpAddressStr);
OpenstackFloatingIP.FloatingIpStatus status =
OpenstackFloatingIP.FloatingIpStatus.valueOf(statusStr);
if (portId != null && portId.equals("null")) {
portId = null;
}
OpenstackFloatingIP.Builder osFloatingIpBuilder =
new OpenstackFloatingIP.Builder();
return osFloatingIpBuilder.networkId(networkId)
.routerId(routerId)
.fixedIpAddress(fixedIpAddress)
.floatingIpAddress(floatingIpAddress)
.tenantId(tenantId)
.status(status)
.portId(portId)
.id(id)
.build();
}
}
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.openstacknetworking.web;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.onosproject.openstackinterface.OpenstackFloatingIP;
import org.onosproject.openstackinterface.web.OpenstackFloatingIpCodec;
import org.onosproject.openstacknetworking.OpenstackRoutingService;
import org.onosproject.rest.AbstractWebResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.InputStream;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Handles REST API call of Neutron L3 plugin.
*/
@Path("floatingips")
public class OpenstackFloatingIpWebResource extends AbstractWebResource {
private final Logger log = LoggerFactory.getLogger(getClass());
private static final OpenstackFloatingIpCodec FLOATING_IP_CODEC
= new OpenstackFloatingIpCodec();
/**
* Create FloatingIP.
*
* @param input JSON data describing FloatingIP
* @return 200 OK
*/
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createFloatingIp(InputStream input) {
checkNotNull(input);
try {
ObjectMapper mapper = new ObjectMapper();
ObjectNode floatingIpNode = (ObjectNode) mapper.readTree(input);
OpenstackFloatingIP osFloatingIp =
FLOATING_IP_CODEC.decode(floatingIpNode, this);
OpenstackRoutingService routingService =
getService(OpenstackRoutingService.class);
routingService.createFloatingIP(osFloatingIp);
log.debug("REST API CREATE floatingip called");
return Response.status(Response.Status.OK).build();
} catch (Exception e) {
log.error("createFloatingIp failed with {}", e.toString());
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString())
.build();
}
}
/**
* Update FloatingIP.
*
* @param id FloatingIP identifier
* @param input JSON data describing FloatingIP
* @return 200 OK
*/
@PUT
@Path("{id}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response updateFloatingIp(@PathParam("id") String id, InputStream input) {
checkNotNull(id);
checkNotNull(input);
try {
ObjectMapper mapper = new ObjectMapper();
ObjectNode floatingIpNode = (ObjectNode) mapper.readTree(input);
OpenstackFloatingIP osFloatingIp =
FLOATING_IP_CODEC.decode(floatingIpNode, this);
OpenstackRoutingService routingService =
getService(OpenstackRoutingService.class);
routingService.updateFloatingIP(osFloatingIp);
log.debug("REST API UPDATE floatingip called {}", id);
return Response.status(Response.Status.OK).build();
} catch (Exception e) {
log.error("updateFloatingIp failed with {}", e.toString());
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString())
.build();
}
}
/**
* Delete FloatingIP.
*
* @param id FloatingIP identifier
* @return 200 OK
*/
@DELETE
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response deleteFloatingIp(@PathParam("id") String id) {
checkNotNull(id);
OpenstackRoutingService routingService =
getService(OpenstackRoutingService.class);
routingService.deleteFloatingIP(id);
log.debug("REST API DELETE floatingip is called {}", id);
return Response.status(Response.Status.OK).build();
}
}
......@@ -33,7 +33,8 @@
org.onosproject.openstacknetworking.web.OpenstackPortWebResource,
org.onosproject.openstacknetworking.web.OpenstackNetworkWebResource,
org.onosproject.openstacknetworking.web.OpenstackSubnetWebResource,
org.onosproject.openstacknetworking.web.OpensatckRouterWebResource
org.onosproject.openstacknetworking.web.OpensatckRouterWebResource,
org.onosproject.openstacknetworking.web.OpenstackFloatingIpWebResource
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
......