2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.openstackswitching.web;
18 import com.fasterxml.jackson.databind.JsonNode;
21 import com.fasterxml.jackson.databind.node.ObjectNode;
22 import org.onosproject.codec.CodecContext;
23 import org.onosproject.codec.JsonCodec;
24 import org.onosproject.openstackswitching.OpenstackSubnet;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * It encodes and decodes the OpenstackSubnet.
32 public class OpenstackSubnetCodec extends JsonCodec<OpenstackSubnet> {
33 private static Logger log = LoggerFactory
34 .getLogger(OpenstackSubnetCodec.class);
37 private static final String SUBNET = "subnet";
38 private static final String NAME = "name";
39 private static final String ENABLE_DHCP = "enable_dhcp";
40 private static final String NETWORK_ID = "network_id";
41 private static final String TENANT_ID = "tenant_id";
42 private static final String DNS_NAMESERVERS = "dns_nameservers";
43 private static final String GATEWAY_IP = "gateway_ip";
44 private static final String CIDR = "cidr";
45 private static final String ID = "id";
48 public OpenstackSubnet decode(ObjectNode json, CodecContext context) {
49 JsonNode subnetInfo = json.get(SUBNET);
51 String name = subnetInfo.path(NAME).asText();
52 boolean enableDhcp = subnetInfo.path(ENABLE_DHCP).asBoolean();
53 String networkId = subnetInfo.path(NETWORK_ID).asText();
54 String tenantId = subnetInfo.path(TENANT_ID).asText();
55 String dnsNameservsers = subnetInfo.path(DNS_NAMESERVERS).asText();
56 String gatewayIp = subnetInfo.path(GATEWAY_IP).asText();
57 String cidr = subnetInfo.path(CIDR).asText();
58 String id = subnetInfo.path(ID).asText();
60 OpenstackSubnet openstackSubnet = OpenstackSubnet.builder()
62 .setEnableDhcp(enableDhcp)
63 .setNetworkId(networkId)
64 .setTenantId(tenantId)
65 .setDnsNameservers(dnsNameservsers)
66 .setGatewayIp(gatewayIp)
70 return openstackSubnet;