blob: 804f4244d6929cbea811c6edf19586835cadd372 [file] [log] [blame]
jiangrui9d54c262015-11-28 14:23:39 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
jiangrui9d54c262015-11-28 14:23:39 +08003 *
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 */
16package org.onosproject.vtnweb.web;
17
18import static com.google.common.base.Preconditions.checkNotNull;
19
20import java.util.Iterator;
21import java.util.List;
22
23import org.onosproject.codec.CodecContext;
24import org.onosproject.codec.JsonCodec;
25import org.onosproject.vtnrsc.FloatingIp;
26
27import com.fasterxml.jackson.databind.node.ObjectNode;
28
29/**
30 * FloatingIp JSON codec.
31 */
32public 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 Halla3fcf672017-03-28 16:53:22 -070063 if ("floating_network_id".equals(s)) {
jiangrui9d54c262015-11-28 14:23:39 +080064 result.put("floating_network_id", floatingIp.networkId()
65 .toString());
66 }
Jon Halla3fcf672017-03-28 16:53:22 -070067 if ("router_id".equals(s)) {
jiangrui9d54c262015-11-28 14:23:39 +080068 result.put("router_id",
69 floatingIp.routerId() == null ? null : floatingIp
70 .routerId().routerId());
71 }
Jon Halla3fcf672017-03-28 16:53:22 -070072 if ("tenant_id".equals(s)) {
jiangrui9d54c262015-11-28 14:23:39 +080073 result.put("tenant_id", floatingIp.tenantId().toString());
74 }
Jon Halla3fcf672017-03-28 16:53:22 -070075 if ("port_id".equals(s)) {
jiangrui9d54c262015-11-28 14:23:39 +080076 result.put("port_id",
77 floatingIp.portId() == null ? null : floatingIp
78 .portId().toString());
79 }
Jon Halla3fcf672017-03-28 16:53:22 -070080 if ("id".equals(s)) {
jiangrui9d54c262015-11-28 14:23:39 +080081 result.put("id", floatingIp.id().floatingIpId().toString());
82 }
Jon Halla3fcf672017-03-28 16:53:22 -070083 if ("fixed_ip_address".equals(s)) {
jiangrui9d54c262015-11-28 14:23:39 +080084 result.put("fixed_ip_address",
85 floatingIp.fixedIp() == null ? null : floatingIp
86 .fixedIp().toString());
87 }
Jon Halla3fcf672017-03-28 16:53:22 -070088 if ("floating_ip_address".equals(s)) {
jiangrui9d54c262015-11-28 14:23:39 +080089 result.put("floating_ip_address", floatingIp.floatingIp()
90 .toString());
91 }
Jon Halla3fcf672017-03-28 16:53:22 -070092 if ("status".equals(s)) {
jiangrui9d54c262015-11-28 14:23:39 +080093 result.put("status", floatingIp.status().toString());
94 }
95 }
96 return result;
97 }
98}