blob: 7692c7d48325c1798bd1fc3b5b14660d70903bf5 [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 */
Priyanka Bb977f562016-07-22 13:02:03 +053016package org.onosproject.pcerest;
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;
Priyanka B3fdb9dd2016-08-08 10:47:24 +053024import org.onosproject.net.intent.constraint.BandwidthConstraint;
25import org.onosproject.pce.pceservice.constraint.CostConstraint;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053026import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
29import com.fasterxml.jackson.databind.node.ObjectNode;
30import com.fasterxml.jackson.databind.JsonNode;
31
32/**
33 * PCE path json codec.
34 */
35public final class PcePathCodec extends JsonCodec<PcePath> {
36 private final Logger log = LoggerFactory.getLogger(PcePathCodec.class);
37 private static final String SOURCE = "source";
38 private static final String DESTINATION = "destination";
39 private static final String LSP_TYPE = "pathType";
40 private static final String SYMBOLIC_PATH_NAME = "name";
41 private static final String CONSTRAINT = "constraint";
42 private static final String COST = "cost";
43 private static final String BANDWIDTH = "bandwidth";
44 private static final String PATH_ID = "pathId";
45 private static final String MISSING_MEMBER_MESSAGE = " member is required in pce-path";
46
47 @Override
48 public PcePath decode(ObjectNode json, CodecContext context) {
49 if (json == null || !json.isObject()) {
50 log.error("Empty json input");
51 return null;
52 }
53
54 // build pce-path
55 PcePath.Builder resultBuilder = new DefaultPcePath.Builder();
56
57 // retrieve source
58 JsonNode jNode = json.get(SOURCE);
59 if (jNode != null) {
60 String src = jNode.asText();
61 resultBuilder.source(src);
62 }
63
64 // retrieve destination
65 jNode = json.get(DESTINATION);
66 if (jNode != null) {
67 String dst = jNode.asText();
68 resultBuilder.destination(dst);
69 }
70
71 // retrieve lsp-type
72 jNode = json.get(LSP_TYPE);
73 if (jNode != null) {
74 String lspType = jNode.asText();
Priyanka B3fdb9dd2016-08-08 10:47:24 +053075 //Validating LSP type
76 int type = Integer.parseInt(lspType);
77 if ((type < 0) || (type > 2)) {
78 return null;
79 }
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053080 resultBuilder.lspType(lspType);
81 }
82
83 // retrieve symbolic-path-name
84 jNode = json.get(SYMBOLIC_PATH_NAME);
85 if (jNode != null) {
86 String name = jNode.asText();
87 resultBuilder.name(name);
88 }
89
90 // retrieve constraint
91 JsonNode constraintJNode = (JsonNode) json.path(CONSTRAINT);
92 if ((constraintJNode != null) && (!constraintJNode.isMissingNode())) {
93 // retrieve cost
94 jNode = constraintJNode.get(COST);
95 if (jNode != null) {
96 String cost = jNode.asText();
Priyanka B3fdb9dd2016-08-08 10:47:24 +053097 //Validating Cost type
98 int costType = Integer.parseInt(cost);
99 if ((costType < 1) || (costType > 2)) {
100 return null;
101 }
Mahesh Poojary S33536202016-05-30 07:22:36 +0530102 resultBuilder.costConstraint(cost);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530103 }
104
105 // retrieve bandwidth
106 jNode = constraintJNode.get(BANDWIDTH);
107 if (jNode != null) {
108 String bandwidth = jNode.asText();
Priyanka B3fdb9dd2016-08-08 10:47:24 +0530109 double bw = Double.parseDouble(bandwidth);
110 if (bw < 0) {
111 return null;
112 }
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530113 resultBuilder.bandwidthConstraint(bandwidth);
114 }
115 }
116
117 return resultBuilder.build();
118 }
119
120 @Override
121 public ObjectNode encode(PcePath path, CodecContext context) {
122 checkNotNull(path, "path output cannot be null");
123 ObjectNode result = context.mapper()
124 .createObjectNode()
125 .put(PATH_ID, path.id().id())
126 .put(SOURCE, path.source())
127 .put(DESTINATION, path.destination())
128 .put(LSP_TYPE, path.lspType().type())
129 .put(SYMBOLIC_PATH_NAME, path.name());
130
131 ObjectNode constraintNode = context.mapper()
132 .createObjectNode()
Priyanka B3fdb9dd2016-08-08 10:47:24 +0530133 .put(COST, ((CostConstraint) path.costConstraint()).type().type())
134 .put(BANDWIDTH, ((BandwidthConstraint) path.bandwidthConstraint()).bandwidth().bps());
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530135
136 result.set(CONSTRAINT, constraintNode);
137 return result;
138 }
139}