blob: 62be35d03eaf50a4ca6e1c6d5c0ba955d3154133 [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;
30import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaId2Octet;
31import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdCharStr;
32import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdIccY1731;
33import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdPrimaryVid;
34import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdRfc2685VpnId;
35import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdShort;
36import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId;
37import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException;
38
39import com.fasterxml.jackson.databind.JsonNode;
40import com.fasterxml.jackson.databind.node.ArrayNode;
41import com.fasterxml.jackson.databind.node.ObjectNode;
42
43/**
44 * Encode and decode to/from JSON to MaintenanceAssociation object.
45 */
46public class MaintenanceAssociationCodec extends JsonCodec<MaintenanceAssociation> {
47
48 private static final String MA_NAME_TYPE = "maNameType";
49 private static final String MA_NUMERIC_ID = "maNumericId";
50 private static final String MA_NAME = "maName";
51 private static final String CCM_INTERVAL = "ccm-interval";
52 private static final String COMPONENT_LIST = "component-list";
53 private static final String RMEP_LIST = "rmep-list";
54 private static final String MA = "ma";
55
56 @Override
57 public ObjectNode encode(MaintenanceAssociation ma, CodecContext context) {
58 checkNotNull(ma, "Maintenance Association cannot be null");
59 ObjectNode result = context.mapper().createObjectNode()
60 .put(MA_NAME, ma.maId().toString())
61 .put(MA_NAME_TYPE, ma.maId().nameType().name());
62 if (ma.maNumericId() > 0) {
63 result = result.put(MA_NUMERIC_ID, ma.maNumericId());
64 }
65 if (ma.ccmInterval() != null) {
66 result = result.put(CCM_INTERVAL, ma.ccmInterval().name());
67 }
68
69 result.set(COMPONENT_LIST, new ComponentCodec().encode(ma.componentList(), context));
70 result.set(RMEP_LIST, new RMepCodec().encode(ma.remoteMepIdList(), context));
71
72 return result;
73 }
74
75 public MaintenanceAssociation decode(ObjectNode json, CodecContext context, int mdNameLen) {
76 if (json == null || !json.isObject()) {
77 return null;
78 }
79
80 JsonNode maNode = json.get(MA);
81
82 String maName = nullIsIllegal(maNode.get(MA_NAME), "maName is required").asText();
83 String maNameType = MaIdShort.MaIdType.CHARACTERSTRING.name();
84 if (maNode.get(MA_NAME_TYPE) != null) {
85 maNameType = maNode.get(MA_NAME_TYPE).asText();
86 }
87
88 try {
89 MaIdShort maId = null;
90 MaIdShort.MaIdType maIdType = MaIdShort.MaIdType.valueOf(maNameType);
91 switch (maIdType) {
92 case PRIMARYVID:
93 maId = MaIdPrimaryVid.asMaId(maName);
94 break;
95 case TWOOCTET:
96 maId = MaId2Octet.asMaId(maName);
97 break;
98 case RFC2685VPNID:
99 maId = MaIdRfc2685VpnId.asMaIdHex(maName);
100 break;
101 case ICCY1731:
102 maId = MaIdIccY1731.asMaId(maName);
103 break;
104 case CHARACTERSTRING:
105 default:
106 maId = MaIdCharStr.asMaId(maName);
107 }
108 MaBuilder builder =
109 DefaultMaintenanceAssociation.builder(maId, mdNameLen);
110
111 JsonNode maNumericIdNode = maNode.get(MA_NUMERIC_ID);
112 if (maNumericIdNode != null) {
113 short mdNumericId = (short) maNumericIdNode.asInt();
114 builder = builder.maNumericId(mdNumericId);
115 }
116 if (maNode.get(CCM_INTERVAL) != null) {
117 builder.ccmInterval(CcmInterval.valueOf(maNode.get(CCM_INTERVAL).asText()));
118 }
119
120 List<Component> componentList = (new ComponentCodec()).decode((ArrayNode)
121 nullIsIllegal(maNode.get(COMPONENT_LIST),
122 "component-list is required"), context);
123 for (Component component:componentList) {
124 builder = builder.addToComponentList(component);
125 }
126
127 List<MepId> remoteMeps = (new RMepCodec()).decode(
128 (ArrayNode) nullIsIllegal(maNode.get(RMEP_LIST), "rmep-list is required"), context);
129 for (MepId remoteMep:remoteMeps) {
130 builder = builder.addToRemoteMepIdList(remoteMep);
131 }
132
133 return builder.build();
134 } catch (CfmConfigException e) {
135 throw new IllegalArgumentException(e);
136 }
137
138 }
139
140 @Override
141 public ArrayNode encode(Iterable<MaintenanceAssociation> maEntities, CodecContext context) {
142 ArrayNode an = context.mapper().createArrayNode();
143 maEntities.forEach(ma -> an.add(encode(ma, context)));
144 return an;
145 }
146}