jiangrui | 9d54c26 | 2015-11-28 14:23:39 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
jiangrui | 9d54c26 | 2015-11-28 14:23:39 +0800 | [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 | package org.onosproject.vtnweb.web; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | |
| 20 | import java.util.Iterator; |
| 21 | import java.util.List; |
| 22 | |
| 23 | import org.onosproject.codec.CodecContext; |
| 24 | import org.onosproject.codec.JsonCodec; |
| 25 | import org.onosproject.vtnrsc.FloatingIp; |
| 26 | |
| 27 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 28 | |
| 29 | /** |
| 30 | * FloatingIp JSON codec. |
| 31 | */ |
| 32 | public final class FloatingIpCodec extends JsonCodec<FloatingIp> { |
| 33 | @Override |
| 34 | public ObjectNode encode(FloatingIp floatingIp, CodecContext context) { |
| 35 | checkNotNull(floatingIp, "floatingIp cannot be null"); |
| 36 | ObjectNode result = context |
| 37 | .mapper() |
| 38 | .createObjectNode() |
| 39 | .put("id", floatingIp.id().floatingIpId().toString()) |
| 40 | .put("floating_network_id", floatingIp.networkId().toString()) |
| 41 | .put("router_id", |
| 42 | floatingIp.routerId() == null ? null : floatingIp |
| 43 | .routerId().routerId()) |
| 44 | .put("tenant_id", floatingIp.tenantId().toString()) |
| 45 | .put("port_id", |
| 46 | floatingIp.portId() == null ? null : floatingIp.portId() |
| 47 | .toString()) |
| 48 | .put("fixed_ip_address", |
| 49 | floatingIp.fixedIp() == null ? null : floatingIp.fixedIp() |
| 50 | .toString()) |
| 51 | .put("floating_ip_address", floatingIp.floatingIp().toString()) |
| 52 | .put("status", floatingIp.status().toString()); |
| 53 | return result; |
| 54 | } |
| 55 | |
| 56 | public ObjectNode extracFields(FloatingIp floatingIp, CodecContext context, |
| 57 | List<String> fields) { |
| 58 | checkNotNull(floatingIp, "floatingIp cannot be null"); |
| 59 | ObjectNode result = context.mapper().createObjectNode(); |
| 60 | Iterator<String> i = fields.iterator(); |
| 61 | while (i.hasNext()) { |
| 62 | String s = i.next(); |
Jon Hall | a3fcf67 | 2017-03-28 16:53:22 -0700 | [diff] [blame^] | 63 | if ("floating_network_id".equals(s)) { |
jiangrui | 9d54c26 | 2015-11-28 14:23:39 +0800 | [diff] [blame] | 64 | result.put("floating_network_id", floatingIp.networkId() |
| 65 | .toString()); |
| 66 | } |
Jon Hall | a3fcf67 | 2017-03-28 16:53:22 -0700 | [diff] [blame^] | 67 | if ("router_id".equals(s)) { |
jiangrui | 9d54c26 | 2015-11-28 14:23:39 +0800 | [diff] [blame] | 68 | result.put("router_id", |
| 69 | floatingIp.routerId() == null ? null : floatingIp |
| 70 | .routerId().routerId()); |
| 71 | } |
Jon Hall | a3fcf67 | 2017-03-28 16:53:22 -0700 | [diff] [blame^] | 72 | if ("tenant_id".equals(s)) { |
jiangrui | 9d54c26 | 2015-11-28 14:23:39 +0800 | [diff] [blame] | 73 | result.put("tenant_id", floatingIp.tenantId().toString()); |
| 74 | } |
Jon Hall | a3fcf67 | 2017-03-28 16:53:22 -0700 | [diff] [blame^] | 75 | if ("port_id".equals(s)) { |
jiangrui | 9d54c26 | 2015-11-28 14:23:39 +0800 | [diff] [blame] | 76 | result.put("port_id", |
| 77 | floatingIp.portId() == null ? null : floatingIp |
| 78 | .portId().toString()); |
| 79 | } |
Jon Hall | a3fcf67 | 2017-03-28 16:53:22 -0700 | [diff] [blame^] | 80 | if ("id".equals(s)) { |
jiangrui | 9d54c26 | 2015-11-28 14:23:39 +0800 | [diff] [blame] | 81 | result.put("id", floatingIp.id().floatingIpId().toString()); |
| 82 | } |
Jon Hall | a3fcf67 | 2017-03-28 16:53:22 -0700 | [diff] [blame^] | 83 | if ("fixed_ip_address".equals(s)) { |
jiangrui | 9d54c26 | 2015-11-28 14:23:39 +0800 | [diff] [blame] | 84 | result.put("fixed_ip_address", |
| 85 | floatingIp.fixedIp() == null ? null : floatingIp |
| 86 | .fixedIp().toString()); |
| 87 | } |
Jon Hall | a3fcf67 | 2017-03-28 16:53:22 -0700 | [diff] [blame^] | 88 | if ("floating_ip_address".equals(s)) { |
jiangrui | 9d54c26 | 2015-11-28 14:23:39 +0800 | [diff] [blame] | 89 | result.put("floating_ip_address", floatingIp.floatingIp() |
| 90 | .toString()); |
| 91 | } |
Jon Hall | a3fcf67 | 2017-03-28 16:53:22 -0700 | [diff] [blame^] | 92 | if ("status".equals(s)) { |
jiangrui | 9d54c26 | 2015-11-28 14:23:39 +0800 | [diff] [blame] | 93 | result.put("status", floatingIp.status().toString()); |
| 94 | } |
| 95 | } |
| 96 | return result; |
| 97 | } |
| 98 | } |