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 org.onlab.util.Tools.nullIsIllegal; |
| 19 | |
| 20 | import java.util.ArrayList; |
| 21 | import java.util.List; |
| 22 | |
| 23 | import org.onosproject.codec.CodecContext; |
| 24 | import org.onosproject.codec.JsonCodec; |
| 25 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId; |
| 26 | |
| 27 | import com.fasterxml.jackson.databind.JsonNode; |
| 28 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 29 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 30 | |
| 31 | /** |
| 32 | * Encode and decode to/from JSON to RMep object. |
| 33 | */ |
| 34 | public class RMepCodec extends JsonCodec<MepId> { |
| 35 | |
| 36 | @Override |
| 37 | public ObjectNode encode(MepId rmep, CodecContext context) { |
| 38 | return context.mapper().createObjectNode().put("rmep", rmep.id()); |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | public ArrayNode encode(Iterable<MepId> rmeps, CodecContext context) { |
| 43 | ArrayNode an = context.mapper().createArrayNode(); |
| 44 | rmeps.forEach(rmep -> { |
| 45 | an.add(encode(rmep, context)); |
| 46 | }); |
| 47 | return an; |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public MepId decode(ObjectNode json, CodecContext context) { |
| 52 | if (json == null || !json.isObject()) { |
| 53 | return null; |
| 54 | } |
| 55 | |
| 56 | JsonNode vidNode = json.get("rmep"); |
| 57 | |
| 58 | return MepId.valueOf( |
| 59 | nullIsIllegal((short) vidNode.asInt(), "rmep is required")); |
| 60 | |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public List<MepId> decode(ArrayNode json, CodecContext context) { |
| 65 | List<MepId> rmepList = new ArrayList<>(); |
| 66 | json.forEach(node -> rmepList.add(decode((ObjectNode) node, context))); |
| 67 | return rmepList; |
| 68 | } |
| 69 | } |