66ee7be7e29ec362473f00850bcf867bf4eff18c
[onosfw.git] /
1 /*
2  * Copyright 2014-2015 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.serializers;
17
18 import com.google.common.collect.ImmutableList;
19 import com.google.common.collect.ImmutableMap;
20 import com.google.common.collect.ImmutableSet;
21 import com.google.common.collect.Maps;
22
23 import org.onlab.packet.ChassisId;
24 import org.onlab.packet.EthType;
25 import org.onlab.packet.Ip4Address;
26 import org.onlab.packet.Ip4Prefix;
27 import org.onlab.packet.Ip6Address;
28 import org.onlab.packet.Ip6Prefix;
29 import org.onlab.packet.IpAddress;
30 import org.onlab.packet.IpPrefix;
31 import org.onlab.packet.MacAddress;
32 import org.onlab.packet.TpPort;
33 import org.onlab.packet.VlanId;
34 import org.onlab.util.Bandwidth;
35 import org.onlab.util.Frequency;
36 import org.onlab.util.KryoNamespace;
37 import org.onosproject.app.ApplicationState;
38 import org.onosproject.cluster.ControllerNode;
39 import org.onosproject.cluster.DefaultControllerNode;
40 import org.onosproject.cluster.Leadership;
41 import org.onosproject.cluster.LeadershipEvent;
42 import org.onosproject.cluster.NodeId;
43 import org.onosproject.cluster.RoleInfo;
44 import org.onosproject.core.ApplicationRole;
45 import org.onosproject.core.DefaultApplication;
46 import org.onosproject.core.DefaultApplicationId;
47 import org.onosproject.core.DefaultGroupId;
48 import org.onosproject.core.Version;
49 import org.onosproject.incubator.net.domain.IntentDomainId;
50 import org.onosproject.mastership.MastershipTerm;
51 import org.onosproject.net.Annotations;
52 import org.onosproject.net.ChannelSpacing;
53 import org.onosproject.net.ConnectPoint;
54 import org.onosproject.net.DefaultAnnotations;
55 import org.onosproject.net.DefaultDevice;
56 import org.onosproject.net.DefaultEdgeLink;
57 import org.onosproject.net.DefaultHost;
58 import org.onosproject.net.DefaultLink;
59 import org.onosproject.net.DefaultPath;
60 import org.onosproject.net.DefaultPort;
61 import org.onosproject.net.Device;
62 import org.onosproject.net.DeviceId;
63 import org.onosproject.net.Element;
64 import org.onosproject.net.GridType;
65 import org.onosproject.net.HostId;
66 import org.onosproject.net.HostLocation;
67 import org.onosproject.net.IndexedLambda;
68 import org.onosproject.net.Link;
69 import org.onosproject.net.LinkKey;
70 import org.onosproject.net.OchPort;
71 import org.onosproject.net.OchSignal;
72 import org.onosproject.net.OchSignalType;
73 import org.onosproject.net.OduCltPort;
74 import org.onosproject.net.OduSignalType;
75 import org.onosproject.net.OmsPort;
76 import org.onosproject.net.Port;
77 import org.onosproject.net.PortNumber;
78 import org.onosproject.net.device.DefaultDeviceDescription;
79 import org.onosproject.net.device.DefaultPortDescription;
80 import org.onosproject.net.device.DefaultPortStatistics;
81 import org.onosproject.net.device.OchPortDescription;
82 import org.onosproject.net.device.OduCltPortDescription;
83 import org.onosproject.net.device.OmsPortDescription;
84 import org.onosproject.net.device.PortStatistics;
85 import org.onosproject.net.flow.CompletedBatchOperation;
86 import org.onosproject.net.flow.DefaultFlowEntry;
87 import org.onosproject.net.flow.DefaultFlowRule;
88 import org.onosproject.net.flow.DefaultTrafficSelector;
89 import org.onosproject.net.flow.DefaultTrafficTreatment;
90 import org.onosproject.net.flow.FlowEntry;
91 import org.onosproject.net.flow.FlowId;
92 import org.onosproject.net.flow.FlowRule;
93 import org.onosproject.net.flow.FlowRuleBatchEntry;
94 import org.onosproject.net.flow.FlowRuleBatchEvent;
95 import org.onosproject.net.flow.FlowRuleBatchOperation;
96 import org.onosproject.net.flow.FlowRuleBatchRequest;
97 import org.onosproject.net.flow.FlowRuleEvent;
98 import org.onosproject.net.flow.FlowRuleExtPayLoad;
99 import org.onosproject.net.flow.StoredFlowEntry;
100 import org.onosproject.net.flow.criteria.Criterion;
101 import org.onosproject.net.flow.criteria.EthCriterion;
102 import org.onosproject.net.flow.criteria.EthTypeCriterion;
103 import org.onosproject.net.flow.criteria.IPCriterion;
104 import org.onosproject.net.flow.criteria.IPDscpCriterion;
105 import org.onosproject.net.flow.criteria.IPEcnCriterion;
106 import org.onosproject.net.flow.criteria.IPProtocolCriterion;
107 import org.onosproject.net.flow.criteria.IPv6ExthdrFlagsCriterion;
108 import org.onosproject.net.flow.criteria.IPv6FlowLabelCriterion;
109 import org.onosproject.net.flow.criteria.IPv6NDLinkLayerAddressCriterion;
110 import org.onosproject.net.flow.criteria.IPv6NDTargetAddressCriterion;
111 import org.onosproject.net.flow.criteria.IcmpCodeCriterion;
112 import org.onosproject.net.flow.criteria.IcmpTypeCriterion;
113 import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
114 import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
115 import org.onosproject.net.flow.criteria.IndexedLambdaCriterion;
116 import org.onosproject.net.flow.criteria.LambdaCriterion;
117 import org.onosproject.net.flow.criteria.MetadataCriterion;
118 import org.onosproject.net.flow.criteria.MplsCriterion;
119 import org.onosproject.net.flow.criteria.OchSignalCriterion;
120 import org.onosproject.net.flow.criteria.OchSignalTypeCriterion;
121 import org.onosproject.net.flow.criteria.OpticalSignalTypeCriterion;
122 import org.onosproject.net.flow.criteria.PortCriterion;
123 import org.onosproject.net.flow.criteria.SctpPortCriterion;
124 import org.onosproject.net.flow.criteria.TcpPortCriterion;
125 import org.onosproject.net.flow.criteria.TunnelIdCriterion;
126 import org.onosproject.net.flow.criteria.UdpPortCriterion;
127 import org.onosproject.net.flow.criteria.VlanIdCriterion;
128 import org.onosproject.net.flow.criteria.VlanPcpCriterion;
129 import org.onosproject.net.flow.instructions.Instructions;
130 import org.onosproject.net.flow.instructions.L0ModificationInstruction;
131 import org.onosproject.net.flow.instructions.L2ModificationInstruction;
132 import org.onosproject.net.flow.instructions.L3ModificationInstruction;
133 import org.onosproject.net.flow.instructions.L4ModificationInstruction;
134 import org.onosproject.net.host.DefaultHostDescription;
135 import org.onosproject.net.host.HostDescription;
136 import org.onosproject.net.intent.ConnectivityIntent;
137 import org.onosproject.net.intent.FlowRuleIntent;
138 import org.onosproject.net.intent.HostToHostIntent;
139 import org.onosproject.net.intent.Intent;
140 import org.onosproject.net.intent.IntentId;
141 import org.onosproject.net.intent.IntentOperation;
142 import org.onosproject.net.intent.IntentState;
143 import org.onosproject.net.intent.Key;
144 import org.onosproject.net.intent.LinkCollectionIntent;
145 import org.onosproject.net.intent.MplsIntent;
146 import org.onosproject.net.intent.MplsPathIntent;
147 import org.onosproject.net.intent.MultiPointToSinglePointIntent;
148 import org.onosproject.net.intent.OpticalCircuitIntent;
149 import org.onosproject.net.intent.OpticalConnectivityIntent;
150 import org.onosproject.net.intent.OpticalPathIntent;
151 import org.onosproject.net.intent.PathIntent;
152 import org.onosproject.net.intent.PointToPointIntent;
153 import org.onosproject.net.intent.SinglePointToMultiPointIntent;
154 import org.onosproject.net.intent.constraint.AnnotationConstraint;
155 import org.onosproject.net.intent.constraint.BandwidthConstraint;
156 import org.onosproject.net.intent.constraint.BooleanConstraint;
157 import org.onosproject.net.intent.constraint.LambdaConstraint;
158 import org.onosproject.net.intent.constraint.LatencyConstraint;
159 import org.onosproject.net.intent.constraint.LinkTypeConstraint;
160 import org.onosproject.net.intent.constraint.ObstacleConstraint;
161 import org.onosproject.net.intent.constraint.PartialFailureConstraint;
162 import org.onosproject.net.intent.constraint.WaypointConstraint;
163 import org.onosproject.net.link.DefaultLinkDescription;
164 import org.onosproject.net.meter.MeterId;
165 import org.onosproject.net.newresource.ResourceAllocation;
166 import org.onosproject.net.newresource.ResourcePath;
167 import org.onosproject.net.packet.DefaultOutboundPacket;
168 import org.onosproject.net.packet.DefaultPacketRequest;
169 import org.onosproject.net.packet.PacketPriority;
170 import org.onosproject.net.provider.ProviderId;
171 import org.onosproject.net.resource.link.BandwidthResource;
172 import org.onosproject.net.resource.link.BandwidthResourceAllocation;
173 import org.onosproject.net.resource.link.BandwidthResourceRequest;
174 import org.onosproject.net.resource.link.DefaultLinkResourceAllocations;
175 import org.onosproject.net.resource.link.DefaultLinkResourceRequest;
176 import org.onosproject.net.resource.link.LambdaResource;
177 import org.onosproject.net.resource.link.LambdaResourceAllocation;
178 import org.onosproject.net.resource.link.LambdaResourceRequest;
179 import org.onosproject.net.resource.link.LinkResourceRequest;
180 import org.onosproject.net.resource.link.MplsLabel;
181 import org.onosproject.net.resource.link.MplsLabelResourceAllocation;
182 import org.onosproject.net.resource.link.MplsLabelResourceRequest;
183 import org.onosproject.store.Timestamp;
184 import org.onosproject.store.service.MapEvent;
185 import org.onosproject.store.service.SetEvent;
186 import org.onosproject.store.service.Versioned;
187
188 import java.net.URI;
189 import java.time.Duration;
190 import java.util.ArrayList;
191 import java.util.Arrays;
192 import java.util.Collections;
193 import java.util.HashMap;
194 import java.util.HashSet;
195 import java.util.LinkedList;
196 import java.util.Optional;
197 import java.util.concurrent.ConcurrentHashMap;
198 import java.util.concurrent.CopyOnWriteArraySet;
199 import java.util.concurrent.atomic.AtomicBoolean;
200 import java.util.concurrent.atomic.AtomicInteger;
201 import java.util.concurrent.atomic.AtomicLong;
202
203 public final class KryoNamespaces {
204
205     public static final KryoNamespace BASIC = KryoNamespace.newBuilder()
206             .nextId(KryoNamespace.FLOATING_ID)
207             .register(byte[].class)
208             .register(AtomicBoolean.class)
209             .register(AtomicInteger.class)
210             .register(AtomicLong.class)
211             .register(new ImmutableListSerializer(),
212                       ImmutableList.class,
213                       ImmutableList.of(1).getClass(),
214                       ImmutableList.of(1, 2).getClass())
215             .register(new ImmutableSetSerializer(),
216                       ImmutableSet.class,
217                       ImmutableSet.of().getClass(),
218                       ImmutableSet.of(1).getClass(),
219                       ImmutableSet.of(1, 2).getClass())
220             .register(new ImmutableMapSerializer(),
221                       ImmutableMap.class,
222                       ImmutableMap.of().getClass(),
223                       ImmutableMap.of("a", 1).getClass(),
224                       ImmutableMap.of("R", 2, "D", 2).getClass())
225             .register(HashMap.class)
226             .register(ConcurrentHashMap.class)
227             .register(CopyOnWriteArraySet.class)
228             .register(ArrayList.class,
229                       LinkedList.class,
230                       HashSet.class
231                       )
232             .register(Maps.immutableEntry("a", "b").getClass())
233             .register(new ArraysAsListSerializer(), Arrays.asList().getClass())
234             .register(Collections.singletonList(1).getClass())
235             .register(Duration.class)
236             .register(Collections.emptySet().getClass())
237             .register(Optional.class)
238             .register(Collections.emptyList().getClass())
239             .register(Collections.unmodifiableSet(Collections.emptySet()).getClass())
240             .register(Collections.singleton(Object.class).getClass())
241             .build();
242
243     /**
244      * KryoNamespace which can serialize ON.lab misc classes.
245      */
246     public static final KryoNamespace MISC = KryoNamespace.newBuilder()
247             .nextId(KryoNamespace.FLOATING_ID)
248             .register(new IpPrefixSerializer(), IpPrefix.class)
249             .register(new Ip4PrefixSerializer(), Ip4Prefix.class)
250             .register(new Ip6PrefixSerializer(), Ip6Prefix.class)
251             .register(new IpAddressSerializer(), IpAddress.class)
252             .register(new Ip4AddressSerializer(), Ip4Address.class)
253             .register(new Ip6AddressSerializer(), Ip6Address.class)
254             .register(new MacAddressSerializer(), MacAddress.class)
255             .register(VlanId.class)
256             .register(Frequency.class)
257             .register(Bandwidth.class)
258             .build();
259
260     /**
261      * Kryo registration Id for user custom registration.
262      */
263     public static final int BEGIN_USER_CUSTOM_ID = 300;
264
265     // TODO: Populate other classes
266     /**
267      * KryoNamespace which can serialize API bundle classes.
268      */
269     public static final KryoNamespace API = KryoNamespace.newBuilder()
270             .nextId(KryoNamespace.INITIAL_ID)
271             .register(BASIC)
272             .nextId(KryoNamespace.INITIAL_ID + 30)
273             .register(MISC)
274             .nextId(KryoNamespace.INITIAL_ID + 30 + 10)
275             .register(
276                     Instructions.MeterInstruction.class,
277                     MeterId.class,
278                     Version.class,
279                     ControllerNode.State.class,
280                     ApplicationState.class,
281                     ApplicationRole.class,
282                     DefaultApplication.class,
283                     Device.Type.class,
284                     Port.Type.class,
285                     ChassisId.class,
286                     DefaultControllerNode.class,
287                     DefaultDevice.class,
288                     DefaultDeviceDescription.class,
289                     DefaultHost.class,
290                     DefaultLinkDescription.class,
291                     Port.class,
292                     DefaultPortDescription.class,
293                     Element.class,
294                     Link.Type.class,
295                     Link.State.class,
296                     Timestamp.class,
297                     Leadership.class,
298                     LeadershipEvent.class,
299                     LeadershipEvent.Type.class,
300                     HostId.class,
301                     HostDescription.class,
302                     DefaultHostDescription.class,
303                     DefaultFlowEntry.class,
304                     StoredFlowEntry.class,
305                     FlowRule.Type.class,
306                     DefaultFlowRule.class,
307                     DefaultFlowEntry.class,
308                     DefaultPacketRequest.class,
309                     PacketPriority.class,
310                     FlowEntry.FlowEntryState.class,
311                     FlowId.class,
312                     DefaultTrafficSelector.class,
313                     PortCriterion.class,
314                     MetadataCriterion.class,
315                     EthCriterion.class,
316                     EthType.class,
317                     EthTypeCriterion.class,
318                     VlanIdCriterion.class,
319                     VlanPcpCriterion.class,
320                     IPDscpCriterion.class,
321                     IPEcnCriterion.class,
322                     IPProtocolCriterion.class,
323                     IPCriterion.class,
324                     TpPort.class,
325                     TcpPortCriterion.class,
326                     UdpPortCriterion.class,
327                     SctpPortCriterion.class,
328                     IcmpTypeCriterion.class,
329                     IcmpCodeCriterion.class,
330                     IPv6FlowLabelCriterion.class,
331                     Icmpv6TypeCriterion.class,
332                     Icmpv6CodeCriterion.class,
333                     IPv6NDTargetAddressCriterion.class,
334                     IPv6NDLinkLayerAddressCriterion.class,
335                     MplsCriterion.class,
336                     TunnelIdCriterion.class,
337                     IPv6ExthdrFlagsCriterion.class,
338                     LambdaCriterion.class,
339                     IndexedLambdaCriterion.class,
340                     OchSignalCriterion.class,
341                     OchSignalTypeCriterion.class,
342                     OpticalSignalTypeCriterion.class,
343                     Criterion.class,
344                     Criterion.Type.class,
345                     DefaultTrafficTreatment.class,
346                     Instructions.DropInstruction.class,
347                     Instructions.OutputInstruction.class,
348                     Instructions.GroupInstruction.class,
349                     Instructions.TableTypeTransition.class,
350                     L0ModificationInstruction.class,
351                     L0ModificationInstruction.L0SubType.class,
352                     L0ModificationInstruction.ModLambdaInstruction.class,
353                     L0ModificationInstruction.ModOchSignalInstruction.class,
354                     L2ModificationInstruction.class,
355                     L2ModificationInstruction.L2SubType.class,
356                     L2ModificationInstruction.ModEtherInstruction.class,
357                     L2ModificationInstruction.PushHeaderInstructions.class,
358                     L2ModificationInstruction.ModVlanIdInstruction.class,
359                     L2ModificationInstruction.ModVlanPcpInstruction.class,
360                     L2ModificationInstruction.PopVlanInstruction.class,
361                     L2ModificationInstruction.ModMplsLabelInstruction.class,
362                     L2ModificationInstruction.ModMplsTtlInstruction.class,
363                     L2ModificationInstruction.ModTunnelIdInstruction.class,
364                     L3ModificationInstruction.class,
365                     L3ModificationInstruction.L3SubType.class,
366                     L3ModificationInstruction.ModIPInstruction.class,
367                     L3ModificationInstruction.ModIPv6FlowLabelInstruction.class,
368                     L3ModificationInstruction.ModTtlInstruction.class,
369                     L4ModificationInstruction.class,
370                     L4ModificationInstruction.L4SubType.class,
371                     L4ModificationInstruction.ModTransportPortInstruction.class,
372                     RoleInfo.class,
373                     FlowRuleBatchEvent.class,
374                     FlowRuleBatchEvent.Type.class,
375                     FlowRuleBatchRequest.class,
376                     FlowRuleBatchOperation.class,
377                     FlowRuleEvent.class,
378                     FlowRuleEvent.Type.class,
379                     CompletedBatchOperation.class,
380                     FlowRuleBatchEntry.class,
381                     FlowRuleBatchEntry.FlowRuleOperation.class,
382                     IntentId.class,
383                     IntentState.class,
384                     //Key.class, is abstract
385                     Key.of(1L, new DefaultApplicationId(0, "bar")).getClass(), //LongKey.class
386                     Key.of("foo", new DefaultApplicationId(0, "bar")).getClass(), //StringKey.class
387                     Intent.class,
388                     ConnectivityIntent.class,
389                     PathIntent.class,
390                     DefaultPath.class,
391                     DefaultEdgeLink.class,
392                     HostToHostIntent.class,
393                     PointToPointIntent.class,
394                     MultiPointToSinglePointIntent.class,
395                     SinglePointToMultiPointIntent.class,
396                     FlowRuleIntent.class,
397                     LinkCollectionIntent.class,
398                     OpticalConnectivityIntent.class,
399                     OpticalPathIntent.class,
400                     OpticalCircuitIntent.class,
401                     LinkResourceRequest.class,
402                     DefaultLinkResourceRequest.class,
403                     BandwidthResourceRequest.class,
404                     LambdaResourceRequest.class,
405                     LambdaResource.class,
406                     BandwidthResource.class,
407                     DefaultLinkResourceAllocations.class,
408                     BandwidthResourceAllocation.class,
409                     LambdaResourceAllocation.class,
410                     ResourcePath.class,
411                     ResourceAllocation.class,
412                     // Constraints
413                     LambdaConstraint.class,
414                     BandwidthConstraint.class,
415                     LinkTypeConstraint.class,
416                     LatencyConstraint.class,
417                     WaypointConstraint.class,
418                     ObstacleConstraint.class,
419                     AnnotationConstraint.class,
420                     BooleanConstraint.class,
421                     PartialFailureConstraint.class,
422                     IntentOperation.class,
423                     FlowRuleExtPayLoad.class,
424                     Frequency.class,
425                     DefaultAnnotations.class,
426                     PortStatistics.class,
427                     DefaultPortStatistics.class,
428                     IntentDomainId.class
429             )
430             .register(new DefaultApplicationIdSerializer(), DefaultApplicationId.class)
431             .register(new URISerializer(), URI.class)
432             .register(new NodeIdSerializer(), NodeId.class)
433             .register(new ProviderIdSerializer(), ProviderId.class)
434             .register(new DeviceIdSerializer(), DeviceId.class)
435             .register(new PortNumberSerializer(), PortNumber.class)
436             .register(new DefaultPortSerializer(), DefaultPort.class)
437             .register(new LinkKeySerializer(), LinkKey.class)
438             .register(new ConnectPointSerializer(), ConnectPoint.class)
439             .register(new DefaultLinkSerializer(), DefaultLink.class)
440             .register(new MastershipTermSerializer(), MastershipTerm.class)
441             .register(new HostLocationSerializer(), HostLocation.class)
442             .register(new DefaultOutboundPacketSerializer(), DefaultOutboundPacket.class)
443             .register(new AnnotationsSerializer(), DefaultAnnotations.class)
444             .register(Versioned.class)
445             .register(MapEvent.class)
446             .register(MapEvent.Type.class)
447             .register(SetEvent.class)
448             .register(SetEvent.Type.class)
449             .register(DefaultGroupId.class)
450             .register(Annotations.class)
451             .register(OmsPort.class)
452             .register(OchPort.class)
453             .register(OduSignalType.class)
454             .register(OchSignalType.class)
455             .register(GridType.class)
456             .register(ChannelSpacing.class)
457             .register(OduCltPort.class)
458             .register(OduCltPort.SignalType.class)
459             .register(IndexedLambda.class)
460             .register(OchSignal.class)
461             .register(OduCltPortDescription.class)
462             .register(OchPortDescription.class)
463             .register(OmsPortDescription.class)
464             .register(
465                     MplsIntent.class,
466                     MplsPathIntent.class,
467                     MplsLabelResourceAllocation.class,
468                     MplsLabelResourceRequest.class,
469                     MplsLabel.class,
470                     org.onlab.packet.MplsLabel.class,
471                     org.onlab.packet.MPLS.class
472             )
473
474             .build();
475
476
477     // not to be instantiated
478     private KryoNamespaces() {}
479 }