blob: 7f4624d76d27ab64ea711c11b9171f0ae62c09c9 [file] [log] [blame]
Daniel Park3a06c522016-01-28 20:51:12 +09001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Daniel Park3a06c522016-01-28 20:51:12 +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;
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;
sangho93447f12016-02-24 00:33:22 +090025import org.onosproject.openstackinterface.OpenstackExternalGateway;
26import org.onosproject.openstackinterface.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());
Daniel Park3a06c522016-01-28 20:51:12 +090071 String adminStateUp = checkNotNull(routerInfo.path(ADMIN_STATE_UP).asText());
72
sangho0c2a3da2016-02-16 13:39:07 +090073 OpenstackExternalGateway.Builder osExtBuiler = new OpenstackExternalGateway.Builder();
Daniel Park3a06c522016-01-28 20:51:12 +090074
75 if (!routerInfo.path(EXTERNAL_GW_INFO).isMissingNode()) {
76 String externalGatewayNetId = checkNotNull(routerInfo.path(EXTERNAL_GW_INFO).path(NETWORK_ID).asText());
77 Map<String, Ip4Address> fixedIpMap = Maps.newHashMap();
78
79
80 if (!routerInfo.path(EXTERNAL_GW_INFO).path(EXTERNAL_FIXED_IPS).isMissingNode()) {
81 ArrayNode fixedIpList = (ArrayNode) routerInfo.path(EXTERNAL_GW_INFO).path(EXTERNAL_FIXED_IPS);
82
83 for (JsonNode fixedIpInfo : fixedIpList) {
84 String subnetId = checkNotNull(fixedIpInfo.path(SUBNET_ID).asText());
85 String ipAddressStr = checkNotNull(fixedIpInfo.path(IP_ADDRESS).asText());
86 if (!fixedIpInfo.path(IP_ADDRESS).isMissingNode() && ipAddressStr != null) {
87 fixedIpMap.put(subnetId, Ip4Address.valueOf(ipAddressStr));
88 }
89 }
90 }
91
92 osExtBuiler.networkId(externalGatewayNetId)
93 .enablePnat(true)
94 .externalFixedIps(fixedIpMap);
95 }
96 OpenstackRouter.Builder osBuilder = new OpenstackRouter.Builder()
97 .tenantId(tenantId)
98 .id(id)
99 .name(name)
Daniel Park81a61a12016-02-26 08:24:44 +0900100 .status(OpenstackRouter.RouterStatus.ACTIVE)
Daniel Park3a06c522016-01-28 20:51:12 +0900101 .adminStateUp(Boolean.valueOf(adminStateUp))
102 .gatewayExternalInfo(osExtBuiler.build());
103
104 return osBuilder.build();
105 }
106}