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 | |
| 28 | import static com.google.common.base.Preconditions.checkNotNull; |
| 29 | import static org.onlab.util.Tools.nullIsIllegal; |
| 30 | |
| 31 | /** |
| 32 | * Encode and decode to/from JSON to FngAddress object. |
| 33 | */ |
| 34 | public class FngAddressCodec extends JsonCodec<FngAddress> { |
| 35 | |
| 36 | @Override |
| 37 | public ObjectNode encode(FngAddress fngAddress, CodecContext context) { |
| 38 | checkNotNull(fngAddress, "FngAddress cannot be null"); |
| 39 | ObjectNode result = context.mapper().createObjectNode() |
| 40 | .put("address-type", fngAddress.addressType().name()); |
| 41 | |
| 42 | if (fngAddress.addressType().equals(Mep.FngAddressType.IPV4) || |
| 43 | fngAddress.addressType().equals(Mep.FngAddressType.IPV6)) { |
| 44 | result.put("ip-address", fngAddress.ipAddress().toString()); |
| 45 | } |
| 46 | |
| 47 | return result; |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public FngAddress decode(ObjectNode json, CodecContext context) { |
| 52 | if (json == null || !json.isObject()) { |
| 53 | return null; |
| 54 | } |
| 55 | |
| 56 | JsonNode node = json.get("fng-address"); |
| 57 | |
| 58 | String addressType = nullIsIllegal(node.get("address-type"), |
| 59 | "address type is required").asText(); |
| 60 | Mep.FngAddressType type = Mep.FngAddressType.valueOf(addressType.toUpperCase()); |
| 61 | JsonNode ipAddressNode = node.get("ipAddress"); |
| 62 | |
| 63 | switch (type) { |
| 64 | case IPV4: |
| 65 | return FngAddress.ipV4Address(Ip4Address.valueOf(ipAddressNode.asText())); |
| 66 | case IPV6: |
| 67 | return FngAddress.ipV6Address(Ip6Address.valueOf(ipAddressNode.asText())); |
| 68 | case NOT_TRANSMITTED: |
| 69 | return FngAddress.notTransmitted(IpAddress.valueOf(ipAddressNode.asText())); |
| 70 | case NOT_SPECIFIED: |
| 71 | default: |
| 72 | return FngAddress.notSpecified(); |
| 73 | } |
| 74 | } |
| 75 | } |