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