2a232f20f3c947499e631e8d7d5a9e05369ab32b
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / TransactCommandAggregator.java
1 /*
2  * Copyright (c) 2015, 2017 China Telecom Beijing Research Institute 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.ovsdb.hwvtepsouthbound.transact;
9
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Objects;
16 import java.util.concurrent.atomic.AtomicInteger;
17 import org.apache.commons.lang3.tuple.Pair;
18 import org.opendaylight.mdsal.binding.api.DataObjectModification;
19 import org.opendaylight.mdsal.binding.api.DataTreeModification;
20 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundConstants;
21 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
26 import org.opendaylight.yangtools.yang.binding.DataObject;
27 import org.opendaylight.yangtools.yang.binding.Identifiable;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class TransactCommandAggregator implements TransactCommand {
33     private static final Logger LOG = LoggerFactory.getLogger(TransactCommandAggregator.class);
34
35     private final List<TransactCommand> commands = new ArrayList<>();
36     private final AtomicInteger retryCount = new AtomicInteger(HwvtepSouthboundConstants.CHAIN_RETRY_COUNT);
37     private final HwvtepOperationalState operationalState;
38     /* stores the modified and deleted data for each child type of each node id
39        Map<nodeid , Pair < updated, deleted >
40        each updated/ deleted contains Map < child type, List<ChildData>>
41        child type is the child of hwvtep Global augmentation
42      */
43     private final Map<InstanceIdentifier<Node>,
44             Pair<Map<Class<? extends Identifiable>, List<Identifiable>>,
45                 Map<Class<? extends Identifiable>, List<Identifiable>>>> modifiedData = new HashMap<>();
46
47
48     public TransactCommandAggregator(HwvtepOperationalState state, Collection<DataTreeModification<Node>> changes) {
49         this.operationalState = state;
50         onDataTreeChanged(changes);
51         commands.add(new PhysicalSwitchUpdateCommand(state,changes));
52         commands.add(new PhysicalSwitchRemoveCommand(state,changes));
53         commands.add(new LogicalSwitchUpdateCommand(state,changes));
54         commands.add(new LogicalSwitchRemoveCommand(state,changes));
55         commands.add(new PhysicalPortUpdateCommand(state,changes));
56         commands.add(new PhysicalPortRemoveCommand(state,changes));
57         commands.add(new McastMacsRemoteUpdateCommand(state,changes));
58         commands.add(new McastMacsRemoteRemoveCommand(state,changes));
59         commands.add(new McastMacsLocalUpdateCommand(state,changes));
60         commands.add(new McastMacsLocalRemoveCommand(state,changes));
61         commands.add(new UcastMacsRemoteUpdateCommand(state,changes));
62         commands.add(new UcastMacsRemoteRemoveCommand(state,changes));
63         commands.add(new UcastMacsLocalUpdateCommand(state,changes));
64         commands.add(new UcastMacsLocalRemoveCommand(state,changes));
65         commands.add(new TunnelUpdateCommand(state,changes));
66         commands.add(new TunnelRemoveCommand(state,changes));
67         commands.add(new LogicalRouterUpdateCommand(state,changes));
68         commands.add(new LogicalRouterRemoveCommand(state,changes));
69     }
70
71     @Override
72     public void execute(TransactionBuilder transaction) {
73         for (TransactCommand command : commands) {
74             command.execute(transaction);
75         }
76     }
77
78     @Override
79     public void onConfigUpdate(TransactionBuilder transaction, InstanceIdentifier nodeIid, Identifiable data,
80                                InstanceIdentifier key,
81                                Object... extraData) {
82     }
83
84     @Override
85     public void doDeviceTransaction(TransactionBuilder transaction, InstanceIdentifier nodeIid, Identifiable data,
86                                     InstanceIdentifier key,
87                                     Object... extraData) {
88     }
89
90     private void onDataTreeChanged(final Collection<DataTreeModification<Node>> changes) {
91         boolean readOperationalNodes = false;
92         for (DataTreeModification<Node> change : changes) {
93             final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
94             final DataObjectModification<Node> mod = change.getRootNode();
95             final Map<Class<? extends Identifiable>, List<Identifiable>> updatedData = new HashMap<>();
96             final Map<Class<? extends Identifiable>, List<Identifiable>> deletedData = new HashMap<>();
97             extractDataChanged(key, mod, updatedData, deletedData);
98             modifiedData.put(key, Pair.of(updatedData, deletedData));
99             operationalState.setModifiedData(modifiedData);
100             if (!isMacOnlyUpdate(updatedData, deletedData)) {
101                 readOperationalNodes = true;
102             }
103         }
104         if (readOperationalNodes) {
105             operationalState.readOperationalNodes();
106         }
107     }
108
109     private static boolean isMacOnlyUpdate(final Map<Class<? extends Identifiable>, List<Identifiable>> updatedData,
110                                            final Map<Class<? extends Identifiable>, List<Identifiable>> deletedData) {
111         return updatedData.containsKey(RemoteUcastMacs.class) && updatedData.size() == 1
112                 || deletedData.containsKey(RemoteUcastMacs.class) && deletedData.size() == 1;
113     }
114
115     private static void extractDataChanged(final InstanceIdentifier<Node> key,
116                                            final DataObjectModification<Node> mod,
117                                            final Map<Class<? extends Identifiable>, List<Identifiable>> updatedData,
118                                            final Map<Class<? extends Identifiable>, List<Identifiable>> deletedData) {
119
120         extractDataChanged(mod.getModifiedChildren(), updatedData, deletedData);
121         DataObjectModification<HwvtepGlobalAugmentation> aug = mod.getModifiedAugmentation(
122                 HwvtepGlobalAugmentation.class);
123         if (aug != null) {
124             extractDataChanged(aug.getModifiedChildren(), updatedData, deletedData);
125         }
126         DataObjectModification<PhysicalSwitchAugmentation> psAug = mod.getModifiedAugmentation(
127                 PhysicalSwitchAugmentation.class);
128         if (psAug != null) {
129             extractDataChanged(psAug.getModifiedChildren(), updatedData, deletedData);
130         }
131     }
132
133     private static void extractDataChanged(
134             final Collection<? extends DataObjectModification<? extends DataObject>> children,
135                     final Map<Class<? extends Identifiable>, List<Identifiable>> updatedData,
136                     final Map<Class<? extends Identifiable>, List<Identifiable>> deletedData) {
137         if (children == null) {
138             return;
139         }
140         for (DataObjectModification<? extends DataObject> child : children) {
141             Class<? extends Identifiable> childClass = (Class<? extends Identifiable>) child.getDataType();
142             switch (child.getModificationType()) {
143                 case WRITE:
144                 case SUBTREE_MODIFIED:
145                     DataObject dataAfter = child.getDataAfter();
146                     if (!(dataAfter instanceof Identifiable)) {
147                         continue;
148                     }
149                     DataObject before = child.getDataBefore();
150                     if (Objects.equals(dataAfter, before)) {
151                         /*
152                         in cluster reboot scenarios,
153                         application rewrites the data tx.put( logicalswitchiid, logicalswitch )
154                         that time it fires the update again ignoring such updates here
155                          */
156                         continue;
157                     }
158                     Identifiable identifiable = (Identifiable) dataAfter;
159                     addToUpdatedData(updatedData, childClass, identifiable);
160                     break;
161                 case DELETE:
162                     DataObject dataBefore = child.getDataBefore();
163                     if (!(dataBefore instanceof Identifiable)) {
164                         continue;
165                     }
166                     addToUpdatedData(deletedData, childClass, (Identifiable)dataBefore);
167                     break;
168                 default:
169                     break;
170             }
171         }
172     }
173
174     private static void addToUpdatedData(Map<Class<? extends Identifiable>, List<Identifiable>> updatedData,
175                                          Class<? extends Identifiable> childClass, Identifiable identifiable) {
176         updatedData.computeIfAbsent(childClass, (cls) -> new ArrayList<>());
177         updatedData.get(childClass).add(identifiable);
178     }
179
180     @Override
181     public void onFailure(TransactionBuilder deviceTransaction) {
182         commands.forEach(cmd -> cmd.onFailure(deviceTransaction));
183         operationalState.clearIntransitKeys();
184     }
185
186     @Override
187     public void onSuccess(TransactionBuilder deviceTransaction) {
188         commands.forEach(cmd -> cmd.onSuccess(deviceTransaction));
189     }
190
191     @Override
192     public boolean retry() {
193         return retryCount.decrementAndGet() > 0;
194     }
195
196
197 }