blob: 9a0dbabf02058947c809d79a6748e5d7903893a9 [file] [log] [blame]
Jian Lib54d14b2017-03-28 21:34:34 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jian Lib54d14b2017-03-28 21:34:34 +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 Lib54d14b2017-03-28 21:34:34 +090017
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import org.onosproject.codec.CodecContext;
20import org.onosproject.mapping.instructions.MappingInstruction;
21import org.onosproject.mapping.instructions.MappingInstructions;
22import org.onosproject.mapping.instructions.MulticastMappingInstruction;
23import org.onosproject.mapping.instructions.UnicastMappingInstruction;
24
25import static org.onlab.util.Tools.nullIsIllegal;
26
27/**
28 * Decoding portion of the mapping instruction codec.
29 */
30public final class DecodeMappingInstructionCodecHelper {
31 private final ObjectNode json;
32 private final CodecContext context;
33
34 /**
35 * Creates a decode mapping instruction codec object.
36 *
37 * @param json JSON object to decode
38 * @param context codec context
39 */
40 public DecodeMappingInstructionCodecHelper(ObjectNode json, CodecContext context) {
41 this.json = json;
42 this.context = context;
43 }
44
45 /**
46 * Decodes an unicast mapping instruction.
47 *
48 * @return mapping instruction object decoded from the JSON
49 * @throws IllegalArgumentException if the JSON is invalid
50 */
51 private MappingInstruction decodeUnicast() {
52 String subType = nullIsIllegal(json.get(MappingInstructionCodec.SUBTYPE),
53 MappingInstructionCodec.SUBTYPE + MappingInstructionCodec.ERROR_MESSAGE).asText();
54
55 if (subType.equals(UnicastMappingInstruction.UnicastType.WEIGHT.name())) {
56 int weight = nullIsIllegal(json.get(MappingInstructionCodec.UNICAST_WEIGHT),
57 MappingInstructionCodec.UNICAST_WEIGHT +
58 MappingInstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
59 return MappingInstructions.unicastWeight(weight);
60 } else if (subType.equals(UnicastMappingInstruction.UnicastType.PRIORITY.name())) {
61 int priority = nullIsIllegal(json.get(MappingInstructionCodec.UNICAST_PRIORITY),
62 MappingInstructionCodec.UNICAST_PRIORITY +
63 MappingInstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
64 return MappingInstructions.unicastPriority(priority);
65 }
66 throw new IllegalArgumentException("Unicast MappingInstruction subtype "
67 + subType + " is not supported");
68 }
69
70 /**
71 * Decodes a multicast mapping instruction.
72 *
73 * @return mapping instruction object decoded from the JSON
74 * @throws IllegalArgumentException if the JSON is invalid
75 */
76 private MappingInstruction decodeMulticast() {
77 String subType = nullIsIllegal(json.get(MappingInstructionCodec.SUBTYPE),
78 MappingInstructionCodec.SUBTYPE + MappingInstructionCodec.ERROR_MESSAGE).asText();
79
80 if (subType.equals(MulticastMappingInstruction.MulticastType.WEIGHT.name())) {
81 int weight = nullIsIllegal(json.get(MappingInstructionCodec.MULTICAST_WEIGHT),
82 MappingInstructionCodec.MULTICAST_WEIGHT +
83 MappingInstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
84 return MappingInstructions.multicastWeight(weight);
85 } else if (subType.equals(MulticastMappingInstruction.MulticastType.PRIORITY.name())) {
86 int priority = nullIsIllegal(json.get(MappingInstructionCodec.MULTICAST_PRIORITY),
87 MappingInstructionCodec.MULTICAST_PRIORITY +
88 MappingInstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
89 return MappingInstructions.multicastPriority(priority);
90 }
91 throw new IllegalArgumentException("Multicast MappingInstruction subtype "
92 + subType + " is not supported");
93 }
94
95 /**
96 * Decodes the JSON into a mapping instruction object.
97 *
98 * @return MappingInstruction object
99 * @throws IllegalArgumentException if the JSON is invalid
100 */
101 public MappingInstruction decode() {
102 String type = nullIsIllegal(json.get(MappingInstructionCodec.TYPE),
103 MappingInstructionCodec.TYPE + MappingInstructionCodec.ERROR_MESSAGE).asText();
104
105 if (type.equals(MappingInstruction.Type.UNICAST.name())) {
106 return decodeUnicast();
107 } else if (type.equals(MappingInstruction.Type.MULTICAST.name())) {
108 return decodeMulticast();
109 }
110 throw new IllegalArgumentException("MappingInstruction type "
111 + type + " is not supported");
112 }
113}