Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [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 | |
| 17 | package org.onosproject.store.serializers; |
| 18 | |
| 19 | import com.esotericsoftware.kryo.Kryo; |
| 20 | import com.esotericsoftware.kryo.Serializer; |
| 21 | import com.esotericsoftware.kryo.io.Input; |
| 22 | import com.esotericsoftware.kryo.io.Output; |
| 23 | import org.onlab.osgi.DefaultServiceDirectory; |
Frank Wang | 72c5e43 | 2016-09-23 17:20:49 +0800 | [diff] [blame] | 24 | import org.onlab.util.ItemNotFoundException; |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 25 | import org.onosproject.net.DeviceId; |
alshabib | 880b644 | 2015-11-23 22:13:04 -0800 | [diff] [blame] | 26 | import org.onosproject.net.behaviour.ExtensionTreatmentResolver; |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 27 | import org.onosproject.net.driver.DefaultDriverData; |
| 28 | import org.onosproject.net.driver.DefaultDriverHandler; |
| 29 | import org.onosproject.net.driver.DriverHandler; |
| 30 | import org.onosproject.net.driver.DriverService; |
alshabib | 880b644 | 2015-11-23 22:13:04 -0800 | [diff] [blame] | 31 | import org.onosproject.net.flow.instructions.ExtensionTreatment; |
| 32 | import org.onosproject.net.flow.instructions.ExtensionTreatmentType; |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 33 | import org.onosproject.net.flow.instructions.Instructions; |
Frank Wang | 72c5e43 | 2016-09-23 17:20:49 +0800 | [diff] [blame] | 34 | import org.onosproject.net.flow.instructions.UnresolvedExtensionTreatment; |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 37 | * Serializer for extension instructions. |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 38 | */ |
| 39 | public class ExtensionInstructionSerializer extends |
| 40 | Serializer<Instructions.ExtensionInstructionWrapper> { |
| 41 | |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 42 | /** |
| 43 | * Constructs a extension instruction serializer. |
| 44 | */ |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 45 | public ExtensionInstructionSerializer() { |
| 46 | super(false, true); |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public void write(Kryo kryo, Output output, Instructions.ExtensionInstructionWrapper object) { |
| 51 | kryo.writeClassAndObject(output, object.extensionInstruction().type()); |
| 52 | kryo.writeClassAndObject(output, object.deviceId()); |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 53 | kryo.writeClassAndObject(output, object.extensionInstruction().serialize()); |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public Instructions.ExtensionInstructionWrapper read(Kryo kryo, Input input, |
| 58 | Class<Instructions.ExtensionInstructionWrapper> type) { |
alshabib | 880b644 | 2015-11-23 22:13:04 -0800 | [diff] [blame] | 59 | ExtensionTreatmentType exType = (ExtensionTreatmentType) kryo.readClassAndObject(input); |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 60 | DeviceId deviceId = (DeviceId) kryo.readClassAndObject(input); |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 61 | DriverService driverService = DefaultServiceDirectory.getService(DriverService.class); |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 62 | byte[] bytes = (byte[]) kryo.readClassAndObject(input); |
Frank Wang | 72c5e43 | 2016-09-23 17:20:49 +0800 | [diff] [blame] | 63 | ExtensionTreatment instruction; |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 64 | |
Frank Wang | 72c5e43 | 2016-09-23 17:20:49 +0800 | [diff] [blame] | 65 | try { |
| 66 | DriverHandler handler = new DefaultDriverHandler( |
| 67 | new DefaultDriverData(driverService.getDriver(deviceId), deviceId)); |
| 68 | ExtensionTreatmentResolver resolver = handler.behaviour(ExtensionTreatmentResolver.class); |
| 69 | instruction = resolver.getExtensionInstruction(exType); |
| 70 | instruction.deserialize(bytes); |
| 71 | } catch (ItemNotFoundException | IllegalArgumentException e) { |
| 72 | instruction = new UnresolvedExtensionTreatment(bytes, exType); |
| 73 | } |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 74 | |
| 75 | return Instructions.extension(instruction, deviceId); |
| 76 | } |
| 77 | } |