Daniel Park | 3a06c52 | 2016-01-28 20:51:12 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016 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; |
| 21 | import com.google.common.collect.Maps; |
| 22 | import org.onlab.packet.Ip4Address; |
| 23 | import org.onosproject.codec.CodecContext; |
| 24 | import org.onosproject.codec.JsonCodec; |
| 25 | import org.onosproject.openstackrouting.OpenstackExternalGateway; |
| 26 | import org.onosproject.openstackrouting.OpenstackRouter; |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | import static com.google.common.base.Preconditions.checkNotNull; |
| 30 | |
| 31 | import java.util.Map; |
| 32 | /** |
| 33 | * Implementation of the OpenstackRouter Codec. |
| 34 | */ |
| 35 | public class OpenstackRouterCodec extends JsonCodec<OpenstackRouter> { |
| 36 | protected static final Logger log = LoggerFactory |
| 37 | .getLogger(OpenstackNetworkCodec.class); |
| 38 | |
| 39 | private static final String ROUTER = "router"; |
| 40 | private static final String TENANT_ID = "tenant_id"; |
| 41 | private static final String NETWORK_ID = "network_id"; |
| 42 | private static final String ID = "id"; |
| 43 | private static final String NAME = "name"; |
| 44 | private static final String STATUS = "status"; |
| 45 | private static final String ADMIN_STATE_UP = "admin_state_up"; |
| 46 | private static final String EXTERNAL_GW_INFO = "external_gateway_info"; |
| 47 | private static final String EXTERNAL_FIXED_IPS = "external_fixed_ips"; |
| 48 | private static final String SUBNET_ID = "subnet_id"; |
| 49 | private static final String IP_ADDRESS = "ip_address"; |
| 50 | |
| 51 | /** |
| 52 | * Decodes the OpenstackRouter. |
| 53 | * |
| 54 | * @param json JSON to decode |
| 55 | * @param context decoding context |
| 56 | * @return OpenstackRouter |
| 57 | */ |
| 58 | @Override |
| 59 | public OpenstackRouter decode(ObjectNode json, CodecContext context) { |
| 60 | |
| 61 | if (json == null || !json.isObject()) { |
| 62 | return null; |
| 63 | } |
| 64 | JsonNode routerInfo = json.get(ROUTER); |
| 65 | if (routerInfo == null) { |
| 66 | routerInfo = json; |
| 67 | } |
| 68 | |
| 69 | String tenantId = checkNotNull(routerInfo.path(TENANT_ID).asText()); |
| 70 | String id = checkNotNull(routerInfo.path(ID).asText()); |
| 71 | String name = checkNotNull(routerInfo.path(NAME).asText()); |
| 72 | String status = checkNotNull(routerInfo.path(STATUS).asText()); |
| 73 | String adminStateUp = checkNotNull(routerInfo.path(ADMIN_STATE_UP).asText()); |
| 74 | |
| 75 | OpenstackExternalGateway.Builder osExtBuiler = OpenstackExternalGateway.builder(); |
| 76 | |
| 77 | if (!routerInfo.path(EXTERNAL_GW_INFO).isMissingNode()) { |
| 78 | String externalGatewayNetId = checkNotNull(routerInfo.path(EXTERNAL_GW_INFO).path(NETWORK_ID).asText()); |
| 79 | Map<String, Ip4Address> fixedIpMap = Maps.newHashMap(); |
| 80 | |
| 81 | |
| 82 | if (!routerInfo.path(EXTERNAL_GW_INFO).path(EXTERNAL_FIXED_IPS).isMissingNode()) { |
| 83 | ArrayNode fixedIpList = (ArrayNode) routerInfo.path(EXTERNAL_GW_INFO).path(EXTERNAL_FIXED_IPS); |
| 84 | |
| 85 | for (JsonNode fixedIpInfo : fixedIpList) { |
| 86 | String subnetId = checkNotNull(fixedIpInfo.path(SUBNET_ID).asText()); |
| 87 | String ipAddressStr = checkNotNull(fixedIpInfo.path(IP_ADDRESS).asText()); |
| 88 | if (!fixedIpInfo.path(IP_ADDRESS).isMissingNode() && ipAddressStr != null) { |
| 89 | fixedIpMap.put(subnetId, Ip4Address.valueOf(ipAddressStr)); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | osExtBuiler.networkId(externalGatewayNetId) |
| 95 | .enablePnat(true) |
| 96 | .externalFixedIps(fixedIpMap); |
| 97 | } |
| 98 | OpenstackRouter.Builder osBuilder = new OpenstackRouter.Builder() |
| 99 | .tenantId(tenantId) |
| 100 | .id(id) |
| 101 | .name(name) |
| 102 | .status(OpenstackRouter.RouterStatus.valueOf(status)) |
| 103 | .adminStateUp(Boolean.valueOf(adminStateUp)) |
| 104 | .gatewayExternalInfo(osExtBuiler.build()); |
| 105 | |
| 106 | return osBuilder.build(); |
| 107 | } |
| 108 | } |