@Override
 protected void setupKryoPool() {
   serializerPool =
       KryoNamespace.newBuilder()
           .register(DistributedStoreSerializers.STORE_COMMON)
           .nextId(DistributedStoreSerializers.STORE_CUSTOM_BEGIN)
           .build();
 }
 @Override
 protected void setupKryoPool() {
   serializerPool =
       KryoNamespace.newBuilder()
           .register(KryoNamespaces.BASIC)
           .register(COPYCAT)
           .register(ONOS_STORE)
           .build();
 }
 private StoreSerializer createSerializer(KryoNamespace ns) {
   return StoreSerializer.using(
       KryoNamespace.newBuilder()
           .register(ns)
           // not so robust way to avoid collision with other
           // user supplied registrations
           .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID + 100)
           .register(KryoNamespaces.BASIC)
           .register(LogicalTimestamp.class)
           .register(WallClockTimestamp.class)
           .register(AntiEntropyAdvertisement.class)
           .register(AntiEntropyResponse.class)
           .register(UpdateEntry.class)
           .register(MapValue.class)
           .register(MapValue.Digest.class)
           .register(UpdateRequest.class)
           .build(name() + "-ecmap"));
 }
Exemple #4
0
  @Activate
  public void activate() {

    KryoNamespace.Builder serializer =
        KryoNamespace.newBuilder()
            .register(KryoNamespaces.API)
            .register(MultiValuedTimestamp.class)
            .register(PortPair.class);

    portPairStore =
        storageService
            .<PortPairId, PortPair>eventuallyConsistentMapBuilder()
            .withName("portpairstore")
            .withSerializer(serializer)
            .withTimestampProvider((k, v) -> new WallClockTimestamp())
            .build();

    log.info("Started");
  }
  @Override
  public void next(NextObjective nextObjective) {
    if (nextObjective.type() != NextObjective.Type.BROADCAST) {
      log.error("OLT only supports broadcast groups.");
      fail(nextObjective, ObjectiveError.BADPARAMS);
    }

    if (nextObjective.next().size() != 1) {
      log.error("OLT only supports singleton broadcast groups.");
      fail(nextObjective, ObjectiveError.BADPARAMS);
    }

    TrafficTreatment treatment = nextObjective.next().stream().findFirst().get();

    GroupBucket bucket = DefaultGroupBucket.createAllGroupBucket(treatment);
    GroupKey key = new DefaultGroupKey(appKryo.serialize(nextObjective.id()));

    GroupDescription groupDesc =
        new DefaultGroupDescription(
            deviceId,
            GroupDescription.Type.ALL,
            new GroupBuckets(Collections.singletonList(bucket)),
            key,
            null,
            nextObjective.appId());

    pendingGroups.put(key, nextObjective);

    switch (nextObjective.op()) {
      case ADD:
        groupService.addGroup(groupDesc);
        break;
      case REMOVE:
        groupService.removeGroup(deviceId, key, nextObjective.appId());
        break;
      case ADD_TO_EXISTING:
      case REMOVE_FROM_EXISTING:
        // TODO: handle addition to group when caller signals it.
        break;
      default:
        log.warn("Unknown next objective operation: {}", nextObjective.op());
    }
  }
  @Activate
  public void activate() {

    mcastRib =
        storageService
            .<McastRoute, MulticastData>consistentMapBuilder()
            .withName(MCASTRIB)
            .withSerializer(
                Serializer.using(
                    KryoNamespace.newBuilder()
                        .register(KryoNamespaces.API)
                        .register(
                            AtomicReference.class,
                            MulticastData.class,
                            McastRoute.class,
                            McastRoute.Type.class)
                        .build()))
            // .withRelaxedReadConsistency()
            .build();

    mcastRoutes = mcastRib.asJavaMap();

    log.info("Started");
  }
