Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 1 | /* |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Laboratory |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +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 | */ |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame^] | 16 | package org.onosproject.pce.rest; |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | |
| 20 | import org.onosproject.codec.CodecContext; |
| 21 | import org.onosproject.codec.JsonCodec; |
| 22 | import org.onosproject.pce.pceservice.PcePath; |
| 23 | import org.onosproject.pce.pceservice.DefaultPcePath; |
| 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
| 26 | |
| 27 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 28 | import com.fasterxml.jackson.databind.JsonNode; |
| 29 | |
| 30 | /** |
| 31 | * PCE path json codec. |
| 32 | */ |
| 33 | public 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 | } |