Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.cfm.web; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import static org.onlab.util.Tools.nullIsIllegal; |
| 20 | |
| 21 | import java.util.List; |
| 22 | |
| 23 | import org.onosproject.codec.CodecContext; |
| 24 | import org.onosproject.codec.JsonCodec; |
| 25 | import org.onosproject.incubator.net.l2monitoring.cfm.Component; |
| 26 | import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMaintenanceAssociation; |
| 27 | import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceAssociation; |
| 28 | import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceAssociation.CcmInterval; |
| 29 | import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceAssociation.MaBuilder; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 30 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdShort; |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 31 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdMaNameUtil; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 32 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId; |
| 33 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException; |
| 34 | |
| 35 | import com.fasterxml.jackson.databind.JsonNode; |
| 36 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 37 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 38 | |
| 39 | /** |
| 40 | * Encode and decode to/from JSON to MaintenanceAssociation object. |
| 41 | */ |
| 42 | public 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 Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 52 | /** |
| 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 Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 61 | @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 Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 80 | /** |
| 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 Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 90 | 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 Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 104 | MaIdShort maId = MdMaNameUtil.parseMaName(maNameType, maName); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 105 | 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 Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 124 | 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 Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | return builder.build(); |
| 134 | } catch (CfmConfigException e) { |
| 135 | throw new IllegalArgumentException(e); |
| 136 | } |
| 137 | |
| 138 | } |
| 139 | |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 140 | /** |
| 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 Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 149 | @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 | } |