blob: b29b3898c9c0801a9e869fa9062f7b4ad1fcd15c [file] [log] [blame]
sanghoshin94872a12015-10-16 18:04:34 +09001/*
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 */
16package org.onosproject.openstackswitching.web;
17
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;
28import org.onosproject.openstackswitching.OpenstackPort;
29import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
sangho5db8e052016-01-29 16:08:23 +090032import java.util.Collection;
sanghoshin94872a12015-10-16 18:04:34 +090033import java.util.HashMap;
Hyunsun Moon10201322016-02-15 05:00:22 -080034import java.util.Map;
sanghoshin94872a12015-10-16 18:04:34 +090035
36/**
sanghoshin46297d22015-11-03 17:51:24 +090037 * Encodes and decodes the OpenstackPort.
sanghoshin94872a12015-10-16 18:04:34 +090038 */
39public class OpenstackPortCodec extends JsonCodec<OpenstackPort> {
40
41 private static Logger log = LoggerFactory
42 .getLogger(OpenstackPortCodec.class);
43
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
65 HashMap<String, Ip4Address> fixedIpMap = new HashMap<>();
66 JsonNode portInfo = json.get(PORT);
sanghoshinf25d2e02015-11-11 23:07:17 +090067 if (portInfo == null) {
68 portInfo = json;
69 }
sanghoshin94872a12015-10-16 18:04:34 +090070
71 String status = portInfo.path(STATUS).asText();
72 String name = portInfo.path(NAME).asText();
73 boolean adminStateUp = portInfo.path(ADMIN_STATUS).asBoolean();
74 String networkId = portInfo.path(NETWORK_ID).asText();
75 String tenantId = portInfo.path(TENANT_ID).asText();
76 String deviceOwner = portInfo.path(DEVICE_OWNER).asText();
77 String macStr = portInfo.path(MAC_ADDRESS).asText();
78 ArrayNode fixedIpList = (ArrayNode) portInfo.path(FIXED_IPS);
79 for (JsonNode fixedIpInfo: fixedIpList) {
80 String subnetId = fixedIpInfo.path(SUBNET_ID).asText();
81 String ipAddressStr = fixedIpInfo.path(IP_ADDRESS).asText();
sanghof2ca7e5c2015-12-23 16:02:43 +090082 if (!fixedIpInfo.path(IP_ADDRESS).isMissingNode() && ipAddressStr != null) {
sanghoshin94872a12015-10-16 18:04:34 +090083 Ip4Address ipAddress = Ip4Address.valueOf(ipAddressStr);
84 fixedIpMap.put(subnetId, ipAddress);
85 }
86 }
87 String id = portInfo.path(ID).asText();
sangho5db8e052016-01-29 16:08:23 +090088 ArrayNode securityGroupList = (ArrayNode) portInfo.path(SECURITY_GROUPS);
89 Collection<String> securityGroupIdList = Lists.newArrayList();
90 securityGroupList.forEach(securityGroup -> securityGroupIdList.add(securityGroup.asText()));
sanghoshin94872a12015-10-16 18:04:34 +090091 String deviceId = portInfo.path(DEVICE_ID).asText();
92
Hyunsun Moon10201322016-02-15 05:00:22 -080093 Map<IpAddress, MacAddress> addressPairs = Maps.newHashMap();
94 for (JsonNode addrPair : (ArrayNode) portInfo.path(ADDRESS_PAIR)) {
95 try {
96 addressPairs.put(IpAddress.valueOf(addrPair.path(IP_ADDRESS).asText()),
97 MacAddress.valueOf(addrPair.path(MAC_ADDRESS).asText()));
98 } catch (IllegalArgumentException e) {
99 log.debug("Invalid address pair {}", addrPair.toString());
100 }
101 }
102
sanghoshin94872a12015-10-16 18:04:34 +0900103 OpenstackPort.Builder openstackPortBuilder = OpenstackPort.builder();
sanghoshinc5827d52015-12-11 12:52:02 +0900104 OpenstackPort.PortStatus portStatus =
Hyunsun Moon10201322016-02-15 05:00:22 -0800105 status.equals(NA) ? OpenstackPort.PortStatus.NA :
sanghoshinc5827d52015-12-11 12:52:02 +0900106 OpenstackPort.PortStatus.valueOf(status);
107
108 openstackPortBuilder.portStatus(portStatus)
sanghoshin94872a12015-10-16 18:04:34 +0900109 .name(name)
110 .adminState(adminStateUp)
111 .netwrokId(networkId)
112 .tenantId(tenantId)
113 .deviceOwner(deviceOwner)
114 .macAddress(MacAddress.valueOf(macStr))
115 .fixedIps(fixedIpMap)
116 .id(id)
sangho5db8e052016-01-29 16:08:23 +0900117 .deviceId(deviceId)
118 .securityGroup(securityGroupIdList);
sanghoshin94872a12015-10-16 18:04:34 +0900119
Hyunsun Moon10201322016-02-15 05:00:22 -0800120 if (!addressPairs.isEmpty()) {
121 openstackPortBuilder.allowedAddressPairs(addressPairs);
122 }
sanghoshin94872a12015-10-16 18:04:34 +0900123
124 OpenstackPort openstackPort = openstackPortBuilder.build();
125
126 return openstackPort;
127 }
128
129}