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.onlab.packet.VlanId; |
| 22 | import org.onosproject.codec.CodecContext; |
| 23 | import org.onosproject.codec.JsonCodec; |
| 24 | import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMep; |
| 25 | import org.onosproject.incubator.net.l2monitoring.cfm.Mep; |
| 26 | import org.onosproject.incubator.net.l2monitoring.cfm.Mep.MepBuilder; |
| 27 | import org.onosproject.incubator.net.l2monitoring.cfm.Mep.MepDirection; |
| 28 | import org.onosproject.incubator.net.l2monitoring.cfm.Mep.Priority; |
| 29 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdCharStr; |
| 30 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdShort; |
| 31 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId; |
| 32 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdCharStr; |
| 33 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId; |
| 34 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException; |
| 35 | import org.onosproject.net.DeviceId; |
| 36 | import org.onosproject.net.PortNumber; |
| 37 | |
| 38 | import com.fasterxml.jackson.databind.JsonNode; |
| 39 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 40 | |
| 41 | import java.time.Duration; |
| 42 | |
| 43 | /** |
| 44 | * Encode and decode to/from JSON to Mep object. |
| 45 | */ |
| 46 | public class MepCodec extends JsonCodec<Mep> { |
| 47 | private static final String ADMINISTRATIVE_STATE = "administrative-state"; |
| 48 | private static final String PRIMARY_VID = "primary-vid"; |
| 49 | private static final String CCM_LTM_PRIORITY = "ccm-ltm-priority"; |
| 50 | private static final String CCI_ENABLED = "cci-enabled"; |
| 51 | private static final String FNG_ADDRESS = "fng-address"; |
| 52 | private static final String LOWEST_FAULT_PRIORITY_DEFECT = "lowest-fault-priority-defect"; |
| 53 | private static final String DEFECT_PRESENT_TIME = "defect-present-time"; |
| 54 | private static final String DEFECT_ABSENT_TIME = "defect-absent-time"; |
| 55 | |
| 56 | public Mep decode(ObjectNode json, CodecContext context, String |
| 57 | mdName, String maName) { |
| 58 | if (json == null || !json.isObject()) { |
| 59 | return null; |
| 60 | } |
| 61 | |
| 62 | JsonNode mepNode = json.get("mep"); |
| 63 | |
| 64 | int mepId = Integer.parseInt( |
| 65 | nullIsIllegal(mepNode.get("mepId"), "mepId is required").asText()); |
| 66 | DeviceId deviceId = DeviceId.deviceId( |
| 67 | nullIsIllegal(mepNode.get("deviceId"), "deviceId is required") |
| 68 | .asText()); |
| 69 | PortNumber port = PortNumber |
| 70 | .portNumber(Long.parseLong( |
| 71 | nullIsIllegal(mepNode.get("port"), "port is required") |
| 72 | .asText())); |
| 73 | MepDirection direction = MepDirection.valueOf( |
| 74 | nullIsIllegal(mepNode.get("direction"), "direction is required"). |
| 75 | asText()); |
| 76 | |
| 77 | try { |
| 78 | MdId mdId = MdIdCharStr.asMdId(mdName); |
| 79 | MaIdShort maId = MaIdCharStr.asMaId(maName); |
| 80 | MepBuilder mepBuilder = DefaultMep |
| 81 | .builder(MepId.valueOf((short) mepId), |
| 82 | deviceId, port, direction, mdId, maId); |
| 83 | |
| 84 | if (mepNode.get(PRIMARY_VID) != null) { |
| 85 | mepBuilder.primaryVid(VlanId.vlanId( |
| 86 | (short) mepNode.get(PRIMARY_VID).asInt(0))); |
| 87 | } |
| 88 | |
| 89 | if (mepNode.get(ADMINISTRATIVE_STATE) != null) { |
| 90 | mepBuilder.administrativeState(mepNode.get(ADMINISTRATIVE_STATE) |
| 91 | .asBoolean()); |
| 92 | } |
| 93 | |
| 94 | if (mepNode.get(CCM_LTM_PRIORITY) != null) { |
| 95 | mepBuilder.ccmLtmPriority( |
| 96 | Priority.values()[mepNode.get(CCM_LTM_PRIORITY).asInt(0)]); |
| 97 | } |
| 98 | |
| 99 | if (mepNode.get(CCI_ENABLED) != null) { |
| 100 | mepBuilder.cciEnabled(mepNode.get(CCI_ENABLED).asBoolean()); |
| 101 | } |
| 102 | |
| 103 | if (mepNode.get(LOWEST_FAULT_PRIORITY_DEFECT) != null) { |
| 104 | mepBuilder.lowestFaultPriorityDefect( |
| 105 | Mep.LowestFaultDefect.values()[mepNode.get(LOWEST_FAULT_PRIORITY_DEFECT).asInt()]); |
| 106 | } |
| 107 | |
| 108 | if (mepNode.get(DEFECT_ABSENT_TIME) != null) { |
| 109 | mepBuilder.defectAbsentTime( |
| 110 | Duration.parse(mepNode.get(DEFECT_ABSENT_TIME).asText())); |
| 111 | } |
| 112 | |
| 113 | if (mepNode.get(DEFECT_PRESENT_TIME) != null) { |
| 114 | mepBuilder.defectPresentTime( |
| 115 | Duration.parse(mepNode.get(DEFECT_PRESENT_TIME).asText())); |
| 116 | } |
| 117 | |
| 118 | if (mepNode.get(FNG_ADDRESS) != null) { |
| 119 | mepBuilder.fngAddress((new FngAddressCodec()) |
| 120 | .decode((ObjectNode) mepNode, context)); |
| 121 | } |
| 122 | |
| 123 | |
| 124 | return mepBuilder.build(); |
| 125 | } catch (CfmConfigException e) { |
| 126 | throw new IllegalArgumentException(e); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public ObjectNode encode(Mep mep, CodecContext context) { |
| 132 | checkNotNull(mep, "Mep cannot be null"); |
| 133 | ObjectNode result = context.mapper().createObjectNode() |
| 134 | .put("mepId", mep.mepId().id()) |
| 135 | .put("deviceId", mep.deviceId().toString()) |
| 136 | .put("port", mep.port().toLong()) |
| 137 | .put("direction", mep.direction().name()) |
| 138 | .put("mdName", mep.mdId().toString()) |
| 139 | .put("maName", mep.maId().toString()) |
| 140 | .put(ADMINISTRATIVE_STATE, mep.administrativeState()) |
| 141 | .put(CCI_ENABLED, mep.cciEnabled()); |
| 142 | if (mep.ccmLtmPriority() != null) { |
| 143 | result.put(CCM_LTM_PRIORITY, mep.ccmLtmPriority().ordinal()); |
| 144 | } |
| 145 | if (mep.primaryVid() != null) { |
| 146 | result.put(PRIMARY_VID, mep.primaryVid().toShort()); |
| 147 | } |
| 148 | if (mep.fngAddress() != null) { |
| 149 | result.put(FNG_ADDRESS, new FngAddressCodec().encode(mep.fngAddress(), context)); |
| 150 | } |
| 151 | if (mep.lowestFaultPriorityDefect() != null) { |
| 152 | result.put(LOWEST_FAULT_PRIORITY_DEFECT, mep.lowestFaultPriorityDefect().ordinal()); |
| 153 | } |
| 154 | if (mep.defectPresentTime() != null) { |
| 155 | result.put(DEFECT_PRESENT_TIME, mep.defectPresentTime().toString()); |
| 156 | } |
| 157 | if (mep.defectAbsentTime() != null) { |
| 158 | result.put(DEFECT_ABSENT_TIME, mep.defectAbsentTime().toString()); |
| 159 | } |
| 160 | |
| 161 | return result; |
| 162 | } |
| 163 | } |