sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [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.segmentrouting.web; |
| 17 | |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 18 | import org.onosproject.codec.CodecContext; |
| 19 | import org.onosproject.codec.JsonCodec; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 21 | import org.onosproject.segmentrouting.Policy; |
| 22 | import org.onosproject.segmentrouting.TunnelPolicy; |
| 23 | |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 24 | /** |
| 25 | * Codec of Policy class. |
| 26 | */ |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 27 | public final class PolicyCodec extends JsonCodec<Policy> { |
| 28 | |
| 29 | // JSON field names |
| 30 | private static final String POLICY_ID = "policy_id"; |
| 31 | private static final String PRIORITY = "priority"; |
| 32 | private static final String TYPE = "policy_type"; |
| 33 | private static final String TUNNEL_ID = "tunnel_id"; |
| 34 | private static final String DST_IP = "dst_ip"; |
| 35 | private static final String SRC_IP = "src_ip"; |
| 36 | private static final String PROTO_TYPE = "proto_type"; |
sangho | f9d0bf1 | 2015-05-19 11:57:42 -0700 | [diff] [blame] | 37 | private static final String SRC_PORT = "src_tp_port"; |
| 38 | private static final String DST_PORT = "dst_tp_port"; |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 39 | |
| 40 | @Override |
| 41 | public ObjectNode encode(Policy policy, CodecContext context) { |
| 42 | final ObjectNode result = context.mapper().createObjectNode() |
| 43 | .put(POLICY_ID, policy.id()); |
| 44 | |
| 45 | result.put(PRIORITY, policy.priority()); |
| 46 | result.put(TYPE, policy.type().toString()); |
| 47 | |
sangho | 0b2b6d1 | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 48 | if (policy.dstIp() != null) { |
| 49 | result.put(DST_IP, policy.dstIp()); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 50 | } |
sangho | 0b2b6d1 | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 51 | if (policy.srcIp() != null) { |
| 52 | result.put(SRC_IP, policy.srcIp()); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 53 | } |
sangho | 0b2b6d1 | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 54 | if (policy.ipProto() != null) { |
| 55 | result.put(PROTO_TYPE, policy.ipProto()); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 56 | } |
| 57 | |
sangho | 0b2b6d1 | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 58 | int srcPort = policy.srcPort() & 0xffff; |
| 59 | if (policy.srcPort() != 0) { |
| 60 | result.put(SRC_PORT, srcPort); |
| 61 | } |
| 62 | int dstPort = policy.dstPort() & 0xffff; |
| 63 | if (policy.dstPort() != 0) { |
| 64 | result.put(DST_PORT, dstPort); |
| 65 | } |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 66 | if (policy.type() == Policy.Type.TUNNEL_FLOW) { |
| 67 | result.put(TUNNEL_ID, ((TunnelPolicy) policy).tunnelId()); |
| 68 | } |
| 69 | |
| 70 | return result; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public Policy decode(ObjectNode json, CodecContext context) { |
| 75 | |
| 76 | String pid = json.path(POLICY_ID).asText(); |
| 77 | String type = json.path(TYPE).asText(); |
| 78 | int priority = json.path(PRIORITY).asInt(); |
| 79 | String dstIp = json.path(DST_IP).asText(); |
| 80 | String srcIp = json.path(SRC_IP).asText(); |
| 81 | String tunnelId = json.path(TUNNEL_ID).asText(); |
| 82 | String protoType = json.path(PROTO_TYPE).asText(); |
sangho | f9d0bf1 | 2015-05-19 11:57:42 -0700 | [diff] [blame] | 83 | short srcPort = json.path(SRC_PORT).shortValue(); |
| 84 | short dstPort = json.path(DST_PORT).shortValue(); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 85 | |
sangho | 0b2b6d1 | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 86 | if (json.path(POLICY_ID).isMissingNode() || pid == null) { |
| 87 | // TODO: handle errors |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 88 | return null; |
| 89 | } |
| 90 | |
sangho | 0b2b6d1 | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 91 | TunnelPolicy.Builder tpb = TunnelPolicy.builder().setPolicyId(pid); |
| 92 | if (!json.path(TYPE).isMissingNode() && type != null && |
| 93 | Policy.Type.valueOf(type).equals(Policy.Type.TUNNEL_FLOW)) { |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 94 | |
sangho | 0b2b6d1 | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 95 | if (json.path(TUNNEL_ID).isMissingNode() || tunnelId == null) { |
| 96 | return null; |
| 97 | } |
| 98 | |
| 99 | tpb.setTunnelId(tunnelId); |
| 100 | tpb.setType(Policy.Type.valueOf(type)); |
| 101 | |
| 102 | if (!json.path(PRIORITY).isMissingNode()) { |
| 103 | tpb.setPriority(priority); |
| 104 | } |
| 105 | if (dstIp != null) { |
| 106 | tpb.setDstIp(dstIp); |
| 107 | } |
| 108 | if (srcIp != null) { |
| 109 | tpb.setSrcIp(srcIp); |
| 110 | } |
| 111 | if (protoType != null) { |
| 112 | tpb.setIpProto(protoType); |
| 113 | } |
| 114 | if (dstPort != 0) { |
| 115 | tpb.setDstPort(dstPort); |
| 116 | } |
| 117 | if (srcPort != 0) { |
| 118 | tpb.setSrcPort(srcPort); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return tpb.build(); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | } |