sanghoshin | 94872a1 | 2015-10-16 18:04:34 +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 | */ |
| 16 | package org.onosproject.openstackswitching.web; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
sangho | 5db8e05 | 2016-01-29 16:08:23 +0900 | [diff] [blame] | 21 | import com.google.common.collect.Lists; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 22 | import org.onlab.packet.Ip4Address; |
| 23 | import org.onlab.packet.MacAddress; |
| 24 | import org.onosproject.codec.CodecContext; |
| 25 | import org.onosproject.codec.JsonCodec; |
| 26 | import org.onosproject.openstackswitching.OpenstackPort; |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
sangho | 5db8e05 | 2016-01-29 16:08:23 +0900 | [diff] [blame] | 30 | import java.util.Collection; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 31 | import java.util.HashMap; |
| 32 | |
| 33 | /** |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 34 | * Encodes and decodes the OpenstackPort. |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 35 | */ |
| 36 | public class OpenstackPortCodec extends JsonCodec<OpenstackPort> { |
| 37 | |
| 38 | private static Logger log = LoggerFactory |
| 39 | .getLogger(OpenstackPortCodec.class); |
| 40 | |
| 41 | // JSON field names |
| 42 | private static final String PORT = "port"; |
| 43 | private static final String STATUS = "status"; |
| 44 | private static final String NAME = "name"; |
| 45 | private static final String ADDRESS_PAIR = "allowed_address_pairs"; |
| 46 | private static final String ADMIN_STATUS = "admin_status"; |
| 47 | private static final String NETWORK_ID = "network_id"; |
| 48 | private static final String TENANT_ID = "tenant_id"; |
| 49 | private static final String DEVICE_OWNER = "device_owner"; |
| 50 | private static final String MAC_ADDRESS = "mac_address"; |
| 51 | private static final String FIXED_IPS = "fixed_ips"; |
| 52 | private static final String SUBNET_ID = "subnet_id"; |
| 53 | private static final String IP_ADDRESS = "ip_address"; |
| 54 | private static final String ID = "id"; |
| 55 | private static final String SECURITY_GROUPS = "security_groups"; |
| 56 | private static final String DEVICE_ID = "device_id"; |
| 57 | |
| 58 | @Override |
| 59 | public OpenstackPort decode(ObjectNode json, CodecContext context) { |
| 60 | |
| 61 | HashMap<String, Ip4Address> fixedIpMap = new HashMap<>(); |
| 62 | JsonNode portInfo = json.get(PORT); |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 63 | if (portInfo == null) { |
| 64 | portInfo = json; |
| 65 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 66 | |
| 67 | String status = portInfo.path(STATUS).asText(); |
| 68 | String name = portInfo.path(NAME).asText(); |
| 69 | boolean adminStateUp = portInfo.path(ADMIN_STATUS).asBoolean(); |
| 70 | String networkId = portInfo.path(NETWORK_ID).asText(); |
| 71 | String tenantId = portInfo.path(TENANT_ID).asText(); |
| 72 | String deviceOwner = portInfo.path(DEVICE_OWNER).asText(); |
| 73 | String macStr = portInfo.path(MAC_ADDRESS).asText(); |
| 74 | ArrayNode fixedIpList = (ArrayNode) portInfo.path(FIXED_IPS); |
| 75 | for (JsonNode fixedIpInfo: fixedIpList) { |
| 76 | String subnetId = fixedIpInfo.path(SUBNET_ID).asText(); |
| 77 | String ipAddressStr = fixedIpInfo.path(IP_ADDRESS).asText(); |
sangho | f2ca7e5c | 2015-12-23 16:02:43 +0900 | [diff] [blame] | 78 | if (!fixedIpInfo.path(IP_ADDRESS).isMissingNode() && ipAddressStr != null) { |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 79 | Ip4Address ipAddress = Ip4Address.valueOf(ipAddressStr); |
| 80 | fixedIpMap.put(subnetId, ipAddress); |
| 81 | } |
| 82 | } |
| 83 | String id = portInfo.path(ID).asText(); |
sangho | 5db8e05 | 2016-01-29 16:08:23 +0900 | [diff] [blame] | 84 | ArrayNode securityGroupList = (ArrayNode) portInfo.path(SECURITY_GROUPS); |
| 85 | Collection<String> securityGroupIdList = Lists.newArrayList(); |
| 86 | securityGroupList.forEach(securityGroup -> securityGroupIdList.add(securityGroup.asText())); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 87 | String deviceId = portInfo.path(DEVICE_ID).asText(); |
| 88 | |
| 89 | OpenstackPort.Builder openstackPortBuilder = OpenstackPort.builder(); |
sanghoshin | c5827d5 | 2015-12-11 12:52:02 +0900 | [diff] [blame] | 90 | OpenstackPort.PortStatus portStatus = |
| 91 | status.equals("N/A") ? OpenstackPort.PortStatus.NA : |
| 92 | OpenstackPort.PortStatus.valueOf(status); |
| 93 | |
| 94 | openstackPortBuilder.portStatus(portStatus) |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 95 | .name(name) |
| 96 | .adminState(adminStateUp) |
| 97 | .netwrokId(networkId) |
| 98 | .tenantId(tenantId) |
| 99 | .deviceOwner(deviceOwner) |
| 100 | .macAddress(MacAddress.valueOf(macStr)) |
| 101 | .fixedIps(fixedIpMap) |
| 102 | .id(id) |
sangho | 5db8e05 | 2016-01-29 16:08:23 +0900 | [diff] [blame] | 103 | .deviceId(deviceId) |
| 104 | .securityGroup(securityGroupIdList); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 105 | |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 106 | |
| 107 | OpenstackPort openstackPort = openstackPortBuilder.build(); |
| 108 | |
| 109 | return openstackPort; |
| 110 | } |
| 111 | |
| 112 | } |