Jian Li | 68925b1 | 2019-02-24 23:01:03 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-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.driver.extensions.serializers; |
| 17 | |
| 18 | import com.esotericsoftware.kryo.Kryo; |
| 19 | import com.esotericsoftware.kryo.Serializer; |
| 20 | import com.esotericsoftware.kryo.io.Input; |
| 21 | import com.esotericsoftware.kryo.io.Output; |
| 22 | import org.onlab.packet.IpAddress; |
| 23 | import org.onosproject.driver.extensions.NiciraNat; |
| 24 | |
| 25 | /** |
| 26 | * Kryo serializer for {@link NiciraNat}. |
| 27 | */ |
| 28 | public class NiciraNatSerializer extends Serializer<NiciraNat> { |
| 29 | |
| 30 | /** |
| 31 | * Creates {@link NiciraNat} serializer instance. |
| 32 | */ |
| 33 | public NiciraNatSerializer() { |
| 34 | // non-null, immutable |
| 35 | super(false, true); |
| 36 | } |
| 37 | |
| 38 | @Override |
| 39 | public void write(Kryo kryo, Output output, NiciraNat object) { |
| 40 | output.writeInt(object.niciraNatFlags()); |
| 41 | output.writeInt(object.niciraNatPresentFlags()); |
| 42 | output.writeInt(object.niciraNatPortMin()); |
| 43 | output.writeInt(object.niciraNatPortMax()); |
| 44 | |
| 45 | output.writeBytes(object.niciraNatIpAddressMin().toOctets()); |
| 46 | output.writeBytes(object.niciraNatIpAddressMax().toOctets()); |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public NiciraNat read(Kryo kryo, Input input, Class<NiciraNat> type) { |
| 51 | int natFlags = input.readInt(); |
| 52 | int natPresentFlags = input.readInt(); |
| 53 | int natPortMin = input.readInt(); |
| 54 | int natPortMax = input.readInt(); |
| 55 | |
| 56 | // TODO: IPv6 address should also be supported |
| 57 | byte[] minOcts = new byte[4]; |
| 58 | byte[] maxOcts = new byte[4]; |
| 59 | |
| 60 | input.readBytes(minOcts); |
| 61 | input.readBytes(maxOcts); |
| 62 | |
| 63 | IpAddress natIpAddressMin = IpAddress.valueOf(IpAddress.Version.INET, minOcts); |
| 64 | IpAddress natIpAddressMax = IpAddress.valueOf(IpAddress.Version.INET, maxOcts); |
| 65 | |
| 66 | return new NiciraNat(natFlags, natPresentFlags, natPortMin, natPortMax, |
| 67 | natIpAddressMin, natIpAddressMax); |
| 68 | } |
| 69 | } |