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