blob: 3ec608370c78a719322f77455b996b8f3fbf3adb [file] [log] [blame]
danielbb83ebc2015-10-29 15:13:06 +09001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
danielbb83ebc2015-10-29 15:13:06 +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 */
sangho93447f12016-02-24 00:33:22 +090016package org.onosproject.openstackinterface.web;
danielbb83ebc2015-10-29 15:13:06 +090017
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;
sangho93447f12016-02-24 00:33:22 +090027import org.onosproject.openstackinterface.OpenstackSubnet;
danielbb83ebc2015-10-29 15:13:06 +090028import 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
sangho0c2a3da2016-02-16 13:39:07 +090033import static com.google.common.base.Preconditions.checkNotNull;
34
sanghoshin46297d22015-11-03 17:51:24 +090035/**
36 * Encodes and decodes the OpenstackSubnet.
37 */
danielbb83ebc2015-10-29 15:13:06 +090038public class OpenstackSubnetCodec extends JsonCodec<OpenstackSubnet> {
sangho0c2a3da2016-02-16 13:39:07 +090039 private final Logger log = LoggerFactory.getLogger(getClass());
danielbb83ebc2015-10-29 15:13:06 +090040
41 // JSON Field names
42 private static final String SUBNET = "subnet";
43 private static final String NAME = "name";
44 private static final String ENABLE_DHCP = "enable_dhcp";
45 private static final String NETWORK_ID = "network_id";
46 private static final String TENANT_ID = "tenant_id";
47 private static final String DNS_NAMESERVERS = "dns_nameservers";
48 private static final String GATEWAY_IP = "gateway_ip";
49 private static final String CIDR = "cidr";
50 private static final String ID = "id";
51
52 @Override
53 public OpenstackSubnet decode(ObjectNode json, CodecContext context) {
sangho0c2a3da2016-02-16 13:39:07 +090054 checkNotNull(json);
danielbb83ebc2015-10-29 15:13:06 +090055 JsonNode subnetInfo = json.get(SUBNET);
sanghoshinf25d2e02015-11-11 23:07:17 +090056 if (subnetInfo == null) {
57 subnetInfo = json;
58 }
danielbb83ebc2015-10-29 15:13:06 +090059
60 String name = subnetInfo.path(NAME).asText();
61 boolean enableDhcp = subnetInfo.path(ENABLE_DHCP).asBoolean();
62 String networkId = subnetInfo.path(NETWORK_ID).asText();
63 String tenantId = subnetInfo.path(TENANT_ID).asText();
sanghoshin46297d22015-11-03 17:51:24 +090064 ArrayNode dnsNameservsers = (ArrayNode) subnetInfo.path(DNS_NAMESERVERS);
65 List<Ip4Address> dnsList = Lists.newArrayList();
66 if (dnsNameservsers != null && !dnsNameservsers.isMissingNode()) {
67 dnsNameservsers.forEach(dns -> dnsList.add(Ip4Address.valueOf(dns.asText())));
68 }
danielbb83ebc2015-10-29 15:13:06 +090069 String gatewayIp = subnetInfo.path(GATEWAY_IP).asText();
70 String cidr = subnetInfo.path(CIDR).asText();
71 String id = subnetInfo.path(ID).asText();
72
73 OpenstackSubnet openstackSubnet = OpenstackSubnet.builder()
74 .setName(name)
75 .setEnableDhcp(enableDhcp)
76 .setNetworkId(networkId)
77 .setTenantId(tenantId)
sanghoshin46297d22015-11-03 17:51:24 +090078 .setDnsNameservers(dnsList)
danielbb83ebc2015-10-29 15:13:06 +090079 .setGatewayIp(gatewayIp)
80 .setCidr(cidr)
81 .setId(id)
82 .build();
83 return openstackSubnet;
84 }
85}