blob: 0dedc7ce36c3bf31e6569310167b838ba7d14836 [file] [log] [blame]
sangho1e575652015-05-14 00:39:53 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
sangho1e575652015-05-14 00:39:53 -07003 *
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
sangho1e575652015-05-14 00:39:53 -070018import org.onosproject.codec.CodecContext;
19import org.onosproject.codec.JsonCodec;
20import com.fasterxml.jackson.databind.node.ObjectNode;
sangho1e575652015-05-14 00:39:53 -070021import org.onosproject.segmentrouting.Policy;
22import org.onosproject.segmentrouting.TunnelPolicy;
23
Charles Chane849c192016-01-11 18:28:54 -080024/**
25 * Codec of Policy class.
26 */
sangho1e575652015-05-14 00:39:53 -070027public 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";
sanghof9d0bf12015-05-19 11:57:42 -070037 private static final String SRC_PORT = "src_tp_port";
38 private static final String DST_PORT = "dst_tp_port";
sangho1e575652015-05-14 00:39:53 -070039
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
sangho0b2b6d12015-05-20 22:16:38 -070048 if (policy.dstIp() != null) {
49 result.put(DST_IP, policy.dstIp());
sangho1e575652015-05-14 00:39:53 -070050 }
sangho0b2b6d12015-05-20 22:16:38 -070051 if (policy.srcIp() != null) {
52 result.put(SRC_IP, policy.srcIp());
sangho1e575652015-05-14 00:39:53 -070053 }
sangho0b2b6d12015-05-20 22:16:38 -070054 if (policy.ipProto() != null) {
55 result.put(PROTO_TYPE, policy.ipProto());
sangho1e575652015-05-14 00:39:53 -070056 }
57
sangho0b2b6d12015-05-20 22:16:38 -070058 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 }
sangho1e575652015-05-14 00:39:53 -070066 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();
sanghof9d0bf12015-05-19 11:57:42 -070083 short srcPort = json.path(SRC_PORT).shortValue();
84 short dstPort = json.path(DST_PORT).shortValue();
sangho1e575652015-05-14 00:39:53 -070085
sangho0b2b6d12015-05-20 22:16:38 -070086 if (json.path(POLICY_ID).isMissingNode() || pid == null) {
87 // TODO: handle errors
sangho1e575652015-05-14 00:39:53 -070088 return null;
89 }
90
sangho0b2b6d12015-05-20 22:16:38 -070091 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)) {
sangho1e575652015-05-14 00:39:53 -070094
sangho0b2b6d12015-05-20 22:16:38 -070095 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();
sangho1e575652015-05-14 00:39:53 -0700123 }
124
125}