blob: f1799fb98e860716048466b98e9be2a42544ca8e [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
Sean Condon3a1efef2018-02-24 13:16:03 +000037 /**
38 * Encodes the MepEntry entity into JSON.
39 *
40 * @param mepEntry MepEntry to encode
41 * @param context encoding context
42 * @return JSON node
43 * @throws java.lang.UnsupportedOperationException if the codec does not
44 * support encode operations
45 */
Sean Condon0e89bda2017-03-21 14:23:19 +000046 @Override
47 public ObjectNode encode(MepEntry mepEntry, CodecContext context) {
48 checkNotNull(mepEntry, "Mep cannot be null");
49
50 ObjectNode result = context.mapper().createObjectNode();
51
52 //Get the common attributes
Sean Condon3a1efef2018-02-24 13:16:03 +000053 Mep mep = mepEntry;
Sean Condon0e89bda2017-03-21 14:23:19 +000054 ObjectNode mepAttrs = new MepCodec().encode(mep, context);
55 Iterator<Entry<String, JsonNode>> elements = mepAttrs.fields();
56 while (elements.hasNext()) {
57 Entry<String, JsonNode> element = elements.next();
58 result.set(element.getKey(), element.getValue());
59 }
60
61 if (mepEntry.macAddress() != null) {
62 result.put("macAddress", mepEntry.macAddress().toString());
63 }
64
65 if (mepEntry.loopbackAttributes() != null) {
66 result.set("loopback", new MepLbEntryCodec()
67 .encode(mepEntry.loopbackAttributes(), context));
68 }
69
70 if (mepEntry.activeRemoteMepList() != null) {
71 result.set("remoteMeps", new RemoteMepEntryCodec()
72 .encode(mepEntry.activeRemoteMepList(), context));
73 }
74
75 if (mepEntry.activeErrorCcmDefect()) {
76 result.put("activeErrorCcmDefect", true);
77 }
78 if (mepEntry.activeMacStatusDefect()) {
79 result.put("activeMacStatusDefect", true);
80 }
81 if (mepEntry.activeRdiCcmDefect()) {
82 result.put("activeRdiCcmDefect", true);
83 }
84 if (mepEntry.activeRemoteCcmDefect()) {
85 result.put("activeRemoteCcmDefect", true);
86 }
87 if (mepEntry.activeXconCcmDefect()) {
88 result.put("activeXconCcmDefect", true);
89 }
90 return result;
91 }
92
Sean Condon3a1efef2018-02-24 13:16:03 +000093 /**
94 * Encodes the collection of the MepEntry entities.
95 *
96 * @param mepEntryEntities collection of MepEntry to encode
97 * @param context encoding context
98 * @return JSON array
99 * @throws java.lang.UnsupportedOperationException if the codec does not
100 * support encode operations
101 */
Sean Condon0e89bda2017-03-21 14:23:19 +0000102 @Override
103 public ArrayNode encode(Iterable<MepEntry> mepEntryEntities, CodecContext context) {
104 ArrayNode an = context.mapper().createArrayNode();
105 if (mepEntryEntities != null) {
106 mepEntryEntities.forEach(mepEntry -> an.add(encode(mepEntry, context)));
107 }
108 return an;
109 }
110
111}