Jian Li | 2d3a1d1 | 2017-04-05 00:56:54 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | ce2a03d | 2017-08-03 19:21:03 -0700 | [diff] [blame^] | 2 | * Copyright 2017-present Open Networking Foundation |
Jian Li | 2d3a1d1 | 2017-04-05 00:56:54 +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 | 2d3a1d1 | 2017-04-05 00:56:54 +0900 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.onosproject.codec.CodecContext; |
| 22 | import org.onosproject.codec.JsonCodec; |
| 23 | import org.onosproject.mapping.DefaultMappingValue; |
| 24 | import org.onosproject.mapping.MappingTreatment; |
| 25 | import org.onosproject.mapping.MappingValue; |
| 26 | import org.onosproject.mapping.actions.MappingAction; |
| 27 | |
| 28 | import java.util.stream.IntStream; |
| 29 | |
| 30 | import static com.google.common.base.Preconditions.checkNotNull; |
| 31 | |
| 32 | /** |
| 33 | * Mapping value codec. |
| 34 | */ |
| 35 | public final class MappingValueCodec extends JsonCodec<MappingValue> { |
| 36 | |
| 37 | protected static final String ACTION = "action"; |
| 38 | protected static final String TREATMENTS = "treatments"; |
| 39 | |
| 40 | @Override |
| 41 | public ObjectNode encode(MappingValue value, CodecContext context) { |
| 42 | checkNotNull(value, "Mapping value cannot be null"); |
| 43 | |
| 44 | final ObjectNode result = context.mapper().createObjectNode(); |
| 45 | final ArrayNode jsonTreatments = result.putArray(TREATMENTS); |
| 46 | |
| 47 | final JsonCodec<MappingTreatment> treatmentCodec = |
| 48 | context.codec(MappingTreatment.class); |
| 49 | |
| 50 | final JsonCodec<MappingAction> actionCodec = |
| 51 | context.codec(MappingAction.class); |
| 52 | |
| 53 | for (final MappingTreatment treatment : value.treatments()) { |
| 54 | jsonTreatments.add(treatmentCodec.encode(treatment, context)); |
| 55 | } |
| 56 | |
| 57 | result.set(ACTION, actionCodec.encode(value.action(), context)); |
| 58 | |
| 59 | return result; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public MappingValue decode(ObjectNode json, CodecContext context) { |
| 64 | if (json == null || !json.isObject()) { |
| 65 | return null; |
| 66 | } |
| 67 | |
| 68 | final JsonCodec<MappingTreatment> treatmentCodec = |
| 69 | context.codec(MappingTreatment.class); |
| 70 | |
| 71 | JsonNode treatmentJson = json.get(TREATMENTS); |
| 72 | MappingValue.Builder builder = DefaultMappingValue.builder(); |
| 73 | |
| 74 | if (treatmentJson != null) { |
| 75 | IntStream.range(0, treatmentJson.size()) |
| 76 | .forEach(i -> builder.add( |
| 77 | treatmentCodec.decode(get(treatmentJson, i), |
| 78 | context))); |
| 79 | } |
| 80 | |
| 81 | ObjectNode actionJson = get(json, ACTION); |
| 82 | if (actionJson != null) { |
| 83 | final JsonCodec<MappingAction> actionCodec = |
| 84 | context.codec(MappingAction.class); |
| 85 | builder.withAction(actionCodec.decode(actionJson, context)); |
| 86 | } |
| 87 | |
| 88 | return builder.build(); |
| 89 | } |
| 90 | } |