blob: d1618a7d852a7d9e40885766b3866f9dc856f2a5 [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;
19
20import java.util.Iterator;
21import java.util.Map.Entry;
22
23import org.onosproject.codec.CodecContext;
24import org.onosproject.codec.JsonCodec;
25import org.onosproject.incubator.net.l2monitoring.cfm.Mep;
26import org.onosproject.incubator.net.l2monitoring.cfm.MepEntry;
27
28import com.fasterxml.jackson.databind.JsonNode;
29import com.fasterxml.jackson.databind.node.ArrayNode;
30import com.fasterxml.jackson.databind.node.ObjectNode;
31
32/**
33 * Encode and decode to/from JSON to MepEntry object.
34 */
35public class MepEntryCodec extends JsonCodec<MepEntry> {
36
37 @Override
38 public ObjectNode encode(MepEntry mepEntry, CodecContext context) {
39 checkNotNull(mepEntry, "Mep cannot be null");
40
41 ObjectNode result = context.mapper().createObjectNode();
42
43 //Get the common attributes
44 Mep mep = (Mep) mepEntry;
45 ObjectNode mepAttrs = new MepCodec().encode(mep, context);
46 Iterator<Entry<String, JsonNode>> elements = mepAttrs.fields();
47 while (elements.hasNext()) {
48 Entry<String, JsonNode> element = elements.next();
49 result.set(element.getKey(), element.getValue());
50 }
51
52 if (mepEntry.macAddress() != null) {
53 result.put("macAddress", mepEntry.macAddress().toString());
54 }
55
56 if (mepEntry.loopbackAttributes() != null) {
57 result.set("loopback", new MepLbEntryCodec()
58 .encode(mepEntry.loopbackAttributes(), context));
59 }
60
61 if (mepEntry.activeRemoteMepList() != null) {
62 result.set("remoteMeps", new RemoteMepEntryCodec()
63 .encode(mepEntry.activeRemoteMepList(), context));
64 }
65
66 if (mepEntry.activeErrorCcmDefect()) {
67 result.put("activeErrorCcmDefect", true);
68 }
69 if (mepEntry.activeMacStatusDefect()) {
70 result.put("activeMacStatusDefect", true);
71 }
72 if (mepEntry.activeRdiCcmDefect()) {
73 result.put("activeRdiCcmDefect", true);
74 }
75 if (mepEntry.activeRemoteCcmDefect()) {
76 result.put("activeRemoteCcmDefect", true);
77 }
78 if (mepEntry.activeXconCcmDefect()) {
79 result.put("activeXconCcmDefect", true);
80 }
81 return result;
82 }
83
84 @Override
85 public ArrayNode encode(Iterable<MepEntry> mepEntryEntities, CodecContext context) {
86 ArrayNode an = context.mapper().createArrayNode();
87 if (mepEntryEntities != null) {
88 mepEntryEntities.forEach(mepEntry -> an.add(encode(mepEntry, context)));
89 }
90 return an;
91 }
92
93}