Jian Li | ee65a23 | 2017-03-29 14:15:30 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jian Li | ee65a23 | 2017-03-29 14:15:30 +0900 | [diff] [blame] | 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 | */ |
Jian Li | 2e818b0 | 2017-04-12 19:28:30 +0900 | [diff] [blame] | 16 | package org.onosproject.mapping.codec; |
Jian Li | ee65a23 | 2017-03-29 14:15:30 +0900 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import org.onosproject.codec.CodecContext; |
| 20 | import org.onosproject.mapping.actions.MappingAction; |
| 21 | import org.onosproject.mapping.actions.NoMappingAction; |
| 22 | import org.onosproject.mapping.actions.DropMappingAction; |
| 23 | import org.onosproject.mapping.actions.ForwardMappingAction; |
| 24 | import org.onosproject.mapping.actions.NativeForwardMappingAction; |
| 25 | import org.slf4j.Logger; |
| 26 | |
| 27 | import static org.slf4j.LoggerFactory.getLogger; |
| 28 | |
| 29 | /** |
| 30 | * Encode portion of the mapping action codec. |
| 31 | */ |
| 32 | public final class EncodeMappingActionCodecHelper { |
Ray Milkey | 9c9cde4 | 2018-01-12 14:22:06 -0800 | [diff] [blame] | 33 | private static final Logger log = getLogger(EncodeMappingActionCodecHelper.class); |
Jian Li | ee65a23 | 2017-03-29 14:15:30 +0900 | [diff] [blame] | 34 | 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 | } |