blob: 01acda15d16f40777d7f7454fc90cea7822471a2 [file] [log] [blame]
sanghoshin94872a12015-10-16 18:04:34 +09001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
sanghoshin94872a12015-10-16 18:04:34 +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;
sanghoshin94872a12015-10-16 18:04:34 +090017
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
sangho5db8e052016-01-29 16:08:23 +090021import com.google.common.collect.Lists;
Hyunsun Moon10201322016-02-15 05:00:22 -080022import com.google.common.collect.Maps;
sanghoshin94872a12015-10-16 18:04:34 +090023import org.onlab.packet.Ip4Address;
Hyunsun Moon10201322016-02-15 05:00:22 -080024import org.onlab.packet.IpAddress;
sanghoshin94872a12015-10-16 18:04:34 +090025import org.onlab.packet.MacAddress;
26import org.onosproject.codec.CodecContext;
27import org.onosproject.codec.JsonCodec;
sangho93447f12016-02-24 00:33:22 +090028import org.onosproject.openstackinterface.OpenstackPort;
sanghoshin94872a12015-10-16 18:04:34 +090029import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
sangho5db8e052016-01-29 16:08:23 +090032import java.util.Collection;
Hyunsun Moon10201322016-02-15 05:00:22 -080033import java.util.Map;
sanghoshin94872a12015-10-16 18:04:34 +090034
sangho0c2a3da2016-02-16 13:39:07 +090035import static com.google.common.base.Preconditions.checkNotNull;
36
sanghoshin94872a12015-10-16 18:04:34 +090037/**
sanghoshin46297d22015-11-03 17:51:24 +090038 * Encodes and decodes the OpenstackPort.
sanghoshin94872a12015-10-16 18:04:34 +090039 */
40public class OpenstackPortCodec extends JsonCodec<OpenstackPort> {
41
sangho0c2a3da2016-02-16 13:39:07 +090042 private final Logger log = LoggerFactory.getLogger(getClass());
sanghoshin94872a12015-10-16 18:04:34 +090043
44 // JSON field names
45 private static final String PORT = "port";
46 private static final String STATUS = "status";
47 private static final String NAME = "name";
48 private static final String ADDRESS_PAIR = "allowed_address_pairs";
49 private static final String ADMIN_STATUS = "admin_status";
50 private static final String NETWORK_ID = "network_id";
51 private static final String TENANT_ID = "tenant_id";
52 private static final String DEVICE_OWNER = "device_owner";
53 private static final String MAC_ADDRESS = "mac_address";
54 private static final String FIXED_IPS = "fixed_ips";
55 private static final String SUBNET_ID = "subnet_id";
56 private static final String IP_ADDRESS = "ip_address";
57 private static final String ID = "id";
58 private static final String SECURITY_GROUPS = "security_groups";
59 private static final String DEVICE_ID = "device_id";
Hyunsun Moon10201322016-02-15 05:00:22 -080060 private static final String NA = "N/A";
sanghoshin94872a12015-10-16 18:04:34 +090061
62 @Override
63 public OpenstackPort decode(ObjectNode json, CodecContext context) {
64
sangho0c2a3da2016-02-16 13:39:07 +090065 checkNotNull(json);
Daniel Park23193902016-03-24 18:17:19 +090066 Map<String, Ip4Address> fixedIpMap = Maps.newHashMap();
sanghoshin94872a12015-10-16 18:04:34 +090067 JsonNode portInfo = json.get(PORT);
sanghoshinf25d2e02015-11-11 23:07:17 +090068 if (portInfo == null) {
69 portInfo = json;
70 }
sanghoshin94872a12015-10-16 18:04:34 +090071
72 String status = portInfo.path(STATUS).asText();
73 String name = portInfo.path(NAME).asText();
74 boolean adminStateUp = portInfo.path(ADMIN_STATUS).asBoolean();
75 String networkId = portInfo.path(NETWORK_ID).asText();
76 String tenantId = portInfo.path(TENANT_ID).asText();
77 String deviceOwner = portInfo.path(DEVICE_OWNER).asText();
78 String macStr = portInfo.path(MAC_ADDRESS).asText();
79 ArrayNode fixedIpList = (ArrayNode) portInfo.path(FIXED_IPS);
80 for (JsonNode fixedIpInfo: fixedIpList) {
81 String subnetId = fixedIpInfo.path(SUBNET_ID).asText();
82 String ipAddressStr = fixedIpInfo.path(IP_ADDRESS).asText();
sanghof2ca7e5c2015-12-23 16:02:43 +090083 if (!fixedIpInfo.path(IP_ADDRESS).isMissingNode() && ipAddressStr != null) {
sanghoshin94872a12015-10-16 18:04:34 +090084 Ip4Address ipAddress = Ip4Address.valueOf(ipAddressStr);
85 fixedIpMap.put(subnetId, ipAddress);
86 }
87 }
88 String id = portInfo.path(ID).asText();
sangho5db8e052016-01-29 16:08:23 +090089 ArrayNode securityGroupList = (ArrayNode) portInfo.path(SECURITY_GROUPS);
90 Collection<String> securityGroupIdList = Lists.newArrayList();
91 securityGroupList.forEach(securityGroup -> securityGroupIdList.add(securityGroup.asText()));
sanghoshin94872a12015-10-16 18:04:34 +090092 String deviceId = portInfo.path(DEVICE_ID).asText();
93
Hyunsun Moon10201322016-02-15 05:00:22 -080094 Map<IpAddress, MacAddress> addressPairs = Maps.newHashMap();
95 for (JsonNode addrPair : (ArrayNode) portInfo.path(ADDRESS_PAIR)) {
96 try {
97 addressPairs.put(IpAddress.valueOf(addrPair.path(IP_ADDRESS).asText()),
98 MacAddress.valueOf(addrPair.path(MAC_ADDRESS).asText()));
99 } catch (IllegalArgumentException e) {
100 log.debug("Invalid address pair {}", addrPair.toString());
101 }
102 }
103
sanghoshin94872a12015-10-16 18:04:34 +0900104 OpenstackPort.Builder openstackPortBuilder = OpenstackPort.builder();
sanghoshinc5827d52015-12-11 12:52:02 +0900105 OpenstackPort.PortStatus portStatus =
Hyunsun Moon10201322016-02-15 05:00:22 -0800106 status.equals(NA) ? OpenstackPort.PortStatus.NA :
sanghoshinc5827d52015-12-11 12:52:02 +0900107 OpenstackPort.PortStatus.valueOf(status);
108
109 openstackPortBuilder.portStatus(portStatus)
sanghoshin94872a12015-10-16 18:04:34 +0900110 .name(name)
111 .adminState(adminStateUp)
112 .netwrokId(networkId)
113 .tenantId(tenantId)
114 .deviceOwner(deviceOwner)
115 .macAddress(MacAddress.valueOf(macStr))
116 .fixedIps(fixedIpMap)
117 .id(id)
sangho5db8e052016-01-29 16:08:23 +0900118 .deviceId(deviceId)
119 .securityGroup(securityGroupIdList);
sanghoshin94872a12015-10-16 18:04:34 +0900120
Hyunsun Moon10201322016-02-15 05:00:22 -0800121 if (!addressPairs.isEmpty()) {
122 openstackPortBuilder.allowedAddressPairs(addressPairs);
123 }
sanghoshin94872a12015-10-16 18:04:34 +0900124
125 OpenstackPort openstackPort = openstackPortBuilder.build();
126
127 return openstackPort;
128 }
129
130}