- @VisibleForTesting
- static KeyedInstanceIdentifier<Flow, FlowKey> createFlowPath(FlowDescriptor flowDescriptor,
- KeyedInstanceIdentifier<Node, NodeKey> nodePath) {
- return nodePath.augmentation(FlowCapableNode.class)
- .child(Table.class, flowDescriptor.getTableKey())
- .child(Flow.class, new FlowKey(flowDescriptor.getFlowId()));
+ private class UpdateFlowCallback implements FutureCallback<RpcResult<UpdateFlowOutput>> {
+ private final UpdateFlowInput input;
+
+ private UpdateFlowCallback(UpdateFlowInput input) {
+ this.input = input;
+ }
+
+ @Override
+ public void onSuccess(final RpcResult<UpdateFlowOutput> o) {
+ final DeviceFlowRegistry deviceFlowRegistry = deviceContext.getDeviceFlowRegistry();
+
+ final UpdatedFlow updated = input.getUpdatedFlow();
+ final OriginalFlow original = input.getOriginalFlow();
+ final FlowRegistryKey origFlowRegistryKey = FlowRegistryKeyFactory.create(deviceContext.getDeviceInfo().getVersion(), original);
+ final FlowRegistryKey updatedFlowRegistryKey = FlowRegistryKeyFactory.create(deviceContext.getDeviceInfo().getVersion(), updated);
+ final FlowDescriptor origFlowDescriptor = deviceFlowRegistry.retrieveIdForFlow(origFlowRegistryKey);
+
+ final boolean isUpdate = Objects.nonNull(origFlowDescriptor);
+ final FlowId fLowId = Objects.nonNull(input.getFlowRef())
+ ? input.getFlowRef().getValue().firstKeyOf(Flow.class).getId()
+ : isUpdate ? origFlowDescriptor.getFlowId() : deviceFlowRegistry.storeIfNecessary(updatedFlowRegistryKey);
+ final FlowDescriptor updatedFlowDescriptor = FlowDescriptorFactory.create(updated.getTableId(), fLowId);
+ if (isUpdate) {
+ deviceFlowRegistry.removeDescriptor(origFlowRegistryKey);
+ deviceFlowRegistry.store(updatedFlowRegistryKey, updatedFlowDescriptor);
+ }
+
+ if (itemLifecycleListener != null) {
+ final KeyedInstanceIdentifier<Flow, FlowKey> flowPath =
+ createFlowPath(
+ updatedFlowDescriptor,
+ deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
+
+ final Flow flow = new FlowBuilder(updated)
+ .setId(updatedFlowDescriptor.getFlowId())
+ .build();
+
+ if (Objects.nonNull(origFlowDescriptor)) {
+ itemLifecycleListener.onUpdated(flowPath, flow);
+ } else {
+ itemLifecycleListener.onAdded(flowPath, flow);
+ }
+ }
+ }
+
+ @Override
+ public void onFailure(final Throwable throwable) {
+ LOG.warn("Service call for updating flow={} failed, reason: {}", input, throwable);
+ }