blob: adc3a51d3c46254b496f8df0cc5a30d16a1da56b [file] [log] [blame]
musonous95c3ee52016-02-23 10:54:34 +09001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
musonous95c3ee52016-02-23 10:54:34 +09003 *
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
17package org.onosproject.openstackinterface.web;
18
19import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.onlab.packet.Ip4Address;
22import org.onosproject.codec.CodecContext;
23import org.onosproject.codec.JsonCodec;
24import org.onosproject.openstackinterface.OpenstackFloatingIP;
25import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
28import static com.google.common.base.Preconditions.checkNotNull;
29
30/**
31 * Implementation of the OpenstackFloatingIP Codec.
32 */
33public 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}