From 13d05bc8458758ee39cb829098241e89616717ee Mon Sep 17 00:00:00 2001 From: Ashlee Young Date: Wed, 9 Sep 2015 22:15:21 -0700 Subject: ONOS checkin based on commit tag e796610b1f721d02f9b0e213cf6f7790c10ecd60 Change-Id: Ife8810491034fe7becdba75dda20de4267bd15cd --- framework/src/onos/core/store/serializers/pom.xml | 50 +++ .../store/serializers/AnnotationsSerializer.java | 32 ++ .../store/serializers/ArraysAsListSerializer.java | 49 +++ .../store/serializers/ConnectPointSerializer.java | 51 +++ .../DefaultApplicationIdSerializer.java | 49 +++ .../store/serializers/DefaultLinkSerializer.java | 61 +++ .../DefaultOutboundPacketSerializer.java | 58 +++ .../store/serializers/DefaultPortSerializer.java | 65 +++ .../store/serializers/DeviceIdSerializer.java | 48 +++ .../store/serializers/HostLocationSerializer.java | 55 +++ .../store/serializers/ImmutableListSerializer.java | 55 +++ .../store/serializers/ImmutableMapSerializer.java | 58 +++ .../store/serializers/ImmutableSetSerializer.java | 55 +++ .../store/serializers/Ip4AddressSerializer.java | 52 +++ .../store/serializers/Ip4PrefixSerializer.java | 57 +++ .../store/serializers/Ip6AddressSerializer.java | 52 +++ .../store/serializers/Ip6PrefixSerializer.java | 57 +++ .../store/serializers/IpAddressSerializer.java | 58 +++ .../store/serializers/IpPrefixSerializer.java | 64 +++ .../store/serializers/KryoNamespaces.java | 479 +++++++++++++++++++++ .../store/serializers/KryoSerializer.java | 86 ++++ .../store/serializers/LinkKeySerializer.java | 51 +++ .../store/serializers/MacAddressSerializer.java | 47 ++ .../serializers/MastershipTermSerializer.java | 51 +++ .../store/serializers/NodeIdSerializer.java | 48 +++ .../store/serializers/PortNumberSerializer.java | 56 +++ .../store/serializers/ProviderIdSerializer.java | 53 +++ .../store/serializers/StoreSerializer.java | 78 ++++ .../store/serializers/URISerializer.java | 46 ++ .../store/serializers/package-info.java | 20 + .../store/serializers/KryoSerializerTest.java | 470 ++++++++++++++++++++ 31 files changed, 2511 insertions(+) create mode 100644 framework/src/onos/core/store/serializers/pom.xml create mode 100644 framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/AnnotationsSerializer.java create mode 100644 framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/ArraysAsListSerializer.java create mode 100644 framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/ConnectPointSerializer.java create mode 100644 framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/DefaultApplicationIdSerializer.java create mode 100644 framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/DefaultLinkSerializer.java create mode 100644 framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/DefaultOutboundPacketSerializer.java create mode 100644 framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/DefaultPortSerializer.java create mode 100644 framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/DeviceIdSerializer.java create mode 100644 framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/HostLocationSerializer.java create mode 100644 framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/ImmutableListSerializer.java create mode 100644 framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/ImmutableMapSerializer.java create mode 100644 framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/ImmutableSetSerializer.java create mode 100644 framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/Ip4AddressSerializer.java create mode 100644 framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/Ip4PrefixSerializer.java create mode 100644 framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/Ip6AddressSerializer.java create mode 100644 framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/Ip6PrefixSerializer.java create mode 100644 framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/IpAddressSerializer.java create mode 100644 framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/IpPrefixSerializer.java create mode 100644 framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java create mode 100644 framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoSerializer.java create mode 100644 framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/LinkKeySerializer.java create mode 100644 framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/MacAddressSerializer.java create mode 100644 framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/MastershipTermSerializer.java create mode 100644 framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/NodeIdSerializer.java create mode 100644 framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/PortNumberSerializer.java create mode 100644 framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/ProviderIdSerializer.java create mode 100644 framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/StoreSerializer.java create mode 100644 framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/URISerializer.java create mode 100644 framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/package-info.java create mode 100644 framework/src/onos/core/store/serializers/src/test/java/org/onosproject/store/serializers/KryoSerializerTest.java (limited to 'framework/src/onos/core/store/serializers') diff --git a/framework/src/onos/core/store/serializers/pom.xml b/framework/src/onos/core/store/serializers/pom.xml new file mode 100644 index 00000000..d869f60c --- /dev/null +++ b/framework/src/onos/core/store/serializers/pom.xml @@ -0,0 +1,50 @@ + + + + 4.0.0 + + + org.onosproject + onos-core-store + 1.3.0-SNAPSHOT + ../pom.xml + + + onos-core-serializers + bundle + + Serializers for ONOS classes + + + + com.esotericsoftware + kryo + + + com.google.guava + guava-testlib + test + + + org.onosproject + onos-incubator-api + + + + diff --git a/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/AnnotationsSerializer.java b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/AnnotationsSerializer.java new file mode 100644 index 00000000..febb4adb --- /dev/null +++ b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/AnnotationsSerializer.java @@ -0,0 +1,32 @@ +package org.onosproject.store.serializers; + +import org.onosproject.net.DefaultAnnotations; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +import java.util.HashMap; + +public class AnnotationsSerializer extends Serializer { + + public AnnotationsSerializer() { + super(false, true); + } + + @Override + public void write(Kryo kryo, Output output, DefaultAnnotations object) { + kryo.writeObject(output, object.asMap()); + } + + @Override + public DefaultAnnotations read(Kryo kryo, Input input, Class type) { + DefaultAnnotations.Builder b = DefaultAnnotations.builder(); + HashMap map = kryo.readObject(input, HashMap.class); + map.forEach((k, v) -> b.set(k, v)); + + return b.build(); + } + +} diff --git a/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/ArraysAsListSerializer.java b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/ArraysAsListSerializer.java new file mode 100644 index 00000000..99c35310 --- /dev/null +++ b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/ArraysAsListSerializer.java @@ -0,0 +1,49 @@ +/* + * Copyright 2014 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onosproject.store.serializers; + +import java.util.ArrayList; +import java.util.List; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +/** + * Kryo Serializer for {@link java.util.Arrays#asList(Object...)}. + */ +public final class ArraysAsListSerializer extends Serializer> { + + @Override + public void write(Kryo kryo, Output output, List object) { + output.writeInt(object.size(), true); + for (Object elm : object) { + kryo.writeClassAndObject(output, elm); + } + } + + @Override + public List read(Kryo kryo, Input input, Class> type) { + final int size = input.readInt(true); + List list = new ArrayList<>(size); + for (int i = 0; i < size; ++i) { + list.add(kryo.readClassAndObject(input)); + } + return list; + } +} diff --git a/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/ConnectPointSerializer.java b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/ConnectPointSerializer.java new file mode 100644 index 00000000..f1027d5e --- /dev/null +++ b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/ConnectPointSerializer.java @@ -0,0 +1,51 @@ +/* + * Copyright 2014 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.store.serializers; + +import org.onosproject.net.ConnectPoint; +import org.onosproject.net.ElementId; +import org.onosproject.net.PortNumber; +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +/** + * Kryo Serializer for {@link ConnectPointSerializer}. + */ +public class ConnectPointSerializer extends Serializer { + + /** + * Creates {@link ConnectPointSerializer} serializer instance. + */ + public ConnectPointSerializer() { + // non-null, immutable + super(false, true); + } + + @Override + public void write(Kryo kryo, Output output, ConnectPoint object) { + kryo.writeClassAndObject(output, object.elementId()); + kryo.writeClassAndObject(output, object.port()); + } + + @Override + public ConnectPoint read(Kryo kryo, Input input, Class type) { + ElementId elementId = (ElementId) kryo.readClassAndObject(input); + PortNumber portNumber = (PortNumber) kryo.readClassAndObject(input); + return new ConnectPoint(elementId, portNumber); + } +} diff --git a/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/DefaultApplicationIdSerializer.java b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/DefaultApplicationIdSerializer.java new file mode 100644 index 00000000..0b1ad957 --- /dev/null +++ b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/DefaultApplicationIdSerializer.java @@ -0,0 +1,49 @@ +/* + * Copyright 2014 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.store.serializers; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; +import org.onosproject.core.DefaultApplicationId; + +/** + * Kryo Serializer for {@link org.onosproject.core.DefaultApplicationId}. + */ +public final class DefaultApplicationIdSerializer extends Serializer { + + /** + * Creates {@link org.onosproject.core.DefaultApplicationId} serializer instance. + */ + public DefaultApplicationIdSerializer() { + // non-null, immutable + super(false, true); + } + + @Override + public void write(Kryo kryo, Output output, DefaultApplicationId object) { + kryo.writeObject(output, object.id()); + kryo.writeObject(output, object.name()); + } + + @Override + public DefaultApplicationId read(Kryo kryo, Input input, Class type) { + short id = kryo.readObject(input, Short.class); + String name = kryo.readObject(input, String.class); + return new DefaultApplicationId(id, name); + } +} diff --git a/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/DefaultLinkSerializer.java b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/DefaultLinkSerializer.java new file mode 100644 index 00000000..6cc90667 --- /dev/null +++ b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/DefaultLinkSerializer.java @@ -0,0 +1,61 @@ +/* + * Copyright 2014 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.store.serializers; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; +import org.onosproject.net.ConnectPoint; +import org.onosproject.net.DefaultLink; +import org.onosproject.net.Link.State; +import org.onosproject.net.Link.Type; +import org.onosproject.net.provider.ProviderId; + +/** + * Kryo Serializer for {@link DefaultLink}. + */ +public class DefaultLinkSerializer extends Serializer { + + /** + * Creates {@link DefaultLink} serializer instance. + */ + public DefaultLinkSerializer() { + // non-null, immutable + super(false, true); + } + + @Override + public void write(Kryo kryo, Output output, DefaultLink object) { + kryo.writeClassAndObject(output, object.providerId()); + kryo.writeClassAndObject(output, object.src()); + kryo.writeClassAndObject(output, object.dst()); + kryo.writeClassAndObject(output, object.type()); + kryo.writeClassAndObject(output, object.state()); + output.writeBoolean(object.isDurable()); + } + + @Override + public DefaultLink read(Kryo kryo, Input input, Class type) { + ProviderId providerId = (ProviderId) kryo.readClassAndObject(input); + ConnectPoint src = (ConnectPoint) kryo.readClassAndObject(input); + ConnectPoint dst = (ConnectPoint) kryo.readClassAndObject(input); + Type linkType = (Type) kryo.readClassAndObject(input); + State state = (State) kryo.readClassAndObject(input); + boolean isDurable = input.readBoolean(); + return new DefaultLink(providerId, src, dst, linkType, state, isDurable); + } +} diff --git a/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/DefaultOutboundPacketSerializer.java b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/DefaultOutboundPacketSerializer.java new file mode 100644 index 00000000..9d12e458 --- /dev/null +++ b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/DefaultOutboundPacketSerializer.java @@ -0,0 +1,58 @@ +/* + * Copyright 2014 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.store.serializers; + +import java.nio.ByteBuffer; + +import org.onosproject.net.DeviceId; +import org.onosproject.net.flow.TrafficTreatment; +import org.onosproject.net.packet.DefaultOutboundPacket; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +/** + * Serializer for a default outbound packet. + */ +public class DefaultOutboundPacketSerializer extends Serializer { + + /** + * Creates {@link DefaultOutboundPacket} serializer instance. + */ + public DefaultOutboundPacketSerializer() { + // non-null, immutable + super(false, true); + } + + @Override + public DefaultOutboundPacket read(Kryo kryo, Input input, + Class type) { + DeviceId sendThrough = (DeviceId) kryo.readClassAndObject(input); + TrafficTreatment treatment = (TrafficTreatment) kryo.readClassAndObject(input); + byte[] data = (byte[]) kryo.readClassAndObject(input); + return new DefaultOutboundPacket(sendThrough, treatment, ByteBuffer.wrap(data)); + } + + @Override + public void write(Kryo kryo, Output output, DefaultOutboundPacket object) { + kryo.writeClassAndObject(output, object.sendThrough()); + kryo.writeClassAndObject(output, object.treatment()); + kryo.writeClassAndObject(output, object.data().array()); + } + +} diff --git a/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/DefaultPortSerializer.java b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/DefaultPortSerializer.java new file mode 100644 index 00000000..60d09fbe --- /dev/null +++ b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/DefaultPortSerializer.java @@ -0,0 +1,65 @@ +/* + * Copyright 2014 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.store.serializers; + +import org.onosproject.net.Annotations; +import org.onosproject.net.DefaultPort; +import org.onosproject.net.Element; +import org.onosproject.net.Port; +import org.onosproject.net.PortNumber; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +/** + * Kryo Serializer for {@link DefaultPort}. + */ +public final class DefaultPortSerializer extends + Serializer { + + /** + * Creates {@link DefaultPort} serializer instance. + */ + public DefaultPortSerializer() { + // non-null, immutable + super(false, true); + } + + @Override + public void write(Kryo kryo, Output output, DefaultPort object) { + kryo.writeClassAndObject(output, object.element()); + kryo.writeObject(output, object.number()); + output.writeBoolean(object.isEnabled()); + kryo.writeObject(output, object.type()); + output.writeLong(object.portSpeed()); + kryo.writeClassAndObject(output, object.annotations()); + } + + @Override + public DefaultPort read(Kryo kryo, Input input, Class aClass) { + Element element = (Element) kryo.readClassAndObject(input); + PortNumber number = kryo.readObject(input, PortNumber.class); + boolean isEnabled = input.readBoolean(); + Port.Type type = kryo.readObject(input, Port.Type.class); + long portSpeed = input.readLong(); + Annotations annotations = (Annotations) kryo.readClassAndObject(input); + + return new DefaultPort(element, number, isEnabled, type, portSpeed, annotations); + } + +} diff --git a/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/DeviceIdSerializer.java b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/DeviceIdSerializer.java new file mode 100644 index 00000000..a74a7695 --- /dev/null +++ b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/DeviceIdSerializer.java @@ -0,0 +1,48 @@ +/* + * Copyright 2014 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.store.serializers; + +import org.onosproject.net.DeviceId; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +/** +* Kryo Serializer for {@link DeviceId}. +*/ +public final class DeviceIdSerializer extends Serializer { + + /** + * Creates {@link DeviceId} serializer instance. + */ + public DeviceIdSerializer() { + // non-null, immutable + super(false, true); + } + + @Override + public void write(Kryo kryo, Output output, DeviceId object) { + output.writeString(object.toString()); + } + + @Override + public DeviceId read(Kryo kryo, Input input, Class type) { + final String str = input.readString(); + return DeviceId.deviceId(str); + } +} diff --git a/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/HostLocationSerializer.java b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/HostLocationSerializer.java new file mode 100644 index 00000000..270eb218 --- /dev/null +++ b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/HostLocationSerializer.java @@ -0,0 +1,55 @@ +/* + * Copyright 2014 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.store.serializers; + +import org.onosproject.net.DeviceId; +import org.onosproject.net.HostLocation; +import org.onosproject.net.PortNumber; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +/** +* Kryo Serializer for {@link HostLocation}. +*/ +public class HostLocationSerializer extends Serializer { + + /** + * Creates {@link HostLocation} serializer instance. + */ + public HostLocationSerializer() { + // non-null, immutable + super(false, true); + } + + @Override + public void write(Kryo kryo, Output output, HostLocation object) { + kryo.writeClassAndObject(output, object.deviceId()); + kryo.writeClassAndObject(output, object.port()); + output.writeLong(object.time()); + } + + @Override + public HostLocation read(Kryo kryo, Input input, Class type) { + DeviceId deviceId = (DeviceId) kryo.readClassAndObject(input); + PortNumber portNumber = (PortNumber) kryo.readClassAndObject(input); + long time = input.readLong(); + return new HostLocation(deviceId, portNumber, time); + } + +} diff --git a/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/ImmutableListSerializer.java b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/ImmutableListSerializer.java new file mode 100644 index 00000000..95166c3b --- /dev/null +++ b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/ImmutableListSerializer.java @@ -0,0 +1,55 @@ +/* + * Copyright 2014 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.store.serializers; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableList.Builder; + +/** + * Creates {@link ImmutableList} serializer instance. + */ +public class ImmutableListSerializer extends Serializer> { + + /** + * Creates {@link ImmutableList} serializer instance. + */ + public ImmutableListSerializer() { + // non-null, immutable + super(false, true); + } + @Override + public void write(Kryo kryo, Output output, ImmutableList object) { + output.writeInt(object.size()); + for (Object e : object) { + kryo.writeClassAndObject(output, e); + } + } + + @Override + public ImmutableList read(Kryo kryo, Input input, + Class> type) { + final int size = input.readInt(); + Builder builder = ImmutableList.builder(); + for (int i = 0; i < size; ++i) { + builder.add(kryo.readClassAndObject(input)); + } + return builder.build(); + } +} diff --git a/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/ImmutableMapSerializer.java b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/ImmutableMapSerializer.java new file mode 100644 index 00000000..4d6af788 --- /dev/null +++ b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/ImmutableMapSerializer.java @@ -0,0 +1,58 @@ +/* + * Copyright 2014 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.store.serializers; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; +import com.esotericsoftware.kryo.serializers.MapSerializer; +import com.google.common.collect.ImmutableMap; + +/** +* Kryo Serializer for {@link ImmutableMap}. +*/ +public class ImmutableMapSerializer extends Serializer> { + + private final MapSerializer mapSerializer = new MapSerializer(); + + /** + * Creates {@link ImmutableMap} serializer instance. + */ + public ImmutableMapSerializer() { + // non-null, immutable + super(false, true); + } + + @Override + public void write(Kryo kryo, Output output, ImmutableMap object) { + // wrapping with unmodifiableMap proxy + // to avoid Kryo from writing only the reference marker of this instance, + // which will be embedded right before this method call. + kryo.writeObject(output, Collections.unmodifiableMap(object), mapSerializer); + } + + @Override + public ImmutableMap read(Kryo kryo, Input input, + Class> type) { + Map map = kryo.readObject(input, HashMap.class, mapSerializer); + return ImmutableMap.copyOf(map); + } +} diff --git a/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/ImmutableSetSerializer.java b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/ImmutableSetSerializer.java new file mode 100644 index 00000000..cb9b0543 --- /dev/null +++ b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/ImmutableSetSerializer.java @@ -0,0 +1,55 @@ +/* + * Copyright 2014 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.store.serializers; + +import java.util.ArrayList; +import java.util.List; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; +import com.esotericsoftware.kryo.serializers.CollectionSerializer; +import com.google.common.collect.ImmutableSet; + +/** +* Kryo Serializer for {@link ImmutableSet}. +*/ +public class ImmutableSetSerializer extends Serializer> { + + private final CollectionSerializer serializer = new CollectionSerializer(); + + /** + * Creates {@link ImmutableSet} serializer instance. + */ + public ImmutableSetSerializer() { + // non-null, immutable + super(false, true); + serializer.setElementsCanBeNull(false); + } + + @Override + public void write(Kryo kryo, Output output, ImmutableSet object) { + kryo.writeObject(output, object.asList(), serializer); + } + + @Override + public ImmutableSet read(Kryo kryo, Input input, + Class> type) { + List elms = kryo.readObject(input, ArrayList.class, serializer); + return ImmutableSet.copyOf(elms); + } +} diff --git a/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/Ip4AddressSerializer.java b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/Ip4AddressSerializer.java new file mode 100644 index 00000000..2370ad92 --- /dev/null +++ b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/Ip4AddressSerializer.java @@ -0,0 +1,52 @@ +/* + * Copyright 2014 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.store.serializers; + +import org.onlab.packet.Ip4Address; +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +/** + * Kryo Serializer for {@link Ip4Address}. + */ +public class Ip4AddressSerializer extends Serializer { + + /** + * Creates {@link Ip4Address} serializer instance. + */ + public Ip4AddressSerializer() { + // non-null, immutable + super(false, true); + } + + @Override + public void write(Kryo kryo, Output output, Ip4Address object) { + byte[] octs = object.toOctets(); + // It is always Ip4Address.BYTE_LENGTH + output.writeInt(octs.length); + output.writeBytes(octs); + } + + @Override + public Ip4Address read(Kryo kryo, Input input, Class type) { + final int octLen = input.readInt(); + byte[] octs = new byte[octLen]; + input.readBytes(octs); + return Ip4Address.valueOf(octs); + } +} diff --git a/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/Ip4PrefixSerializer.java b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/Ip4PrefixSerializer.java new file mode 100644 index 00000000..029ffb1a --- /dev/null +++ b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/Ip4PrefixSerializer.java @@ -0,0 +1,57 @@ +/* + * Copyright 2014 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.store.serializers; + +import org.onlab.packet.Ip4Prefix; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +/** + * Kryo Serializer for {@link Ip4Prefix}. + */ +public final class Ip4PrefixSerializer extends Serializer { + + /** + * Creates {@link Ip4Prefix} serializer instance. + */ + public Ip4PrefixSerializer() { + // non-null, immutable + super(false, true); + } + + @Override + public void write(Kryo kryo, Output output, + Ip4Prefix object) { + byte[] octs = object.address().toOctets(); + // It is always Ip4Address.BYTE_LENGTH + output.writeInt(octs.length); + output.writeBytes(octs); + output.writeInt(object.prefixLength()); + } + + @Override + public Ip4Prefix read(Kryo kryo, Input input, + Class type) { + int octLen = input.readInt(); + byte[] octs = new byte[octLen]; + input.readBytes(octs); + int prefLen = input.readInt(); + return Ip4Prefix.valueOf(octs, prefLen); + } +} diff --git a/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/Ip6AddressSerializer.java b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/Ip6AddressSerializer.java new file mode 100644 index 00000000..f8101f98 --- /dev/null +++ b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/Ip6AddressSerializer.java @@ -0,0 +1,52 @@ +/* + * Copyright 2014 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.store.serializers; + +import org.onlab.packet.Ip6Address; +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +/** + * Kryo Serializer for {@link Ip6Address}. + */ +public class Ip6AddressSerializer extends Serializer { + + /** + * Creates {@link Ip6Address} serializer instance. + */ + public Ip6AddressSerializer() { + // non-null, immutable + super(false, true); + } + + @Override + public void write(Kryo kryo, Output output, Ip6Address object) { + byte[] octs = object.toOctets(); + // It is always Ip6Address.BYTE_LENGTH + output.writeInt(octs.length); + output.writeBytes(octs); + } + + @Override + public Ip6Address read(Kryo kryo, Input input, Class type) { + final int octLen = input.readInt(); + byte[] octs = new byte[octLen]; + input.readBytes(octs); + return Ip6Address.valueOf(octs); + } +} diff --git a/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/Ip6PrefixSerializer.java b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/Ip6PrefixSerializer.java new file mode 100644 index 00000000..91ba5a29 --- /dev/null +++ b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/Ip6PrefixSerializer.java @@ -0,0 +1,57 @@ +/* + * Copyright 2014 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.store.serializers; + +import org.onlab.packet.Ip6Prefix; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +/** + * Kryo Serializer for {@link Ip6Prefix}. + */ +public final class Ip6PrefixSerializer extends Serializer { + + /** + * Creates {@link Ip6Prefix} serializer instance. + */ + public Ip6PrefixSerializer() { + // non-null, immutable + super(false, true); + } + + @Override + public void write(Kryo kryo, Output output, + Ip6Prefix object) { + byte[] octs = object.address().toOctets(); + // It is always Ip6Address.BYTE_LENGTH + output.writeInt(octs.length); + output.writeBytes(octs); + output.writeInt(object.prefixLength()); + } + + @Override + public Ip6Prefix read(Kryo kryo, Input input, + Class type) { + int octLen = input.readInt(); + byte[] octs = new byte[octLen]; + input.readBytes(octs); + int prefLen = input.readInt(); + return Ip6Prefix.valueOf(octs, prefLen); + } +} diff --git a/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/IpAddressSerializer.java b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/IpAddressSerializer.java new file mode 100644 index 00000000..cb85fec8 --- /dev/null +++ b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/IpAddressSerializer.java @@ -0,0 +1,58 @@ +/* + * Copyright 2014 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.store.serializers; + +import org.onlab.packet.IpAddress; +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +/** + * Kryo Serializer for {@link IpAddress}. + */ +public class IpAddressSerializer extends Serializer { + + /** + * Creates {@link IpAddress} serializer instance. + */ + public IpAddressSerializer() { + // non-null, immutable + super(false, true); + } + + @Override + public void write(Kryo kryo, Output output, IpAddress object) { + byte[] octs = object.toOctets(); + output.writeInt(octs.length); + output.writeBytes(octs); + } + + @Override + public IpAddress read(Kryo kryo, Input input, Class type) { + final int octLen = input.readInt(); + byte[] octs = new byte[octLen]; + input.readBytes(octs); + // Use the address size to decide whether it is IPv4 or IPv6 address + if (octLen == IpAddress.INET_BYTE_LENGTH) { + return IpAddress.valueOf(IpAddress.Version.INET, octs); + } + if (octLen == IpAddress.INET6_BYTE_LENGTH) { + return IpAddress.valueOf(IpAddress.Version.INET6, octs); + } + return null; // Shouldn't be reached + } +} diff --git a/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/IpPrefixSerializer.java b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/IpPrefixSerializer.java new file mode 100644 index 00000000..cf0a20b9 --- /dev/null +++ b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/IpPrefixSerializer.java @@ -0,0 +1,64 @@ +/* + * Copyright 2014 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.store.serializers; + +import org.onlab.packet.IpAddress; +import org.onlab.packet.IpPrefix; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +/** + * Kryo Serializer for {@link IpPrefix}. + */ +public final class IpPrefixSerializer extends Serializer { + + /** + * Creates {@link IpPrefix} serializer instance. + */ + public IpPrefixSerializer() { + // non-null, immutable + super(false, true); + } + + @Override + public void write(Kryo kryo, Output output, + IpPrefix object) { + byte[] octs = object.address().toOctets(); + output.writeInt(octs.length); + output.writeBytes(octs); + output.writeInt(object.prefixLength()); + } + + @Override + public IpPrefix read(Kryo kryo, Input input, + Class type) { + int octLen = input.readInt(); + byte[] octs = new byte[octLen]; + input.readBytes(octs); + int prefLen = input.readInt(); + // Use the address size to decide whether it is IPv4 or IPv6 address + if (octLen == IpAddress.INET_BYTE_LENGTH) { + return IpPrefix.valueOf(IpAddress.Version.INET, octs, prefLen); + } + if (octLen == IpAddress.INET6_BYTE_LENGTH) { + return IpPrefix.valueOf(IpAddress.Version.INET6, octs, prefLen); + } + return null; // Shouldn't be reached + } +} diff --git a/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java new file mode 100644 index 00000000..66ee7be7 --- /dev/null +++ b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java @@ -0,0 +1,479 @@ +/* + * Copyright 2014-2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.store.serializers; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Maps; + +import org.onlab.packet.ChassisId; +import org.onlab.packet.EthType; +import org.onlab.packet.Ip4Address; +import org.onlab.packet.Ip4Prefix; +import org.onlab.packet.Ip6Address; +import org.onlab.packet.Ip6Prefix; +import org.onlab.packet.IpAddress; +import org.onlab.packet.IpPrefix; +import org.onlab.packet.MacAddress; +import org.onlab.packet.TpPort; +import org.onlab.packet.VlanId; +import org.onlab.util.Bandwidth; +import org.onlab.util.Frequency; +import org.onlab.util.KryoNamespace; +import org.onosproject.app.ApplicationState; +import org.onosproject.cluster.ControllerNode; +import org.onosproject.cluster.DefaultControllerNode; +import org.onosproject.cluster.Leadership; +import org.onosproject.cluster.LeadershipEvent; +import org.onosproject.cluster.NodeId; +import org.onosproject.cluster.RoleInfo; +import org.onosproject.core.ApplicationRole; +import org.onosproject.core.DefaultApplication; +import org.onosproject.core.DefaultApplicationId; +import org.onosproject.core.DefaultGroupId; +import org.onosproject.core.Version; +import org.onosproject.incubator.net.domain.IntentDomainId; +import org.onosproject.mastership.MastershipTerm; +import org.onosproject.net.Annotations; +import org.onosproject.net.ChannelSpacing; +import org.onosproject.net.ConnectPoint; +import org.onosproject.net.DefaultAnnotations; +import org.onosproject.net.DefaultDevice; +import org.onosproject.net.DefaultEdgeLink; +import org.onosproject.net.DefaultHost; +import org.onosproject.net.DefaultLink; +import org.onosproject.net.DefaultPath; +import org.onosproject.net.DefaultPort; +import org.onosproject.net.Device; +import org.onosproject.net.DeviceId; +import org.onosproject.net.Element; +import org.onosproject.net.GridType; +import org.onosproject.net.HostId; +import org.onosproject.net.HostLocation; +import org.onosproject.net.IndexedLambda; +import org.onosproject.net.Link; +import org.onosproject.net.LinkKey; +import org.onosproject.net.OchPort; +import org.onosproject.net.OchSignal; +import org.onosproject.net.OchSignalType; +import org.onosproject.net.OduCltPort; +import org.onosproject.net.OduSignalType; +import org.onosproject.net.OmsPort; +import org.onosproject.net.Port; +import org.onosproject.net.PortNumber; +import org.onosproject.net.device.DefaultDeviceDescription; +import org.onosproject.net.device.DefaultPortDescription; +import org.onosproject.net.device.DefaultPortStatistics; +import org.onosproject.net.device.OchPortDescription; +import org.onosproject.net.device.OduCltPortDescription; +import org.onosproject.net.device.OmsPortDescription; +import org.onosproject.net.device.PortStatistics; +import org.onosproject.net.flow.CompletedBatchOperation; +import org.onosproject.net.flow.DefaultFlowEntry; +import org.onosproject.net.flow.DefaultFlowRule; +import org.onosproject.net.flow.DefaultTrafficSelector; +import org.onosproject.net.flow.DefaultTrafficTreatment; +import org.onosproject.net.flow.FlowEntry; +import org.onosproject.net.flow.FlowId; +import org.onosproject.net.flow.FlowRule; +import org.onosproject.net.flow.FlowRuleBatchEntry; +import org.onosproject.net.flow.FlowRuleBatchEvent; +import org.onosproject.net.flow.FlowRuleBatchOperation; +import org.onosproject.net.flow.FlowRuleBatchRequest; +import org.onosproject.net.flow.FlowRuleEvent; +import org.onosproject.net.flow.FlowRuleExtPayLoad; +import org.onosproject.net.flow.StoredFlowEntry; +import org.onosproject.net.flow.criteria.Criterion; +import org.onosproject.net.flow.criteria.EthCriterion; +import org.onosproject.net.flow.criteria.EthTypeCriterion; +import org.onosproject.net.flow.criteria.IPCriterion; +import org.onosproject.net.flow.criteria.IPDscpCriterion; +import org.onosproject.net.flow.criteria.IPEcnCriterion; +import org.onosproject.net.flow.criteria.IPProtocolCriterion; +import org.onosproject.net.flow.criteria.IPv6ExthdrFlagsCriterion; +import org.onosproject.net.flow.criteria.IPv6FlowLabelCriterion; +import org.onosproject.net.flow.criteria.IPv6NDLinkLayerAddressCriterion; +import org.onosproject.net.flow.criteria.IPv6NDTargetAddressCriterion; +import org.onosproject.net.flow.criteria.IcmpCodeCriterion; +import org.onosproject.net.flow.criteria.IcmpTypeCriterion; +import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion; +import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion; +import org.onosproject.net.flow.criteria.IndexedLambdaCriterion; +import org.onosproject.net.flow.criteria.LambdaCriterion; +import org.onosproject.net.flow.criteria.MetadataCriterion; +import org.onosproject.net.flow.criteria.MplsCriterion; +import org.onosproject.net.flow.criteria.OchSignalCriterion; +import org.onosproject.net.flow.criteria.OchSignalTypeCriterion; +import org.onosproject.net.flow.criteria.OpticalSignalTypeCriterion; +import org.onosproject.net.flow.criteria.PortCriterion; +import org.onosproject.net.flow.criteria.SctpPortCriterion; +import org.onosproject.net.flow.criteria.TcpPortCriterion; +import org.onosproject.net.flow.criteria.TunnelIdCriterion; +import org.onosproject.net.flow.criteria.UdpPortCriterion; +import org.onosproject.net.flow.criteria.VlanIdCriterion; +import org.onosproject.net.flow.criteria.VlanPcpCriterion; +import org.onosproject.net.flow.instructions.Instructions; +import org.onosproject.net.flow.instructions.L0ModificationInstruction; +import org.onosproject.net.flow.instructions.L2ModificationInstruction; +import org.onosproject.net.flow.instructions.L3ModificationInstruction; +import org.onosproject.net.flow.instructions.L4ModificationInstruction; +import org.onosproject.net.host.DefaultHostDescription; +import org.onosproject.net.host.HostDescription; +import org.onosproject.net.intent.ConnectivityIntent; +import org.onosproject.net.intent.FlowRuleIntent; +import org.onosproject.net.intent.HostToHostIntent; +import org.onosproject.net.intent.Intent; +import org.onosproject.net.intent.IntentId; +import org.onosproject.net.intent.IntentOperation; +import org.onosproject.net.intent.IntentState; +import org.onosproject.net.intent.Key; +import org.onosproject.net.intent.LinkCollectionIntent; +import org.onosproject.net.intent.MplsIntent; +import org.onosproject.net.intent.MplsPathIntent; +import org.onosproject.net.intent.MultiPointToSinglePointIntent; +import org.onosproject.net.intent.OpticalCircuitIntent; +import org.onosproject.net.intent.OpticalConnectivityIntent; +import org.onosproject.net.intent.OpticalPathIntent; +import org.onosproject.net.intent.PathIntent; +import org.onosproject.net.intent.PointToPointIntent; +import org.onosproject.net.intent.SinglePointToMultiPointIntent; +import org.onosproject.net.intent.constraint.AnnotationConstraint; +import org.onosproject.net.intent.constraint.BandwidthConstraint; +import org.onosproject.net.intent.constraint.BooleanConstraint; +import org.onosproject.net.intent.constraint.LambdaConstraint; +import org.onosproject.net.intent.constraint.LatencyConstraint; +import org.onosproject.net.intent.constraint.LinkTypeConstraint; +import org.onosproject.net.intent.constraint.ObstacleConstraint; +import org.onosproject.net.intent.constraint.PartialFailureConstraint; +import org.onosproject.net.intent.constraint.WaypointConstraint; +import org.onosproject.net.link.DefaultLinkDescription; +import org.onosproject.net.meter.MeterId; +import org.onosproject.net.newresource.ResourceAllocation; +import org.onosproject.net.newresource.ResourcePath; +import org.onosproject.net.packet.DefaultOutboundPacket; +import org.onosproject.net.packet.DefaultPacketRequest; +import org.onosproject.net.packet.PacketPriority; +import org.onosproject.net.provider.ProviderId; +import org.onosproject.net.resource.link.BandwidthResource; +import org.onosproject.net.resource.link.BandwidthResourceAllocation; +import org.onosproject.net.resource.link.BandwidthResourceRequest; +import org.onosproject.net.resource.link.DefaultLinkResourceAllocations; +import org.onosproject.net.resource.link.DefaultLinkResourceRequest; +import org.onosproject.net.resource.link.LambdaResource; +import org.onosproject.net.resource.link.LambdaResourceAllocation; +import org.onosproject.net.resource.link.LambdaResourceRequest; +import org.onosproject.net.resource.link.LinkResourceRequest; +import org.onosproject.net.resource.link.MplsLabel; +import org.onosproject.net.resource.link.MplsLabelResourceAllocation; +import org.onosproject.net.resource.link.MplsLabelResourceRequest; +import org.onosproject.store.Timestamp; +import org.onosproject.store.service.MapEvent; +import org.onosproject.store.service.SetEvent; +import org.onosproject.store.service.Versioned; + +import java.net.URI; +import java.time.Duration; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CopyOnWriteArraySet; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; + +public final class KryoNamespaces { + + public static final KryoNamespace BASIC = KryoNamespace.newBuilder() + .nextId(KryoNamespace.FLOATING_ID) + .register(byte[].class) + .register(AtomicBoolean.class) + .register(AtomicInteger.class) + .register(AtomicLong.class) + .register(new ImmutableListSerializer(), + ImmutableList.class, + ImmutableList.of(1).getClass(), + ImmutableList.of(1, 2).getClass()) + .register(new ImmutableSetSerializer(), + ImmutableSet.class, + ImmutableSet.of().getClass(), + ImmutableSet.of(1).getClass(), + ImmutableSet.of(1, 2).getClass()) + .register(new ImmutableMapSerializer(), + ImmutableMap.class, + ImmutableMap.of().getClass(), + ImmutableMap.of("a", 1).getClass(), + ImmutableMap.of("R", 2, "D", 2).getClass()) + .register(HashMap.class) + .register(ConcurrentHashMap.class) + .register(CopyOnWriteArraySet.class) + .register(ArrayList.class, + LinkedList.class, + HashSet.class + ) + .register(Maps.immutableEntry("a", "b").getClass()) + .register(new ArraysAsListSerializer(), Arrays.asList().getClass()) + .register(Collections.singletonList(1).getClass()) + .register(Duration.class) + .register(Collections.emptySet().getClass()) + .register(Optional.class) + .register(Collections.emptyList().getClass()) + .register(Collections.unmodifiableSet(Collections.emptySet()).getClass()) + .register(Collections.singleton(Object.class).getClass()) + .build(); + + /** + * KryoNamespace which can serialize ON.lab misc classes. + */ + public static final KryoNamespace MISC = KryoNamespace.newBuilder() + .nextId(KryoNamespace.FLOATING_ID) + .register(new IpPrefixSerializer(), IpPrefix.class) + .register(new Ip4PrefixSerializer(), Ip4Prefix.class) + .register(new Ip6PrefixSerializer(), Ip6Prefix.class) + .register(new IpAddressSerializer(), IpAddress.class) + .register(new Ip4AddressSerializer(), Ip4Address.class) + .register(new Ip6AddressSerializer(), Ip6Address.class) + .register(new MacAddressSerializer(), MacAddress.class) + .register(VlanId.class) + .register(Frequency.class) + .register(Bandwidth.class) + .build(); + + /** + * Kryo registration Id for user custom registration. + */ + public static final int BEGIN_USER_CUSTOM_ID = 300; + + // TODO: Populate other classes + /** + * KryoNamespace which can serialize API bundle classes. + */ + public static final KryoNamespace API = KryoNamespace.newBuilder() + .nextId(KryoNamespace.INITIAL_ID) + .register(BASIC) + .nextId(KryoNamespace.INITIAL_ID + 30) + .register(MISC) + .nextId(KryoNamespace.INITIAL_ID + 30 + 10) + .register( + Instructions.MeterInstruction.class, + MeterId.class, + Version.class, + ControllerNode.State.class, + ApplicationState.class, + ApplicationRole.class, + DefaultApplication.class, + Device.Type.class, + Port.Type.class, + ChassisId.class, + DefaultControllerNode.class, + DefaultDevice.class, + DefaultDeviceDescription.class, + DefaultHost.class, + DefaultLinkDescription.class, + Port.class, + DefaultPortDescription.class, + Element.class, + Link.Type.class, + Link.State.class, + Timestamp.class, + Leadership.class, + LeadershipEvent.class, + LeadershipEvent.Type.class, + HostId.class, + HostDescription.class, + DefaultHostDescription.class, + DefaultFlowEntry.class, + StoredFlowEntry.class, + FlowRule.Type.class, + DefaultFlowRule.class, + DefaultFlowEntry.class, + DefaultPacketRequest.class, + PacketPriority.class, + FlowEntry.FlowEntryState.class, + FlowId.class, + DefaultTrafficSelector.class, + PortCriterion.class, + MetadataCriterion.class, + EthCriterion.class, + EthType.class, + EthTypeCriterion.class, + VlanIdCriterion.class, + VlanPcpCriterion.class, + IPDscpCriterion.class, + IPEcnCriterion.class, + IPProtocolCriterion.class, + IPCriterion.class, + TpPort.class, + TcpPortCriterion.class, + UdpPortCriterion.class, + SctpPortCriterion.class, + IcmpTypeCriterion.class, + IcmpCodeCriterion.class, + IPv6FlowLabelCriterion.class, + Icmpv6TypeCriterion.class, + Icmpv6CodeCriterion.class, + IPv6NDTargetAddressCriterion.class, + IPv6NDLinkLayerAddressCriterion.class, + MplsCriterion.class, + TunnelIdCriterion.class, + IPv6ExthdrFlagsCriterion.class, + LambdaCriterion.class, + IndexedLambdaCriterion.class, + OchSignalCriterion.class, + OchSignalTypeCriterion.class, + OpticalSignalTypeCriterion.class, + Criterion.class, + Criterion.Type.class, + DefaultTrafficTreatment.class, + Instructions.DropInstruction.class, + Instructions.OutputInstruction.class, + Instructions.GroupInstruction.class, + Instructions.TableTypeTransition.class, + L0ModificationInstruction.class, + L0ModificationInstruction.L0SubType.class, + L0ModificationInstruction.ModLambdaInstruction.class, + L0ModificationInstruction.ModOchSignalInstruction.class, + L2ModificationInstruction.class, + L2ModificationInstruction.L2SubType.class, + L2ModificationInstruction.ModEtherInstruction.class, + L2ModificationInstruction.PushHeaderInstructions.class, + L2ModificationInstruction.ModVlanIdInstruction.class, + L2ModificationInstruction.ModVlanPcpInstruction.class, + L2ModificationInstruction.PopVlanInstruction.class, + L2ModificationInstruction.ModMplsLabelInstruction.class, + L2ModificationInstruction.ModMplsTtlInstruction.class, + L2ModificationInstruction.ModTunnelIdInstruction.class, + L3ModificationInstruction.class, + L3ModificationInstruction.L3SubType.class, + L3ModificationInstruction.ModIPInstruction.class, + L3ModificationInstruction.ModIPv6FlowLabelInstruction.class, + L3ModificationInstruction.ModTtlInstruction.class, + L4ModificationInstruction.class, + L4ModificationInstruction.L4SubType.class, + L4ModificationInstruction.ModTransportPortInstruction.class, + RoleInfo.class, + FlowRuleBatchEvent.class, + FlowRuleBatchEvent.Type.class, + FlowRuleBatchRequest.class, + FlowRuleBatchOperation.class, + FlowRuleEvent.class, + FlowRuleEvent.Type.class, + CompletedBatchOperation.class, + FlowRuleBatchEntry.class, + FlowRuleBatchEntry.FlowRuleOperation.class, + IntentId.class, + IntentState.class, + //Key.class, is abstract + Key.of(1L, new DefaultApplicationId(0, "bar")).getClass(), //LongKey.class + Key.of("foo", new DefaultApplicationId(0, "bar")).getClass(), //StringKey.class + Intent.class, + ConnectivityIntent.class, + PathIntent.class, + DefaultPath.class, + DefaultEdgeLink.class, + HostToHostIntent.class, + PointToPointIntent.class, + MultiPointToSinglePointIntent.class, + SinglePointToMultiPointIntent.class, + FlowRuleIntent.class, + LinkCollectionIntent.class, + OpticalConnectivityIntent.class, + OpticalPathIntent.class, + OpticalCircuitIntent.class, + LinkResourceRequest.class, + DefaultLinkResourceRequest.class, + BandwidthResourceRequest.class, + LambdaResourceRequest.class, + LambdaResource.class, + BandwidthResource.class, + DefaultLinkResourceAllocations.class, + BandwidthResourceAllocation.class, + LambdaResourceAllocation.class, + ResourcePath.class, + ResourceAllocation.class, + // Constraints + LambdaConstraint.class, + BandwidthConstraint.class, + LinkTypeConstraint.class, + LatencyConstraint.class, + WaypointConstraint.class, + ObstacleConstraint.class, + AnnotationConstraint.class, + BooleanConstraint.class, + PartialFailureConstraint.class, + IntentOperation.class, + FlowRuleExtPayLoad.class, + Frequency.class, + DefaultAnnotations.class, + PortStatistics.class, + DefaultPortStatistics.class, + IntentDomainId.class + ) + .register(new DefaultApplicationIdSerializer(), DefaultApplicationId.class) + .register(new URISerializer(), URI.class) + .register(new NodeIdSerializer(), NodeId.class) + .register(new ProviderIdSerializer(), ProviderId.class) + .register(new DeviceIdSerializer(), DeviceId.class) + .register(new PortNumberSerializer(), PortNumber.class) + .register(new DefaultPortSerializer(), DefaultPort.class) + .register(new LinkKeySerializer(), LinkKey.class) + .register(new ConnectPointSerializer(), ConnectPoint.class) + .register(new DefaultLinkSerializer(), DefaultLink.class) + .register(new MastershipTermSerializer(), MastershipTerm.class) + .register(new HostLocationSerializer(), HostLocation.class) + .register(new DefaultOutboundPacketSerializer(), DefaultOutboundPacket.class) + .register(new AnnotationsSerializer(), DefaultAnnotations.class) + .register(Versioned.class) + .register(MapEvent.class) + .register(MapEvent.Type.class) + .register(SetEvent.class) + .register(SetEvent.Type.class) + .register(DefaultGroupId.class) + .register(Annotations.class) + .register(OmsPort.class) + .register(OchPort.class) + .register(OduSignalType.class) + .register(OchSignalType.class) + .register(GridType.class) + .register(ChannelSpacing.class) + .register(OduCltPort.class) + .register(OduCltPort.SignalType.class) + .register(IndexedLambda.class) + .register(OchSignal.class) + .register(OduCltPortDescription.class) + .register(OchPortDescription.class) + .register(OmsPortDescription.class) + .register( + MplsIntent.class, + MplsPathIntent.class, + MplsLabelResourceAllocation.class, + MplsLabelResourceRequest.class, + MplsLabel.class, + org.onlab.packet.MplsLabel.class, + org.onlab.packet.MPLS.class + ) + + .build(); + + + // not to be instantiated + private KryoNamespaces() {} +} diff --git a/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoSerializer.java b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoSerializer.java new file mode 100644 index 00000000..dbad6d0d --- /dev/null +++ b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoSerializer.java @@ -0,0 +1,86 @@ +/* + * Copyright 2014 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.store.serializers; + +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.ByteBuffer; + +import org.onlab.util.KryoNamespace; + +import com.google.common.base.MoreObjects; + +/** + * StoreSerializer implementation using Kryo. + */ +public class KryoSerializer implements StoreSerializer { + + protected KryoNamespace serializerPool; + + public KryoSerializer() { + setupKryoPool(); + } + + /** + * Sets up the common serializers pool. + */ + protected void setupKryoPool() { + serializerPool = KryoNamespace.newBuilder() + .register(KryoNamespaces.API) + .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) + .build(); + } + + @Override + public byte[] encode(final Object obj) { + return serializerPool.serialize(obj); + } + + @Override + public T decode(final byte[] bytes) { + if (bytes == null) { + return null; + } + return serializerPool.deserialize(bytes); + } + + @Override + public void encode(Object obj, ByteBuffer buffer) { + serializerPool.serialize(obj, buffer); + } + + @Override + public T decode(ByteBuffer buffer) { + return serializerPool.deserialize(buffer); + } + + @Override + public void encode(Object obj, OutputStream stream) { + serializerPool.serialize(obj, stream); + } + + @Override + public T decode(InputStream stream) { + return serializerPool.deserialize(stream); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(getClass()) + .add("serializerPool", serializerPool) + .toString(); + } +} diff --git a/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/LinkKeySerializer.java b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/LinkKeySerializer.java new file mode 100644 index 00000000..4bef3369 --- /dev/null +++ b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/LinkKeySerializer.java @@ -0,0 +1,51 @@ +/* + * Copyright 2014 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.store.serializers; + +import org.onosproject.net.ConnectPoint; +import org.onosproject.net.LinkKey; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +/** + * Kryo Serializer for {@link LinkKey}. + */ +public class LinkKeySerializer extends Serializer { + + /** + * Creates {@link LinkKey} serializer instance. + */ + public LinkKeySerializer() { + // non-null, immutable + super(false, true); + } + + @Override + public void write(Kryo kryo, Output output, LinkKey object) { + kryo.writeClassAndObject(output, object.src()); + kryo.writeClassAndObject(output, object.dst()); + } + + @Override + public LinkKey read(Kryo kryo, Input input, Class type) { + ConnectPoint src = (ConnectPoint) kryo.readClassAndObject(input); + ConnectPoint dst = (ConnectPoint) kryo.readClassAndObject(input); + return LinkKey.linkKey(src, dst); + } +} diff --git a/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/MacAddressSerializer.java b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/MacAddressSerializer.java new file mode 100644 index 00000000..881e0ec0 --- /dev/null +++ b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/MacAddressSerializer.java @@ -0,0 +1,47 @@ +/* + * Copyright 2014 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.store.serializers; + +import org.onlab.packet.MacAddress; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +/** + * Kryo Serializer for {@link MacAddress}. + */ +public class MacAddressSerializer extends Serializer { + + /** + * Creates {@link MacAddress} serializer instance. + */ + public MacAddressSerializer() { + super(false, true); + } + + @Override + public void write(Kryo kryo, Output output, MacAddress object) { + output.writeBytes(object.toBytes()); + } + + @Override + public MacAddress read(Kryo kryo, Input input, Class type) { + return MacAddress.valueOf(input.readBytes(MacAddress.MAC_ADDRESS_LENGTH)); + } + +} diff --git a/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/MastershipTermSerializer.java b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/MastershipTermSerializer.java new file mode 100644 index 00000000..43bce71c --- /dev/null +++ b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/MastershipTermSerializer.java @@ -0,0 +1,51 @@ +/* + * Copyright 2014-2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.store.serializers; + +import org.onosproject.cluster.NodeId; +import org.onosproject.mastership.MastershipTerm; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +/** + * Kryo Serializer for {@link org.onosproject.mastership.MastershipTerm}. + */ +public class MastershipTermSerializer extends Serializer { + + /** + * Creates {@link MastershipTerm} serializer instance. + */ + public MastershipTermSerializer() { + // non-null, immutable + super(false, true); + } + + @Override + public MastershipTerm read(Kryo kryo, Input input, Class type) { + final NodeId node = (NodeId) kryo.readClassAndObject(input); + final long term = input.readLong(); + return MastershipTerm.of(node, term); + } + + @Override + public void write(Kryo kryo, Output output, MastershipTerm object) { + kryo.writeClassAndObject(output, object.master()); + output.writeLong(object.termNumber()); + } +} diff --git a/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/NodeIdSerializer.java b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/NodeIdSerializer.java new file mode 100644 index 00000000..da8939b0 --- /dev/null +++ b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/NodeIdSerializer.java @@ -0,0 +1,48 @@ +/* + * Copyright 2014 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.store.serializers; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +import org.onosproject.cluster.NodeId; + +/** + * Kryo Serializer for {@link org.onosproject.cluster.NodeId}. + */ +public final class NodeIdSerializer extends Serializer { + + /** + * Creates {@link NodeId} serializer instance. + */ + public NodeIdSerializer() { + // non-null, immutable + super(false, true); + } + + @Override + public void write(Kryo kryo, Output output, NodeId object) { + output.writeString(object.toString()); + } + + @Override + public NodeId read(Kryo kryo, Input input, Class type) { + final String id = input.readString(); + return new NodeId(id); + } +} diff --git a/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/PortNumberSerializer.java b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/PortNumberSerializer.java new file mode 100644 index 00000000..74db644d --- /dev/null +++ b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/PortNumberSerializer.java @@ -0,0 +1,56 @@ +/* + * Copyright 2014 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.store.serializers; + +import org.onosproject.net.PortNumber; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +/** + * Serializer for {@link PortNumber}. + */ +public final class PortNumberSerializer extends + Serializer { + + /** + * Creates {@link PortNumber} serializer instance. + */ + public PortNumberSerializer() { + // non-null, immutable + super(false, true); + } + + @Override + public void write(Kryo kryo, Output output, PortNumber object) { + output.writeBoolean(object.hasName()); + output.writeLong(object.toLong()); + if (object.hasName()) { + output.writeString(object.name()); + } + } + + @Override + public PortNumber read(Kryo kryo, Input input, Class type) { + if (input.readBoolean()) { + return PortNumber.portNumber(input.readLong(), input.readString()); + } else { + return PortNumber.portNumber(input.readLong()); + } + } +} diff --git a/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/ProviderIdSerializer.java b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/ProviderIdSerializer.java new file mode 100644 index 00000000..7f9b6ffc --- /dev/null +++ b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/ProviderIdSerializer.java @@ -0,0 +1,53 @@ +/* + * Copyright 2014 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.store.serializers; + +import org.onosproject.net.provider.ProviderId; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +/** + * Serializer for {@link ProviderId}. + */ +public class ProviderIdSerializer extends Serializer { + + /** + * Creates {@link ProviderId} serializer instance. + */ + public ProviderIdSerializer() { + // non-null, immutable + super(false, true); + } + + @Override + public void write(Kryo kryo, Output output, ProviderId object) { + output.writeString(object.scheme()); + output.writeString(object.id()); + output.writeBoolean(object.isAncillary()); + } + + @Override + public ProviderId read(Kryo kryo, Input input, Class type) { + String scheme = input.readString(); + String id = input.readString(); + boolean isAncillary = input.readBoolean(); + return new ProviderId(scheme, id, isAncillary); + } + +} diff --git a/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/StoreSerializer.java b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/StoreSerializer.java new file mode 100644 index 00000000..f0149fc5 --- /dev/null +++ b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/StoreSerializer.java @@ -0,0 +1,78 @@ +/* + * Copyright 2014 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.store.serializers; + +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.ByteBuffer; + +// TODO: To be replaced with SerializationService from IOLoop activity +/** + * Service to serialize Objects into byte array. + */ +public interface StoreSerializer { + + /** + * Serializes the specified object into bytes. + * + * @param obj object to be serialized + * @return serialized bytes + */ + byte[] encode(final Object obj); + + /** + * Serializes the specified object into bytes. + * + * @param obj object to be serialized + * @param buffer to write serialized bytes + */ + void encode(final Object obj, ByteBuffer buffer); + + /** + * Serializes the specified object into bytes. + * + * @param obj object to be serialized + * @param stream to write serialized bytes + */ + void encode(final Object obj, final OutputStream stream); + + /** + * Deserializes the specified bytes into an object. + * + * @param bytes bytes to be deserialized + * @return deserialized object + * @param decoded type + */ + T decode(final byte[] bytes); + + /** + * Deserializes the specified bytes into an object. + * + * @param buffer bytes to be deserialized + * @return deserialized object + * @param decoded type + */ + T decode(final ByteBuffer buffer); + + /** + * Deserializes the specified bytes into an object. + * + * @param stream stream containing the bytes to be deserialized + * @return deserialized object + * @param decoded type + */ + T decode(final InputStream stream); +} diff --git a/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/URISerializer.java b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/URISerializer.java new file mode 100644 index 00000000..43969666 --- /dev/null +++ b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/URISerializer.java @@ -0,0 +1,46 @@ +/* + * Copyright 2014 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.store.serializers; + +import java.net.URI; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +/** + * Serializer for {@link URI}. + */ +public class URISerializer extends Serializer { + + /** + * Creates {@link URI} serializer instance. + */ + public URISerializer() { + super(false); + } + + @Override + public void write(Kryo kryo, Output output, URI object) { + output.writeString(object.toString()); + } + + @Override + public URI read(Kryo kryo, Input input, Class type) { + return URI.create(input.readString()); + } +} diff --git a/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/package-info.java b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/package-info.java new file mode 100644 index 00000000..9471b43e --- /dev/null +++ b/framework/src/onos/core/store/serializers/src/main/java/org/onosproject/store/serializers/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright 2014 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Various Kryo serializers for use in distributed stores. + */ +package org.onosproject.store.serializers; diff --git a/framework/src/onos/core/store/serializers/src/test/java/org/onosproject/store/serializers/KryoSerializerTest.java b/framework/src/onos/core/store/serializers/src/test/java/org/onosproject/store/serializers/KryoSerializerTest.java new file mode 100644 index 00000000..97ccb836 --- /dev/null +++ b/framework/src/onos/core/store/serializers/src/test/java/org/onosproject/store/serializers/KryoSerializerTest.java @@ -0,0 +1,470 @@ +/* + * Copyright 2014-2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.store.serializers; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.testing.EqualsTester; + +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onlab.packet.VlanId; +import org.onlab.util.Bandwidth; +import org.onlab.util.Frequency; +import org.onosproject.cluster.NodeId; +import org.onosproject.cluster.RoleInfo; +import org.onosproject.core.DefaultApplicationId; +import org.onosproject.core.DefaultGroupId; +import org.onosproject.mastership.MastershipTerm; +import org.onosproject.net.Annotations; +import org.onosproject.net.ChannelSpacing; +import org.onosproject.net.ConnectPoint; +import org.onosproject.net.DefaultAnnotations; +import org.onosproject.net.DefaultDevice; +import org.onosproject.net.DefaultLink; +import org.onosproject.net.DefaultPort; +import org.onosproject.net.Device; +import org.onosproject.net.DeviceId; +import org.onosproject.net.GridType; +import org.onosproject.net.HostLocation; +import org.onosproject.net.Link; +import org.onosproject.net.Link.Type; +import org.onosproject.net.LinkKey; +import org.onosproject.net.OchPort; +import org.onosproject.net.OchSignal; +import org.onosproject.net.OduCltPort; +import org.onosproject.net.OmsPort; +import org.onosproject.net.PortNumber; +import org.onosproject.net.OduSignalType; +import org.onosproject.net.SparseAnnotations; +import org.onosproject.net.flow.DefaultFlowRule; +import org.onosproject.net.flow.DefaultTrafficSelector; +import org.onosproject.net.flow.DefaultTrafficTreatment; +import org.onosproject.net.flow.FlowId; +import org.onosproject.net.flow.FlowRule; +import org.onosproject.net.flow.FlowRuleBatchEntry; +import org.onosproject.net.intent.IntentId; +import org.onosproject.net.newresource.ResourcePath; +import org.onosproject.net.provider.ProviderId; +import org.onosproject.net.resource.link.BandwidthResource; +import org.onosproject.net.resource.link.BandwidthResourceAllocation; +import org.onosproject.net.resource.link.DefaultLinkResourceAllocations; +import org.onosproject.net.resource.link.DefaultLinkResourceRequest; +import org.onosproject.net.resource.link.LambdaResource; +import org.onosproject.net.resource.link.LambdaResourceAllocation; +import org.onosproject.net.resource.link.LinkResourceRequest; +import org.onosproject.net.resource.ResourceAllocation; +import org.onosproject.net.intent.constraint.AnnotationConstraint; +import org.onosproject.net.intent.constraint.BandwidthConstraint; +import org.onosproject.net.intent.constraint.LambdaConstraint; +import org.onosproject.net.intent.constraint.LatencyConstraint; +import org.onosproject.net.intent.constraint.LinkTypeConstraint; +import org.onosproject.net.intent.constraint.ObstacleConstraint; +import org.onosproject.net.intent.constraint.WaypointConstraint; +import org.onlab.packet.ChassisId; +import org.onlab.packet.IpAddress; +import org.onlab.packet.Ip4Address; +import org.onlab.packet.Ip6Address; +import org.onlab.packet.IpPrefix; +import org.onlab.packet.Ip4Prefix; +import org.onlab.packet.Ip6Prefix; +import org.onlab.packet.MacAddress; +import org.onlab.util.KryoNamespace; + +import java.nio.ByteBuffer; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.time.Duration; + +import static java.util.Arrays.asList; +import static org.junit.Assert.*; +import static org.onosproject.net.DeviceId.deviceId; +import static org.onosproject.net.PortNumber.portNumber; + +public class KryoSerializerTest { + + private static final ProviderId PID = new ProviderId("of", "foo"); + private static final ProviderId PIDA = new ProviderId("of", "foo", true); + private static final DeviceId DID1 = deviceId("of:foo"); + private static final DeviceId DID2 = deviceId("of:bar"); + private static final PortNumber P1 = portNumber(1); + private static final PortNumber P2 = portNumber(2); + private static final ConnectPoint CP1 = new ConnectPoint(DID1, P1); + private static final ConnectPoint CP2 = new ConnectPoint(DID2, P2); + private static final String MFR = "whitebox"; + private static final String HW = "1.1.x"; + private static final String SW1 = "3.8.1"; + private static final String SW2 = "3.9.5"; + private static final String SN = "43311-12345"; + private static final ChassisId CID = new ChassisId(); + private static final Device DEV1 = new DefaultDevice(PID, DID1, Device.Type.SWITCH, MFR, HW, + SW1, SN, CID); + private static final SparseAnnotations A1 = DefaultAnnotations.builder() + .set("A1", "a1") + .set("B1", "b1") + .build(); + private static final SparseAnnotations A1_2 = DefaultAnnotations.builder() + .remove("A1") + .set("B3", "b3") + .build(); + private static final OchSignal OCH_SIGNAL1 = (OchSignal) org.onosproject.net.Lambda.ochSignal( + GridType.DWDM, ChannelSpacing.CHL_100GHZ, -8, 4); + private static final VlanId VLAN1 = VlanId.vlanId((short) 100); + + private KryoSerializer serializer; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + @Before + public void setUp() throws Exception { + serializer = new KryoSerializer() { + + @Override + protected void setupKryoPool() { + serializerPool = KryoNamespace.newBuilder() + .register(KryoNamespaces.API) + .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) + .build(); + } + }; + } + + @After + public void tearDown() throws Exception { + } + + private void testSerializedEquals(T original) { + ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024); + serializer.encode(original, buffer); + buffer.flip(); + T copy = serializer.decode(buffer); + + T copy2 = serializer.decode(serializer.encode(original)); + + new EqualsTester() + .addEqualityGroup(original, copy, copy2) + .testEquals(); + } + + private void testSerializable(T original) { + byte[] bs = serializer.encode(original); + T copy = serializer.decode(bs); + assertNotNull(copy); + } + + + @Test + public void testConnectPoint() { + testSerializedEquals(new ConnectPoint(DID1, P1)); + } + + @Test + public void testDefaultLink() { + testSerializedEquals(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT)); + testSerializedEquals(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT, A1)); + } + + @Test + public void testDefaultPort() { + testSerializedEquals(new DefaultPort(DEV1, P1, true)); + testSerializedEquals(new DefaultPort(DEV1, P1, true, A1_2)); + } + + @Test + public void testOmsPort() { + testSerializedEquals(new OmsPort(DEV1, P1, true, Frequency.ofGHz(190_100), Frequency.ofGHz(197_300), + Frequency.ofGHz(100))); + testSerializedEquals(new OmsPort(DEV1, P1, true, Frequency.ofGHz(190_100), Frequency.ofGHz(197_300), + Frequency.ofGHz(100), A1_2)); + } + + @Test + public void testOchPort() { + testSerializedEquals(new OchPort(DEV1, P1, true, OduSignalType.ODU0, false, OCH_SIGNAL1)); + testSerializedEquals(new OchPort(DEV1, P1, true, OduSignalType.ODU0, false, OCH_SIGNAL1, A1_2)); + } + + @Test + public void testOduCltPort() { + testSerializedEquals(new OduCltPort(DEV1, P1, true, OduCltPort.SignalType.CLT_10GBE)); + testSerializedEquals(new OduCltPort(DEV1, P1, true, OduCltPort.SignalType.CLT_10GBE, A1_2)); + } + + @Test + public void testDeviceId() { + testSerializedEquals(DID1); + } + + @Test + public void testImmutableMap() { + testSerializedEquals(ImmutableMap.of(DID1, DEV1, DID2, DEV1)); + testSerializedEquals(ImmutableMap.of(DID1, DEV1)); + testSerializedEquals(ImmutableMap.of()); + } + + @Test + public void testImmutableSet() { + testSerializedEquals(ImmutableSet.of(DID1, DID2)); + testSerializedEquals(ImmutableSet.of(DID1)); + testSerializedEquals(ImmutableSet.of()); + } + + @Test + public void testImmutableList() { + testSerializedEquals(ImmutableList.of(DID1, DID2)); + testSerializedEquals(ImmutableList.of(DID1)); + testSerializedEquals(ImmutableList.of()); + } + + @Test + public void testFlowRuleBatchEntry() { + final FlowRule rule1 = + DefaultFlowRule.builder() + .forDevice(DID1) + .withSelector(DefaultTrafficSelector.emptySelector()) + .withTreatment(DefaultTrafficTreatment.emptyTreatment()) + .withPriority(0) + .fromApp(new DefaultApplicationId(1, "1")) + .makeTemporary(1) + .build(); + + final FlowRuleBatchEntry entry1 = + new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, rule1); + final FlowRuleBatchEntry entry2 = + new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, rule1, 100L); + + testSerializedEquals(entry1); + testSerializedEquals(entry2); + } + + @Test + public void testIpPrefix() { + testSerializedEquals(IpPrefix.valueOf("192.168.0.1/24")); + } + + @Test + public void testIp4Prefix() { + testSerializedEquals(Ip4Prefix.valueOf("192.168.0.1/24")); + } + + @Test + public void testIp6Prefix() { + testSerializedEquals(Ip6Prefix.valueOf("1111:2222::/120")); + } + + @Test + public void testIpAddress() { + testSerializedEquals(IpAddress.valueOf("192.168.0.1")); + } + + @Test + public void testIp4Address() { + testSerializedEquals(Ip4Address.valueOf("192.168.0.1")); + } + + @Test + public void testIp6Address() { + testSerializedEquals(Ip6Address.valueOf("1111:2222::")); + } + + @Test + public void testMacAddress() { + testSerializedEquals(MacAddress.valueOf("12:34:56:78:90:ab")); + } + + @Test + public void testLinkKey() { + testSerializedEquals(LinkKey.linkKey(CP1, CP2)); + } + + @Test + public void testNodeId() { + testSerializedEquals(new NodeId("SomeNodeIdentifier")); + } + + @Test + public void testPortNumber() { + testSerializedEquals(P1); + } + + @Test + public void testProviderId() { + testSerializedEquals(PID); + testSerializedEquals(PIDA); + } + + @Test + public void testMastershipTerm() { + testSerializedEquals(MastershipTerm.of(new NodeId("foo"), 2)); + testSerializedEquals(MastershipTerm.of(null, 0)); + } + + @Test + public void testHostLocation() { + testSerializedEquals(new HostLocation(CP1, 1234L)); + } + + @Test + public void testFlowId() { + testSerializedEquals(FlowId.valueOf(0x12345678L)); + } + + @Test + public void testRoleInfo() { + testSerializedEquals(new RoleInfo(new NodeId("master"), + asList(new NodeId("stby1"), new NodeId("stby2")))); + } + + @Test + public void testIndexedLambda() { + testSerializedEquals(org.onosproject.net.Lambda.indexedLambda(10L)); + } + + @Test + public void testOchSignal() { + testSerializedEquals(org.onosproject.net.Lambda.ochSignal( + GridType.DWDM, ChannelSpacing.CHL_100GHZ, 1, 1 + )); + } + + @Test + public void testDefaultLinkResourceRequest() { + testSerializable(DefaultLinkResourceRequest.builder(IntentId.valueOf(2501), ImmutableList.of()) + .addLambdaRequest() + .addBandwidthRequest(32.195) + .build() + ); + } + + @Test + public void testDefaultLinkResourceAllocations() { + LinkResourceRequest request = DefaultLinkResourceRequest + .builder(IntentId.valueOf(2501), ImmutableList.of()) + .addLambdaRequest() + .addBandwidthRequest(32.195) + .build(); + Map> allocations = new HashMap<>(); + allocations.put(new DefaultLink(PID, CP1, CP2, Type.DIRECT), + ImmutableSet.of(new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.bps(10.0))), + new LambdaResourceAllocation(LambdaResource.valueOf(1)))); + testSerializable(new DefaultLinkResourceAllocations(request, allocations)); + } + + @Test + public void testResourcePath() { + testSerializedEquals(new ResourcePath(LinkKey.linkKey(CP1, CP2), VLAN1)); + } + + @Test + public void testResourceAllocation() { + testSerializedEquals(new org.onosproject.net.newresource.ResourceAllocation( + new ResourcePath(LinkKey.linkKey(CP1, CP2), VLAN1), + IntentId.valueOf(30))); + } + + @Test + public void testFrequency() { + testSerializedEquals(Frequency.ofGHz(100)); + } + + @Test + public void testBandwidth() { + testSerializedEquals(Bandwidth.mbps(1000.0)); + } + + @Test + public void testLambdaConstraint() { + testSerializable(new LambdaConstraint(LambdaResource.valueOf(1))); + } + + @Test + public void testBandwidthConstraint() { + testSerializable(new BandwidthConstraint(new BandwidthResource(Bandwidth.bps(1000.0)))); + } + + @Test + public void testLinkTypeConstraint() { + testSerializable(new LinkTypeConstraint(true, Link.Type.DIRECT)); + } + + @Test + public void testLatencyConstraint() { + testSerializable(new LatencyConstraint(Duration.ofSeconds(10))); + } + + @Test + public void testWaypointConstraint() { + testSerializable(new WaypointConstraint(deviceId("of:1"), deviceId("of:2"))); + } + + @Test + public void testObstacleConstraint() { + testSerializable(new ObstacleConstraint(deviceId("of:1"), deviceId("of:2"))); + } + + @Test + public void testArraysAsList() { + testSerializedEquals(Arrays.asList(1, 2, 3)); + } + + @Test + public void testAnnotationConstraint() { + testSerializable(new AnnotationConstraint("distance", 100.0)); + } + + @Test + public void testDefaultGroupId() { + testSerializedEquals(new DefaultGroupId(99)); + } + + @Test + public void testEmptySet() { + testSerializedEquals(Collections.emptySet()); + } + + @Test + public void testAnnotations() { + // Annotations does not have equals defined, manually test equality + final byte[] a1Bytes = serializer.encode(A1); + SparseAnnotations copiedA1 = serializer.decode(a1Bytes); + assertAnnotationsEquals(copiedA1, A1); + + final byte[] a12Bytes = serializer.encode(A1_2); + SparseAnnotations copiedA12 = serializer.decode(a12Bytes); + assertAnnotationsEquals(copiedA12, A1_2); + } + + // code clone + protected static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) { + SparseAnnotations expected = DefaultAnnotations.builder().build(); + for (SparseAnnotations a : annotations) { + expected = DefaultAnnotations.union(expected, a); + } + assertEquals(expected.keys(), actual.keys()); + for (String key : expected.keys()) { + assertEquals(expected.value(key), actual.value(key)); + } + } + +} -- cgit 1.2.3-korg