Jayasree Ghosh | d3ff540 | 2016-08-17 18:41:19 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jayasree Ghosh | d3ff540 | 2016-08-17 18:41:19 +0530 | [diff] [blame] | 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.codec.impl; |
| 17 | |
| 18 | import org.onosproject.codec.CodecContext; |
| 19 | import org.onosproject.codec.JsonCodec; |
| 20 | import org.onosproject.net.Link; |
| 21 | import org.onosproject.net.DisjointPath; |
| 22 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 23 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 24 | |
| 25 | import static com.google.common.base.Preconditions.checkNotNull; |
| 26 | |
| 27 | /** |
| 28 | * DisjointPath JSON codec. |
| 29 | */ |
| 30 | public final class DisjointPathCodec extends AnnotatedCodec<DisjointPath> { |
| 31 | @Override |
| 32 | public ObjectNode encode(DisjointPath disjointPath, CodecContext context) { |
| 33 | checkNotNull(disjointPath, "Path cannot be null"); |
| 34 | JsonCodec<Link> codec = context.codec(Link.class); |
| 35 | ObjectNode result = context.mapper() |
| 36 | .createObjectNode(); |
| 37 | |
| 38 | ObjectNode primary = context.mapper() |
| 39 | .createObjectNode() |
| 40 | .put("cost", disjointPath.primary().cost()); |
| 41 | |
| 42 | result.set("primary", primary); |
| 43 | ArrayNode jsonLinks = primary.putArray("links"); |
| 44 | for (Link link : disjointPath.primary().links()) { |
| 45 | jsonLinks.add(codec.encode(link, context)); |
| 46 | } |
| 47 | if (disjointPath.backup() != null) { |
| 48 | ObjectNode backup = context.mapper() |
| 49 | .createObjectNode() |
| 50 | .put("cost", disjointPath.backup().cost()); |
| 51 | result.set("backup", backup); |
| 52 | ArrayNode jsonLinks1 = backup.putArray("links"); |
| 53 | for (Link link1 : disjointPath.backup().links()) { |
| 54 | jsonLinks1.add(codec.encode(link1, context)); |
| 55 | } |
| 56 | } |
| 57 | return annotate(result, disjointPath, context); |
| 58 | } |
| 59 | |
| 60 | } |
| 61 | |