blob: b1b9baa3d344f098710b7bb45217f9155cf908dc [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 java.util.List;
22
23import org.onosproject.codec.CodecContext;
24import org.onosproject.codec.JsonCodec;
25import org.onosproject.incubator.net.l2monitoring.cfm.Component;
26import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMaintenanceAssociation;
27import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceAssociation;
28import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceAssociation.CcmInterval;
29import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceAssociation.MaBuilder;
Sean Condon0e89bda2017-03-21 14:23:19 +000030import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdShort;
Sean Condon3a1efef2018-02-24 13:16:03 +000031import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdMaNameUtil;
Sean Condon0e89bda2017-03-21 14:23:19 +000032import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId;
33import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException;
34
35import com.fasterxml.jackson.databind.JsonNode;
36import com.fasterxml.jackson.databind.node.ArrayNode;
37import com.fasterxml.jackson.databind.node.ObjectNode;
38
39/**
40 * Encode and decode to/from JSON to MaintenanceAssociation object.
41 */
42public class MaintenanceAssociationCodec extends JsonCodec<MaintenanceAssociation> {
43
44 private static final String MA_NAME_TYPE = "maNameType";
45 private static final String MA_NUMERIC_ID = "maNumericId";
46 private static final String MA_NAME = "maName";
47 private static final String CCM_INTERVAL = "ccm-interval";
48 private static final String COMPONENT_LIST = "component-list";
49 private static final String RMEP_LIST = "rmep-list";
50 private static final String MA = "ma";
51
Sean Condon3a1efef2018-02-24 13:16:03 +000052 /**
53 * Encodes the MaintenanceAssociation entity into JSON.
54 *
55 * @param ma MaintenanceAssociation to encode
56 * @param context encoding context
57 * @return JSON node
58 * @throws java.lang.UnsupportedOperationException if the codec does not
59 * support encode operations
60 */
Sean Condon0e89bda2017-03-21 14:23:19 +000061 @Override
62 public ObjectNode encode(MaintenanceAssociation ma, CodecContext context) {
63 checkNotNull(ma, "Maintenance Association cannot be null");
64 ObjectNode result = context.mapper().createObjectNode()
65 .put(MA_NAME, ma.maId().toString())
66 .put(MA_NAME_TYPE, ma.maId().nameType().name());
67 if (ma.maNumericId() > 0) {
68 result = result.put(MA_NUMERIC_ID, ma.maNumericId());
69 }
70 if (ma.ccmInterval() != null) {
71 result = result.put(CCM_INTERVAL, ma.ccmInterval().name());
72 }
73
74 result.set(COMPONENT_LIST, new ComponentCodec().encode(ma.componentList(), context));
75 result.set(RMEP_LIST, new RMepCodec().encode(ma.remoteMepIdList(), context));
76
77 return result;
78 }
79
Sean Condon3a1efef2018-02-24 13:16:03 +000080 /**
81 * Decodes the MaintenanceAssociation entity from JSON.
82 *
83 * @param json JSON to decode
84 * @param context decoding context
85 * @param mdNameLen the length of the corresponding MD's name
86 * @return decoded MaintenanceAssociation
87 * @throws java.lang.UnsupportedOperationException if the codec does not
88 * support decode operations
89 */
Sean Condon0e89bda2017-03-21 14:23:19 +000090 public MaintenanceAssociation decode(ObjectNode json, CodecContext context, int mdNameLen) {
91 if (json == null || !json.isObject()) {
92 return null;
93 }
94
95 JsonNode maNode = json.get(MA);
96
97 String maName = nullIsIllegal(maNode.get(MA_NAME), "maName is required").asText();
98 String maNameType = MaIdShort.MaIdType.CHARACTERSTRING.name();
99 if (maNode.get(MA_NAME_TYPE) != null) {
100 maNameType = maNode.get(MA_NAME_TYPE).asText();
101 }
102
103 try {
Sean Condon3a1efef2018-02-24 13:16:03 +0000104 MaIdShort maId = MdMaNameUtil.parseMaName(maNameType, maName);
Sean Condon0e89bda2017-03-21 14:23:19 +0000105 MaBuilder builder =
106 DefaultMaintenanceAssociation.builder(maId, mdNameLen);
107
108 JsonNode maNumericIdNode = maNode.get(MA_NUMERIC_ID);
109 if (maNumericIdNode != null) {
110 short mdNumericId = (short) maNumericIdNode.asInt();
111 builder = builder.maNumericId(mdNumericId);
112 }
113 if (maNode.get(CCM_INTERVAL) != null) {
114 builder.ccmInterval(CcmInterval.valueOf(maNode.get(CCM_INTERVAL).asText()));
115 }
116
117 List<Component> componentList = (new ComponentCodec()).decode((ArrayNode)
118 nullIsIllegal(maNode.get(COMPONENT_LIST),
119 "component-list is required"), context);
120 for (Component component:componentList) {
121 builder = builder.addToComponentList(component);
122 }
123
Sean Condon96b896d2017-12-11 12:44:29 -0800124 JsonNode rmepListJson = maNode.get(RMEP_LIST);
125 if (rmepListJson != null) {
126 List<MepId> remoteMeps = (new RMepCodec()).decode(
127 (ArrayNode) rmepListJson, context);
128 for (MepId remoteMep:remoteMeps) {
129 builder = builder.addToRemoteMepIdList(remoteMep);
130 }
Sean Condon0e89bda2017-03-21 14:23:19 +0000131 }
132
133 return builder.build();
134 } catch (CfmConfigException e) {
135 throw new IllegalArgumentException(e);
136 }
137
138 }
139
Sean Condon3a1efef2018-02-24 13:16:03 +0000140 /**
141 * Encodes the collection of the MaintenanceAssociation entities.
142 *
143 * @param maEntities collection of MaintenanceAssociation to encode
144 * @param context encoding context
145 * @return JSON array
146 * @throws java.lang.UnsupportedOperationException if the codec does not
147 * support encode operations
148 */
Sean Condon0e89bda2017-03-21 14:23:19 +0000149 @Override
150 public ArrayNode encode(Iterable<MaintenanceAssociation> maEntities, CodecContext context) {
151 ArrayNode an = context.mapper().createArrayNode();
152 maEntities.forEach(ma -> an.add(encode(ma, context)));
153 return an;
154 }
155}