blob: 9203321c67fe23f21227297447f8d01409e45e7a [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.ObjectNode;
20import org.onosproject.codec.CodecContext;
21import org.onosproject.codec.JsonCodec;
22import org.onosproject.openstackswitching.OpenstackNetwork;
23import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
26/**
27 * Implementation of the OpenstackNetwork Codec.
28 *
29 */
30public class OpenstackNetworkCodec extends JsonCodec<OpenstackNetwork> {
31
32 protected static final Logger log = LoggerFactory
33 .getLogger(OpenstackNetworkCodec.class);
34
35 private static final String NETWORK = "network";
36 private static final String NAME = "name";
37 private static final String TENANT_ID = "tenant_id";
38 private static final String SEGMENTATION_ID = "provider:segmentation_id";
39 private static final String NETWORK_TYPE = "provider:network_type";
40 private static final String ID = "id";
41
42 @Override
43 public OpenstackNetwork decode(ObjectNode json, CodecContext context) {
44
45 JsonNode networkInfo = json.get(NETWORK);
sanghoshinf25d2e02015-11-11 23:07:17 +090046 if (networkInfo == null) {
47 networkInfo = json;
48 }
sanghoshin94872a12015-10-16 18:04:34 +090049
50 String name = networkInfo.path(NAME).asText();
51 String tenantId = networkInfo.path(TENANT_ID).asText();
52 String id = networkInfo.path(ID).asText();
53
54 OpenstackNetwork.Builder onb = OpenstackNetwork.builder();
55 onb.name(name)
56 .tenantId(tenantId)
57 .id(id);
58
Hyunsun Moonf7895202016-01-12 12:21:48 -080059 if (networkInfo.path(NETWORK_TYPE).isMissingNode()) {
Hyunsun Moon91866132016-02-01 23:30:58 -080060 log.debug("Network {} has no network type, ignore it.", name);
Hyunsun Moonf7895202016-01-12 12:21:48 -080061 return null;
sanghoshin94872a12015-10-16 18:04:34 +090062 }
63
Hyunsun Moonf7895202016-01-12 12:21:48 -080064 String networkType = networkInfo.path(NETWORK_TYPE).asText();
65 try {
66 onb.networkType(OpenstackNetwork.NetworkType.valueOf(networkType.toUpperCase()));
67 } catch (IllegalArgumentException e) {
Hyunsun Moon91866132016-02-01 23:30:58 -080068 log.debug("Network {} has unsupported network type {}, ignore it.",
Hyunsun Moonf7895202016-01-12 12:21:48 -080069 name, networkType);
70 return null;
71 }
72
73 onb.segmentId(networkInfo.path(SEGMENTATION_ID).asText());
74
sanghoshin94872a12015-10-16 18:04:34 +090075 return onb.build();
76 }
sanghoshin94872a12015-10-16 18:04:34 +090077}