blob: ef559776835a567725c768e349adc66d8f69b6d6 [file] [log] [blame]
Jonathan Hart3c259162015-10-21 21:31:19 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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;
24import org.onosproject.net.DeviceId;
alshabib880b6442015-11-23 22:13:04 -080025import org.onosproject.net.behaviour.ExtensionTreatmentResolver;
Jonathan Hart3c259162015-10-21 21:31:19 -070026import org.onosproject.net.driver.DefaultDriverData;
27import org.onosproject.net.driver.DefaultDriverHandler;
28import org.onosproject.net.driver.DriverHandler;
29import org.onosproject.net.driver.DriverService;
alshabib880b6442015-11-23 22:13:04 -080030import org.onosproject.net.flow.instructions.ExtensionTreatment;
31import org.onosproject.net.flow.instructions.ExtensionTreatmentType;
Jonathan Hart3c259162015-10-21 21:31:19 -070032import org.onosproject.net.flow.instructions.Instructions;
33
34/**
Charles Chan14967c22015-12-07 11:11:50 -080035 * Serializer for extension instructions.
Jonathan Hart3c259162015-10-21 21:31:19 -070036 */
37public class ExtensionInstructionSerializer extends
38 Serializer<Instructions.ExtensionInstructionWrapper> {
39
Charles Chan14967c22015-12-07 11:11:50 -080040 /**
41 * Constructs a extension instruction serializer.
42 */
Jonathan Hart3c259162015-10-21 21:31:19 -070043 public ExtensionInstructionSerializer() {
44 super(false, true);
45 }
46
47 @Override
48 public void write(Kryo kryo, Output output, Instructions.ExtensionInstructionWrapper object) {
49 kryo.writeClassAndObject(output, object.extensionInstruction().type());
50 kryo.writeClassAndObject(output, object.deviceId());
Jonathan Hart3c259162015-10-21 21:31:19 -070051 kryo.writeClassAndObject(output, object.extensionInstruction().serialize());
Jonathan Hart3c259162015-10-21 21:31:19 -070052 }
53
54 @Override
55 public Instructions.ExtensionInstructionWrapper read(Kryo kryo, Input input,
56 Class<Instructions.ExtensionInstructionWrapper> type) {
alshabib880b6442015-11-23 22:13:04 -080057 ExtensionTreatmentType exType = (ExtensionTreatmentType) kryo.readClassAndObject(input);
Jonathan Hart3c259162015-10-21 21:31:19 -070058 DeviceId deviceId = (DeviceId) kryo.readClassAndObject(input);
59
60 DriverService driverService = DefaultServiceDirectory.getService(DriverService.class);
61 DriverHandler handler = new DefaultDriverHandler(
62 new DefaultDriverData(driverService.getDriver(deviceId), deviceId));
63
alshabib880b6442015-11-23 22:13:04 -080064 ExtensionTreatmentResolver resolver = handler.behaviour(ExtensionTreatmentResolver.class);
alshabib880b6442015-11-23 22:13:04 -080065 ExtensionTreatment instruction = resolver.getExtensionInstruction(exType);
Jonathan Hart3c259162015-10-21 21:31:19 -070066
67 byte[] bytes = (byte[]) kryo.readClassAndObject(input);
68
69 instruction.deserialize(bytes);
70
71 return Instructions.extension(instruction, deviceId);
72 }
73}