Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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; |
jaegonkim | a9acf7a | 2018-03-16 00:12:59 +0900 | [diff] [blame] | 29 | import org.onosproject.net.driver.Driver; |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 30 | import org.onosproject.net.driver.DriverHandler; |
| 31 | import org.onosproject.net.driver.DriverService; |
alshabib | 880b644 | 2015-11-23 22:13:04 -0800 | [diff] [blame] | 32 | import org.onosproject.net.flow.instructions.ExtensionTreatment; |
| 33 | import org.onosproject.net.flow.instructions.ExtensionTreatmentType; |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 34 | import org.onosproject.net.flow.instructions.Instructions; |
Frank Wang | 72c5e43 | 2016-09-23 17:20:49 +0800 | [diff] [blame] | 35 | import org.onosproject.net.flow.instructions.UnresolvedExtensionTreatment; |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 38 | * Serializer for extension instructions. |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 39 | */ |
| 40 | public class ExtensionInstructionSerializer extends |
| 41 | Serializer<Instructions.ExtensionInstructionWrapper> { |
| 42 | |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 43 | /** |
| 44 | * Constructs a extension instruction serializer. |
| 45 | */ |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 46 | public ExtensionInstructionSerializer() { |
| 47 | super(false, true); |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public void write(Kryo kryo, Output output, Instructions.ExtensionInstructionWrapper object) { |
| 52 | kryo.writeClassAndObject(output, object.extensionInstruction().type()); |
| 53 | kryo.writeClassAndObject(output, object.deviceId()); |
jaegonkim | a9acf7a | 2018-03-16 00:12:59 +0900 | [diff] [blame] | 54 | DriverService driverService = DefaultServiceDirectory.getService(DriverService.class); |
| 55 | // It raises ItemNotFoundException if it failed to find driver |
| 56 | Driver driver = driverService.getDriver(object.deviceId()); |
| 57 | kryo.writeClassAndObject(output, driver.name()); |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 58 | kryo.writeClassAndObject(output, object.extensionInstruction().serialize()); |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public Instructions.ExtensionInstructionWrapper read(Kryo kryo, Input input, |
| 63 | Class<Instructions.ExtensionInstructionWrapper> type) { |
alshabib | 880b644 | 2015-11-23 22:13:04 -0800 | [diff] [blame] | 64 | ExtensionTreatmentType exType = (ExtensionTreatmentType) kryo.readClassAndObject(input); |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 65 | DeviceId deviceId = (DeviceId) kryo.readClassAndObject(input); |
jaegonkim | a9acf7a | 2018-03-16 00:12:59 +0900 | [diff] [blame] | 66 | String driverName = (String) kryo.readClassAndObject(input); |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 67 | DriverService driverService = DefaultServiceDirectory.getService(DriverService.class); |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 68 | byte[] bytes = (byte[]) kryo.readClassAndObject(input); |
Frank Wang | 72c5e43 | 2016-09-23 17:20:49 +0800 | [diff] [blame] | 69 | ExtensionTreatment instruction; |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 70 | |
Frank Wang | 72c5e43 | 2016-09-23 17:20:49 +0800 | [diff] [blame] | 71 | try { |
| 72 | DriverHandler handler = new DefaultDriverHandler( |
jaegonkim | a9acf7a | 2018-03-16 00:12:59 +0900 | [diff] [blame] | 73 | new DefaultDriverData(driverService.getDriver(driverName), deviceId)); |
Frank Wang | 72c5e43 | 2016-09-23 17:20:49 +0800 | [diff] [blame] | 74 | ExtensionTreatmentResolver resolver = handler.behaviour(ExtensionTreatmentResolver.class); |
| 75 | instruction = resolver.getExtensionInstruction(exType); |
| 76 | instruction.deserialize(bytes); |
| 77 | } catch (ItemNotFoundException | IllegalArgumentException e) { |
| 78 | instruction = new UnresolvedExtensionTreatment(bytes, exType); |
| 79 | } |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 80 | |
| 81 | return Instructions.extension(instruction, deviceId); |
| 82 | } |
| 83 | } |