blob: 2a7af82a1522d67602d03a0c5830fde0d428e658 [file] [log] [blame]
danielbb83ebc2015-10-29 15:13:06 +09001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
16package org.onosproject.openstackswitching.web;
17
18import com.fasterxml.jackson.databind.JsonNode;
19
20
sanghoshin46297d22015-11-03 17:51:24 +090021import com.fasterxml.jackson.databind.node.ArrayNode;
danielbb83ebc2015-10-29 15:13:06 +090022import com.fasterxml.jackson.databind.node.ObjectNode;
sanghoshin46297d22015-11-03 17:51:24 +090023import com.google.common.collect.Lists;
24import org.onlab.packet.Ip4Address;
danielbb83ebc2015-10-29 15:13:06 +090025import org.onosproject.codec.CodecContext;
26import org.onosproject.codec.JsonCodec;
27import org.onosproject.openstackswitching.OpenstackSubnet;
28import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
sanghoshin46297d22015-11-03 17:51:24 +090031import java.util.List;
danielbb83ebc2015-10-29 15:13:06 +090032
sanghoshin46297d22015-11-03 17:51:24 +090033/**
34 * Encodes and decodes the OpenstackSubnet.
35 */
danielbb83ebc2015-10-29 15:13:06 +090036public class OpenstackSubnetCodec extends JsonCodec<OpenstackSubnet> {
37 private static Logger log = LoggerFactory
38 .getLogger(OpenstackSubnetCodec.class);
39
40 // JSON Field names
41 private static final String SUBNET = "subnet";
42 private static final String NAME = "name";
43 private static final String ENABLE_DHCP = "enable_dhcp";
44 private static final String NETWORK_ID = "network_id";
45 private static final String TENANT_ID = "tenant_id";
46 private static final String DNS_NAMESERVERS = "dns_nameservers";
47 private static final String GATEWAY_IP = "gateway_ip";
48 private static final String CIDR = "cidr";
49 private static final String ID = "id";
50
51 @Override
52 public OpenstackSubnet decode(ObjectNode json, CodecContext context) {
53 JsonNode subnetInfo = json.get(SUBNET);
sanghoshinf25d2e02015-11-11 23:07:17 +090054 if (subnetInfo == null) {
55 subnetInfo = json;
56 }
danielbb83ebc2015-10-29 15:13:06 +090057
58 String name = subnetInfo.path(NAME).asText();
59 boolean enableDhcp = subnetInfo.path(ENABLE_DHCP).asBoolean();
60 String networkId = subnetInfo.path(NETWORK_ID).asText();
61 String tenantId = subnetInfo.path(TENANT_ID).asText();
sanghoshin46297d22015-11-03 17:51:24 +090062 ArrayNode dnsNameservsers = (ArrayNode) subnetInfo.path(DNS_NAMESERVERS);
63 List<Ip4Address> dnsList = Lists.newArrayList();
64 if (dnsNameservsers != null && !dnsNameservsers.isMissingNode()) {
65 dnsNameservsers.forEach(dns -> dnsList.add(Ip4Address.valueOf(dns.asText())));
66 }
danielbb83ebc2015-10-29 15:13:06 +090067 String gatewayIp = subnetInfo.path(GATEWAY_IP).asText();
68 String cidr = subnetInfo.path(CIDR).asText();
69 String id = subnetInfo.path(ID).asText();
70
71 OpenstackSubnet openstackSubnet = OpenstackSubnet.builder()
72 .setName(name)
73 .setEnableDhcp(enableDhcp)
74 .setNetworkId(networkId)
75 .setTenantId(tenantId)
sanghoshin46297d22015-11-03 17:51:24 +090076 .setDnsNameservers(dnsList)
danielbb83ebc2015-10-29 15:13:06 +090077 .setGatewayIp(gatewayIp)
78 .setCidr(cidr)
79 .setId(id)
80 .build();
81 return openstackSubnet;
82 }
83}