blob: e29415c89cdbceecdf98bf6b0ac928c41d25225c [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
45 @Override
46 public ObjectNode encode(MepLbCreate mepLbCreate, CodecContext context) {
47 checkNotNull(mepLbCreate, "Mep Lb Create cannot be null");
48 ObjectNode result = context.mapper().createObjectNode()
49 .put(NUMBER_MESSAGES, mepLbCreate.numberMessages());
50
51 if (mepLbCreate.remoteMepId() != null) {
52 result.put(REMOTE_MEP_ID, mepLbCreate.remoteMepId().value());
53 } else {
54 result.put(REMOTE_MEP_MAC, mepLbCreate.remoteMepAddress().toString());
55 }
56
57 if (mepLbCreate.dataTlvHex() != null) {
58 result.put(DATA_TLV_HEX, mepLbCreate.dataTlvHex());
59 }
60 if (mepLbCreate.vlanDropEligible() != null) {
61 result.put(VLAN_DROP_ELIGIBLE, mepLbCreate.vlanDropEligible());
62 }
63 if (mepLbCreate.vlanPriority() != null) {
64 result.put(VLAN_PRIORITY, mepLbCreate.vlanPriority().ordinal());
65 }
66 return result;
67 }
68
69
70 @Override
71 public MepLbCreate decode(ObjectNode json, CodecContext context) {
72 if (json == null || !json.isObject()) {
73 return null;
74 }
75
76 JsonNode loopbackNode = json.get(LOOPBACK);
77
78 JsonNode remoteMepIdNode = loopbackNode.get(REMOTE_MEP_ID);
79 JsonNode remoteMepMacNode = loopbackNode.get(REMOTE_MEP_MAC);
80
81 MepLbCreate.MepLbCreateBuilder lbCreateBuilder = null;
82 if (remoteMepIdNode != null) {
83 MepId remoteMepId = MepId.valueOf((short) remoteMepIdNode.asInt());
84 lbCreateBuilder = DefaultMepLbCreate.builder(remoteMepId);
85 } else if (remoteMepMacNode != null) {
86 MacAddress remoteMepMac = MacAddress.valueOf(
87 remoteMepMacNode.asText());
88 lbCreateBuilder = DefaultMepLbCreate.builder(remoteMepMac);
89 } else {
90 throw new IllegalArgumentException(
91 "Either a remoteMepId or a remoteMepMac");
92 }
93
94 JsonNode numMessagesNode = loopbackNode.get(NUMBER_MESSAGES);
95 if (numMessagesNode != null) {
96 int numMessages = numMessagesNode.asInt();
97 lbCreateBuilder.numberMessages(numMessages);
98 }
99
100 JsonNode vlanDropEligibleNode = loopbackNode.get(VLAN_DROP_ELIGIBLE);
101 if (vlanDropEligibleNode != null) {
102 boolean vlanDropEligible = vlanDropEligibleNode.asBoolean();
103 lbCreateBuilder.vlanDropEligible(vlanDropEligible);
104 }
105
106 JsonNode vlanPriorityNode = loopbackNode.get(VLAN_PRIORITY);
107 if (vlanPriorityNode != null) {
108 short vlanPriority = (short) vlanPriorityNode.asInt();
109 lbCreateBuilder.vlanPriority(Priority.values()[vlanPriority]);
110 }
111
112 JsonNode dataTlvHexNode = loopbackNode.get(DATA_TLV_HEX);
113 if (dataTlvHexNode != null) {
114 String dataTlvHex = loopbackNode.get(DATA_TLV_HEX).asText();
115 if (!dataTlvHex.isEmpty()) {
116 lbCreateBuilder.dataTlv(HexString.fromHexString(dataTlvHex));
117 }
118 }
119
120 return lbCreateBuilder.build();
121 }
122}