blob: 1c0e725a0b5839a19954e6c2174ee9b63b37f1fc [file] [log] [blame]
Sean Condon0e89bda2017-03-21 14:23:19 +00001/*
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 */
16package org.onosproject.cfm.web;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.onlab.packet.Ip4Address;
21import org.onlab.packet.Ip6Address;
22import org.onlab.packet.IpAddress;
23import org.onosproject.codec.CodecContext;
24import org.onosproject.codec.JsonCodec;
25import org.onosproject.incubator.net.l2monitoring.cfm.Mep;
26import org.onosproject.incubator.net.l2monitoring.cfm.Mep.FngAddress;
27
Sean Condon3a1efef2018-02-24 13:16:03 +000028import java.util.Locale;
29
Sean Condon0e89bda2017-03-21 14:23:19 +000030import static com.google.common.base.Preconditions.checkNotNull;
31import static org.onlab.util.Tools.nullIsIllegal;
32
33/**
34 * Encode and decode to/from JSON to FngAddress object.
35 */
36public class FngAddressCodec extends JsonCodec<FngAddress> {
37
Sean Condon3a1efef2018-02-24 13:16:03 +000038 /**
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 Condon0e89bda2017-03-21 14:23:19 +000047 @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 Condon3a1efef2018-02-24 13:16:03 +000061 /**
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 Condon0e89bda2017-03-21 14:23:19 +000070 @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 Condon3a1efef2018-02-24 13:16:03 +000080 Mep.FngAddressType type = Mep.FngAddressType.valueOf(addressType.toUpperCase(Locale.ENGLISH));
Sean Condon0e89bda2017-03-21 14:23:19 +000081 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}