Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.store.primitives.impl; |
| 17 | |
| 18 | import io.atomix.catalyst.serializer.CatalystSerializable; |
| 19 | import io.atomix.catalyst.serializer.Serializer; |
| 20 | import io.atomix.catalyst.serializer.TypeSerializerFactory; |
| 21 | import io.atomix.copycat.client.Query; |
| 22 | import io.atomix.manager.state.GetResource; |
| 23 | import io.atomix.manager.state.GetResourceKeys; |
| 24 | import io.atomix.resource.ResourceQuery; |
| 25 | |
| 26 | import java.io.IOException; |
| 27 | import java.net.URL; |
| 28 | import java.util.Arrays; |
| 29 | import java.util.Enumeration; |
| 30 | import java.util.Scanner; |
| 31 | |
| 32 | import org.onlab.util.Match; |
| 33 | import org.onosproject.cluster.NodeId; |
| 34 | import org.onosproject.event.Change; |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 35 | import org.onosproject.store.primitives.TransactionId; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 36 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands; |
| 37 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapState; |
| 38 | import org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorCommands; |
| 39 | import org.onosproject.store.primitives.resources.impl.CommitResult; |
| 40 | import org.onosproject.store.primitives.resources.impl.MapEntryUpdateResult; |
| 41 | import org.onosproject.store.primitives.resources.impl.MapUpdate; |
| 42 | import org.onosproject.store.primitives.resources.impl.PrepareResult; |
| 43 | import org.onosproject.store.primitives.resources.impl.RollbackResult; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 44 | import org.onosproject.store.serializers.KryoNamespaces; |
| 45 | import org.onosproject.store.service.MapEvent; |
| 46 | import org.onosproject.store.service.Versioned; |
| 47 | |
| 48 | import com.google.common.base.Throwables; |
| 49 | import com.google.common.collect.Maps; |
| 50 | |
| 51 | /** |
| 52 | * Serializer utility for Atomix Catalyst. |
| 53 | */ |
| 54 | public final class CatalystSerializers { |
| 55 | |
| 56 | private CatalystSerializers() { |
| 57 | } |
| 58 | |
| 59 | public static Serializer getSerializer() { |
| 60 | Serializer serializer = new Serializer(); |
| 61 | TypeSerializerFactory factory = |
| 62 | new DefaultCatalystTypeSerializerFactory( |
| 63 | org.onosproject.store.service.Serializer.using(Arrays.asList((KryoNamespaces.API)), |
| 64 | MapEntryUpdateResult.class, |
| 65 | MapEntryUpdateResult.Status.class, |
| 66 | MapUpdate.class, |
| 67 | MapUpdate.Type.class, |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 68 | Transaction.class, |
| 69 | Transaction.State.class, |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 70 | TransactionId.class, |
| 71 | PrepareResult.class, |
| 72 | CommitResult.class, |
| 73 | RollbackResult.class, |
| 74 | AtomixConsistentMapCommands.Get.class, |
| 75 | AtomixConsistentMapCommands.ContainsKey.class, |
| 76 | AtomixConsistentMapCommands.ContainsValue.class, |
| 77 | AtomixConsistentMapCommands.Size.class, |
| 78 | AtomixConsistentMapCommands.IsEmpty.class, |
| 79 | AtomixConsistentMapCommands.KeySet.class, |
| 80 | AtomixConsistentMapCommands.EntrySet.class, |
| 81 | AtomixConsistentMapCommands.Values.class, |
| 82 | AtomixConsistentMapCommands.UpdateAndGet.class, |
| 83 | AtomixConsistentMapCommands.TransactionPrepare.class, |
| 84 | AtomixConsistentMapCommands.TransactionCommit.class, |
| 85 | AtomixConsistentMapCommands.TransactionRollback.class, |
| 86 | AtomixLeaderElectorCommands.GetLeadership.class, |
| 87 | AtomixLeaderElectorCommands.GetAllLeaderships.class, |
| 88 | AtomixLeaderElectorCommands.GetElectedTopics.class, |
| 89 | AtomixLeaderElectorCommands.Run.class, |
| 90 | AtomixLeaderElectorCommands.Withdraw.class, |
| 91 | AtomixLeaderElectorCommands.Anoint.class, |
| 92 | GetResource.class, |
| 93 | GetResourceKeys.class, |
| 94 | ResourceQuery.class, |
| 95 | Query.ConsistencyLevel.class)); |
| 96 | // ONOS classes |
| 97 | serializer.register(Change.class, factory); |
| 98 | serializer.register(NodeId.class, factory); |
| 99 | serializer.register(Match.class, factory); |
| 100 | serializer.register(MapEntryUpdateResult.class, factory); |
| 101 | serializer.register(MapEntryUpdateResult.Status.class, factory); |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 102 | serializer.register(Transaction.class, factory); |
| 103 | serializer.register(Transaction.State.class, factory); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 104 | serializer.register(PrepareResult.class, factory); |
| 105 | serializer.register(CommitResult.class, factory); |
| 106 | serializer.register(RollbackResult.class, factory); |
| 107 | serializer.register(TransactionId.class, factory); |
| 108 | serializer.register(MapUpdate.class, factory); |
| 109 | serializer.register(Versioned.class, factory); |
| 110 | serializer.register(MapEvent.class, factory); |
| 111 | serializer.register(Maps.immutableEntry("a", "b").getClass(), factory); |
| 112 | serializer.register(AtomixConsistentMapState.class, factory); |
| 113 | |
| 114 | serializer.register(ResourceQuery.class, factory); |
| 115 | serializer.register(GetResource.class, factory); |
| 116 | serializer.register(GetResourceKeys.class, factory); |
| 117 | |
| 118 | // ConsistentMap |
| 119 | serializer.register(AtomixConsistentMapCommands.UpdateAndGet.class, factory); |
| 120 | serializer.register(AtomixConsistentMapCommands.Clear.class); |
| 121 | serializer.register(AtomixConsistentMapCommands.Listen.class); |
| 122 | serializer.register(AtomixConsistentMapCommands.Unlisten.class); |
| 123 | serializer.register(AtomixConsistentMapCommands.Get.class); |
| 124 | serializer.register(AtomixConsistentMapCommands.ContainsKey.class); |
| 125 | serializer.register(AtomixConsistentMapCommands.ContainsValue.class); |
| 126 | serializer.register(AtomixConsistentMapCommands.EntrySet.class); |
| 127 | serializer.register(AtomixConsistentMapCommands.IsEmpty.class); |
| 128 | serializer.register(AtomixConsistentMapCommands.KeySet.class); |
| 129 | serializer.register(AtomixConsistentMapCommands.Size.class); |
| 130 | serializer.register(AtomixConsistentMapCommands.Values.class); |
| 131 | serializer.register(AtomixConsistentMapCommands.TransactionPrepare.class); |
| 132 | serializer.register(AtomixConsistentMapCommands.TransactionCommit.class); |
| 133 | serializer.register(AtomixConsistentMapCommands.TransactionRollback.class); |
| 134 | // LeaderElector |
| 135 | serializer.register(AtomixLeaderElectorCommands.Run.class, factory); |
| 136 | serializer.register(AtomixLeaderElectorCommands.Withdraw.class, factory); |
| 137 | serializer.register(AtomixLeaderElectorCommands.Anoint.class, factory); |
| 138 | serializer.register(AtomixLeaderElectorCommands.GetElectedTopics.class, factory); |
| 139 | serializer.register(AtomixLeaderElectorCommands.GetElectedTopics.class, factory); |
| 140 | serializer.register(AtomixLeaderElectorCommands.GetLeadership.class, factory); |
| 141 | serializer.register(AtomixLeaderElectorCommands.GetAllLeaderships.class, factory); |
| 142 | serializer.register(AtomixLeaderElectorCommands.Listen.class); |
| 143 | serializer.register(AtomixLeaderElectorCommands.Unlisten.class); |
| 144 | // Atomix types |
| 145 | try { |
| 146 | ClassLoader cl = CatalystSerializable.class.getClassLoader(); |
| 147 | Enumeration<URL> urls = cl.getResources( |
| 148 | String.format("META-INF/services/%s", CatalystSerializable.class.getName())); |
| 149 | while (urls.hasMoreElements()) { |
| 150 | URL url = urls.nextElement(); |
| 151 | try (Scanner scanner = new Scanner(url.openStream(), "UTF-8")) { |
| 152 | scanner.useDelimiter("\n").forEachRemaining(line -> { |
| 153 | if (!line.trim().startsWith("#")) { |
| 154 | line = line.trim(); |
| 155 | if (line.length() > 0) { |
| 156 | try { |
| 157 | serializer.register(cl.loadClass(line)); |
| 158 | } catch (ClassNotFoundException e) { |
| 159 | Throwables.propagate(e); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | }); |
| 164 | } |
| 165 | } |
| 166 | } catch (IOException e) { |
| 167 | Throwables.propagate(e); |
| 168 | } |
| 169 | return serializer; |
| 170 | } |
| 171 | } |