blob: b7163185bc5fe1bb73dc09b771cf1ccc85aa9d78 [file] [log] [blame]
Jian Liee65a232017-03-29 14:15:30 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jian Liee65a232017-03-29 14:15:30 +09003 *
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 */
Jian Li2e818b02017-04-12 19:28:30 +090016package org.onosproject.mapping.codec;
Jian Liee65a232017-03-29 14:15:30 +090017
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import org.onosproject.mapping.actions.MappingAction;
20import org.onosproject.mapping.actions.MappingActions;
21
22import static org.onlab.util.Tools.nullIsIllegal;
23
24/**
25 * Decode portion of the mapping action codec.
26 */
27public final class DecodeMappingActionCodecHelper {
28 private final ObjectNode json;
29
30 /**
31 * Creates a decode mapping action codec object.
32 *
33 * @param json JSON object to decode
34 */
35 public DecodeMappingActionCodecHelper(ObjectNode json) {
36 this.json = json;
37 }
38
39 /**
40 * Decodes a no mapping action.
41 *
42 * @return mapping action object decoded from the JSON
43 */
44 private MappingAction decodeNoAction() {
45 return MappingActions.noAction();
46 }
47
48 /**
49 * Decodes a forward mapping action.
50 *
51 * @return mapping action object decoded from the JSON
52 */
53 private MappingAction decodeForwardAction() {
54 return MappingActions.forward();
55 }
56
57 /**
58 * Decodes a native forward mapping action.
59 *
60 * @return mapping action object decoded from the JSON
61 */
62 private MappingAction decodeNativeForwardAction() {
63 return MappingActions.nativeForward();
64 }
65
66 /**
67 * Decodes a drop mapping action.
68 *
69 * @return mapping action object decoded from the JSON
70 */
71 private MappingAction decodeDropAction() {
72 return MappingActions.drop();
73 }
74
75 /**
76 * Decodes the JSON into a mapping action object.
77 *
78 * @return MappingAction object
79 * @throws IllegalArgumentException if the JSON is invalid
80 */
81 public MappingAction decode() {
82 String type = nullIsIllegal(json.get(MappingActionCodec.TYPE),
83 MappingActionCodec.TYPE + MappingActionCodec.ERROR_MESSAGE).asText();
84
85 if (type.equals(MappingAction.Type.DROP.name())) {
86 return decodeDropAction();
87 } else if (type.equals(MappingAction.Type.FORWARD.name())) {
88 return decodeForwardAction();
89 } else if (type.equals(MappingAction.Type.NATIVE_FORWARD.name())) {
90 return decodeNativeForwardAction();
91 } else if (type.equals(MappingAction.Type.NO_ACTION.name())) {
92 return decodeNoAction();
93 }
94 throw new IllegalArgumentException("MappingAction type "
95 + type + " is not supported");
96 }
97}