Jian Li | cb42c31 | 2017-03-30 16:20:33 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 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 | cb42c31 | 2017-03-30 16:20:33 +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.DefaultMappingTreatment; |
| 24 | import org.onosproject.mapping.MappingTreatment; |
| 25 | import org.onosproject.mapping.addresses.MappingAddress; |
| 26 | import org.onosproject.mapping.instructions.MappingInstruction; |
| 27 | |
| 28 | import java.util.stream.IntStream; |
| 29 | |
| 30 | import static com.google.common.base.Preconditions.checkNotNull; |
| 31 | |
| 32 | /** |
| 33 | * Mapping treatment codec. |
| 34 | */ |
| 35 | public final class MappingTreatmentCodec extends JsonCodec<MappingTreatment> { |
| 36 | |
| 37 | private static final String INSTRUCTIONS = "instructions"; |
| 38 | private static final String ADDRESS = "address"; |
| 39 | |
| 40 | @Override |
| 41 | public ObjectNode encode(MappingTreatment treatment, CodecContext context) { |
| 42 | checkNotNull(treatment, "Mapping treatment cannot be null"); |
| 43 | |
| 44 | final ObjectNode result = context.mapper().createObjectNode(); |
| 45 | final ArrayNode jsonInstructions = result.putArray(INSTRUCTIONS); |
| 46 | |
| 47 | final JsonCodec<MappingInstruction> instructionCodec = |
| 48 | context.codec(MappingInstruction.class); |
| 49 | |
| 50 | final JsonCodec<MappingAddress> addressCodec = |
| 51 | context.codec(MappingAddress.class); |
| 52 | |
| 53 | for (final MappingInstruction instruction : treatment.instructions()) { |
| 54 | jsonInstructions.add(instructionCodec.encode(instruction, context)); |
| 55 | } |
| 56 | |
| 57 | result.set(ADDRESS, addressCodec.encode(treatment.address(), context)); |
| 58 | |
| 59 | return result; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public MappingTreatment decode(ObjectNode json, CodecContext context) { |
| 64 | if (json == null || !json.isObject()) { |
| 65 | return null; |
| 66 | } |
| 67 | |
| 68 | final JsonCodec<MappingInstruction> instructionCodec = |
| 69 | context.codec(MappingInstruction.class); |
| 70 | |
| 71 | JsonNode instructionJson = json.get(INSTRUCTIONS); |
| 72 | MappingTreatment.Builder builder = DefaultMappingTreatment.builder(); |
| 73 | |
| 74 | if (instructionJson != null) { |
| 75 | IntStream.range(0, instructionJson.size()) |
| 76 | .forEach(i -> builder.add( |
| 77 | instructionCodec.decode(get(instructionJson, i), |
| 78 | context))); |
| 79 | } |
| 80 | |
| 81 | ObjectNode addressJson = get(json, ADDRESS); |
| 82 | if (addressJson != null) { |
| 83 | final JsonCodec<MappingAddress> addressCodec = |
| 84 | context.codec(MappingAddress.class); |
| 85 | builder.withAddress(addressCodec.decode(addressJson, context)); |
| 86 | } |
| 87 | |
| 88 | return builder.build(); |
| 89 | } |
| 90 | } |