blob: fe834e3272d381e5417fa8027d20ff529c3ae4f6 [file] [log] [blame]
Wailok Shumee90c132021-03-11 21:00:11 +08001/*
2 * Copyright 2021-present Open Networking Foundation
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 */
pierventreb37a11a2021-03-18 16:50:04 +010016package org.onosproject.segmentrouting.policy.impl;
Wailok Shumee90c132021-03-11 21:00:11 +080017
18import com.fasterxml.jackson.databind.node.ObjectNode;
19
20import org.onosproject.codec.CodecContext;
21import org.onosproject.codec.JsonCodec;
22import org.onosproject.net.flow.TrafficSelector;
pierventreb37a11a2021-03-18 16:50:04 +010023import org.onosproject.segmentrouting.policy.api.PolicyId;
24import org.onosproject.segmentrouting.policy.api.TrafficMatch;
Wailok Shum37dd29a2021-04-27 18:13:55 +080025import org.onosproject.segmentrouting.policy.api.TrafficMatchPriority;
Wailok Shumee90c132021-03-11 21:00:11 +080026
27import static org.onlab.util.Tools.nullIsIllegal;
28
29/**
30 * Codec of TrafficMatch class.
31 */
32public final class TrafficMatchCodec extends JsonCodec<TrafficMatch> {
33
34 // JSON field names
35 public static final String TRAFFIC_MATCH_ID = "traffic_match_id";
36 public static final String TRAFFIC_SELECTOR = "selector";
37 public static final String POLICY_ID = "policy_id";
Wailok Shum37dd29a2021-04-27 18:13:55 +080038 public static final String TRAFFIC_MATCH_PRIORITY = "priority";
Wailok Shumee90c132021-03-11 21:00:11 +080039 public static final String MISSING_MEMBER_MESSAGE =
40 " member is required in Traffic Match";
41
42 @Override
43 public ObjectNode encode(TrafficMatch trafficMatch, CodecContext context) {
44 final JsonCodec<TrafficSelector> selectorCodec =
45 context.codec(TrafficSelector.class);
46
47 final ObjectNode selector = selectorCodec.encode(trafficMatch.trafficSelector(), context);
48 final ObjectNode result = context.mapper().createObjectNode()
49 .put(TRAFFIC_MATCH_ID, trafficMatch.trafficMatchId().toString())
50 .put(POLICY_ID, trafficMatch.policyId().toString())
Wailok Shum37dd29a2021-04-27 18:13:55 +080051 .put(TRAFFIC_MATCH_PRIORITY, trafficMatch.trafficMatchPriority().priority())
Wailok Shumee90c132021-03-11 21:00:11 +080052 .set(TRAFFIC_SELECTOR, selector);
53
54 return result;
55 }
56
57 @Override
58 public TrafficMatch decode(ObjectNode json, CodecContext context) {
59 final JsonCodec<TrafficSelector> selectorCodec =
60 context.codec(TrafficSelector.class);
61
62 ObjectNode selectorJson = nullIsIllegal(get(json, TRAFFIC_SELECTOR),
63 TRAFFIC_SELECTOR + MISSING_MEMBER_MESSAGE);
64 TrafficSelector trafficSelector = selectorCodec.decode(selectorJson, context);
65
66 PolicyId policyId = PolicyId.of(nullIsIllegal(json.get(POLICY_ID),
67 POLICY_ID + MISSING_MEMBER_MESSAGE).asText());
68
Wailok Shum37dd29a2021-04-27 18:13:55 +080069 int priority = nullIsIllegal(json.get(TRAFFIC_MATCH_PRIORITY),
70 TRAFFIC_MATCH_PRIORITY + MISSING_MEMBER_MESSAGE).asInt();
71 TrafficMatchPriority trafficMatchPriority;
72 try {
73 trafficMatchPriority = new TrafficMatchPriority(priority);
74 } catch (IllegalArgumentException ex) {
75 throw ex;
76 }
77
78 return new TrafficMatch(trafficSelector, policyId, trafficMatchPriority);
Wailok Shumee90c132021-03-11 21:00:11 +080079 }
80}