sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame^] | 16 | package org.onosproject.openstacknetworking.web; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.onosproject.codec.CodecContext; |
| 21 | import org.onosproject.codec.JsonCodec; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame^] | 22 | import org.onosproject.openstacknetworking.OpenstackNetwork; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 23 | import org.slf4j.Logger; |
| 24 | import org.slf4j.LoggerFactory; |
| 25 | |
| 26 | /** |
| 27 | * Implementation of the OpenstackNetwork Codec. |
| 28 | * |
| 29 | */ |
| 30 | public class OpenstackNetworkCodec extends JsonCodec<OpenstackNetwork> { |
| 31 | |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame^] | 32 | private final Logger log = LoggerFactory.getLogger(getClass()); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 33 | |
| 34 | private static final String NETWORK = "network"; |
| 35 | private static final String NAME = "name"; |
| 36 | private static final String TENANT_ID = "tenant_id"; |
| 37 | private static final String SEGMENTATION_ID = "provider:segmentation_id"; |
| 38 | private static final String NETWORK_TYPE = "provider:network_type"; |
| 39 | private static final String ID = "id"; |
| 40 | |
| 41 | @Override |
| 42 | public OpenstackNetwork decode(ObjectNode json, CodecContext context) { |
| 43 | |
| 44 | JsonNode networkInfo = json.get(NETWORK); |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 45 | if (networkInfo == null) { |
| 46 | networkInfo = json; |
| 47 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 48 | |
| 49 | String name = networkInfo.path(NAME).asText(); |
| 50 | String tenantId = networkInfo.path(TENANT_ID).asText(); |
| 51 | String id = networkInfo.path(ID).asText(); |
| 52 | |
| 53 | OpenstackNetwork.Builder onb = OpenstackNetwork.builder(); |
| 54 | onb.name(name) |
| 55 | .tenantId(tenantId) |
| 56 | .id(id); |
| 57 | |
Hyunsun Moon | f789520 | 2016-01-12 12:21:48 -0800 | [diff] [blame] | 58 | if (networkInfo.path(NETWORK_TYPE).isMissingNode()) { |
Hyunsun Moon | 9186613 | 2016-02-01 23:30:58 -0800 | [diff] [blame] | 59 | log.debug("Network {} has no network type, ignore it.", name); |
Hyunsun Moon | f789520 | 2016-01-12 12:21:48 -0800 | [diff] [blame] | 60 | return null; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 61 | } |
| 62 | |
Hyunsun Moon | f789520 | 2016-01-12 12:21:48 -0800 | [diff] [blame] | 63 | String networkType = networkInfo.path(NETWORK_TYPE).asText(); |
| 64 | try { |
| 65 | onb.networkType(OpenstackNetwork.NetworkType.valueOf(networkType.toUpperCase())); |
| 66 | } catch (IllegalArgumentException e) { |
Hyunsun Moon | 9186613 | 2016-02-01 23:30:58 -0800 | [diff] [blame] | 67 | log.debug("Network {} has unsupported network type {}, ignore it.", |
Hyunsun Moon | f789520 | 2016-01-12 12:21:48 -0800 | [diff] [blame] | 68 | name, networkType); |
| 69 | return null; |
| 70 | } |
| 71 | |
| 72 | onb.segmentId(networkInfo.path(SEGMENTATION_ID).asText()); |
| 73 | |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 74 | return onb.build(); |
| 75 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 76 | } |