musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
musonous | 95c3ee5 | 2016-02-23 10:54:34 +0900 | [diff] [blame] | 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 | } |