blob: ded1462279889beac7c208a7da811d80c22b154e [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.codec.CodecContext;
20import org.onosproject.mapping.actions.MappingAction;
21import org.onosproject.mapping.actions.NoMappingAction;
22import org.onosproject.mapping.actions.DropMappingAction;
23import org.onosproject.mapping.actions.ForwardMappingAction;
24import org.onosproject.mapping.actions.NativeForwardMappingAction;
25import org.slf4j.Logger;
26
27import static org.slf4j.LoggerFactory.getLogger;
28
29/**
30 * Encode portion of the mapping action codec.
31 */
32public final class EncodeMappingActionCodecHelper {
Ray Milkey9c9cde42018-01-12 14:22:06 -080033 private static final Logger log = getLogger(EncodeMappingActionCodecHelper.class);
Jian Liee65a232017-03-29 14:15:30 +090034 private final MappingAction action;
35 private final CodecContext context;
36
37 /**
38 * Creates a mapping action object encoder.
39 *
40 * @param action mapping action to encode
41 * @param context codec context for the encoding
42 */
43 public EncodeMappingActionCodecHelper(MappingAction action,
44 CodecContext context) {
45 this.action = action;
46 this.context = context;
47 }
48
49 /**
50 * Encodes a no mapping action.
51 *
52 * @param result json node that the mapping action attributes are added to
53 */
54 private void encodeNoMappingAction(ObjectNode result) {
55 NoMappingAction noMappingAction = (NoMappingAction) action;
56 result.put(MappingActionCodec.TYPE, noMappingAction.type().name());
57 }
58
59 /**
60 * Encodes a drop mapping action.
61 *
62 * @param result json node that the mapping action attributes are added to
63 */
64 private void encodeDropMappingAction(ObjectNode result) {
65 DropMappingAction dropMappingAction = (DropMappingAction) action;
66 result.put(MappingActionCodec.TYPE, dropMappingAction.type().name());
67 }
68
69 /**
70 * Encodes a forward mapping action.
71 *
72 * @param result json node that the mapping action attributes are added to
73 */
74 private void encodeForwardMappingAction(ObjectNode result) {
75 ForwardMappingAction forwardMappingAction = (ForwardMappingAction) action;
76 result.put(MappingActionCodec.TYPE, forwardMappingAction.type().name());
77 }
78
79 /**
80 * Encodes a native forward mapping action.
81 *
82 * @param result json node that the mapping action attributes are added to
83 */
84 private void encodeNativeForwardMappingAction(ObjectNode result) {
85 NativeForwardMappingAction nativeMappingAction = (NativeForwardMappingAction) action;
86 result.put(MappingActionCodec.TYPE, nativeMappingAction.type().name());
87 }
88
89 /**
90 * Encodes the given mapping instruction into JSON.
91 *
92 * @return JSON object node representing the mapping action
93 */
94 public ObjectNode encode() {
95 final ObjectNode result = context.mapper().createObjectNode()
96 .put(MappingActionCodec.TYPE, action.type().toString());
97
98 switch (action.type()) {
99 case DROP:
100 encodeDropMappingAction(result);
101 break;
102 case FORWARD:
103 encodeForwardMappingAction(result);
104 break;
105 case NATIVE_FORWARD:
106 encodeNativeForwardMappingAction(result);
107 break;
108 case NO_ACTION:
109 encodeNoMappingAction(result);
110 break;
111 default:
112 log.info("Cannot convert mapping action type of {}", action.type());
113 break;
114 }
115 return result;
116 }
117}