blob: a095da8525d2a4a0cce72e1b9347a3ae7c8636cd [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 static com.google.common.base.Preconditions.checkNotNull;
19import static org.onlab.util.Tools.nullIsIllegal;
20
21import org.onlab.packet.VlanId;
22import org.onosproject.codec.CodecContext;
23import org.onosproject.codec.JsonCodec;
24import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMep;
25import org.onosproject.incubator.net.l2monitoring.cfm.Mep;
26import org.onosproject.incubator.net.l2monitoring.cfm.Mep.MepBuilder;
27import org.onosproject.incubator.net.l2monitoring.cfm.Mep.MepDirection;
28import org.onosproject.incubator.net.l2monitoring.cfm.Mep.Priority;
29import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdCharStr;
30import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdShort;
31import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId;
32import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdCharStr;
33import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId;
34import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException;
35import org.onosproject.net.DeviceId;
36import org.onosproject.net.PortNumber;
37
38import com.fasterxml.jackson.databind.JsonNode;
39import com.fasterxml.jackson.databind.node.ObjectNode;
40
41import java.time.Duration;
42
43/**
44 * Encode and decode to/from JSON to Mep object.
45 */
46public class MepCodec extends JsonCodec<Mep> {
47 private static final String ADMINISTRATIVE_STATE = "administrative-state";
48 private static final String PRIMARY_VID = "primary-vid";
49 private static final String CCM_LTM_PRIORITY = "ccm-ltm-priority";
50 private static final String CCI_ENABLED = "cci-enabled";
51 private static final String FNG_ADDRESS = "fng-address";
52 private static final String LOWEST_FAULT_PRIORITY_DEFECT = "lowest-fault-priority-defect";
53 private static final String DEFECT_PRESENT_TIME = "defect-present-time";
54 private static final String DEFECT_ABSENT_TIME = "defect-absent-time";
55
Sean Condon3a1efef2018-02-24 13:16:03 +000056 /**
57 * Decodes the Mep entity from JSON.
58 *
59 * @param json JSON to decode
60 * @param context decoding context
61 * @param mdName The MD name
62 * @param maName The MA name
63 * @return decoded Mep
64 * @throws java.lang.UnsupportedOperationException if the codec does not
65 * support decode operations
66 */
Sean Condon0e89bda2017-03-21 14:23:19 +000067 public Mep decode(ObjectNode json, CodecContext context, String
68 mdName, String maName) {
69 if (json == null || !json.isObject()) {
70 return null;
71 }
72
73 JsonNode mepNode = json.get("mep");
74
75 int mepId = Integer.parseInt(
76 nullIsIllegal(mepNode.get("mepId"), "mepId is required").asText());
77 DeviceId deviceId = DeviceId.deviceId(
78 nullIsIllegal(mepNode.get("deviceId"), "deviceId is required")
79 .asText());
80 PortNumber port = PortNumber
81 .portNumber(Long.parseLong(
82 nullIsIllegal(mepNode.get("port"), "port is required")
83 .asText()));
84 MepDirection direction = MepDirection.valueOf(
85 nullIsIllegal(mepNode.get("direction"), "direction is required").
86 asText());
87
88 try {
89 MdId mdId = MdIdCharStr.asMdId(mdName);
90 MaIdShort maId = MaIdCharStr.asMaId(maName);
91 MepBuilder mepBuilder = DefaultMep
92 .builder(MepId.valueOf((short) mepId),
93 deviceId, port, direction, mdId, maId);
94
95 if (mepNode.get(PRIMARY_VID) != null) {
96 mepBuilder.primaryVid(VlanId.vlanId(
97 (short) mepNode.get(PRIMARY_VID).asInt(0)));
98 }
99
100 if (mepNode.get(ADMINISTRATIVE_STATE) != null) {
101 mepBuilder.administrativeState(mepNode.get(ADMINISTRATIVE_STATE)
102 .asBoolean());
103 }
104
105 if (mepNode.get(CCM_LTM_PRIORITY) != null) {
106 mepBuilder.ccmLtmPriority(
107 Priority.values()[mepNode.get(CCM_LTM_PRIORITY).asInt(0)]);
108 }
109
110 if (mepNode.get(CCI_ENABLED) != null) {
111 mepBuilder.cciEnabled(mepNode.get(CCI_ENABLED).asBoolean());
112 }
113
114 if (mepNode.get(LOWEST_FAULT_PRIORITY_DEFECT) != null) {
115 mepBuilder.lowestFaultPriorityDefect(
116 Mep.LowestFaultDefect.values()[mepNode.get(LOWEST_FAULT_PRIORITY_DEFECT).asInt()]);
117 }
118
119 if (mepNode.get(DEFECT_ABSENT_TIME) != null) {
120 mepBuilder.defectAbsentTime(
121 Duration.parse(mepNode.get(DEFECT_ABSENT_TIME).asText()));
122 }
123
124 if (mepNode.get(DEFECT_PRESENT_TIME) != null) {
125 mepBuilder.defectPresentTime(
126 Duration.parse(mepNode.get(DEFECT_PRESENT_TIME).asText()));
127 }
128
129 if (mepNode.get(FNG_ADDRESS) != null) {
130 mepBuilder.fngAddress((new FngAddressCodec())
131 .decode((ObjectNode) mepNode, context));
132 }
133
134
135 return mepBuilder.build();
136 } catch (CfmConfigException e) {
137 throw new IllegalArgumentException(e);
138 }
139 }
140
Sean Condon3a1efef2018-02-24 13:16:03 +0000141 /**
142 * Encodes the Mep entity into JSON.
143 *
144 * @param mep Mep to encode
145 * @param context encoding context
146 * @return JSON node
147 * @throws java.lang.UnsupportedOperationException if the codec does not
148 * support encode operations
149 */
Sean Condon0e89bda2017-03-21 14:23:19 +0000150 @Override
151 public ObjectNode encode(Mep mep, CodecContext context) {
152 checkNotNull(mep, "Mep cannot be null");
153 ObjectNode result = context.mapper().createObjectNode()
154 .put("mepId", mep.mepId().id())
155 .put("deviceId", mep.deviceId().toString())
156 .put("port", mep.port().toLong())
157 .put("direction", mep.direction().name())
158 .put("mdName", mep.mdId().toString())
159 .put("maName", mep.maId().toString())
160 .put(ADMINISTRATIVE_STATE, mep.administrativeState())
161 .put(CCI_ENABLED, mep.cciEnabled());
162 if (mep.ccmLtmPriority() != null) {
163 result.put(CCM_LTM_PRIORITY, mep.ccmLtmPriority().ordinal());
164 }
165 if (mep.primaryVid() != null) {
166 result.put(PRIMARY_VID, mep.primaryVid().toShort());
167 }
168 if (mep.fngAddress() != null) {
169 result.put(FNG_ADDRESS, new FngAddressCodec().encode(mep.fngAddress(), context));
170 }
171 if (mep.lowestFaultPriorityDefect() != null) {
172 result.put(LOWEST_FAULT_PRIORITY_DEFECT, mep.lowestFaultPriorityDefect().ordinal());
173 }
174 if (mep.defectPresentTime() != null) {
175 result.put(DEFECT_PRESENT_TIME, mep.defectPresentTime().toString());
176 }
177 if (mep.defectAbsentTime() != null) {
178 result.put(DEFECT_ABSENT_TIME, mep.defectAbsentTime().toString());
179 }
180
181 return result;
182 }
183}