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