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 static com.google.common.base.Preconditions.checkNotNull; |
| 19 | |
| 20 | import org.onosproject.codec.CodecContext; |
| 21 | import org.onosproject.codec.JsonCodec; |
| 22 | import org.onosproject.incubator.net.l2monitoring.cfm.RemoteMepEntry; |
| 23 | |
| 24 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 25 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 26 | |
| 27 | /** |
| 28 | * Encode and decode to/from JSON to RemoteMepEntry object. |
| 29 | */ |
| 30 | public class RemoteMepEntryCodec extends JsonCodec<RemoteMepEntry> { |
| 31 | |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame^] | 32 | /** |
| 33 | * Encodes the RemoteMepEntry entity into JSON. |
| 34 | * |
| 35 | * @param remoteMepEntry RemoteMepEntry to encode |
| 36 | * @param context encoding context |
| 37 | * @return JSON node |
| 38 | * @throws java.lang.UnsupportedOperationException if the codec does not |
| 39 | * support encode operations |
| 40 | */ |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 41 | @Override |
| 42 | public ObjectNode encode(RemoteMepEntry remoteMepEntry, CodecContext context) { |
| 43 | checkNotNull(remoteMepEntry, "Mep cannot be null"); |
| 44 | ObjectNode result = context.mapper().createObjectNode() |
| 45 | .put("remoteMepId", remoteMepEntry.remoteMepId().toString()) |
| 46 | .put("remoteMepState", remoteMepEntry.state().name()) |
| 47 | .put("rdi", remoteMepEntry.rdi()); |
| 48 | |
| 49 | if (remoteMepEntry.failedOrOkTime() != null) { |
| 50 | result = result.put("failedOrOkTime", |
| 51 | remoteMepEntry.failedOrOkTime().toString()); |
| 52 | } |
| 53 | |
| 54 | if (remoteMepEntry.macAddress() != null) { |
| 55 | result = result.put("macAddress", remoteMepEntry.macAddress().toString()); |
| 56 | } |
| 57 | |
| 58 | if (remoteMepEntry.portStatusTlvType() != null) { |
| 59 | result = result.put("portStatusTlvType", |
| 60 | remoteMepEntry.portStatusTlvType().name()); |
| 61 | } |
| 62 | if (remoteMepEntry.interfaceStatusTlvType() != null) { |
| 63 | result = result.put("interfaceStatusTlvType", |
| 64 | remoteMepEntry.interfaceStatusTlvType().name()); |
| 65 | } |
| 66 | if (remoteMepEntry.senderIdTlvType() != null) { |
| 67 | result = result.put("senderIdTlvType", |
| 68 | remoteMepEntry.senderIdTlvType().name()); |
| 69 | } |
| 70 | |
| 71 | return result; |
| 72 | } |
| 73 | |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame^] | 74 | /** |
| 75 | * Encodes the collection of the RemoteMepEntry entities. |
| 76 | * |
| 77 | * @param remoteMepEntries collection of RemoteMepEntry to encode |
| 78 | * @param context encoding context |
| 79 | * @return JSON array |
| 80 | * @throws java.lang.UnsupportedOperationException if the codec does not |
| 81 | * support encode operations |
| 82 | */ |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 83 | @Override |
| 84 | public ArrayNode encode(Iterable<RemoteMepEntry> remoteMepEntries, |
| 85 | CodecContext context) { |
| 86 | ArrayNode an = context.mapper().createArrayNode(); |
| 87 | remoteMepEntries.forEach(remoteMepEntry -> |
| 88 | an.add(encode(remoteMepEntry, context))); |
| 89 | return an; |
| 90 | } |
| 91 | |
| 92 | } |