blob: 8b802374f7e10509efc0c8be8813dab2cbabffd0 [file] [log] [blame]
Sean Condon0e89bda2017-03-21 14:23:19 +00001/*
2 * Copyright 2017-present Open Networking Foundation
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 */
16package org.onosproject.cfm.web;
17
18import org.onlab.packet.MacAddress;
19import org.onlab.util.HexString;
20import org.onosproject.codec.CodecContext;
21import org.onosproject.codec.JsonCodec;
22import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMepLbCreate;
23import org.onosproject.incubator.net.l2monitoring.cfm.Mep.Priority;
24import org.onosproject.incubator.net.l2monitoring.cfm.MepLbCreate;
25import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId;
26
27import com.fasterxml.jackson.databind.JsonNode;
28import com.fasterxml.jackson.databind.node.ObjectNode;
29
30import static com.google.common.base.Preconditions.checkNotNull;
31
32/**
33 * Encode and decode to/from JSON to MepLbCreate object.
34 */
35public class MepLbCreateCodec extends JsonCodec<MepLbCreate> {
36
37 public static final String NUMBER_MESSAGES = "numberMessages";
38 public static final String REMOTE_MEP_ID = "remoteMepId";
39 public static final String REMOTE_MEP_MAC = "remoteMepMac";
40 public static final String DATA_TLV_HEX = "dataTlvHex";
41 public static final String VLAN_DROP_ELIGIBLE = "vlanDropEligible";
42 public static final String VLAN_PRIORITY = "vlanPriority";
43 public static final String LOOPBACK = "loopback";
44
Sean Condon3a1efef2018-02-24 13:16:03 +000045 /**
46 * Encodes the MepLbCreate entity into JSON.
47 *
48 * @param mepLbCreate MepLbCreate to encode
49 * @param context encoding context
50 * @return JSON node
51 * @throws java.lang.UnsupportedOperationException if the codec does not
52 * support encode operations
53 */
Sean Condon0e89bda2017-03-21 14:23:19 +000054 @Override
55 public ObjectNode encode(MepLbCreate mepLbCreate, CodecContext context) {
56 checkNotNull(mepLbCreate, "Mep Lb Create cannot be null");
57 ObjectNode result = context.mapper().createObjectNode()
58 .put(NUMBER_MESSAGES, mepLbCreate.numberMessages());
59
60 if (mepLbCreate.remoteMepId() != null) {
61 result.put(REMOTE_MEP_ID, mepLbCreate.remoteMepId().value());
62 } else {
63 result.put(REMOTE_MEP_MAC, mepLbCreate.remoteMepAddress().toString());
64 }
65
66 if (mepLbCreate.dataTlvHex() != null) {
67 result.put(DATA_TLV_HEX, mepLbCreate.dataTlvHex());
68 }
69 if (mepLbCreate.vlanDropEligible() != null) {
70 result.put(VLAN_DROP_ELIGIBLE, mepLbCreate.vlanDropEligible());
71 }
72 if (mepLbCreate.vlanPriority() != null) {
73 result.put(VLAN_PRIORITY, mepLbCreate.vlanPriority().ordinal());
74 }
75 return result;
76 }
77
Sean Condon3a1efef2018-02-24 13:16:03 +000078 /**
79 * Decodes the MepLbCreate entity from JSON.
80 *
81 * @param json JSON to decode
82 * @param context decoding context
83 * @return decoded MepLbCreate
84 * @throws java.lang.UnsupportedOperationException if the codec does not
85 * support decode operations
86 */
Sean Condon0e89bda2017-03-21 14:23:19 +000087 @Override
88 public MepLbCreate decode(ObjectNode json, CodecContext context) {
89 if (json == null || !json.isObject()) {
90 return null;
91 }
92
93 JsonNode loopbackNode = json.get(LOOPBACK);
94
95 JsonNode remoteMepIdNode = loopbackNode.get(REMOTE_MEP_ID);
96 JsonNode remoteMepMacNode = loopbackNode.get(REMOTE_MEP_MAC);
97
Sean Condon3a1efef2018-02-24 13:16:03 +000098 MepLbCreate.MepLbCreateBuilder lbCreateBuilder;
Sean Condon0e89bda2017-03-21 14:23:19 +000099 if (remoteMepIdNode != null) {
100 MepId remoteMepId = MepId.valueOf((short) remoteMepIdNode.asInt());
101 lbCreateBuilder = DefaultMepLbCreate.builder(remoteMepId);
102 } else if (remoteMepMacNode != null) {
103 MacAddress remoteMepMac = MacAddress.valueOf(
104 remoteMepMacNode.asText());
105 lbCreateBuilder = DefaultMepLbCreate.builder(remoteMepMac);
106 } else {
107 throw new IllegalArgumentException(
108 "Either a remoteMepId or a remoteMepMac");
109 }
110
111 JsonNode numMessagesNode = loopbackNode.get(NUMBER_MESSAGES);
112 if (numMessagesNode != null) {
113 int numMessages = numMessagesNode.asInt();
114 lbCreateBuilder.numberMessages(numMessages);
115 }
116
117 JsonNode vlanDropEligibleNode = loopbackNode.get(VLAN_DROP_ELIGIBLE);
118 if (vlanDropEligibleNode != null) {
119 boolean vlanDropEligible = vlanDropEligibleNode.asBoolean();
120 lbCreateBuilder.vlanDropEligible(vlanDropEligible);
121 }
122
123 JsonNode vlanPriorityNode = loopbackNode.get(VLAN_PRIORITY);
124 if (vlanPriorityNode != null) {
125 short vlanPriority = (short) vlanPriorityNode.asInt();
126 lbCreateBuilder.vlanPriority(Priority.values()[vlanPriority]);
127 }
128
129 JsonNode dataTlvHexNode = loopbackNode.get(DATA_TLV_HEX);
130 if (dataTlvHexNode != null) {
131 String dataTlvHex = loopbackNode.get(DATA_TLV_HEX).asText();
132 if (!dataTlvHex.isEmpty()) {
133 lbCreateBuilder.dataTlv(HexString.fromHexString(dataTlvHex));
134 }
135 }
136
137 return lbCreateBuilder.build();
138 }
139}