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 org.onosproject.codec.CodecContext; |
| 22 | import org.onosproject.codec.JsonCodec; |
| 23 | import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMaintenanceDomain; |
| 24 | import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceDomain; |
| 25 | import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceDomain.MdLevel; |
| 26 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId; |
| 27 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdCharStr; |
| 28 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdDomainName; |
| 29 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdMacUint; |
| 30 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdNone; |
| 31 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException; |
| 32 | |
| 33 | import com.fasterxml.jackson.databind.JsonNode; |
| 34 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 35 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 36 | |
| 37 | /** |
| 38 | * Encode and decode to/from JSON to MaintenanceDomain object. |
| 39 | */ |
| 40 | public class MaintenanceDomainCodec extends JsonCodec<MaintenanceDomain> { |
| 41 | |
| 42 | private static final String MD_LEVEL = "mdLevel"; |
| 43 | private static final String MD_NUMERIC_ID = "mdNumericId"; |
| 44 | private static final String MD = "md"; |
| 45 | private static final String MD_NAME = "mdName"; |
| 46 | private static final String MD_NAME_TYPE = "mdNameType"; |
| 47 | |
| 48 | public MaintenanceDomainCodec() { |
| 49 | super(); |
| 50 | |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public ObjectNode encode(MaintenanceDomain md, CodecContext context) { |
| 55 | checkNotNull(md, "Maintenance Domain cannot be null"); |
| 56 | ObjectNode result = context.mapper().createObjectNode() |
| 57 | .put(MD_NAME, md.mdId().toString()) |
| 58 | .put(MD_NAME_TYPE, md.mdId().nameType().name()) |
| 59 | .put(MD_LEVEL, md.mdLevel().name()); |
| 60 | if (md.mdNumericId() > 0) { |
| 61 | result = result.put(MD_NUMERIC_ID, md.mdNumericId()); |
| 62 | } |
| 63 | result.set("maList", |
| 64 | new MaintenanceAssociationCodec() |
| 65 | .encode(md.maintenanceAssociationList(), context)); |
| 66 | |
| 67 | return result; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public MaintenanceDomain decode(ObjectNode json, CodecContext context) { |
| 72 | if (json == null || !json.isObject()) { |
| 73 | return null; |
| 74 | } |
| 75 | |
| 76 | JsonNode mdNode = json.get(MD); |
| 77 | |
| 78 | String mdName = nullIsIllegal(mdNode.get(MD_NAME), "mdName is required").asText(); |
| 79 | String mdNameType = MdId.MdNameType.CHARACTERSTRING.name(); |
| 80 | if (mdNode.get(MD_NAME_TYPE) != null) { |
| 81 | mdNameType = mdNode.get(MD_NAME_TYPE).asText(); |
| 82 | } |
| 83 | |
| 84 | try { |
| 85 | MdId mdId = null; |
| 86 | MdId.MdNameType nameType = |
| 87 | MdId.MdNameType.valueOf(mdNameType); |
| 88 | switch (nameType) { |
| 89 | case DOMAINNAME: |
| 90 | mdId = MdIdDomainName.asMdId(mdName); |
| 91 | break; |
| 92 | case MACANDUINT: |
| 93 | mdId = MdIdMacUint.asMdId(mdName); |
| 94 | break; |
| 95 | case NONE: |
| 96 | mdId = MdIdNone.asMdId(); |
| 97 | break; |
| 98 | case CHARACTERSTRING: |
| 99 | default: |
| 100 | mdId = MdIdCharStr.asMdId(mdName); |
| 101 | } |
| 102 | |
| 103 | MaintenanceDomain.MdBuilder builder = DefaultMaintenanceDomain.builder(mdId); |
| 104 | JsonNode mdLevelNode = mdNode.get(MD_LEVEL); |
| 105 | if (mdLevelNode != null) { |
| 106 | MdLevel mdLevel = MdLevel.valueOf(mdLevelNode.asText()); |
| 107 | builder = builder.mdLevel(mdLevel); |
| 108 | } |
| 109 | JsonNode mdNumericIdNode = mdNode.get(MD_NUMERIC_ID); |
| 110 | if (mdNumericIdNode != null) { |
| 111 | short mdNumericId = (short) mdNumericIdNode.asInt(); |
| 112 | builder = builder.mdNumericId(mdNumericId); |
| 113 | } |
| 114 | |
| 115 | return builder.build(); |
| 116 | } catch (CfmConfigException e) { |
| 117 | throw new IllegalArgumentException(e); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public ArrayNode encode(Iterable<MaintenanceDomain> mdEntities, CodecContext context) { |
| 123 | ArrayNode an = context.mapper().createArrayNode(); |
| 124 | mdEntities.forEach(md -> an.add(encode(md, context))); |
| 125 | return an; |
| 126 | } |
| 127 | } |