blob: 585886d6a8ccd3f33e3c6e23981088fde7fd7235 [file] [log] [blame]
Jayasree Ghoshd3ff5402016-08-17 18:41:19 +05301/*
2 * Copyright 2016-present Open Networking Laboratory
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.codec.impl;
17
18import org.onosproject.codec.CodecContext;
19import org.onosproject.codec.JsonCodec;
20import org.onosproject.net.Link;
21import org.onosproject.net.DisjointPath;
22import com.fasterxml.jackson.databind.node.ArrayNode;
23import com.fasterxml.jackson.databind.node.ObjectNode;
24
25import static com.google.common.base.Preconditions.checkNotNull;
26
27/**
28 * DisjointPath JSON codec.
29 */
30public 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