blob: 99f1f80083b7d4a9da551678248f090425d7da52 [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;
sanghoshin94872a12015-10-16 18:04:34 +090022import org.onlab.packet.Ip4Address;
23import org.onlab.packet.MacAddress;
24import org.onosproject.codec.CodecContext;
25import org.onosproject.codec.JsonCodec;
26import org.onosproject.openstackswitching.OpenstackPort;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
sangho5db8e052016-01-29 16:08:23 +090030import java.util.Collection;
sanghoshin94872a12015-10-16 18:04:34 +090031import java.util.HashMap;
32
33/**
sanghoshin46297d22015-11-03 17:51:24 +090034 * Encodes and decodes the OpenstackPort.
sanghoshin94872a12015-10-16 18:04:34 +090035 */
36public 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);
sanghoshinf25d2e02015-11-11 23:07:17 +090063 if (portInfo == null) {
64 portInfo = json;
65 }
sanghoshin94872a12015-10-16 18:04:34 +090066
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();
sanghof2ca7e5c2015-12-23 16:02:43 +090078 if (!fixedIpInfo.path(IP_ADDRESS).isMissingNode() && ipAddressStr != null) {
sanghoshin94872a12015-10-16 18:04:34 +090079 Ip4Address ipAddress = Ip4Address.valueOf(ipAddressStr);
80 fixedIpMap.put(subnetId, ipAddress);
81 }
82 }
83 String id = portInfo.path(ID).asText();
sangho5db8e052016-01-29 16:08:23 +090084 ArrayNode securityGroupList = (ArrayNode) portInfo.path(SECURITY_GROUPS);
85 Collection<String> securityGroupIdList = Lists.newArrayList();
86 securityGroupList.forEach(securityGroup -> securityGroupIdList.add(securityGroup.asText()));
sanghoshin94872a12015-10-16 18:04:34 +090087 String deviceId = portInfo.path(DEVICE_ID).asText();
88
89 OpenstackPort.Builder openstackPortBuilder = OpenstackPort.builder();
sanghoshinc5827d52015-12-11 12:52:02 +090090 OpenstackPort.PortStatus portStatus =
91 status.equals("N/A") ? OpenstackPort.PortStatus.NA :
92 OpenstackPort.PortStatus.valueOf(status);
93
94 openstackPortBuilder.portStatus(portStatus)
sanghoshin94872a12015-10-16 18:04:34 +090095 .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)
sangho5db8e052016-01-29 16:08:23 +0900103 .deviceId(deviceId)
104 .securityGroup(securityGroupIdList);
sanghoshin94872a12015-10-16 18:04:34 +0900105
sanghoshin94872a12015-10-16 18:04:34 +0900106
107 OpenstackPort openstackPort = openstackPortBuilder.build();
108
109 return openstackPort;
110 }
111
112}