Merge "Update docs header to Magnesium in the master"
[openflowplugin.git] / applications / forwardingrules-sync / src / main / java / org / opendaylight / openflowplugin / applications / frsync / impl / SyncReactorImpl.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.applications.frsync.impl;
9
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.Collections;
18 import java.util.List;
19 import java.util.Map;
20 import org.opendaylight.openflowplugin.applications.frsync.SyncPlanPushStrategy;
21 import org.opendaylight.openflowplugin.applications.frsync.SyncReactor;
22 import org.opendaylight.openflowplugin.applications.frsync.impl.strategy.SynchronizationDiffInput;
23 import org.opendaylight.openflowplugin.applications.frsync.util.CrudCounts;
24 import org.opendaylight.openflowplugin.applications.frsync.util.FlowCapableNodeLookups;
25 import org.opendaylight.openflowplugin.applications.frsync.util.ItemSyncBox;
26 import org.opendaylight.openflowplugin.applications.frsync.util.PathUtil;
27 import org.opendaylight.openflowplugin.applications.frsync.util.ReconcileUtil;
28 import org.opendaylight.openflowplugin.applications.frsync.util.SyncCrudCounters;
29 import org.opendaylight.openflowplugin.applications.frsync.util.SyncupEntry;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.opendaylight.yangtools.yang.common.RpcResult;
40 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 /**
45  * Synchronization reactor implementation, applicable for both - syncup and reconciliation.
46  */
47 public class SyncReactorImpl implements SyncReactor {
48
49     private static final Logger LOG = LoggerFactory.getLogger(SyncReactorImpl.class);
50     private final SyncPlanPushStrategy syncPlanPushStrategy;
51
52     public SyncReactorImpl(final SyncPlanPushStrategy syncPlanPushStrategy) {
53         this.syncPlanPushStrategy = Preconditions.checkNotNull(syncPlanPushStrategy, "execution strategy is mandatory");
54     }
55
56     @Override
57     public ListenableFuture<Boolean> syncup(final InstanceIdentifier<FlowCapableNode> nodeIdent,
58                                             final SyncupEntry syncupEntry) {
59         final NodeId nodeId = PathUtil.digNodeId(nodeIdent);
60         FlowCapableNode configTree = syncupEntry.getAfter();
61         FlowCapableNode operationalTree = syncupEntry.getBefore();
62         final SyncCrudCounters counters = new SyncCrudCounters();
63
64         /**
65          * instructions:
66          *  - extract diff changes and prepare change steps in safe order
67          *    - optimization: decide if updates needed
68          *  - execute chosen implementation (e.g. conventional API, bulk API, flat bulk API)
69          *  - recommended order follows:
70          * reconciliation strategy - phase 1: - add/update missing objects in following order:
71          *  - table features - groups (reordered) - meters - flows
72          * reconciliation strategy - phase 2: - remove redundant objects in following order:
73          *  - flows - meters - groups (reordered)
74          **/
75
76         final List<ItemSyncBox<Group>> groupsToAddOrUpdate =
77                 extractGroupsToAddOrUpdate(nodeId, configTree, operationalTree);
78         final ItemSyncBox<Meter> metersToAddOrUpdate = extractMetersToAddOrUpdate(nodeId, configTree, operationalTree);
79         final Map<TableKey, ItemSyncBox<Flow>> flowsToAddOrUpdate =
80                 extractFlowsToAddOrUpdate(nodeId, configTree, operationalTree);
81
82         final Map<TableKey, ItemSyncBox<Flow>> flowsToRemove =
83                 extractFlowsToRemove(nodeId, configTree, operationalTree);
84         final ItemSyncBox<Meter> metersToRemove = extractMetersToRemove(nodeId, configTree, operationalTree);
85         final List<ItemSyncBox<Group>> groupsToRemove = extractGroupsToRemove(nodeId, configTree, operationalTree);
86
87         final SynchronizationDiffInput input = new SynchronizationDiffInput(nodeIdent,
88                 groupsToAddOrUpdate, metersToAddOrUpdate, flowsToAddOrUpdate,
89                 flowsToRemove, metersToRemove, groupsToRemove);
90
91         final ListenableFuture<RpcResult<Void>> bootstrapResultFuture = RpcResultBuilder.<Void>success().buildFuture();
92         final ListenableFuture<RpcResult<Void>> resultVehicle = syncPlanPushStrategy.executeSyncStrategy(
93                 bootstrapResultFuture, input, counters);
94
95         return Futures.transform(resultVehicle, input1 -> {
96             if (input1 == null) {
97                 return false;
98             }
99             if (LOG.isDebugEnabled()) {
100                 final CrudCounts flowCrudCounts = counters.getFlowCrudCounts();
101                 final CrudCounts meterCrudCounts = counters.getMeterCrudCounts();
102                 final CrudCounts groupCrudCounts = counters.getGroupCrudCounts();
103                 LOG.debug("Syncup outcome[{}] (added/updated/removed): flow={}/{}/{}, group={}/{}/{}, "
104                                 + "meter={}/{}/{}, errors={}",
105                         nodeId.getValue(),
106                         flowCrudCounts.getAdded(), flowCrudCounts.getUpdated(), flowCrudCounts.getRemoved(),
107                         groupCrudCounts.getAdded(), groupCrudCounts.getUpdated(), groupCrudCounts.getRemoved(),
108                         meterCrudCounts.getAdded(), meterCrudCounts.getUpdated(), meterCrudCounts.getRemoved(),
109                         Arrays.toString(input1.getErrors().toArray()));
110             }
111             return input1.isSuccessful();
112         }, MoreExecutors.directExecutor());
113     }
114
115     @VisibleForTesting
116     private static List<ItemSyncBox<Group>> extractGroupsToAddOrUpdate(final NodeId nodeId,
117             final FlowCapableNode flowCapableNodeConfigured, final FlowCapableNode flowCapableNodeOperational) {
118         final List<Group> groupsConfigured = ReconcileUtil.safeGroups(flowCapableNodeConfigured);
119         final List<Group> groupsOperational = ReconcileUtil.safeGroups(flowCapableNodeOperational);
120         final Map<Long, Group> groupOperationalMap = FlowCapableNodeLookups.wrapGroupsToMap(groupsOperational);
121
122         final List<Group> pendingGroups = new ArrayList<>();
123         pendingGroups.addAll(groupsConfigured);
124
125         return ReconcileUtil.resolveAndDivideGroupDiffs(nodeId, groupOperationalMap, pendingGroups, true);
126     }
127
128     @VisibleForTesting
129     private static ItemSyncBox<Meter> extractMetersToAddOrUpdate(final NodeId nodeId,
130                                                                  final FlowCapableNode flowCapableNodeConfigured,
131                                                                  final FlowCapableNode flowCapableNodeOperational) {
132         final List<Meter> metersConfigured = ReconcileUtil.safeMeters(flowCapableNodeConfigured);
133         final List<Meter> metersOperational = ReconcileUtil.safeMeters(flowCapableNodeOperational);
134         final Map<MeterId, Meter> meterOperationalMap = FlowCapableNodeLookups.wrapMetersToMap(metersOperational);
135
136         return ReconcileUtil.resolveMeterDiffs(nodeId, meterOperationalMap, metersConfigured, true);
137     }
138
139     @VisibleForTesting
140     private static Map<TableKey, ItemSyncBox<Flow>> extractFlowsToAddOrUpdate(final NodeId nodeId,
141             final FlowCapableNode flowCapableNodeConfigured, final FlowCapableNode flowCapableNodeOperational) {
142         final List<Table> tablesConfigured = ReconcileUtil.safeTables(flowCapableNodeConfigured);
143         if (tablesConfigured.isEmpty()) {
144             return Collections.emptyMap();
145         }
146
147         final List<Table> tablesOperational = ReconcileUtil.safeTables(flowCapableNodeOperational);
148         final Map<Short, Table> tableOperationalMap = FlowCapableNodeLookups.wrapTablesToMap(tablesOperational);
149
150         return ReconcileUtil.resolveFlowDiffsInAllTables(nodeId, tableOperationalMap, tablesConfigured, true);
151     }
152
153     @VisibleForTesting
154     private static Map<TableKey, ItemSyncBox<Flow>> extractFlowsToRemove(final NodeId nodeId,
155             final FlowCapableNode flowCapableNodeConfigured, final FlowCapableNode flowCapableNodeOperational) {
156         final List<Table> tablesOperational = ReconcileUtil.safeTables(flowCapableNodeOperational);
157         if (tablesOperational.isEmpty()) {
158             return Collections.emptyMap();
159         }
160
161         final List<Table> tablesConfigured = ReconcileUtil.safeTables(flowCapableNodeConfigured);
162         final Map<Short, Table> tableConfiguredMap = FlowCapableNodeLookups.wrapTablesToMap(tablesConfigured);
163
164         return ReconcileUtil.resolveFlowDiffsInAllTables(nodeId, tableConfiguredMap, tablesOperational, false);
165     }
166
167     @VisibleForTesting
168     private static ItemSyncBox<Meter> extractMetersToRemove(final NodeId nodeId,
169                                                             final FlowCapableNode flowCapableNodeConfigured,
170                                                             final FlowCapableNode flowCapableNodeOperational) {
171         final List<Meter> metersConfigured = ReconcileUtil.safeMeters(flowCapableNodeConfigured);
172         final List<Meter> metersOperational = ReconcileUtil.safeMeters(flowCapableNodeOperational);
173         final Map<MeterId, Meter> meterConfiguredMap = FlowCapableNodeLookups.wrapMetersToMap(metersConfigured);
174
175         return ReconcileUtil.resolveMeterDiffs(nodeId, meterConfiguredMap, metersOperational, false);
176     }
177
178     @VisibleForTesting
179     private static List<ItemSyncBox<Group>> extractGroupsToRemove(final NodeId nodeId,
180                                                                   final FlowCapableNode flowCapableNodeConfigured,
181                                                                   final FlowCapableNode flowCapableNodeOperational) {
182         final List<Group> groupsConfigured = ReconcileUtil.safeGroups(flowCapableNodeConfigured);
183         final List<Group> groupsOperational = ReconcileUtil.safeGroups(flowCapableNodeOperational);
184         final Map<Long, Group> groupConfiguredMap = FlowCapableNodeLookups.wrapGroupsToMap(groupsConfigured);
185
186         final List<Group> pendingGroups = new ArrayList<>();
187         pendingGroups.addAll(groupsOperational);
188
189         return ReconcileUtil.resolveAndDivideGroupDiffs(nodeId, groupConfiguredMap, pendingGroups, false);
190     }
191 }