Wailok Shum | ee90c13 | 2021-03-11 21:00:11 +0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
pierventre | b37a11a | 2021-03-18 16:50:04 +0100 | [diff] [blame] | 16 | package org.onosproject.segmentrouting.policy.impl; |
Wailok Shum | ee90c13 | 2021-03-11 21:00:11 +0800 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | |
| 20 | import org.onosproject.codec.CodecContext; |
| 21 | import org.onosproject.codec.JsonCodec; |
| 22 | import org.onosproject.net.flow.TrafficSelector; |
pierventre | b37a11a | 2021-03-18 16:50:04 +0100 | [diff] [blame] | 23 | import org.onosproject.segmentrouting.policy.api.PolicyId; |
| 24 | import org.onosproject.segmentrouting.policy.api.TrafficMatch; |
Wailok Shum | 37dd29a | 2021-04-27 18:13:55 +0800 | [diff] [blame] | 25 | import org.onosproject.segmentrouting.policy.api.TrafficMatchPriority; |
Wailok Shum | ee90c13 | 2021-03-11 21:00:11 +0800 | [diff] [blame] | 26 | |
| 27 | import static org.onlab.util.Tools.nullIsIllegal; |
| 28 | |
| 29 | /** |
| 30 | * Codec of TrafficMatch class. |
| 31 | */ |
| 32 | public 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 Shum | 37dd29a | 2021-04-27 18:13:55 +0800 | [diff] [blame] | 38 | public static final String TRAFFIC_MATCH_PRIORITY = "priority"; |
Wailok Shum | ee90c13 | 2021-03-11 21:00:11 +0800 | [diff] [blame] | 39 | 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 Shum | 37dd29a | 2021-04-27 18:13:55 +0800 | [diff] [blame] | 51 | .put(TRAFFIC_MATCH_PRIORITY, trafficMatch.trafficMatchPriority().priority()) |
Wailok Shum | ee90c13 | 2021-03-11 21:00:11 +0800 | [diff] [blame] | 52 | .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 Shum | 37dd29a | 2021-04-27 18:13:55 +0800 | [diff] [blame] | 69 | 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 Shum | ee90c13 | 2021-03-11 21:00:11 +0800 | [diff] [blame] | 79 | } |
| 80 | } |