blob: 8e508872fea7bb7031487f37ecf845bf3d19ac81 [file] [log] [blame]
sangho27462c62015-05-14 00:39:53 -07001/*
2 * Copyright 2015 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.segmentrouting.web;
17
sangho27462c62015-05-14 00:39:53 -070018import org.onosproject.codec.CodecContext;
19import org.onosproject.codec.JsonCodec;
20import com.fasterxml.jackson.databind.node.ObjectNode;
sangho27462c62015-05-14 00:39:53 -070021import org.onosproject.segmentrouting.Policy;
22import org.onosproject.segmentrouting.TunnelPolicy;
23
24public final class PolicyCodec extends JsonCodec<Policy> {
25
26 // JSON field names
27 private static final String POLICY_ID = "policy_id";
28 private static final String PRIORITY = "priority";
29 private static final String TYPE = "policy_type";
30 private static final String TUNNEL_ID = "tunnel_id";
31 private static final String DST_IP = "dst_ip";
32 private static final String SRC_IP = "src_ip";
33 private static final String PROTO_TYPE = "proto_type";
sangho80f1f892015-05-19 11:57:42 -070034 private static final String SRC_PORT = "src_tp_port";
35 private static final String DST_PORT = "dst_tp_port";
sangho27462c62015-05-14 00:39:53 -070036
37 @Override
38 public ObjectNode encode(Policy policy, CodecContext context) {
39 final ObjectNode result = context.mapper().createObjectNode()
40 .put(POLICY_ID, policy.id());
41
42 result.put(PRIORITY, policy.priority());
43 result.put(TYPE, policy.type().toString());
44
sangho4a5c42a2015-05-20 22:16:38 -070045 if (policy.dstIp() != null) {
46 result.put(DST_IP, policy.dstIp());
sangho27462c62015-05-14 00:39:53 -070047 }
sangho4a5c42a2015-05-20 22:16:38 -070048 if (policy.srcIp() != null) {
49 result.put(SRC_IP, policy.srcIp());
sangho27462c62015-05-14 00:39:53 -070050 }
sangho4a5c42a2015-05-20 22:16:38 -070051 if (policy.ipProto() != null) {
52 result.put(PROTO_TYPE, policy.ipProto());
sangho27462c62015-05-14 00:39:53 -070053 }
54
sangho4a5c42a2015-05-20 22:16:38 -070055 int srcPort = policy.srcPort() & 0xffff;
56 if (policy.srcPort() != 0) {
57 result.put(SRC_PORT, srcPort);
58 }
59 int dstPort = policy.dstPort() & 0xffff;
60 if (policy.dstPort() != 0) {
61 result.put(DST_PORT, dstPort);
62 }
sangho27462c62015-05-14 00:39:53 -070063 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
64 result.put(TUNNEL_ID, ((TunnelPolicy) policy).tunnelId());
65 }
66
67 return result;
68 }
69
70 @Override
71 public Policy decode(ObjectNode json, CodecContext context) {
72
73 String pid = json.path(POLICY_ID).asText();
74 String type = json.path(TYPE).asText();
75 int priority = json.path(PRIORITY).asInt();
76 String dstIp = json.path(DST_IP).asText();
77 String srcIp = json.path(SRC_IP).asText();
78 String tunnelId = json.path(TUNNEL_ID).asText();
79 String protoType = json.path(PROTO_TYPE).asText();
sangho80f1f892015-05-19 11:57:42 -070080 short srcPort = json.path(SRC_PORT).shortValue();
81 short dstPort = json.path(DST_PORT).shortValue();
sangho27462c62015-05-14 00:39:53 -070082
sangho4a5c42a2015-05-20 22:16:38 -070083 if (json.path(POLICY_ID).isMissingNode() || pid == null) {
84 // TODO: handle errors
sangho27462c62015-05-14 00:39:53 -070085 return null;
86 }
87
sangho4a5c42a2015-05-20 22:16:38 -070088 TunnelPolicy.Builder tpb = TunnelPolicy.builder().setPolicyId(pid);
89 if (!json.path(TYPE).isMissingNode() && type != null &&
90 Policy.Type.valueOf(type).equals(Policy.Type.TUNNEL_FLOW)) {
sangho27462c62015-05-14 00:39:53 -070091
sangho4a5c42a2015-05-20 22:16:38 -070092 if (json.path(TUNNEL_ID).isMissingNode() || tunnelId == null) {
93 return null;
94 }
95
96 tpb.setTunnelId(tunnelId);
97 tpb.setType(Policy.Type.valueOf(type));
98
99 if (!json.path(PRIORITY).isMissingNode()) {
100 tpb.setPriority(priority);
101 }
102 if (dstIp != null) {
103 tpb.setDstIp(dstIp);
104 }
105 if (srcIp != null) {
106 tpb.setSrcIp(srcIp);
107 }
108 if (protoType != null) {
109 tpb.setIpProto(protoType);
110 }
111 if (dstPort != 0) {
112 tpb.setDstPort(dstPort);
113 }
114 if (srcPort != 0) {
115 tpb.setSrcPort(srcPort);
116 }
117 }
118
119 return tpb.build();
sangho27462c62015-05-14 00:39:53 -0700120 }
121
122}