Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 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 | |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 17 | package org.onosproject.codec.impl; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.onosproject.codec.CodecContext; |
| 21 | import org.onosproject.codec.JsonCodec; |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 22 | import org.onosproject.incubator.net.virtual.DefaultVirtualLink; |
| 23 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 24 | import org.onosproject.incubator.net.virtual.VirtualLink; |
| 25 | import org.onosproject.net.Link; |
| 26 | |
| 27 | import static com.google.common.base.Preconditions.checkNotNull; |
| 28 | import static org.onlab.util.Tools.nullIsIllegal; |
| 29 | |
| 30 | /** |
| 31 | * Codec for the VirtualLink class. |
| 32 | */ |
| 33 | public class VirtualLinkCodec extends JsonCodec<VirtualLink> { |
| 34 | |
| 35 | // JSON field names |
| 36 | private static final String NETWORK_ID = "networkId"; |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 37 | |
| 38 | private static final String NULL_OBJECT_MSG = "VirtualLink cannot be null"; |
| 39 | private static final String MISSING_MEMBER_MSG = " member is required in VirtualLink"; |
| 40 | |
| 41 | @Override |
| 42 | public ObjectNode encode(VirtualLink vLink, CodecContext context) { |
| 43 | checkNotNull(vLink, NULL_OBJECT_MSG); |
| 44 | |
Claudine Chiu | 1decd53 | 2016-04-19 18:30:01 +0000 | [diff] [blame] | 45 | ObjectNode result = context.mapper().createObjectNode() |
| 46 | .put(NETWORK_ID, vLink.networkId().toString()); |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 47 | JsonCodec<Link> codec = context.codec(Link.class); |
Claudine Chiu | 1decd53 | 2016-04-19 18:30:01 +0000 | [diff] [blame] | 48 | ObjectNode linkResult = codec.encode(vLink, context); |
| 49 | result.setAll(linkResult); |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 50 | return result; |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public VirtualLink decode(ObjectNode json, CodecContext context) { |
| 55 | if (json == null || !json.isObject()) { |
| 56 | return null; |
| 57 | } |
| 58 | JsonCodec<Link> codec = context.codec(Link.class); |
| 59 | Link link = codec.decode(json, context); |
| 60 | NetworkId nId = NetworkId.networkId(Long.parseLong(extractMember(NETWORK_ID, json))); |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 61 | return DefaultVirtualLink.builder() |
| 62 | .networkId(nId) |
| 63 | .src(link.src()) |
| 64 | .dst(link.dst()) |
| 65 | .build(); |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Extract member from JSON ObjectNode. |
| 70 | * |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 71 | * @param key key for which value is needed |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 72 | * @param json JSON ObjectNode |
| 73 | * @return member value |
| 74 | */ |
| 75 | private String extractMember(String key, ObjectNode json) { |
| 76 | return nullIsIllegal(json.get(key), key + MISSING_MEMBER_MSG).asText(); |
| 77 | } |
| 78 | } |