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