blob: c6d2907fbd454c79339ed5ca061efeb1924820a4 [file] [log] [blame]
Jonathan Hart3c259162015-10-21 21:31:19 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Jonathan Hart3c259162015-10-21 21:31:19 -07003 *
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
17package org.onosproject.store.serializers;
18
19import com.esotericsoftware.kryo.Kryo;
20import com.esotericsoftware.kryo.Serializer;
21import com.esotericsoftware.kryo.io.Input;
22import com.esotericsoftware.kryo.io.Output;
23import org.onlab.osgi.DefaultServiceDirectory;
Frank Wang72c5e432016-09-23 17:20:49 +080024import org.onlab.util.ItemNotFoundException;
Jonathan Hart3c259162015-10-21 21:31:19 -070025import org.onosproject.net.DeviceId;
alshabib880b6442015-11-23 22:13:04 -080026import org.onosproject.net.behaviour.ExtensionTreatmentResolver;
Jonathan Hart3c259162015-10-21 21:31:19 -070027import org.onosproject.net.driver.DefaultDriverData;
28import org.onosproject.net.driver.DefaultDriverHandler;
jaegonkima9acf7a2018-03-16 00:12:59 +090029import org.onosproject.net.driver.Driver;
Jonathan Hart3c259162015-10-21 21:31:19 -070030import org.onosproject.net.driver.DriverHandler;
31import org.onosproject.net.driver.DriverService;
alshabib880b6442015-11-23 22:13:04 -080032import org.onosproject.net.flow.instructions.ExtensionTreatment;
33import org.onosproject.net.flow.instructions.ExtensionTreatmentType;
Jonathan Hart3c259162015-10-21 21:31:19 -070034import org.onosproject.net.flow.instructions.Instructions;
Frank Wang72c5e432016-09-23 17:20:49 +080035import org.onosproject.net.flow.instructions.UnresolvedExtensionTreatment;
Jonathan Hart3c259162015-10-21 21:31:19 -070036
37/**
Charles Chan14967c22015-12-07 11:11:50 -080038 * Serializer for extension instructions.
Jonathan Hart3c259162015-10-21 21:31:19 -070039 */
40public class ExtensionInstructionSerializer extends
41 Serializer<Instructions.ExtensionInstructionWrapper> {
42
Charles Chan14967c22015-12-07 11:11:50 -080043 /**
44 * Constructs a extension instruction serializer.
45 */
Jonathan Hart3c259162015-10-21 21:31:19 -070046 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());
jaegonkima9acf7a2018-03-16 00:12:59 +090054 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 Hart3c259162015-10-21 21:31:19 -070058 kryo.writeClassAndObject(output, object.extensionInstruction().serialize());
Jonathan Hart3c259162015-10-21 21:31:19 -070059 }
60
61 @Override
62 public Instructions.ExtensionInstructionWrapper read(Kryo kryo, Input input,
63 Class<Instructions.ExtensionInstructionWrapper> type) {
alshabib880b6442015-11-23 22:13:04 -080064 ExtensionTreatmentType exType = (ExtensionTreatmentType) kryo.readClassAndObject(input);
Jonathan Hart3c259162015-10-21 21:31:19 -070065 DeviceId deviceId = (DeviceId) kryo.readClassAndObject(input);
jaegonkima9acf7a2018-03-16 00:12:59 +090066 String driverName = (String) kryo.readClassAndObject(input);
Jonathan Hart3c259162015-10-21 21:31:19 -070067 DriverService driverService = DefaultServiceDirectory.getService(DriverService.class);
Jonathan Hart3c259162015-10-21 21:31:19 -070068 byte[] bytes = (byte[]) kryo.readClassAndObject(input);
Frank Wang72c5e432016-09-23 17:20:49 +080069 ExtensionTreatment instruction;
Jonathan Hart3c259162015-10-21 21:31:19 -070070
Frank Wang72c5e432016-09-23 17:20:49 +080071 try {
72 DriverHandler handler = new DefaultDriverHandler(
jaegonkima9acf7a2018-03-16 00:12:59 +090073 new DefaultDriverData(driverService.getDriver(driverName), deviceId));
Frank Wang72c5e432016-09-23 17:20:49 +080074 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 Hart3c259162015-10-21 21:31:19 -070080
81 return Instructions.extension(instruction, deviceId);
82 }
83}