Committed by
Gerrit Code Review
[ONOS-3947] Implement Openstack FloatingIP REST call json parser
- Implement Openstack FloatingIP REST call json parser Change-Id: I6123bc13eea00a973bdec3ff5503949b25ebceca
Showing
3 changed files
with
258 additions
and
1 deletions
| 1 | +/* | ||
| 2 | + * Copyright 2016 Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | + | ||
| 17 | +package org.onosproject.openstackinterface.web; | ||
| 18 | + | ||
| 19 | +import com.fasterxml.jackson.databind.JsonNode; | ||
| 20 | +import com.fasterxml.jackson.databind.node.ObjectNode; | ||
| 21 | +import org.onlab.packet.Ip4Address; | ||
| 22 | +import org.onosproject.codec.CodecContext; | ||
| 23 | +import org.onosproject.codec.JsonCodec; | ||
| 24 | +import org.onosproject.openstackinterface.OpenstackFloatingIP; | ||
| 25 | +import org.slf4j.Logger; | ||
| 26 | +import org.slf4j.LoggerFactory; | ||
| 27 | + | ||
| 28 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
| 29 | + | ||
| 30 | +/** | ||
| 31 | + * Implementation of the OpenstackFloatingIP Codec. | ||
| 32 | + */ | ||
| 33 | +public class OpenstackFloatingIpCodec extends JsonCodec<OpenstackFloatingIP> { | ||
| 34 | + private final Logger log = LoggerFactory.getLogger(getClass()); | ||
| 35 | + | ||
| 36 | + private static final String FLOATINGIP = "floatingip"; | ||
| 37 | + private static final String FLOATING_NETWORK_ID = "floating_network_id"; | ||
| 38 | + private static final String ROUTER_ID = "router_id"; | ||
| 39 | + private static final String FIXED_IP_ADDRESS = "fixed_ip_address"; | ||
| 40 | + private static final String FLOATING_IP_ADDRESS = "floating_ip_address"; | ||
| 41 | + private static final String TENANT_ID = "tenant_id"; | ||
| 42 | + private static final String STATUS = "status"; | ||
| 43 | + private static final String PORT_ID = "port_id"; | ||
| 44 | + private static final String ID = "id"; | ||
| 45 | + | ||
| 46 | + /** | ||
| 47 | + * Decodes the OpenstackFloatingIP. | ||
| 48 | + * | ||
| 49 | + * @param json JSON to decode | ||
| 50 | + * @param context decoding context | ||
| 51 | + * @return OpenstackFloatingIP | ||
| 52 | + */ | ||
| 53 | + @Override | ||
| 54 | + public OpenstackFloatingIP decode(ObjectNode json, CodecContext context) { | ||
| 55 | + if (json == null || !json.isObject()) { | ||
| 56 | + return null; | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + JsonNode floatingIpInfo = json.get(FLOATINGIP); | ||
| 60 | + if (floatingIpInfo == null) { | ||
| 61 | + floatingIpInfo = json; | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + String networkId = floatingIpInfo.path(FLOATING_NETWORK_ID).asText(); | ||
| 65 | + String routerId = floatingIpInfo.path(ROUTER_ID).asText(); | ||
| 66 | + String fixedIpAddressStr = floatingIpInfo.path(FIXED_IP_ADDRESS).asText(); | ||
| 67 | + String floatingIpAddressStr = floatingIpInfo.path(FLOATING_IP_ADDRESS).asText(); | ||
| 68 | + String tenantId = floatingIpInfo.path(TENANT_ID).asText(); | ||
| 69 | + String statusStr = floatingIpInfo.path(STATUS).asText(); | ||
| 70 | + String portId = floatingIpInfo.path(PORT_ID).asText(); | ||
| 71 | + String id = floatingIpInfo.path(ID).asText(); | ||
| 72 | + | ||
| 73 | + checkNotNull(networkId); | ||
| 74 | + checkNotNull(floatingIpAddressStr); | ||
| 75 | + checkNotNull(tenantId); | ||
| 76 | + checkNotNull(statusStr); | ||
| 77 | + checkNotNull(id); | ||
| 78 | + | ||
| 79 | + if (routerId != null && routerId.equals("null")) { | ||
| 80 | + routerId = null; | ||
| 81 | + } | ||
| 82 | + | ||
| 83 | + Ip4Address fixedIpAddress = null; | ||
| 84 | + if (fixedIpAddressStr != null && !fixedIpAddressStr.equals("null")) { | ||
| 85 | + fixedIpAddress = Ip4Address.valueOf(fixedIpAddressStr); | ||
| 86 | + } | ||
| 87 | + | ||
| 88 | + Ip4Address floatingIpAddress = Ip4Address.valueOf(floatingIpAddressStr); | ||
| 89 | + | ||
| 90 | + OpenstackFloatingIP.FloatingIpStatus status = | ||
| 91 | + OpenstackFloatingIP.FloatingIpStatus.valueOf(statusStr); | ||
| 92 | + | ||
| 93 | + if (portId != null && portId.equals("null")) { | ||
| 94 | + portId = null; | ||
| 95 | + } | ||
| 96 | + | ||
| 97 | + OpenstackFloatingIP.Builder osFloatingIpBuilder = | ||
| 98 | + new OpenstackFloatingIP.Builder(); | ||
| 99 | + | ||
| 100 | + return osFloatingIpBuilder.networkId(networkId) | ||
| 101 | + .routerId(routerId) | ||
| 102 | + .fixedIpAddress(fixedIpAddress) | ||
| 103 | + .floatingIpAddress(floatingIpAddress) | ||
| 104 | + .tenantId(tenantId) | ||
| 105 | + .status(status) | ||
| 106 | + .portId(portId) | ||
| 107 | + .id(id) | ||
| 108 | + .build(); | ||
| 109 | + } | ||
| 110 | + | ||
| 111 | +} |
| 1 | +/* | ||
| 2 | + * Copyright 2016 Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | + | ||
| 17 | +package org.onosproject.openstacknetworking.web; | ||
| 18 | + | ||
| 19 | +import com.fasterxml.jackson.databind.ObjectMapper; | ||
| 20 | +import com.fasterxml.jackson.databind.node.ObjectNode; | ||
| 21 | +import org.onosproject.openstackinterface.OpenstackFloatingIP; | ||
| 22 | +import org.onosproject.openstackinterface.web.OpenstackFloatingIpCodec; | ||
| 23 | +import org.onosproject.openstacknetworking.OpenstackRoutingService; | ||
| 24 | +import org.onosproject.rest.AbstractWebResource; | ||
| 25 | +import org.slf4j.Logger; | ||
| 26 | +import org.slf4j.LoggerFactory; | ||
| 27 | + | ||
| 28 | +import javax.ws.rs.Consumes; | ||
| 29 | +import javax.ws.rs.DELETE; | ||
| 30 | +import javax.ws.rs.POST; | ||
| 31 | +import javax.ws.rs.PUT; | ||
| 32 | +import javax.ws.rs.Path; | ||
| 33 | +import javax.ws.rs.PathParam; | ||
| 34 | +import javax.ws.rs.Produces; | ||
| 35 | +import javax.ws.rs.core.MediaType; | ||
| 36 | +import javax.ws.rs.core.Response; | ||
| 37 | +import java.io.InputStream; | ||
| 38 | + | ||
| 39 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
| 40 | + | ||
| 41 | +/** | ||
| 42 | + * Handles REST API call of Neutron L3 plugin. | ||
| 43 | + */ | ||
| 44 | +@Path("floatingips") | ||
| 45 | +public class OpenstackFloatingIpWebResource extends AbstractWebResource { | ||
| 46 | + private final Logger log = LoggerFactory.getLogger(getClass()); | ||
| 47 | + | ||
| 48 | + private static final OpenstackFloatingIpCodec FLOATING_IP_CODEC | ||
| 49 | + = new OpenstackFloatingIpCodec(); | ||
| 50 | + | ||
| 51 | + /** | ||
| 52 | + * Create FloatingIP. | ||
| 53 | + * | ||
| 54 | + * @param input JSON data describing FloatingIP | ||
| 55 | + * @return 200 OK | ||
| 56 | + */ | ||
| 57 | + @POST | ||
| 58 | + @Consumes(MediaType.APPLICATION_JSON) | ||
| 59 | + @Produces(MediaType.APPLICATION_JSON) | ||
| 60 | + public Response createFloatingIp(InputStream input) { | ||
| 61 | + checkNotNull(input); | ||
| 62 | + | ||
| 63 | + try { | ||
| 64 | + ObjectMapper mapper = new ObjectMapper(); | ||
| 65 | + ObjectNode floatingIpNode = (ObjectNode) mapper.readTree(input); | ||
| 66 | + | ||
| 67 | + OpenstackFloatingIP osFloatingIp = | ||
| 68 | + FLOATING_IP_CODEC.decode(floatingIpNode, this); | ||
| 69 | + | ||
| 70 | + OpenstackRoutingService routingService = | ||
| 71 | + getService(OpenstackRoutingService.class); | ||
| 72 | + | ||
| 73 | + routingService.createFloatingIP(osFloatingIp); | ||
| 74 | + | ||
| 75 | + log.debug("REST API CREATE floatingip called"); | ||
| 76 | + | ||
| 77 | + return Response.status(Response.Status.OK).build(); | ||
| 78 | + } catch (Exception e) { | ||
| 79 | + log.error("createFloatingIp failed with {}", e.toString()); | ||
| 80 | + | ||
| 81 | + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString()) | ||
| 82 | + .build(); | ||
| 83 | + } | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + /** | ||
| 87 | + * Update FloatingIP. | ||
| 88 | + * | ||
| 89 | + * @param id FloatingIP identifier | ||
| 90 | + * @param input JSON data describing FloatingIP | ||
| 91 | + * @return 200 OK | ||
| 92 | + */ | ||
| 93 | + @PUT | ||
| 94 | + @Path("{id}") | ||
| 95 | + @Consumes(MediaType.APPLICATION_JSON) | ||
| 96 | + @Produces(MediaType.APPLICATION_JSON) | ||
| 97 | + public Response updateFloatingIp(@PathParam("id") String id, InputStream input) { | ||
| 98 | + checkNotNull(id); | ||
| 99 | + checkNotNull(input); | ||
| 100 | + | ||
| 101 | + try { | ||
| 102 | + ObjectMapper mapper = new ObjectMapper(); | ||
| 103 | + ObjectNode floatingIpNode = (ObjectNode) mapper.readTree(input); | ||
| 104 | + | ||
| 105 | + OpenstackFloatingIP osFloatingIp = | ||
| 106 | + FLOATING_IP_CODEC.decode(floatingIpNode, this); | ||
| 107 | + | ||
| 108 | + OpenstackRoutingService routingService = | ||
| 109 | + getService(OpenstackRoutingService.class); | ||
| 110 | + | ||
| 111 | + routingService.updateFloatingIP(osFloatingIp); | ||
| 112 | + | ||
| 113 | + log.debug("REST API UPDATE floatingip called {}", id); | ||
| 114 | + | ||
| 115 | + return Response.status(Response.Status.OK).build(); | ||
| 116 | + } catch (Exception e) { | ||
| 117 | + log.error("updateFloatingIp failed with {}", e.toString()); | ||
| 118 | + | ||
| 119 | + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString()) | ||
| 120 | + .build(); | ||
| 121 | + } | ||
| 122 | + } | ||
| 123 | + | ||
| 124 | + /** | ||
| 125 | + * Delete FloatingIP. | ||
| 126 | + * | ||
| 127 | + * @param id FloatingIP identifier | ||
| 128 | + * @return 200 OK | ||
| 129 | + */ | ||
| 130 | + @DELETE | ||
| 131 | + @Path("{id}") | ||
| 132 | + @Produces(MediaType.APPLICATION_JSON) | ||
| 133 | + public Response deleteFloatingIp(@PathParam("id") String id) { | ||
| 134 | + checkNotNull(id); | ||
| 135 | + | ||
| 136 | + OpenstackRoutingService routingService = | ||
| 137 | + getService(OpenstackRoutingService.class); | ||
| 138 | + routingService.deleteFloatingIP(id); | ||
| 139 | + | ||
| 140 | + log.debug("REST API DELETE floatingip is called {}", id); | ||
| 141 | + | ||
| 142 | + return Response.status(Response.Status.OK).build(); | ||
| 143 | + } | ||
| 144 | + | ||
| 145 | +} |
| ... | @@ -33,7 +33,8 @@ | ... | @@ -33,7 +33,8 @@ |
| 33 | org.onosproject.openstacknetworking.web.OpenstackPortWebResource, | 33 | org.onosproject.openstacknetworking.web.OpenstackPortWebResource, |
| 34 | org.onosproject.openstacknetworking.web.OpenstackNetworkWebResource, | 34 | org.onosproject.openstacknetworking.web.OpenstackNetworkWebResource, |
| 35 | org.onosproject.openstacknetworking.web.OpenstackSubnetWebResource, | 35 | org.onosproject.openstacknetworking.web.OpenstackSubnetWebResource, |
| 36 | - org.onosproject.openstacknetworking.web.OpensatckRouterWebResource | 36 | + org.onosproject.openstacknetworking.web.OpensatckRouterWebResource, |
| 37 | + org.onosproject.openstacknetworking.web.OpenstackFloatingIpWebResource | ||
| 37 | </param-value> | 38 | </param-value> |
| 38 | </init-param> | 39 | </init-param> |
| 39 | <load-on-startup>1</load-on-startup> | 40 | <load-on-startup>1</load-on-startup> | ... | ... |
-
Please register or login to post a comment