blob: e0149e769e299bf3fd3df4a1e22eb7b95d95e2ce [file] [log] [blame]
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +05301/*
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +05302 * Copyright 2016-present Open Networking Laboratory
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +05303 *
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 */
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053016package org.onosproject.pce.rest;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053017
18import static com.google.common.base.Preconditions.checkNotNull;
19
20import org.onosproject.codec.CodecContext;
21import org.onosproject.codec.JsonCodec;
22import org.onosproject.pce.pceservice.PcePath;
23import org.onosproject.pce.pceservice.DefaultPcePath;
24import org.slf4j.Logger;
25import org.slf4j.LoggerFactory;
26
27import com.fasterxml.jackson.databind.node.ObjectNode;
28import com.fasterxml.jackson.databind.JsonNode;
29
30/**
31 * PCE path json codec.
32 */
33public final class PcePathCodec extends JsonCodec<PcePath> {
34 private final Logger log = LoggerFactory.getLogger(PcePathCodec.class);
35 private static final String SOURCE = "source";
36 private static final String DESTINATION = "destination";
37 private static final String LSP_TYPE = "pathType";
38 private static final String SYMBOLIC_PATH_NAME = "name";
39 private static final String CONSTRAINT = "constraint";
40 private static final String COST = "cost";
41 private static final String BANDWIDTH = "bandwidth";
42 private static final String PATH_ID = "pathId";
43 private static final String MISSING_MEMBER_MESSAGE = " member is required in pce-path";
44
45 @Override
46 public PcePath decode(ObjectNode json, CodecContext context) {
47 if (json == null || !json.isObject()) {
48 log.error("Empty json input");
49 return null;
50 }
51
52 // build pce-path
53 PcePath.Builder resultBuilder = new DefaultPcePath.Builder();
54
55 // retrieve source
56 JsonNode jNode = json.get(SOURCE);
57 if (jNode != null) {
58 String src = jNode.asText();
59 resultBuilder.source(src);
60 }
61
62 // retrieve destination
63 jNode = json.get(DESTINATION);
64 if (jNode != null) {
65 String dst = jNode.asText();
66 resultBuilder.destination(dst);
67 }
68
69 // retrieve lsp-type
70 jNode = json.get(LSP_TYPE);
71 if (jNode != null) {
72 String lspType = jNode.asText();
73 resultBuilder.lspType(lspType);
74 }
75
76 // retrieve symbolic-path-name
77 jNode = json.get(SYMBOLIC_PATH_NAME);
78 if (jNode != null) {
79 String name = jNode.asText();
80 resultBuilder.name(name);
81 }
82
83 // retrieve constraint
84 JsonNode constraintJNode = (JsonNode) json.path(CONSTRAINT);
85 if ((constraintJNode != null) && (!constraintJNode.isMissingNode())) {
86 // retrieve cost
87 jNode = constraintJNode.get(COST);
88 if (jNode != null) {
89 String cost = jNode.asText();
90 resultBuilder.bandwidthConstraint(cost);
91 }
92
93 // retrieve bandwidth
94 jNode = constraintJNode.get(BANDWIDTH);
95 if (jNode != null) {
96 String bandwidth = jNode.asText();
97 resultBuilder.bandwidthConstraint(bandwidth);
98 }
99 }
100
101 return resultBuilder.build();
102 }
103
104 @Override
105 public ObjectNode encode(PcePath path, CodecContext context) {
106 checkNotNull(path, "path output cannot be null");
107 ObjectNode result = context.mapper()
108 .createObjectNode()
109 .put(PATH_ID, path.id().id())
110 .put(SOURCE, path.source())
111 .put(DESTINATION, path.destination())
112 .put(LSP_TYPE, path.lspType().type())
113 .put(SYMBOLIC_PATH_NAME, path.name());
114
115 ObjectNode constraintNode = context.mapper()
116 .createObjectNode()
117 .put(COST, path.costConstraint().toString())
118 .put(BANDWIDTH, path.bandwidthConstraint().toString());
119
120 result.set(CONSTRAINT, constraintNode);
121 return result;
122 }
123}