Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | package org.onosproject.cfm.web; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.onlab.packet.Ip4Address; |
| 21 | import org.onlab.packet.Ip6Address; |
| 22 | import org.onlab.packet.IpAddress; |
| 23 | import org.onosproject.codec.CodecContext; |
| 24 | import org.onosproject.codec.JsonCodec; |
| 25 | import org.onosproject.incubator.net.l2monitoring.cfm.Mep; |
| 26 | import org.onosproject.incubator.net.l2monitoring.cfm.Mep.FngAddress; |
| 27 | |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 28 | import java.util.Locale; |
| 29 | |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 30 | import static com.google.common.base.Preconditions.checkNotNull; |
| 31 | import static org.onlab.util.Tools.nullIsIllegal; |
| 32 | |
| 33 | /** |
| 34 | * Encode and decode to/from JSON to FngAddress object. |
| 35 | */ |
| 36 | public class FngAddressCodec extends JsonCodec<FngAddress> { |
| 37 | |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 38 | /** |
| 39 | * Encodes the FngAddress entity into JSON. |
| 40 | * |
| 41 | * @param fngAddress FngAddress to encode |
| 42 | * @param context encoding context |
| 43 | * @return JSON node |
| 44 | * @throws java.lang.UnsupportedOperationException if the codec does not |
| 45 | * support encode operations |
| 46 | */ |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 47 | @Override |
| 48 | public ObjectNode encode(FngAddress fngAddress, CodecContext context) { |
| 49 | checkNotNull(fngAddress, "FngAddress cannot be null"); |
| 50 | ObjectNode result = context.mapper().createObjectNode() |
| 51 | .put("address-type", fngAddress.addressType().name()); |
| 52 | |
| 53 | if (fngAddress.addressType().equals(Mep.FngAddressType.IPV4) || |
| 54 | fngAddress.addressType().equals(Mep.FngAddressType.IPV6)) { |
| 55 | result.put("ip-address", fngAddress.ipAddress().toString()); |
| 56 | } |
| 57 | |
| 58 | return result; |
| 59 | } |
| 60 | |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 61 | /** |
| 62 | * Decodes the FngAddress entity from JSON. |
| 63 | * |
| 64 | * @param json JSON to decode |
| 65 | * @param context decoding context |
| 66 | * @return decoded FngAddress |
| 67 | * @throws java.lang.UnsupportedOperationException if the codec does not |
| 68 | * support decode operations |
| 69 | */ |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 70 | @Override |
| 71 | public FngAddress decode(ObjectNode json, CodecContext context) { |
| 72 | if (json == null || !json.isObject()) { |
| 73 | return null; |
| 74 | } |
| 75 | |
| 76 | JsonNode node = json.get("fng-address"); |
| 77 | |
| 78 | String addressType = nullIsIllegal(node.get("address-type"), |
| 79 | "address type is required").asText(); |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 80 | Mep.FngAddressType type = Mep.FngAddressType.valueOf(addressType.toUpperCase(Locale.ENGLISH)); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 81 | JsonNode ipAddressNode = node.get("ipAddress"); |
| 82 | |
| 83 | switch (type) { |
| 84 | case IPV4: |
| 85 | return FngAddress.ipV4Address(Ip4Address.valueOf(ipAddressNode.asText())); |
| 86 | case IPV6: |
| 87 | return FngAddress.ipV6Address(Ip6Address.valueOf(ipAddressNode.asText())); |
| 88 | case NOT_TRANSMITTED: |
| 89 | return FngAddress.notTransmitted(IpAddress.valueOf(ipAddressNode.asText())); |
| 90 | case NOT_SPECIFIED: |
| 91 | default: |
| 92 | return FngAddress.notSpecified(); |
| 93 | } |
| 94 | } |
| 95 | } |