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
9 package org.opendaylight.openflowplugin.applications.frsync.impl;
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Function;
13 import com.google.common.base.Preconditions;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import java.util.ArrayList;
17 import java.util.Collections;
18 import java.util.List;
20 import java.util.concurrent.TimeUnit;
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.FxChainUtil;
27 import org.opendaylight.openflowplugin.applications.frsync.util.ItemSyncBox;
28 import org.opendaylight.openflowplugin.applications.frsync.util.PathUtil;
29 import org.opendaylight.openflowplugin.applications.frsync.util.ReconcileUtil;
30 import org.opendaylight.openflowplugin.applications.frsync.util.SyncCrudCounters;
31 import org.opendaylight.openflowplugin.applications.frsync.util.SyncupEntry;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.opendaylight.yangtools.yang.common.RpcResult;
42 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * Synchronization reactor implementation, applicable for both - syncup and reconciliation.
49 public class SyncReactorImpl implements SyncReactor {
51 private static final Logger LOG = LoggerFactory.getLogger(SyncReactorImpl.class);
52 private final SyncPlanPushStrategy syncPlanPushStrategy;
54 public SyncReactorImpl(SyncPlanPushStrategy syncPlanPushStrategy) {
55 this.syncPlanPushStrategy = Preconditions.checkNotNull(syncPlanPushStrategy, "execution strategy is mandatory");
59 public ListenableFuture<Boolean> syncup(final InstanceIdentifier<FlowCapableNode> nodeIdent,
60 final SyncupEntry syncupEntry) {
61 final NodeId nodeId = PathUtil.digNodeId(nodeIdent);
62 FlowCapableNode configTree = syncupEntry.getAfter();
63 FlowCapableNode operationalTree = syncupEntry.getBefore();
65 LOG.trace("syncup reactor {} cfg:{} oper:{}",
67 configTree == null ? "is null" : "non null",
68 operationalTree == null ? "is null" : "non null");
69 final SyncCrudCounters counters = new SyncCrudCounters();
73 * - extract diff changes and prepare change steps in safe order
74 * - optimization: decide if updates needed
75 * - execute chosen implementation (e.g. conventional API, bulk API, flat bulk API)
76 * - recommended order follows:
77 * reconciliation strategy - phase 1: - add/update missing objects in following order:
78 * - table features - groups (reordered) - meters - flows
80 * reconciliation strategy - phase 2: - remove redundant objects in following order:
81 * - flows - meters - groups (reordered)
84 final List<ItemSyncBox<Group>> groupsToAddOrUpdate = extractGroupsToAddOrUpdate(nodeId, configTree, operationalTree);
85 final ItemSyncBox<Meter> metersToAddOrUpdate = extractMetersToAddOrUpdate(nodeId, configTree, operationalTree);
86 final Map<TableKey, ItemSyncBox<Flow>> flowsToAddOrUpdate = extractFlowsToAddOrUpdate(nodeId, configTree, operationalTree);
88 final Map<TableKey, ItemSyncBox<Flow>> flowsToRemove = extractFlowsToRemove(nodeId, configTree, operationalTree);
89 final ItemSyncBox<Meter> metersToRemove = extractMetersToRemove(nodeId, configTree, operationalTree);
90 final List<ItemSyncBox<Group>> groupsToRemove = extractGroupsToRemove(nodeId, configTree, operationalTree);
92 final SynchronizationDiffInput input = new SynchronizationDiffInput(nodeIdent,
93 groupsToAddOrUpdate, metersToAddOrUpdate, flowsToAddOrUpdate,
94 flowsToRemove, metersToRemove, groupsToRemove);
96 counters.setStartNano(System.nanoTime());
97 final ListenableFuture<RpcResult<Void>> bootstrapResultFuture = RpcResultBuilder.<Void>success().buildFuture();
98 final ListenableFuture<RpcResult<Void>> resultVehicle = syncPlanPushStrategy.executeSyncStrategy(
99 bootstrapResultFuture, input, counters);
102 Futures.addCallback(resultVehicle, FxChainUtil.logResultCallback(nodeId, "final result"));
104 return Futures.transform(resultVehicle, new Function<RpcResult<Void>, Boolean>() {
106 public Boolean apply(RpcResult<Void> input) {
111 if (LOG.isDebugEnabled()) {
112 final CrudCounts flowCrudCounts = counters.getFlowCrudCounts();
113 final CrudCounts meterCrudCounts = counters.getMeterCrudCounts();
114 final CrudCounts groupCrudCounts = counters.getGroupCrudCounts();
115 LOG.debug("syncup outcome[{}] (added/updated/removed): flow={}/{}/{}, meter={}/{}/{}, group={}/{}/{}, took={} ms",
117 flowCrudCounts.getAdded(),
118 flowCrudCounts.getUpdated(),
119 flowCrudCounts.getRemoved(),
120 meterCrudCounts.getAdded(),
121 meterCrudCounts.getUpdated(),
122 meterCrudCounts.getRemoved(),
123 groupCrudCounts.getAdded(),
124 groupCrudCounts.getUpdated(),
125 groupCrudCounts.getRemoved(),
126 TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - counters.getStartNano())
130 LOG.trace("syncup errors: {}", input.getErrors());
131 return input.isSuccessful();
137 private static List<ItemSyncBox<Group>> extractGroupsToAddOrUpdate(final NodeId nodeId,
138 final FlowCapableNode flowCapableNodeConfigured,
139 final FlowCapableNode flowCapableNodeOperational) {
140 final List<Group> groupsConfigured = ReconcileUtil.safeGroups(flowCapableNodeConfigured);
141 final List<Group> groupsOperational = ReconcileUtil.safeGroups(flowCapableNodeOperational);
142 final Map<Long, Group> groupOperationalMap = FlowCapableNodeLookups.wrapGroupsToMap(groupsOperational);
144 final List<Group> pendingGroups = new ArrayList<>();
145 pendingGroups.addAll(groupsConfigured);
147 return ReconcileUtil.resolveAndDivideGroupDiffs(nodeId, groupOperationalMap, pendingGroups, true);
151 private static ItemSyncBox<Meter> extractMetersToAddOrUpdate(final NodeId nodeId,
152 final FlowCapableNode flowCapableNodeConfigured,
153 final FlowCapableNode flowCapableNodeOperational) {
154 final List<Meter> metersConfigured = ReconcileUtil.safeMeters(flowCapableNodeConfigured);
155 final List<Meter> metersOperational = ReconcileUtil.safeMeters(flowCapableNodeOperational);
156 final Map<MeterId, Meter> meterOperationalMap = FlowCapableNodeLookups.wrapMetersToMap(metersOperational);
158 return ReconcileUtil.resolveMeterDiffs(nodeId, meterOperationalMap, metersConfigured, true);
162 private static Map<TableKey, ItemSyncBox<Flow>> extractFlowsToAddOrUpdate(final NodeId nodeId,
163 final FlowCapableNode flowCapableNodeConfigured,
164 final FlowCapableNode flowCapableNodeOperational) {
165 final List<Table> tablesConfigured = ReconcileUtil.safeTables(flowCapableNodeConfigured);
166 if (tablesConfigured.isEmpty()) {
167 return Collections.emptyMap();
170 final List<Table> tablesOperational = ReconcileUtil.safeTables(flowCapableNodeOperational);
171 final Map<Short, Table> tableOperationalMap = FlowCapableNodeLookups.wrapTablesToMap(tablesOperational);
173 return ReconcileUtil.resolveFlowDiffsInAllTables(nodeId, tableOperationalMap, tablesConfigured, true);
177 private static Map<TableKey, ItemSyncBox<Flow>> extractFlowsToRemove(final NodeId nodeId,
178 final FlowCapableNode flowCapableNodeConfigured,
179 final FlowCapableNode flowCapableNodeOperational) {
180 final List<Table> tablesOperational = ReconcileUtil.safeTables(flowCapableNodeOperational);
181 if (tablesOperational.isEmpty()) {
182 return Collections.emptyMap();
185 final List<Table> tablesConfigured = ReconcileUtil.safeTables(flowCapableNodeConfigured);
186 final Map<Short, Table> tableConfiguredMap = FlowCapableNodeLookups.wrapTablesToMap(tablesConfigured);
188 return ReconcileUtil.resolveFlowDiffsInAllTables(nodeId, tableConfiguredMap, tablesOperational, false);
192 private static ItemSyncBox<Meter> extractMetersToRemove(final NodeId nodeId,
193 final FlowCapableNode flowCapableNodeConfigured,
194 final FlowCapableNode flowCapableNodeOperational) {
195 final List<Meter> metersConfigured = ReconcileUtil.safeMeters(flowCapableNodeConfigured);
196 final List<Meter> metersOperational = ReconcileUtil.safeMeters(flowCapableNodeOperational);
197 final Map<MeterId, Meter> meterConfiguredMap = FlowCapableNodeLookups.wrapMetersToMap(metersConfigured);
199 return ReconcileUtil.resolveMeterDiffs(nodeId, meterConfiguredMap, metersOperational, false);
203 private static List<ItemSyncBox<Group>> extractGroupsToRemove(final NodeId nodeId,
204 final FlowCapableNode flowCapableNodeConfigured,
205 final FlowCapableNode flowCapableNodeOperational) {
206 final List<Group> groupsConfigured = ReconcileUtil.safeGroups(flowCapableNodeConfigured);
207 final List<Group> groupsOperational = ReconcileUtil.safeGroups(flowCapableNodeOperational);
208 final Map<Long, Group> groupConfiguredMap = FlowCapableNodeLookups.wrapGroupsToMap(groupsConfigured);
210 final List<Group> pendingGroups = new ArrayList<>();
211 pendingGroups.addAll(groupsOperational);
213 return ReconcileUtil.resolveAndDivideGroupDiffs(nodeId, groupConfiguredMap, pendingGroups, false);