blob: da4405dd0a94bcbff965f405e3d6077896af335b [file] [log] [blame]
Ray Milkey1f95bd32014-12-10 11:11:00 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Ray Milkey1f95bd32014-12-10 11:11:00 -08003 *
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.codec.impl;
17
18import org.onosproject.codec.CodecContext;
19import org.onosproject.codec.JsonCodec;
Seyeon Jeong9ceeacb2020-02-28 01:46:39 -080020import org.onosproject.net.DeviceId;
Ray Milkey1f95bd32014-12-10 11:11:00 -080021import org.onosproject.net.HostLocation;
22
23import com.fasterxml.jackson.databind.node.ObjectNode;
Seyeon Jeong9ceeacb2020-02-28 01:46:39 -080024import org.onosproject.net.PortNumber;
Ray Milkey1f95bd32014-12-10 11:11:00 -080025
26import static com.google.common.base.Preconditions.checkNotNull;
Seyeon Jeong9ceeacb2020-02-28 01:46:39 -080027import static org.onlab.util.Tools.nullIsIllegal;
Ray Milkey1f95bd32014-12-10 11:11:00 -080028
29/**
Seyeon Jeong9ceeacb2020-02-28 01:46:39 -080030 * HostLocation JSON codec.
Ray Milkey1f95bd32014-12-10 11:11:00 -080031 */
Ray Milkey540b2ce2015-02-04 17:50:20 -080032public final class HostLocationCodec extends JsonCodec<HostLocation> {
Ray Milkey1f95bd32014-12-10 11:11:00 -080033
Seyeon Jeong9ceeacb2020-02-28 01:46:39 -080034 public static final String ELEMENT_ID = "elementId";
35 public static final String PORT = "port";
36
37 private static final String MISSING_MEMBER_MESSAGE =
38 " member is required in HostLocation";
39
Ray Milkey1f95bd32014-12-10 11:11:00 -080040 @Override
41 public ObjectNode encode(HostLocation hostLocation, CodecContext context) {
42 checkNotNull(hostLocation, "Host location cannot be null");
43 return context.mapper().createObjectNode()
Seyeon Jeong9ceeacb2020-02-28 01:46:39 -080044 .put(ELEMENT_ID, hostLocation.elementId().toString())
45 .put(PORT, hostLocation.port().toString());
Ray Milkey1f95bd32014-12-10 11:11:00 -080046 }
47
Seyeon Jeong9ceeacb2020-02-28 01:46:39 -080048 @Override
49 public HostLocation decode(ObjectNode json, CodecContext context) {
50 if (json == null || !json.isObject()) {
51 return null;
52 }
53
54 DeviceId deviceId = DeviceId.deviceId(nullIsIllegal(
55 json.get(ELEMENT_ID), ELEMENT_ID + MISSING_MEMBER_MESSAGE).asText());
56 PortNumber portNumber = PortNumber.portNumber(nullIsIllegal(
57 json.get(PORT), PORT + MISSING_MEMBER_MESSAGE).asText());
58
59 return new HostLocation(deviceId, portNumber, 0);
60 }
Ray Milkey1f95bd32014-12-10 11:11:00 -080061}