Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 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.codec.impl; |
| 17 | |
| 18 | import org.onosproject.codec.CodecContext; |
| 19 | import org.onosproject.codec.JsonCodec; |
Seyeon Jeong | 9ceeacb | 2020-02-28 01:46:39 -0800 | [diff] [blame] | 20 | import org.onosproject.net.DeviceId; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 21 | import org.onosproject.net.HostLocation; |
| 22 | |
| 23 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Seyeon Jeong | 9ceeacb | 2020-02-28 01:46:39 -0800 | [diff] [blame] | 24 | import org.onosproject.net.PortNumber; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 25 | |
| 26 | import static com.google.common.base.Preconditions.checkNotNull; |
Seyeon Jeong | 9ceeacb | 2020-02-28 01:46:39 -0800 | [diff] [blame] | 27 | import static org.onlab.util.Tools.nullIsIllegal; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 28 | |
| 29 | /** |
Seyeon Jeong | 9ceeacb | 2020-02-28 01:46:39 -0800 | [diff] [blame] | 30 | * HostLocation JSON codec. |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 31 | */ |
Ray Milkey | 540b2ce | 2015-02-04 17:50:20 -0800 | [diff] [blame] | 32 | public final class HostLocationCodec extends JsonCodec<HostLocation> { |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 33 | |
Seyeon Jeong | 9ceeacb | 2020-02-28 01:46:39 -0800 | [diff] [blame] | 34 | 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 Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 40 | @Override |
| 41 | public ObjectNode encode(HostLocation hostLocation, CodecContext context) { |
| 42 | checkNotNull(hostLocation, "Host location cannot be null"); |
| 43 | return context.mapper().createObjectNode() |
Seyeon Jeong | 9ceeacb | 2020-02-28 01:46:39 -0800 | [diff] [blame] | 44 | .put(ELEMENT_ID, hostLocation.elementId().toString()) |
| 45 | .put(PORT, hostLocation.port().toString()); |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Seyeon Jeong | 9ceeacb | 2020-02-28 01:46:39 -0800 | [diff] [blame] | 48 | @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 Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 61 | } |