daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
sangho | 93447f1 | 2016-02-24 00:33:22 +0900 | [diff] [blame] | 16 | package org.onosproject.openstackinterface.web; |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | |
| 20 | |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 21 | import com.fasterxml.jackson.databind.node.ArrayNode; |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 22 | import com.fasterxml.jackson.databind.node.ObjectNode; |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 23 | import com.google.common.collect.Lists; |
| 24 | import org.onlab.packet.Ip4Address; |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 25 | import org.onosproject.codec.CodecContext; |
| 26 | import org.onosproject.codec.JsonCodec; |
sangho | 93447f1 | 2016-02-24 00:33:22 +0900 | [diff] [blame] | 27 | import org.onosproject.openstackinterface.OpenstackSubnet; |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
| 30 | |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 31 | import java.util.List; |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 32 | |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 33 | import static com.google.common.base.Preconditions.checkNotNull; |
| 34 | |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 35 | /** |
| 36 | * Encodes and decodes the OpenstackSubnet. |
| 37 | */ |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 38 | public class OpenstackSubnetCodec extends JsonCodec<OpenstackSubnet> { |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 39 | private final Logger log = LoggerFactory.getLogger(getClass()); |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 40 | |
| 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) { |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 54 | checkNotNull(json); |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 55 | JsonNode subnetInfo = json.get(SUBNET); |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 56 | if (subnetInfo == null) { |
| 57 | subnetInfo = json; |
| 58 | } |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 59 | |
| 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(); |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 64 | 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 | } |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 69 | 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) |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 78 | .setDnsNameservers(dnsList) |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 79 | .setGatewayIp(gatewayIp) |
| 80 | .setCidr(cidr) |
| 81 | .setId(id) |
| 82 | .build(); |
| 83 | return openstackSubnet; |
| 84 | } |
| 85 | } |