blob: fa04df8a7a91dd7b16bba38ab9367177b7e65240 [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.MulticastMappingInstruction;
22import org.onosproject.mapping.instructions.UnicastMappingInstruction;
23import org.slf4j.Logger;
24
25import static org.slf4j.LoggerFactory.getLogger;
26
27/**
28 * JSON encoding of MappingInstructions.
29 */
30public final class EncodeMappingInstructionCodecHelper {
Ray Milkey9c9cde42018-01-12 14:22:06 -080031 private static final Logger log = getLogger(EncodeMappingInstructionCodecHelper.class);
Jian Lib54d14b2017-03-28 21:34:34 +090032 private final MappingInstruction instruction;
33 private final CodecContext context;
34
35 /**
36 * Creates a mapping instruction object encoder.
37 *
38 * @param instruction mapping instruction to encode
39 * @param context codec context for the encoding
40 */
41 public EncodeMappingInstructionCodecHelper(MappingInstruction instruction,
42 CodecContext context) {
43 this.instruction = instruction;
44 this.context = context;
45 }
46
47 /**
48 * Encodes an unicast mapping instruction.
49 *
50 * @param result json node that the mapping instruction
51 * attributes are added to
52 */
53 private void encodeUnicast(ObjectNode result) {
54 UnicastMappingInstruction unicastInstruction =
55 (UnicastMappingInstruction) instruction;
56 result.put(MappingInstructionCodec.SUBTYPE, unicastInstruction.subtype().name());
57
58 switch (unicastInstruction.subtype()) {
59 case WEIGHT:
60 UnicastMappingInstruction.WeightMappingInstruction wmi =
61 (UnicastMappingInstruction.WeightMappingInstruction)
62 unicastInstruction;
63 result.put(MappingInstructionCodec.UNICAST_WEIGHT, wmi.weight());
64 break;
65 case PRIORITY:
66 UnicastMappingInstruction.PriorityMappingInstruction pmi =
67 (UnicastMappingInstruction.PriorityMappingInstruction)
68 unicastInstruction;
69 result.put(MappingInstructionCodec.UNICAST_PRIORITY, pmi.priority());
70 break;
71 default:
72 log.info("Cannot convert unicast subtype of {}",
73 unicastInstruction.subtype());
74 }
75 }
76
77 /**
78 * Encodes a multicast mapping instruction.
79 *
80 * @param result json node that the mapping instruction
81 * attributes are added to
82 */
83 private void encodeMulticast(ObjectNode result) {
84 MulticastMappingInstruction multicastMappingInstruction =
85 (MulticastMappingInstruction) instruction;
86 result.put(MappingInstructionCodec.SUBTYPE, multicastMappingInstruction.subtype().name());
87
88 switch (multicastMappingInstruction.subtype()) {
89 case WEIGHT:
90 MulticastMappingInstruction.WeightMappingInstruction wmi =
91 (MulticastMappingInstruction.WeightMappingInstruction)
92 multicastMappingInstruction;
93 result.put(MappingInstructionCodec.MULTICAST_WEIGHT, wmi.weight());
94 break;
95 case PRIORITY:
96 MulticastMappingInstruction.PriorityMappingInstruction pmi =
97 (MulticastMappingInstruction.PriorityMappingInstruction)
98 multicastMappingInstruction;
99 result.put(MappingInstructionCodec.MULTICAST_PRIORITY, pmi.priority());
100 break;
101 default:
102 log.info("Cannot convert multicast subtype of {}",
103 multicastMappingInstruction.subtype());
104 }
105 }
106
107 /**
108 * Encodes the given mapping instruction into JSON.
109 *
110 * @return JSON object node representing the mapping instruction.
111 */
112 public ObjectNode encode() {
113 final ObjectNode result = context.mapper().createObjectNode()
114 .put(MappingInstructionCodec.TYPE, instruction.type().toString());
115
116 switch (instruction.type()) {
117 case UNICAST:
118 encodeUnicast(result);
119 break;
120 case MULTICAST:
121 encodeMulticast(result);
122 break;
123 default:
124 log.info("Cannot convert mapping instruction type of {}", instruction.type());
125 break;
126 }
127 return result;
128 }
129}