blob: 4baaa80cf5a337d0a595a00997b440481cfa5188 [file] [log] [blame]
Jian Liab053212017-04-08 01:37:04 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jian Liab053212017-04-08 01:37:04 +09003 *
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.drivers.lisp.extensions.codec;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import org.onosproject.codec.CodecContext;
20import org.onosproject.codec.JsonCodec;
21import org.onosproject.drivers.lisp.extensions.LispGcAddress;
22import org.onosproject.mapping.addresses.MappingAddress;
23
24import static com.google.common.base.Preconditions.checkNotNull;
25import static org.onlab.util.Tools.nullIsIllegal;
26
27/**
28 * LISP geo coordinate address codec.
29 */
30public final class LispGcAddressCodec extends JsonCodec<LispGcAddress> {
31
Ray Milkey9c9cde42018-01-12 14:22:06 -080032 static final String NORTH = "north";
33 static final String LATITUDE_DEGREE = "latitudeDegree";
34 static final String LATITUDE_MINUTE = "latitudeMinute";
35 static final String LATITUDE_SECOND = "latitudeSecond";
36 static final String EAST = "east";
37 static final String LONGITUDE_DEGREE = "longitudeDegree";
38 static final String LONGITUDE_MINUTE = "longitudeMinute";
39 static final String LONGITUDE_SECOND = "longitudeSecond";
40 static final String ALTITUDE = "altitude";
41 static final String ADDRESS = "address";
Jian Liab053212017-04-08 01:37:04 +090042
43 private static final String MISSING_MEMBER_MESSAGE =
44 " member is required in LispGcAddress";
45
46 @Override
47 public ObjectNode encode(LispGcAddress address, CodecContext context) {
48 checkNotNull(address, "LispGcAddress cannot be null");
49
50 final ObjectNode result = context.mapper().createObjectNode()
51 .put(NORTH, address.isNorth())
52 .put(LATITUDE_DEGREE, address.getLatitudeDegree())
53 .put(LATITUDE_MINUTE, address.getLatitudeMinute())
54 .put(LATITUDE_SECOND, address.getLatitudeSecond())
55 .put(EAST, address.isEast())
56 .put(LONGITUDE_DEGREE, address.getLongitudeDegree())
57 .put(LONGITUDE_MINUTE, address.getLongitudeMinute())
58 .put(LONGITUDE_SECOND, address.getLongitudeSecond())
59 .put(ALTITUDE, address.getAltitude());
60
61 if (address.getAddress() != null) {
62 final JsonCodec<MappingAddress> addressCodec =
63 context.codec(MappingAddress.class);
64 ObjectNode addressNode = addressCodec.encode(address.getAddress(), context);
65 result.set(ADDRESS, addressNode);
66 }
67
68 return result;
69 }
70
71 @Override
72 public LispGcAddress decode(ObjectNode json, CodecContext context) {
73 if (json == null || !json.isObject()) {
74 return null;
75 }
76
77 boolean north = nullIsIllegal(json.get(NORTH),
78 NORTH + MISSING_MEMBER_MESSAGE).asBoolean();
79 short latitudeDegree = (short) nullIsIllegal(json.get(LATITUDE_DEGREE),
80 LATITUDE_DEGREE + MISSING_MEMBER_MESSAGE).asInt();
81 byte latitudeMinute = (byte) nullIsIllegal(json.get(LATITUDE_MINUTE),
82 LATITUDE_MINUTE + MISSING_MEMBER_MESSAGE).asInt();
83 byte latitudeSecond = (byte) nullIsIllegal(json.get(LATITUDE_SECOND),
84 LATITUDE_SECOND + MISSING_MEMBER_MESSAGE).asInt();
85 boolean east = nullIsIllegal(json.get(EAST),
86 EAST + MISSING_MEMBER_MESSAGE).asBoolean();
87 short longitudeDegree = (short) nullIsIllegal(json.get(LONGITUDE_DEGREE),
88 LONGITUDE_DEGREE + MISSING_MEMBER_MESSAGE).asInt();
89 byte longitudeMinute = (byte) nullIsIllegal(json.get(LONGITUDE_MINUTE),
90 LONGITUDE_MINUTE + MISSING_MEMBER_MESSAGE).asInt();
91 byte longitudeSecond = (byte) nullIsIllegal(json.get(LONGITUDE_SECOND),
92 LONGITUDE_SECOND + MISSING_MEMBER_MESSAGE).asInt();
93 int altitude = nullIsIllegal(json.get(ALTITUDE),
94 ALTITUDE + MISSING_MEMBER_MESSAGE).asInt();
95
96 ObjectNode addressJson = get(json, ADDRESS);
97 MappingAddress mappingAddress = null;
98
99 if (addressJson != null) {
100 final JsonCodec<MappingAddress> addressCodec =
101 context.codec(MappingAddress.class);
102 mappingAddress = addressCodec.decode(addressJson, context);
103 }
104
105 return new LispGcAddress.Builder()
106 .withIsNorth(north)
107 .withLatitudeDegree(latitudeDegree)
108 .withLatitudeMinute(latitudeMinute)
109 .withLatitudeSecond(latitudeSecond)
110 .withIsEast(east)
111 .withLongitudeDegree(longitudeDegree)
112 .withLongitudeMinute(longitudeMinute)
113 .withLongitudeSecond(longitudeSecond)
114 .withAltitude(altitude)
115 .withAddress(mappingAddress)
116 .build();
117 }
118}