2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.openflowplugin.applications.frsync.impl;
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.List;
21 import org.opendaylight.openflowplugin.applications.frsync.SyncPlanPushStrategy;
22 import org.opendaylight.openflowplugin.applications.frsync.SyncReactor;
23 import org.opendaylight.openflowplugin.applications.frsync.impl.strategy.SynchronizationDiffInput;
24 import org.opendaylight.openflowplugin.applications.frsync.util.CrudCounts;
25 import org.opendaylight.openflowplugin.applications.frsync.util.FlowCapableNodeLookups;
26 import org.opendaylight.openflowplugin.applications.frsync.util.ItemSyncBox;
27 import org.opendaylight.openflowplugin.applications.frsync.util.PathUtil;
28 import org.opendaylight.openflowplugin.applications.frsync.util.ReconcileUtil;
29 import org.opendaylight.openflowplugin.applications.frsync.util.SyncCrudCounters;
30 import org.opendaylight.openflowplugin.applications.frsync.util.SyncupEntry;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.opendaylight.yangtools.yang.common.RpcResult;
41 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
42 import org.opendaylight.yangtools.yang.common.Uint32;
43 import org.opendaylight.yangtools.yang.common.Uint8;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * Synchronization reactor implementation, applicable for both - syncup and reconciliation.
50 public class SyncReactorImpl implements SyncReactor {
52 private static final Logger LOG = LoggerFactory.getLogger(SyncReactorImpl.class);
53 private final SyncPlanPushStrategy syncPlanPushStrategy;
55 public SyncReactorImpl(final SyncPlanPushStrategy syncPlanPushStrategy) {
56 this.syncPlanPushStrategy = Preconditions.checkNotNull(syncPlanPushStrategy, "execution strategy is mandatory");
60 public ListenableFuture<Boolean> syncup(final InstanceIdentifier<FlowCapableNode> nodeIdent,
61 final SyncupEntry syncupEntry) {
62 final NodeId nodeId = PathUtil.digNodeId(nodeIdent);
63 FlowCapableNode configTree = syncupEntry.getAfter();
64 FlowCapableNode operationalTree = syncupEntry.getBefore();
65 final SyncCrudCounters counters = new SyncCrudCounters();
69 * - extract diff changes and prepare change steps in safe order
70 * - optimization: decide if updates needed
71 * - execute chosen implementation (e.g. conventional API, bulk API, flat bulk API)
72 * - recommended order follows:
73 * reconciliation strategy - phase 1: - add/update missing objects in following order:
74 * - table features - groups (reordered) - meters - flows
75 * reconciliation strategy - phase 2: - remove redundant objects in following order:
76 * - flows - meters - groups (reordered)
79 final List<ItemSyncBox<Group>> groupsToAddOrUpdate =
80 extractGroupsToAddOrUpdate(nodeId, configTree, operationalTree);
81 final ItemSyncBox<Meter> metersToAddOrUpdate = extractMetersToAddOrUpdate(nodeId, configTree, operationalTree);
82 final Map<TableKey, ItemSyncBox<Flow>> flowsToAddOrUpdate =
83 extractFlowsToAddOrUpdate(nodeId, configTree, operationalTree);
85 final Map<TableKey, ItemSyncBox<Flow>> flowsToRemove =
86 extractFlowsToRemove(nodeId, configTree, operationalTree);
87 final ItemSyncBox<Meter> metersToRemove = extractMetersToRemove(nodeId, configTree, operationalTree);
88 final List<ItemSyncBox<Group>> groupsToRemove = extractGroupsToRemove(nodeId, configTree, operationalTree);
90 final SynchronizationDiffInput input = new SynchronizationDiffInput(nodeIdent,
91 groupsToAddOrUpdate, metersToAddOrUpdate, flowsToAddOrUpdate,
92 flowsToRemove, metersToRemove, groupsToRemove);
94 final ListenableFuture<RpcResult<Void>> bootstrapResultFuture = RpcResultBuilder.<Void>success().buildFuture();
95 final ListenableFuture<RpcResult<Void>> resultVehicle = syncPlanPushStrategy.executeSyncStrategy(
96 bootstrapResultFuture, input, counters);
98 return Futures.transform(resultVehicle, input1 -> {
102 if (LOG.isDebugEnabled()) {
103 final CrudCounts flowCrudCounts = counters.getFlowCrudCounts();
104 final CrudCounts meterCrudCounts = counters.getMeterCrudCounts();
105 final CrudCounts groupCrudCounts = counters.getGroupCrudCounts();
106 LOG.debug("Syncup outcome[{}] (added/updated/removed): flow={}/{}/{}, group={}/{}/{}, "
107 + "meter={}/{}/{}, errors={}",
109 flowCrudCounts.getAdded(), flowCrudCounts.getUpdated(), flowCrudCounts.getRemoved(),
110 groupCrudCounts.getAdded(), groupCrudCounts.getUpdated(), groupCrudCounts.getRemoved(),
111 meterCrudCounts.getAdded(), meterCrudCounts.getUpdated(), meterCrudCounts.getRemoved(),
112 Arrays.toString(input1.getErrors().toArray()));
114 return input1.isSuccessful();
115 }, MoreExecutors.directExecutor());
119 private static List<ItemSyncBox<Group>> extractGroupsToAddOrUpdate(final NodeId nodeId,
120 final FlowCapableNode flowCapableNodeConfigured, final FlowCapableNode flowCapableNodeOperational) {
121 final Collection<Group> groupsConfigured = ReconcileUtil.safeGroups(flowCapableNodeConfigured);
122 final Collection<Group> groupsOperational = ReconcileUtil.safeGroups(flowCapableNodeOperational);
123 final Map<Uint32, Group> groupOperationalMap = FlowCapableNodeLookups.wrapGroupsToMap(groupsOperational);
125 final List<Group> pendingGroups = new ArrayList<>();
126 pendingGroups.addAll(groupsConfigured);
128 return ReconcileUtil.resolveAndDivideGroupDiffs(nodeId, groupOperationalMap, pendingGroups, true);
132 private static ItemSyncBox<Meter> extractMetersToAddOrUpdate(final NodeId nodeId,
133 final FlowCapableNode flowCapableNodeConfigured,
134 final FlowCapableNode flowCapableNodeOperational) {
135 final Collection<Meter> metersConfigured = ReconcileUtil.safeMeters(flowCapableNodeConfigured);
136 final Collection<Meter> metersOperational = ReconcileUtil.safeMeters(flowCapableNodeOperational);
137 final Map<MeterId, Meter> meterOperationalMap = FlowCapableNodeLookups.wrapMetersToMap(metersOperational);
139 return ReconcileUtil.resolveMeterDiffs(nodeId, meterOperationalMap, metersConfigured, true);
143 private static Map<TableKey, ItemSyncBox<Flow>> extractFlowsToAddOrUpdate(final NodeId nodeId,
144 final FlowCapableNode flowCapableNodeConfigured, final FlowCapableNode flowCapableNodeOperational) {
145 final Collection<Table> tablesConfigured = ReconcileUtil.safeTables(flowCapableNodeConfigured);
146 if (tablesConfigured.isEmpty()) {
147 return Collections.emptyMap();
150 final Collection<Table> tablesOperational = ReconcileUtil.safeTables(flowCapableNodeOperational);
151 final Map<Uint8, Table> tableOperationalMap = FlowCapableNodeLookups.wrapTablesToMap(tablesOperational);
153 return ReconcileUtil.resolveFlowDiffsInAllTables(nodeId, tableOperationalMap, tablesConfigured, true);
157 private static Map<TableKey, ItemSyncBox<Flow>> extractFlowsToRemove(final NodeId nodeId,
158 final FlowCapableNode flowCapableNodeConfigured, final FlowCapableNode flowCapableNodeOperational) {
159 final Collection<Table> tablesOperational = ReconcileUtil.safeTables(flowCapableNodeOperational);
160 if (tablesOperational.isEmpty()) {
161 return Collections.emptyMap();
164 final Collection<Table> tablesConfigured = ReconcileUtil.safeTables(flowCapableNodeConfigured);
165 final Map<Uint8, Table> tableConfiguredMap = FlowCapableNodeLookups.wrapTablesToMap(tablesConfigured);
167 return ReconcileUtil.resolveFlowDiffsInAllTables(nodeId, tableConfiguredMap, tablesOperational, false);
171 private static ItemSyncBox<Meter> extractMetersToRemove(final NodeId nodeId,
172 final FlowCapableNode flowCapableNodeConfigured,
173 final FlowCapableNode flowCapableNodeOperational) {
174 final Collection<Meter> metersConfigured = ReconcileUtil.safeMeters(flowCapableNodeConfigured);
175 final Collection<Meter> metersOperational = ReconcileUtil.safeMeters(flowCapableNodeOperational);
176 final Map<MeterId, Meter> meterConfiguredMap = FlowCapableNodeLookups.wrapMetersToMap(metersConfigured);
178 return ReconcileUtil.resolveMeterDiffs(nodeId, meterConfiguredMap, metersOperational, false);
182 private static List<ItemSyncBox<Group>> extractGroupsToRemove(final NodeId nodeId,
183 final FlowCapableNode flowCapableNodeConfigured,
184 final FlowCapableNode flowCapableNodeOperational) {
185 final Collection<Group> groupsConfigured = ReconcileUtil.safeGroups(flowCapableNodeConfigured);
186 final Collection<Group> groupsOperational = ReconcileUtil.safeGroups(flowCapableNodeOperational);
187 final Map<Uint32, Group> groupConfiguredMap = FlowCapableNodeLookups.wrapGroupsToMap(groupsConfigured);
189 final List<Group> pendingGroups = new ArrayList<>();
190 pendingGroups.addAll(groupsOperational);
192 return ReconcileUtil.resolveAndDivideGroupDiffs(nodeId, groupConfiguredMap, pendingGroups, false);