Exemple #7
0
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(Collections.unmodifiableSet(Collections.emptySet()).getClass())
          .register(HashMap.class)
          .register(ConcurrentHashMap.class)
          .register(CopyOnWriteArraySet.class)
          .register(ArrayList.class, LinkedList.class, HashSet.class, LinkedHashSet.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.singleton(Object.class).getClass())
          .register(int[].class)
          .register(long[].class)
          .register(short[].class)
          .register(double[].class)
          .register(float[].class)
          .register(char[].class)
          .register(String[].class)
          .register(boolean[].class)
          .build("BASIC");

  /** 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(Match.class)
          .register(VlanId.class)
          .register(Frequency.class)
          .register(Bandwidth.class)
          .register(Bandwidth.bps(1L).getClass())
          .register(Bandwidth.bps(1.0).getClass())
          .build("MISC");

  /** Kryo registration Id for user custom registration. */
  public static final int BEGIN_USER_CUSTOM_ID = 500;

  // 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 + 50)
          .register(MISC)
          .nextId(KryoNamespace.INITIAL_ID + 50 + 30)
          .register(
              Instructions.MeterInstruction.class,
              MeterId.class,
              Version.class,
              ControllerNode.State.class,
              ApplicationState.class,
              ApplicationRole.class,
              DefaultApplication.class,
              Permission.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,
              Change.class,
              Leader.class,
              Leadership.class,
              LeadershipEvent.class,
              LeadershipEvent.Type.class,
              HostId.class,
              HostDescription.class,
              DefaultHostDescription.class,
              DefaultFlowEntry.class,
              StoredFlowEntry.class,
              DefaultFlowRule.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,
              MplsBosCriterion.class,
              TunnelIdCriterion.class,
              IPv6ExthdrFlagsCriterion.class,
              LambdaCriterion.class,
              OchSignalCriterion.class,
              OchSignalTypeCriterion.class,
              OduSignalIdCriterion.class,
              OduSignalTypeCriterion.class,
              ArpOpCriterion.class,
              ArpHaCriterion.class,
              ArpPaCriterion.class,
              Criterion.class,
              Criterion.Type.class,
              DefaultTrafficTreatment.class,
              Instructions.NoActionInstruction.class,
              Instructions.OutputInstruction.class,
              Instructions.GroupInstruction.class,
              Instructions.TableTypeTransition.class,
              L0ModificationInstruction.class,
              L0ModificationInstruction.L0SubType.class,
              L0ModificationInstruction.ModOchSignalInstruction.class,
              L1ModificationInstruction.class,
              L1ModificationInstruction.L1SubType.class,
              L1ModificationInstruction.ModOduSignalIdInstruction.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.ModMplsBosInstruction.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,
              OpticalOduIntent.class,
              FlowObjectiveIntent.class,
              DiscreteResource.class,
              ContinuousResource.class,
              DiscreteResourceId.class,
              ContinuousResourceId.class,
              ResourceAllocation.class,
              ResourceConsumerId.class,
              // Constraints
              BandwidthConstraint.class,
              LinkTypeConstraint.class,
              LatencyConstraint.class,
              WaypointConstraint.class,
              ObstacleConstraint.class,
              AnnotationConstraint.class,
              BooleanConstraint.class,
              PartialFailureConstraint.class,
              IntentOperation.class,
              FlowRuleExtPayLoad.class,
              DefaultAnnotations.class,
              PortStatistics.class,
              DefaultPortStatistics.class,
              IntentDomainId.class,
              TableStatisticsEntry.class,
              DefaultTableStatisticsEntry.class,
              EncapsulationConstraint.class,
              EncapsulationType.class,
              // Flow Objectives
              DefaultForwardingObjective.class,
              ForwardingObjective.Flag.class,
              DefaultFilteringObjective.class,
              FilteringObjective.Type.class,
              DefaultNextObjective.class,
              NextObjective.Type.class,
              Objective.Operation.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(
              new ExtensionInstructionSerializer(), Instructions.ExtensionInstructionWrapper.class)
          .register(new ExtensionCriterionSerializer(), ExtensionCriterion.class)
          .register(ExtensionSelectorType.class)
          .register(ExtensionTreatmentType.class)
          .register(TransactionId.class)
          .register(MapTransaction.class)
          .register(MapUpdate.class)
          .register(MapUpdate.Type.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(CltSignalType.class)
          .register(OchSignal.class)
          .register(OduSignalId.class)
          .register(OduCltPortDescription.class)
          .register(OchPortDescription.class)
          .register(OmsPortDescription.class)
          .register(TributarySlot.class)
          .register(OtuPort.class)
          .register(OtuSignalType.class)
          .register(OtuPortDescription.class)
          .register(
              MplsIntent.class,
              MplsPathIntent.class,
              org.onlab.packet.MplsLabel.class,
              org.onlab.packet.MPLS.class)
          .register(ClosedOpenRange.class)
          .register(DiscreteResourceCodec.class)
          .register(new ImmutableByteSequenceSerializer(), ImmutableByteSequence.class)
          .build("API");

  // not to be instantiated
  private KryoNamespaces() {}
}
Exemple #8
0
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(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(
              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,
              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,
              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,
              // Constraints
              LambdaConstraint.class,
              BandwidthConstraint.class,
              LinkTypeConstraint.class,
              LatencyConstraint.class,
              WaypointConstraint.class,
              ObstacleConstraint.class,
              AnnotationConstraint.class,
              BooleanConstraint.class,
              IntentOperation.class,
              FlowRuleExtPayLoad.class,
              Frequency.class,
              DefaultAnnotations.class,
              PortStatistics.class,
              DefaultPortStatistics.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() {}
}
/** Serializer for DatabaseManager's interaction with Copycat. */
public class DatabaseSerializer extends SerializerConfig {

  private static final KryoNamespace COPYCAT =
      KryoNamespace.newBuilder()
          .nextId(KryoNamespace.FLOATING_ID)
          .register(AppendRequest.class)
          .register(AppendResponse.class)
          .register(SyncRequest.class)
          .register(SyncResponse.class)
          .register(VoteRequest.class)
          .register(VoteResponse.class)
          .register(PollRequest.class)
          .register(PollResponse.class)
          .register(QueryRequest.class)
          .register(QueryResponse.class)
          .register(CommitRequest.class)
          .register(CommitResponse.class)
          .register(ReplicaInfo.class)
          .register(MemberInfo.class)
          .build();

  private static final KryoNamespace ONOS_STORE =
      KryoNamespace.newBuilder()
          .nextId(KryoNamespace.FLOATING_ID)
          .register(Versioned.class)
          .register(MapUpdate.class)
          .register(MapUpdate.Type.class)
          .register(Result.class)
          .register(UpdateResult.class)
          .register(Result.Status.class)
          .register(Transaction.class)
          .register(Transaction.State.class)
          .register(TransactionId.class)
          .register(org.onosproject.store.primitives.impl.CommitResponse.class)
          .register(Match.class)
          .register(NodeId.class)
          .build();

  private static final KryoSerializer SERIALIZER =
      new KryoSerializer() {
        @Override
        protected void setupKryoPool() {
          serializerPool =
              KryoNamespace.newBuilder()
                  .register(KryoNamespaces.BASIC)
                  .register(COPYCAT)
                  .register(ONOS_STORE)
                  .build();
        }
      };

  @Override
  public ByteBuffer writeObject(Object object) {
    return ByteBuffer.wrap(SERIALIZER.encode(object));
  }

  @Override
  public <T> T readObject(ByteBuffer buffer) {
    return SERIALIZER.decode(buffer);
  }
}
Exemple #10
0
 @Override
 public byte[] data() {
   return appKryo.serialize(key);
 }
Exemple #11
0
 private OLTPipelineGroup getGroupForNextObjective(Integer nextId) {
   NextGroup next = flowObjectiveStore.getNextGroup(nextId);
   return (OLTPipelineGroup) appKryo.deserialize(next.data());
 }
/** Manages inventory of flow rules using a distributed state management protocol. */
@Component(immediate = true, enabled = true)
@Service
public class NewDistributedFlowRuleStore
    extends AbstractStore<FlowRuleBatchEvent, FlowRuleStoreDelegate> implements FlowRuleStore {

  private final Logger log = getLogger(getClass());

  private static final int MESSAGE_HANDLER_THREAD_POOL_SIZE = 8;
  private static final boolean DEFAULT_BACKUP_ENABLED = true;
  private static final int DEFAULT_BACKUP_PERIOD_MILLIS = 2000;
  private static final long FLOW_RULE_STORE_TIMEOUT_MILLIS = 5000;
  // number of devices whose flow entries will be backed up in one communication round
  private static final int FLOW_TABLE_BACKUP_BATCH_SIZE = 1;

  @Property(
      name = "msgHandlerPoolSize",
      intValue = MESSAGE_HANDLER_THREAD_POOL_SIZE,
      label = "Number of threads in the message handler pool")
  private int msgHandlerPoolSize = MESSAGE_HANDLER_THREAD_POOL_SIZE;

  @Property(
      name = "backupEnabled",
      boolValue = DEFAULT_BACKUP_ENABLED,
      label = "Indicates whether backups are enabled or not")
  private boolean backupEnabled = DEFAULT_BACKUP_ENABLED;

  @Property(
      name = "backupPeriod",
      intValue = DEFAULT_BACKUP_PERIOD_MILLIS,
      label = "Delay in ms between successive backup runs")
  private int backupPeriod = DEFAULT_BACKUP_PERIOD_MILLIS;

  private InternalFlowTable flowTable = new InternalFlowTable();

  @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
  protected ReplicaInfoService replicaInfoManager;

  @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
  protected ClusterCommunicationService clusterCommunicator;

  @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
  protected ClusterService clusterService;

  @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
  protected DeviceService deviceService;

  @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
  protected CoreService coreService;

  @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
  protected ComponentConfigService configService;

  @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
  protected MastershipService mastershipService;

  private Map<Long, NodeId> pendingResponses = Maps.newConcurrentMap();
  private ExecutorService messageHandlingExecutor;

  private ScheduledFuture<?> backupTask;
  private final ScheduledExecutorService backupSenderExecutor =
      Executors.newSingleThreadScheduledExecutor(groupedThreads("onos/flow", "backup-sender"));

  private EventuallyConsistentMap<DeviceId, List<TableStatisticsEntry>> deviceTableStats;
  private final EventuallyConsistentMapListener<DeviceId, List<TableStatisticsEntry>>
      tableStatsListener = new InternalTableStatsListener();

  @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
  protected StorageService storageService;

  protected static final StoreSerializer SERIALIZER =
      new KryoSerializer() {
        @Override
        protected void setupKryoPool() {
          serializerPool =
              KryoNamespace.newBuilder()
                  .register(DistributedStoreSerializers.STORE_COMMON)
                  .nextId(DistributedStoreSerializers.STORE_CUSTOM_BEGIN)
                  .build();
        }
      };

  protected static final KryoNamespace.Builder SERIALIZER_BUILDER =
      KryoNamespace.newBuilder()
          .register(KryoNamespaces.API)
          .register(MastershipBasedTimestamp.class);

  private IdGenerator idGenerator;
  private NodeId local;

  @Activate
  public void activate(ComponentContext context) {
    configService.registerProperties(getClass());

    idGenerator = coreService.getIdGenerator(FlowRuleService.FLOW_OP_TOPIC);

    local = clusterService.getLocalNode().id();

    messageHandlingExecutor =
        Executors.newFixedThreadPool(
            msgHandlerPoolSize, groupedThreads("onos/store/flow", "message-handlers"));

    registerMessageHandlers(messageHandlingExecutor);

    if (backupEnabled) {
      replicaInfoManager.addListener(flowTable);
      backupTask =
          backupSenderExecutor.scheduleWithFixedDelay(
              flowTable::backup, 0, backupPeriod, TimeUnit.MILLISECONDS);
    }

    deviceTableStats =
        storageService
            .<DeviceId, List<TableStatisticsEntry>>eventuallyConsistentMapBuilder()
            .withName("onos-flow-table-stats")
            .withSerializer(SERIALIZER_BUILDER)
            .withAntiEntropyPeriod(5, TimeUnit.SECONDS)
            .withTimestampProvider((k, v) -> new WallClockTimestamp())
            .withTombstonesDisabled()
            .build();
    deviceTableStats.addListener(tableStatsListener);

    logConfig("Started");
  }

  @Deactivate
  public void deactivate(ComponentContext context) {
    if (backupEnabled) {
      replicaInfoManager.removeListener(flowTable);
      backupTask.cancel(true);
    }
    configService.unregisterProperties(getClass(), false);
    unregisterMessageHandlers();
    deviceTableStats.removeListener(tableStatsListener);
    deviceTableStats.destroy();
    messageHandlingExecutor.shutdownNow();
    backupSenderExecutor.shutdownNow();
    log.info("Stopped");
  }

  @SuppressWarnings("rawtypes")
  @Modified
  public void modified(ComponentContext context) {
    if (context == null) {
      backupEnabled = DEFAULT_BACKUP_ENABLED;
      logConfig("Default config");
      return;
    }

    Dictionary properties = context.getProperties();
    int newPoolSize;
    boolean newBackupEnabled;
    int newBackupPeriod;
    try {
      String s = get(properties, "msgHandlerPoolSize");
      newPoolSize = isNullOrEmpty(s) ? msgHandlerPoolSize : Integer.parseInt(s.trim());

      s = get(properties, "backupEnabled");
      newBackupEnabled = isNullOrEmpty(s) ? backupEnabled : Boolean.parseBoolean(s.trim());

      s = get(properties, "backupPeriod");
      newBackupPeriod = isNullOrEmpty(s) ? backupPeriod : Integer.parseInt(s.trim());

    } catch (NumberFormatException | ClassCastException e) {
      newPoolSize = MESSAGE_HANDLER_THREAD_POOL_SIZE;
      newBackupEnabled = DEFAULT_BACKUP_ENABLED;
      newBackupPeriod = DEFAULT_BACKUP_PERIOD_MILLIS;
    }

    boolean restartBackupTask = false;
    if (newBackupEnabled != backupEnabled) {
      backupEnabled = newBackupEnabled;
      if (!backupEnabled) {
        replicaInfoManager.removeListener(flowTable);
        if (backupTask != null) {
          backupTask.cancel(false);
          backupTask = null;
        }
      } else {
        replicaInfoManager.addListener(flowTable);
      }
      restartBackupTask = backupEnabled;
    }
    if (newBackupPeriod != backupPeriod) {
      backupPeriod = newBackupPeriod;
      restartBackupTask = backupEnabled;
    }
    if (restartBackupTask) {
      if (backupTask != null) {
        // cancel previously running task
        backupTask.cancel(false);
      }
      backupTask =
          backupSenderExecutor.scheduleWithFixedDelay(
              flowTable::backup, 0, backupPeriod, TimeUnit.MILLISECONDS);
    }
    if (newPoolSize != msgHandlerPoolSize) {
      msgHandlerPoolSize = newPoolSize;
      ExecutorService oldMsgHandler = messageHandlingExecutor;
      messageHandlingExecutor =
          Executors.newFixedThreadPool(
              msgHandlerPoolSize, groupedThreads("onos/store/flow", "message-handlers"));

      // replace previously registered handlers.
      registerMessageHandlers(messageHandlingExecutor);
      oldMsgHandler.shutdown();
    }
    logConfig("Reconfigured");
  }

  private void registerMessageHandlers(ExecutorService executor) {

    clusterCommunicator.addSubscriber(APPLY_BATCH_FLOWS, new OnStoreBatch(), executor);
    clusterCommunicator.<FlowRuleBatchEvent>addSubscriber(
        REMOTE_APPLY_COMPLETED, SERIALIZER::decode, this::notifyDelegate, executor);
    clusterCommunicator.addSubscriber(
        GET_FLOW_ENTRY, SERIALIZER::decode, flowTable::getFlowEntry, SERIALIZER::encode, executor);
    clusterCommunicator.addSubscriber(
        GET_DEVICE_FLOW_ENTRIES,
        SERIALIZER::decode,
        flowTable::getFlowEntries,
        SERIALIZER::encode,
        executor);
    clusterCommunicator.addSubscriber(
        REMOVE_FLOW_ENTRY,
        SERIALIZER::decode,
        this::removeFlowRuleInternal,
        SERIALIZER::encode,
        executor);
    clusterCommunicator.addSubscriber(
        REMOVE_FLOW_ENTRY,
        SERIALIZER::decode,
        this::removeFlowRuleInternal,
        SERIALIZER::encode,
        executor);
    clusterCommunicator.addSubscriber(
        FLOW_TABLE_BACKUP,
        SERIALIZER::decode,
        flowTable::onBackupReceipt,
        SERIALIZER::encode,
        executor);
  }

  private void unregisterMessageHandlers() {
    clusterCommunicator.removeSubscriber(REMOVE_FLOW_ENTRY);
    clusterCommunicator.removeSubscriber(GET_DEVICE_FLOW_ENTRIES);
    clusterCommunicator.removeSubscriber(GET_FLOW_ENTRY);
    clusterCommunicator.removeSubscriber(APPLY_BATCH_FLOWS);
    clusterCommunicator.removeSubscriber(REMOTE_APPLY_COMPLETED);
    clusterCommunicator.removeSubscriber(FLOW_TABLE_BACKUP);
  }

  private void logConfig(String prefix) {
    log.info(
        "{} with msgHandlerPoolSize = {}; backupEnabled = {}, backupPeriod = {}",
        prefix,
        msgHandlerPoolSize,
        backupEnabled,
        backupPeriod);
  }

  // This is not a efficient operation on a distributed sharded
  // flow store. We need to revisit the need for this operation or at least
  // make it device specific.
  @Override
  public int getFlowRuleCount() {
    AtomicInteger sum = new AtomicInteger(0);
    deviceService
        .getDevices()
        .forEach(device -> sum.addAndGet(Iterables.size(getFlowEntries(device.id()))));
    return sum.get();
  }

  @Override
  public FlowEntry getFlowEntry(FlowRule rule) {
    NodeId master = mastershipService.getMasterFor(rule.deviceId());

    if (master == null) {
      log.debug("Failed to getFlowEntry: No master for {}", rule.deviceId());
      return null;
    }

    if (Objects.equal(local, master)) {
      return flowTable.getFlowEntry(rule);
    }

    log.trace(
        "Forwarding getFlowEntry to {}, which is the primary (master) for device {}",
        master,
        rule.deviceId());

    return Tools.futureGetOrElse(
        clusterCommunicator.sendAndReceive(
            rule,
            FlowStoreMessageSubjects.GET_FLOW_ENTRY,
            SERIALIZER::encode,
            SERIALIZER::decode,
            master),
        FLOW_RULE_STORE_TIMEOUT_MILLIS,
        TimeUnit.MILLISECONDS,
        null);
  }

  @Override
  public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
    NodeId master = mastershipService.getMasterFor(deviceId);

    if (master == null) {
      log.debug("Failed to getFlowEntries: No master for {}", deviceId);
      return Collections.emptyList();
    }

    if (Objects.equal(local, master)) {
      return flowTable.getFlowEntries(deviceId);
    }

    log.trace(
        "Forwarding getFlowEntries to {}, which is the primary (master) for device {}",
        master,
        deviceId);

    return Tools.futureGetOrElse(
        clusterCommunicator.sendAndReceive(
            deviceId,
            FlowStoreMessageSubjects.GET_DEVICE_FLOW_ENTRIES,
            SERIALIZER::encode,
            SERIALIZER::decode,
            master),
        FLOW_RULE_STORE_TIMEOUT_MILLIS,
        TimeUnit.MILLISECONDS,
        Collections.emptyList());
  }

  @Override
  public void storeFlowRule(FlowRule rule) {
    storeBatch(
        new FlowRuleBatchOperation(
            Collections.singletonList(new FlowRuleBatchEntry(FlowRuleOperation.ADD, rule)),
            rule.deviceId(),
            idGenerator.getNewId()));
  }

  @Override
  public void storeBatch(FlowRuleBatchOperation operation) {
    if (operation.getOperations().isEmpty()) {
      notifyDelegate(
          FlowRuleBatchEvent.completed(
              new FlowRuleBatchRequest(operation.id(), Collections.emptySet()),
              new CompletedBatchOperation(true, Collections.emptySet(), operation.deviceId())));
      return;
    }

    DeviceId deviceId = operation.deviceId();
    NodeId master = mastershipService.getMasterFor(deviceId);

    if (master == null) {
      log.warn("No master for {} : flows will be marked for removal", deviceId);

      updateStoreInternal(operation);

      notifyDelegate(
          FlowRuleBatchEvent.completed(
              new FlowRuleBatchRequest(operation.id(), Collections.emptySet()),
              new CompletedBatchOperation(true, Collections.emptySet(), operation.deviceId())));
      return;
    }

    if (Objects.equal(local, master)) {
      storeBatchInternal(operation);
      return;
    }

    log.trace(
        "Forwarding storeBatch to {}, which is the primary (master) for device {}",
        master,
        deviceId);

    clusterCommunicator
        .unicast(operation, APPLY_BATCH_FLOWS, SERIALIZER::encode, master)
        .whenComplete(
            (result, error) -> {
              if (error != null) {
                log.warn("Failed to storeBatch: {} to {}", operation, master, error);

                Set<FlowRule> allFailures =
                    operation
                        .getOperations()
                        .stream()
                        .map(op -> op.target())
                        .collect(Collectors.toSet());

                notifyDelegate(
                    FlowRuleBatchEvent.completed(
                        new FlowRuleBatchRequest(operation.id(), Collections.emptySet()),
                        new CompletedBatchOperation(false, allFailures, deviceId)));
              }
            });
  }

  private void storeBatchInternal(FlowRuleBatchOperation operation) {

    final DeviceId did = operation.deviceId();
    // final Collection<FlowEntry> ft = flowTable.getFlowEntries(did);
    Set<FlowRuleBatchEntry> currentOps = updateStoreInternal(operation);
    if (currentOps.isEmpty()) {
      batchOperationComplete(
          FlowRuleBatchEvent.completed(
              new FlowRuleBatchRequest(operation.id(), Collections.emptySet()),
              new CompletedBatchOperation(true, Collections.emptySet(), did)));
      return;
    }

    notifyDelegate(
        FlowRuleBatchEvent.requested(
            new FlowRuleBatchRequest(operation.id(), currentOps), operation.deviceId()));
  }

  private Set<FlowRuleBatchEntry> updateStoreInternal(FlowRuleBatchOperation operation) {
    return operation
        .getOperations()
        .stream()
        .map(
            op -> {
              StoredFlowEntry entry;
              switch (op.operator()) {
                case ADD:
                  entry = new DefaultFlowEntry(op.target());
                  // always add requested FlowRule
                  // Note: 2 equal FlowEntry may have different treatment
                  flowTable.remove(entry.deviceId(), entry);
                  flowTable.add(entry);

                  return op;
                case REMOVE:
                  entry = flowTable.getFlowEntry(op.target());
                  if (entry != null) {
                    entry.setState(FlowEntryState.PENDING_REMOVE);
                    return op;
                  }
                  break;
                case MODIFY:
                  // TODO: figure this out at some point
                  break;
                default:
                  log.warn("Unknown flow operation operator: {}", op.operator());
              }
              return null;
            })
        .filter(op -> op != null)
        .collect(Collectors.toSet());
  }

  @Override
  public void deleteFlowRule(FlowRule rule) {
    storeBatch(
        new FlowRuleBatchOperation(
            Collections.singletonList(new FlowRuleBatchEntry(FlowRuleOperation.REMOVE, rule)),
            rule.deviceId(),
            idGenerator.getNewId()));
  }

  @Override
  public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) {
    NodeId master = mastershipService.getMasterFor(rule.deviceId());
    if (Objects.equal(local, master)) {
      return addOrUpdateFlowRuleInternal(rule);
    }

    log.warn("Tried to update FlowRule {} state," + " while the Node was not the master.", rule);
    return null;
  }

  private FlowRuleEvent addOrUpdateFlowRuleInternal(FlowEntry rule) {
    // check if this new rule is an update to an existing entry
    StoredFlowEntry stored = flowTable.getFlowEntry(rule);
    if (stored != null) {
      stored.setBytes(rule.bytes());
      stored.setLife(rule.life());
      stored.setPackets(rule.packets());
      if (stored.state() == FlowEntryState.PENDING_ADD) {
        stored.setState(FlowEntryState.ADDED);
        return new FlowRuleEvent(Type.RULE_ADDED, rule);
      }
      return new FlowRuleEvent(Type.RULE_UPDATED, rule);
    }

    // TODO: Confirm if this behavior is correct. See SimpleFlowRuleStore
    // TODO: also update backup if the behavior is correct.
    flowTable.add(rule);
    return null;
  }

  @Override
  public FlowRuleEvent removeFlowRule(FlowEntry rule) {
    final DeviceId deviceId = rule.deviceId();
    NodeId master = mastershipService.getMasterFor(deviceId);

    if (Objects.equal(local, master)) {
      // bypass and handle it locally
      return removeFlowRuleInternal(rule);
    }

    if (master == null) {
      log.warn("Failed to removeFlowRule: No master for {}", deviceId);
      // TODO: revisit if this should be null (="no-op") or Exception
      return null;
    }

    log.trace(
        "Forwarding removeFlowRule to {}, which is the master for device {}", master, deviceId);

    return Futures.get(
        clusterCommunicator.sendAndReceive(
            rule, REMOVE_FLOW_ENTRY, SERIALIZER::encode, SERIALIZER::decode, master),
        FLOW_RULE_STORE_TIMEOUT_MILLIS,
        TimeUnit.MILLISECONDS,
        RuntimeException.class);
  }

  private FlowRuleEvent removeFlowRuleInternal(FlowEntry rule) {
    final DeviceId deviceId = rule.deviceId();
    // This is where one could mark a rule as removed and still keep it in the store.
    final boolean removed = flowTable.remove(deviceId, rule); // flowEntries.remove(deviceId, rule);
    return removed ? new FlowRuleEvent(RULE_REMOVED, rule) : null;
  }

  @Override
  public void batchOperationComplete(FlowRuleBatchEvent event) {
    // FIXME: need a per device pending response
    NodeId nodeId = pendingResponses.remove(event.subject().batchId());
    if (nodeId == null) {
      notifyDelegate(event);
    } else {
      // TODO check unicast return value
      clusterCommunicator.unicast(event, REMOTE_APPLY_COMPLETED, SERIALIZER::encode, nodeId);
      // error log: log.warn("Failed to respond to peer for batch operation result");
    }
  }

  private final class OnStoreBatch implements ClusterMessageHandler {

    @Override
    public void handle(final ClusterMessage message) {
      FlowRuleBatchOperation operation = SERIALIZER.decode(message.payload());
      log.debug("received batch request {}", operation);

      final DeviceId deviceId = operation.deviceId();
      NodeId master = mastershipService.getMasterFor(deviceId);
      if (!Objects.equal(local, master)) {
        Set<FlowRule> failures = new HashSet<>(operation.size());
        for (FlowRuleBatchEntry op : operation.getOperations()) {
          failures.add(op.target());
        }
        CompletedBatchOperation allFailed = new CompletedBatchOperation(false, failures, deviceId);
        // This node is no longer the master, respond as all failed.
        // TODO: we might want to wrap response in envelope
        // to distinguish sw programming failure and hand over
        // it make sense in the latter case to retry immediately.
        message.respond(SERIALIZER.encode(allFailed));
        return;
      }

      pendingResponses.put(operation.id(), message.sender());
      storeBatchInternal(operation);
    }
  }

  private class InternalFlowTable implements ReplicaInfoEventListener {

    private final Map<DeviceId, Map<FlowId, Set<StoredFlowEntry>>> flowEntries =
        Maps.newConcurrentMap();

    private final Map<DeviceId, Long> lastBackupTimes = Maps.newConcurrentMap();
    private final Map<DeviceId, Long> lastUpdateTimes = Maps.newConcurrentMap();
    private final Map<DeviceId, NodeId> lastBackupNodes = Maps.newConcurrentMap();

    @Override
    public void event(ReplicaInfoEvent event) {
      if (!backupEnabled) {
        return;
      }
      if (event.type() == ReplicaInfoEvent.Type.BACKUPS_CHANGED) {
        DeviceId deviceId = event.subject();
        NodeId master = mastershipService.getMasterFor(deviceId);
        if (!Objects.equal(local, master)) {
          // ignore since this event is for a device this node does not manage.
          return;
        }
        NodeId newBackupNode = getBackupNode(deviceId);
        NodeId currentBackupNode = lastBackupNodes.get(deviceId);
        if (Objects.equal(newBackupNode, currentBackupNode)) {
          // ignore since backup location hasn't changed.
          return;
        }
        if (currentBackupNode != null && newBackupNode == null) {
          // Current backup node is most likely down and no alternate backup node
          // has been chosen. Clear current backup location so that we can resume
          // backups when either current backup comes online or a different backup node
          // is chosen.
          log.warn(
              "Lost backup location {} for deviceId {} and no alternate backup node exists. "
                  + "Flows can be lost if the master goes down",
              currentBackupNode,
              deviceId);
          lastBackupNodes.remove(deviceId);
          lastBackupTimes.remove(deviceId);
          return;
          // TODO: Pick any available node as backup and ensure hand-off occurs when
          // a new master is elected.
        }
        log.debug(
            "Backup location for {} has changed from {} to {}.",
            deviceId,
            currentBackupNode,
            newBackupNode);
        backupSenderExecutor.schedule(
            () -> backupFlowEntries(newBackupNode, Sets.newHashSet(deviceId)), 0, TimeUnit.SECONDS);
      }
    }

    private void sendBackups(NodeId nodeId, Set<DeviceId> deviceIds) {
      // split up the devices into smaller batches and send them separately.
      Iterables.partition(deviceIds, FLOW_TABLE_BACKUP_BATCH_SIZE)
          .forEach(ids -> backupFlowEntries(nodeId, Sets.newHashSet(ids)));
    }

    private void backupFlowEntries(NodeId nodeId, Set<DeviceId> deviceIds) {
      if (deviceIds.isEmpty()) {
        return;
      }
      log.debug("Sending flowEntries for devices {} to {} as backup.", deviceIds, nodeId);
      Map<DeviceId, Map<FlowId, Set<StoredFlowEntry>>> deviceFlowEntries = Maps.newConcurrentMap();
      deviceIds.forEach(id -> deviceFlowEntries.put(id, ImmutableMap.copyOf(getFlowTable(id))));
      clusterCommunicator
          .<Map<DeviceId, Map<FlowId, Set<StoredFlowEntry>>>, Set<DeviceId>>sendAndReceive(
              deviceFlowEntries, FLOW_TABLE_BACKUP, SERIALIZER::encode, SERIALIZER::decode, nodeId)
          .whenComplete(
              (backedupDevices, error) -> {
                Set<DeviceId> devicesNotBackedup =
                    error != null
                        ? deviceFlowEntries.keySet()
                        : Sets.difference(deviceFlowEntries.keySet(), backedupDevices);
                if (devicesNotBackedup.size() > 0) {
                  log.warn(
                      "Failed to backup devices: {}. Reason: {}",
                      devicesNotBackedup,
                      error.getMessage());
                }
                if (backedupDevices != null) {
                  backedupDevices.forEach(
                      id -> {
                        lastBackupTimes.put(id, System.currentTimeMillis());
                        lastBackupNodes.put(id, nodeId);
                      });
                }
              });
    }

    /**
     * Returns the flow table for specified device.
     *
     * @param deviceId identifier of the device
     * @return Map representing Flow Table of given device.
     */
    private Map<FlowId, Set<StoredFlowEntry>> getFlowTable(DeviceId deviceId) {
      return flowEntries.computeIfAbsent(deviceId, id -> Maps.newConcurrentMap());
    }

    private Set<StoredFlowEntry> getFlowEntriesInternal(DeviceId deviceId, FlowId flowId) {
      return getFlowTable(deviceId).computeIfAbsent(flowId, id -> Sets.newCopyOnWriteArraySet());
    }

    private StoredFlowEntry getFlowEntryInternal(FlowRule rule) {
      Set<StoredFlowEntry> flowEntries = getFlowEntriesInternal(rule.deviceId(), rule.id());
      return flowEntries
          .stream()
          .filter(entry -> Objects.equal(entry, rule))
          .findAny()
          .orElse(null);
    }

    private Set<FlowEntry> getFlowEntriesInternal(DeviceId deviceId) {
      Set<FlowEntry> result = Sets.newHashSet();
      getFlowTable(deviceId).values().forEach(result::addAll);
      return result;
    }

    public StoredFlowEntry getFlowEntry(FlowRule rule) {
      return getFlowEntryInternal(rule);
    }

    public Set<FlowEntry> getFlowEntries(DeviceId deviceId) {
      return getFlowEntriesInternal(deviceId);
    }

    public void add(FlowEntry rule) {
      getFlowEntriesInternal(rule.deviceId(), rule.id()).add((StoredFlowEntry) rule);
      lastUpdateTimes.put(rule.deviceId(), System.currentTimeMillis());
    }

    public boolean remove(DeviceId deviceId, FlowEntry rule) {
      try {
        return getFlowEntriesInternal(deviceId, rule.id()).remove(rule);
      } finally {
        lastUpdateTimes.put(deviceId, System.currentTimeMillis());
      }
    }

    private NodeId getBackupNode(DeviceId deviceId) {
      List<NodeId> deviceStandbys = replicaInfoManager.getReplicaInfoFor(deviceId).backups();
      // pick the standby which is most likely to become next master
      return deviceStandbys.isEmpty() ? null : deviceStandbys.get(0);
    }

    private void backup() {
      if (!backupEnabled) {
        return;
      }
      try {
        // determine the set of devices that we need to backup during this run.
        Set<DeviceId> devicesToBackup =
            mastershipService
                .getDevicesOf(local)
                .stream()
                .filter(
                    deviceId -> {
                      Long lastBackupTime = lastBackupTimes.get(deviceId);
                      Long lastUpdateTime = lastUpdateTimes.get(deviceId);
                      NodeId lastBackupNode = lastBackupNodes.get(deviceId);
                      NodeId newBackupNode = getBackupNode(deviceId);
                      return lastBackupTime == null
                          || !Objects.equal(lastBackupNode, newBackupNode)
                          || (lastUpdateTime != null && lastUpdateTime > lastBackupTime);
                    })
                .collect(Collectors.toSet());

        // compute a mapping from node to the set of devices whose flow entries it should backup
        Map<NodeId, Set<DeviceId>> devicesToBackupByNode = Maps.newHashMap();
        devicesToBackup.forEach(
            deviceId -> {
              NodeId backupLocation = getBackupNode(deviceId);
              if (backupLocation != null) {
                devicesToBackupByNode
                    .computeIfAbsent(backupLocation, nodeId -> Sets.newHashSet())
                    .add(deviceId);
              }
            });
        // send the device flow entries to their respective backup nodes
        devicesToBackupByNode.forEach(this::sendBackups);
      } catch (Exception e) {
        log.error("Backup failed.", e);
      }
    }

    private Set<DeviceId> onBackupReceipt(
        Map<DeviceId, Map<FlowId, Set<StoredFlowEntry>>> flowTables) {
      log.debug("Received flowEntries for {} to backup", flowTables.keySet());
      Set<DeviceId> backedupDevices = Sets.newHashSet();
      try {
        flowTables.forEach(
            (deviceId, deviceFlowTable) -> {
              // Only process those devices are that not managed by the local node.
              if (!Objects.equal(local, mastershipService.getMasterFor(deviceId))) {
                Map<FlowId, Set<StoredFlowEntry>> backupFlowTable = getFlowTable(deviceId);
                backupFlowTable.clear();
                backupFlowTable.putAll(deviceFlowTable);
                backedupDevices.add(deviceId);
              }
            });
      } catch (Exception e) {
        log.warn("Failure processing backup request", e);
      }
      return backedupDevices;
    }
  }

  @Override
  public FlowRuleEvent updateTableStatistics(
      DeviceId deviceId, List<TableStatisticsEntry> tableStats) {
    deviceTableStats.put(deviceId, tableStats);
    return null;
  }

  @Override
  public Iterable<TableStatisticsEntry> getTableStatistics(DeviceId deviceId) {
    NodeId master = mastershipService.getMasterFor(deviceId);

    if (master == null) {
      log.debug("Failed to getTableStats: No master for {}", deviceId);
      return Collections.emptyList();
    }

    List<TableStatisticsEntry> tableStats = deviceTableStats.get(deviceId);
    if (tableStats == null) {
      return Collections.emptyList();
    }
    return ImmutableList.copyOf(tableStats);
  }

  private class InternalTableStatsListener
      implements EventuallyConsistentMapListener<DeviceId, List<TableStatisticsEntry>> {
    @Override
    public void event(EventuallyConsistentMapEvent<DeviceId, List<TableStatisticsEntry>> event) {
      // TODO: Generate an event to listeners (do we need?)
    }
  }
}