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 java.time.Duration; |
| 19 | import java.time.OffsetDateTime; |
| 20 | |
| 21 | import org.onosproject.codec.CodecContext; |
| 22 | import org.onosproject.codec.JsonCodec; |
| 23 | import org.onosproject.incubator.net.l2monitoring.soam.StartTime; |
| 24 | |
| 25 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 26 | |
| 27 | /** |
| 28 | * Encode and decode to/from JSON to StartTime object. |
| 29 | */ |
| 30 | public class StartTimeCodec extends JsonCodec<StartTime> { |
| 31 | |
| 32 | @Override |
| 33 | public StartTime decode(ObjectNode json, CodecContext context) { |
| 34 | if (json == null || !json.isObject()) { |
| 35 | return null; |
| 36 | } |
| 37 | |
| 38 | if (json.get("immediate") != null) { |
| 39 | return StartTime.immediate(); |
| 40 | } else if (json.get("absolute") != null) { |
| 41 | if (json.get("absolute").get("start-time") != null) { |
| 42 | return StartTime.absolute(OffsetDateTime |
| 43 | .parse(json.get("absolute").get("start-time").asText()) |
| 44 | .toInstant()); |
| 45 | } |
| 46 | throw new IllegalArgumentException("StartTime absolute must contain " |
| 47 | + "a start-time in date-time format with offset"); |
| 48 | } else if (json.get("relative") != null) { |
| 49 | if (json.get("relative").get("start-time") != null) { |
| 50 | return StartTime.relative(Duration.parse(json.get("relative").get("start-time").asText())); |
| 51 | } |
| 52 | throw new IllegalArgumentException("StartTime relative must contain a start-time duration"); |
| 53 | } else { |
| 54 | throw new IllegalArgumentException("StartTime must be either immediate, absolute or relative"); |
| 55 | } |
| 56 | } |
| 57 | } |