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.soam.web; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import static org.onlab.util.Tools.nullIsIllegal; |
| 20 | |
| 21 | import java.time.Duration; |
| 22 | |
| 23 | import org.onosproject.codec.CodecContext; |
| 24 | import org.onosproject.codec.JsonCodec; |
| 25 | import org.onosproject.incubator.net.l2monitoring.cfm.Mep.Priority; |
| 26 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId; |
| 27 | import org.onosproject.incubator.net.l2monitoring.soam.SoamConfigException; |
| 28 | import org.onosproject.incubator.net.l2monitoring.soam.StartTime; |
| 29 | import org.onosproject.incubator.net.l2monitoring.soam.StopTime; |
| 30 | import org.onosproject.incubator.net.l2monitoring.soam.delay.DefaultDelayMeasurementCreate; |
| 31 | import org.onosproject.incubator.net.l2monitoring.soam.delay.DelayMeasurementCreate; |
| 32 | import org.onosproject.incubator.net.l2monitoring.soam.delay.DelayMeasurementCreate.DmCreateBuilder; |
| 33 | import org.onosproject.incubator.net.l2monitoring.soam.delay.DelayMeasurementCreate.DmType; |
| 34 | import org.onosproject.incubator.net.l2monitoring.soam.delay.DelayMeasurementCreate.MeasurementOption; |
| 35 | import org.onosproject.incubator.net.l2monitoring.soam.delay.DelayMeasurementCreate.Version; |
| 36 | |
| 37 | import com.fasterxml.jackson.databind.JsonNode; |
| 38 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 39 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 40 | |
| 41 | /** |
| 42 | * Encode and decode to/from JSON to DelayMeasurementCreate object. |
| 43 | */ |
| 44 | public class DmCreateCodec extends JsonCodec<DelayMeasurementCreate> { |
| 45 | |
| 46 | private static final String VERSION = "version"; |
| 47 | private static final String DM = "dm"; |
| 48 | private static final String DM_CFG_TYPE = "dmCfgType"; |
| 49 | private static final String DMDMM = "DMDMM"; |
| 50 | private static final String REMOTE_MEP_ID = "remoteMepId"; |
| 51 | private static final String PRIORITY = "priority"; |
| 52 | private static final String MEASUREMENTS_ENABLED = "measurementsEnabled"; |
| 53 | private static final String BINS_PER_FD_INTERVAL = "binsPerFdInterval"; |
| 54 | private static final String BINS_PER_IFDV_INTERVAL = "binsPerIfdvInterval"; |
| 55 | private static final String IFDV_SELECTION_OFFSET = "ifdvSelectionOffset"; |
| 56 | private static final String BINS_PER_FDR_INTERVAL = "binsPerFdrInterval"; |
| 57 | private static final String FRAME_SIZE = "frameSize"; |
| 58 | private static final String MESSAGE_PERIOD_MS = "messagePeriodMs"; |
| 59 | private static final String MEASUREMENT_INTERVAL_MINS = "measurementIntervalMins"; |
| 60 | private static final String ALIGN_MEASUREMENT_INTERVALS = "alignMeasurementIntervals"; |
| 61 | private static final String ALIGN_MEASUREMENT_OFFSET_MINS = "alignMeasurementOffsetMins"; |
| 62 | private static final String START_TIME = "startTime"; |
| 63 | private static final String STOP_TIME = "stopTime"; |
| 64 | |
| 65 | @Override |
| 66 | public DelayMeasurementCreate decode(ObjectNode json, |
| 67 | CodecContext context) { |
| 68 | |
| 69 | if (json == null || !json.isObject()) { |
| 70 | return null; |
| 71 | } |
| 72 | |
| 73 | JsonNode dmNode = json.get(DM); |
| 74 | Version version = Version.Y17312011; |
| 75 | if (dmNode.get(VERSION) != null) { |
| 76 | version = Version.valueOf(dmNode.get(VERSION).asText()); |
| 77 | } |
| 78 | DmType dmCfgType = DmType.DMDMM; |
| 79 | if (dmNode.get(DM_CFG_TYPE) != null) { |
| 80 | dmCfgType = DmType.valueOf(dmNode.get(DM_CFG_TYPE).asText(DMDMM)); |
| 81 | } |
| 82 | MepId remoteMepId = MepId.valueOf( |
| 83 | nullIsIllegal(dmNode.get(REMOTE_MEP_ID), REMOTE_MEP_ID + " is required") |
| 84 | .shortValue()); |
| 85 | Priority prio = Priority.valueOf(nullIsIllegal(dmNode.get(PRIORITY), |
| 86 | PRIORITY + " is required in the format 'PRIOn'").asText()); |
| 87 | |
| 88 | try { |
| 89 | DmCreateBuilder builder = DefaultDelayMeasurementCreate |
| 90 | .builder(dmCfgType, version, remoteMepId, prio); |
| 91 | |
| 92 | if (dmNode.get(MEASUREMENTS_ENABLED) != null) { |
| 93 | context.codec(MeasurementOption.class) |
| 94 | .decode((ArrayNode) (dmNode.get(MEASUREMENTS_ENABLED)), context) |
| 95 | .forEach(builder::addToMeasurementsEnabled); |
| 96 | } |
| 97 | |
| 98 | if (dmNode.get(BINS_PER_FD_INTERVAL) != null) { |
| 99 | builder = builder.binsPerFdInterval( |
| 100 | (short) dmNode.get(BINS_PER_FD_INTERVAL).asInt()); |
| 101 | } |
| 102 | if (dmNode.get(BINS_PER_IFDV_INTERVAL) != null) { |
| 103 | builder = builder.binsPerIfdvInterval( |
| 104 | (short) dmNode.get(BINS_PER_IFDV_INTERVAL).asInt()); |
| 105 | } |
| 106 | if (dmNode.get(IFDV_SELECTION_OFFSET) != null) { |
| 107 | builder = builder.ifdvSelectionOffset( |
| 108 | (short) dmNode.get(IFDV_SELECTION_OFFSET).asInt()); |
| 109 | } |
| 110 | if (dmNode.get(BINS_PER_FDR_INTERVAL) != null) { |
| 111 | builder = builder.binsPerFdrInterval( |
| 112 | (short) dmNode.get(BINS_PER_FDR_INTERVAL).asInt()); |
| 113 | } |
| 114 | if (dmNode.get(FRAME_SIZE) != null) { |
| 115 | builder = (DmCreateBuilder) builder.frameSize( |
| 116 | (short) dmNode.get(FRAME_SIZE).asInt()); |
| 117 | } |
| 118 | if (dmNode.get(MESSAGE_PERIOD_MS) != null) { |
| 119 | builder = (DmCreateBuilder) builder.messagePeriod(Duration.ofMillis( |
| 120 | dmNode.get(MESSAGE_PERIOD_MS).asInt())); |
| 121 | } |
| 122 | if (dmNode.get(MEASUREMENT_INTERVAL_MINS) != null) { |
| 123 | builder = (DmCreateBuilder) builder.measurementInterval( |
| 124 | Duration.ofMinutes( |
| 125 | dmNode.get(MEASUREMENT_INTERVAL_MINS).asInt())); |
| 126 | } |
| 127 | if (dmNode.get(ALIGN_MEASUREMENT_INTERVALS) != null) { |
| 128 | builder = (DmCreateBuilder) builder.alignMeasurementIntervals( |
| 129 | dmNode.get(ALIGN_MEASUREMENT_INTERVALS).asBoolean()); |
| 130 | } |
| 131 | if (dmNode.get(ALIGN_MEASUREMENT_OFFSET_MINS) != null) { |
| 132 | builder = (DmCreateBuilder) builder.alignMeasurementOffset(Duration.ofMinutes( |
| 133 | dmNode.get(ALIGN_MEASUREMENT_OFFSET_MINS).asInt())); |
| 134 | } |
| 135 | if (dmNode.get(START_TIME) != null) { |
| 136 | builder = (DmCreateBuilder) builder.startTime(context.codec(StartTime.class) |
| 137 | .decode((ObjectNode) dmNode.get(START_TIME), context)); |
| 138 | } |
| 139 | if (dmNode.get(STOP_TIME) != null) { |
| 140 | builder = (DmCreateBuilder) builder.stopTime(context.codec(StopTime.class) |
| 141 | .decode((ObjectNode) dmNode.get(STOP_TIME), context)); |
| 142 | } |
| 143 | |
| 144 | return builder.build(); |
| 145 | } catch (SoamConfigException e) { |
| 146 | throw new IllegalArgumentException(e); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public ObjectNode encode(DelayMeasurementCreate dm, CodecContext context) { |
| 152 | checkNotNull(dm, "DM cannot be null"); |
| 153 | ObjectNode result = context.mapper().createObjectNode() |
| 154 | .put(DM_CFG_TYPE, dm.dmCfgType().name()) |
| 155 | .put(VERSION, dm.version().name()) |
| 156 | .put(REMOTE_MEP_ID, dm.remoteMepId().id()) |
| 157 | .put(PRIORITY, dm.priority().name()); |
| 158 | |
| 159 | if (dm.measurementsEnabled() != null) { |
| 160 | result.set(MEASUREMENTS_ENABLED, new DmMeasurementOptionCodec() |
| 161 | .encode(dm.measurementsEnabled(), context)); |
| 162 | } |
| 163 | |
| 164 | if (dm.messagePeriod() != null) { |
| 165 | result.put(MESSAGE_PERIOD_MS, dm.messagePeriod().toMillis()); |
| 166 | } |
| 167 | if (dm.frameSize() != null) { |
| 168 | result.put(FRAME_SIZE, dm.frameSize()); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | return result; |
| 173 | } |
| 174 | } |