Jian Li | dab7256 | 2016-04-12 14:10:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-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 | */ |
| 16 | package org.onosproject.driver.extensions.codec; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import org.onosproject.codec.CodecContext; |
| 20 | import org.onosproject.codec.JsonCodec; |
| 21 | import org.onosproject.driver.extensions.NiciraSetNshContextHeader; |
| 22 | import org.onosproject.net.NshContextHeader; |
| 23 | import org.onosproject.net.flow.instructions.ExtensionTreatmentType; |
| 24 | |
| 25 | import static com.google.common.base.Preconditions.checkNotNull; |
| 26 | import static org.onlab.util.Tools.nullIsIllegal; |
| 27 | |
| 28 | /** |
| 29 | * JSON Codec for NiciraSetNshContextHeader class. |
| 30 | */ |
| 31 | public class NiciraSetNshContextHeaderCodec extends JsonCodec<NiciraSetNshContextHeader> { |
| 32 | |
| 33 | private static final String NSH_CONTEXT_HEADER = "nshch"; |
| 34 | private static final String TYPE = "type"; |
| 35 | |
| 36 | private static final String MISSING_MEMBER_MESSAGE = " member is required in NiciraSetNshContextHeader"; |
| 37 | |
| 38 | @Override |
| 39 | public ObjectNode encode(NiciraSetNshContextHeader niciraSetNshContextHeader, CodecContext context) { |
| 40 | checkNotNull(niciraSetNshContextHeader, "Nicira Set Nsh Context Header cannot be null"); |
| 41 | ObjectNode root = context.mapper().createObjectNode() |
| 42 | .put(NSH_CONTEXT_HEADER, niciraSetNshContextHeader.nshCh().nshContextHeader()) |
| 43 | .put(TYPE, niciraSetNshContextHeader.type().type()); |
| 44 | return root; |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public NiciraSetNshContextHeader decode(ObjectNode json, CodecContext context) { |
| 49 | if (json == null || !json.isObject()) { |
| 50 | return null; |
| 51 | } |
| 52 | |
| 53 | // parse nsh context header |
| 54 | int contextHeaderInt = nullIsIllegal(json.get(NSH_CONTEXT_HEADER), |
| 55 | NSH_CONTEXT_HEADER + MISSING_MEMBER_MESSAGE).asInt(); |
| 56 | |
| 57 | NshContextHeader contextHeader = NshContextHeader.of(contextHeaderInt); |
| 58 | |
| 59 | // parse type |
| 60 | int extensionTypeInt = nullIsIllegal(json.get(TYPE), |
| 61 | TYPE + MISSING_MEMBER_MESSAGE).asInt(); |
| 62 | |
| 63 | ExtensionTreatmentType type = new ExtensionTreatmentType(extensionTypeInt); |
| 64 | |
| 65 | NiciraSetNshContextHeader niciraSetNshContextHeader = |
| 66 | new NiciraSetNshContextHeader(contextHeader, type); |
| 67 | |
| 68 | return niciraSetNshContextHeader; |
| 69 | } |
| 70 | } |