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