blob: daf6910fd64ef73ad2c00671249c93ddb39c57a4 [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;
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;
29import org.onosproject.net.driver.DriverHandler;
30import org.onosproject.net.driver.DriverService;
alshabib880b6442015-11-23 22:13:04 -080031import org.onosproject.net.flow.instructions.ExtensionTreatment;
32import org.onosproject.net.flow.instructions.ExtensionTreatmentType;
Jonathan Hart3c259162015-10-21 21:31:19 -070033import org.onosproject.net.flow.instructions.Instructions;
Frank Wang72c5e432016-09-23 17:20:49 +080034import org.onosproject.net.flow.instructions.UnresolvedExtensionTreatment;
Jonathan Hart3c259162015-10-21 21:31:19 -070035
36/**
Charles Chan14967c22015-12-07 11:11:50 -080037 * Serializer for extension instructions.
Jonathan Hart3c259162015-10-21 21:31:19 -070038 */
39public class ExtensionInstructionSerializer extends
40 Serializer<Instructions.ExtensionInstructionWrapper> {
41
Charles Chan14967c22015-12-07 11:11:50 -080042 /**
43 * Constructs a extension instruction serializer.
44 */
Jonathan Hart3c259162015-10-21 21:31:19 -070045 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 Hart3c259162015-10-21 21:31:19 -070053 kryo.writeClassAndObject(output, object.extensionInstruction().serialize());
Jonathan Hart3c259162015-10-21 21:31:19 -070054 }
55
56 @Override
57 public Instructions.ExtensionInstructionWrapper read(Kryo kryo, Input input,
58 Class<Instructions.ExtensionInstructionWrapper> type) {
alshabib880b6442015-11-23 22:13:04 -080059 ExtensionTreatmentType exType = (ExtensionTreatmentType) kryo.readClassAndObject(input);
Jonathan Hart3c259162015-10-21 21:31:19 -070060 DeviceId deviceId = (DeviceId) kryo.readClassAndObject(input);
Jonathan Hart3c259162015-10-21 21:31:19 -070061 DriverService driverService = DefaultServiceDirectory.getService(DriverService.class);
Jonathan Hart3c259162015-10-21 21:31:19 -070062 byte[] bytes = (byte[]) kryo.readClassAndObject(input);
Frank Wang72c5e432016-09-23 17:20:49 +080063 ExtensionTreatment instruction;
Jonathan Hart3c259162015-10-21 21:31:19 -070064
Frank Wang72c5e432016-09-23 17:20:49 +080065 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 Hart3c259162015-10-21 21:31:19 -070074
75 return Instructions.extension(instruction, deviceId);
76 }
77}