bug 6579 added dependency queue
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / McastMacsRemoteUpdateCommand.java
1 /*
2  * Copyright (c) 2015, 2016 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
9 package org.opendaylight.ovsdb.hwvtepsouthbound.transact;
10
11 import com.google.common.base.Optional;
12 import com.google.common.collect.Lists;
13 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
14 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
15 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundConstants;
16 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundUtil;
17 import org.opendaylight.ovsdb.lib.notation.UUID;
18 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
19 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
20 import org.opendaylight.ovsdb.schema.hardwarevtep.McastMacsRemote;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
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.hwvtep.global.attributes.LogicalSwitches;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.locator.set.attributes.LocatorSet;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.Collections;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Map.Entry;
38
39 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
40
41 public class McastMacsRemoteUpdateCommand extends AbstractTransactCommand {
42     private static final Logger LOG = LoggerFactory.getLogger(McastMacsRemoteUpdateCommand.class);
43     private static final McastMacUnMetDependencyGetter MCAST_MAC_DATA_VALIDATOR = new McastMacUnMetDependencyGetter();
44
45     public McastMacsRemoteUpdateCommand(HwvtepOperationalState state,
46             Collection<DataTreeModification<Node>> changes) {
47         super(state, changes);
48     }
49
50     @Override
51     public void execute(TransactionBuilder transaction) {
52         Map<InstanceIdentifier<Node>, List<RemoteMcastMacs>> createds =
53                 extractCreated(getChanges(),RemoteMcastMacs.class);
54         if (!createds.isEmpty()) {
55             for (Entry<InstanceIdentifier<Node>, List<RemoteMcastMacs>> created:
56                 createds.entrySet()) {
57                 updateMcastMacRemote(transaction,  created.getKey(), created.getValue());
58             }
59         }
60         Map<InstanceIdentifier<Node>, List<RemoteMcastMacs>> updateds =
61                 extractUpdated(getChanges(),RemoteMcastMacs.class);
62         if (!updateds.isEmpty()) {
63             for (Entry<InstanceIdentifier<Node>, List<RemoteMcastMacs>> updated:
64                 updateds.entrySet()) {
65                 updateMcastMacRemote(transaction,  updated.getKey(), updated.getValue());
66             }
67         }
68     }
69
70     private void updateMcastMacRemote(TransactionBuilder transaction,
71             InstanceIdentifier<Node> instanceIdentifier, List<RemoteMcastMacs> macList) {
72         for (RemoteMcastMacs mac: macList) {
73             LOG.debug("Creating remoteMcastMacs, mac address: {}", mac.getMacEntryKey().getValue());
74             Optional<RemoteMcastMacs> operationalMacOptional =
75                     getOperationalState().getRemoteMcastMacs(instanceIdentifier, mac.getKey());
76             McastMacsRemote mcastMacsRemote = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), McastMacsRemote.class);
77             setIpAddress(mcastMacsRemote, mac);
78             setLocatorSet(transaction, mcastMacsRemote, mac);
79             setLogicalSwitch(mcastMacsRemote, mac);
80             if (!operationalMacOptional.isPresent()) {
81                 setMac(mcastMacsRemote, mac, operationalMacOptional);
82                 LOG.trace("execute: create RemoteMcastMac entry: {}", mcastMacsRemote);
83                 transaction.add(op.insert(mcastMacsRemote));
84                 transaction.add(op.comment("McastMacRemote: Creating " + mac.getMacEntryKey().getValue()));
85             } else if (operationalMacOptional.get().getMacEntryUuid() != null) {
86                 UUID macEntryUUID = new UUID(operationalMacOptional.get().getMacEntryUuid().getValue());
87                 McastMacsRemote extraMac = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
88                                 McastMacsRemote.class, null);
89                 extraMac.getUuidColumn().setData(macEntryUUID);
90                 LOG.trace("execute: update RemoteMcastMac entry: {}", mcastMacsRemote);
91                 transaction.add(op.update(mcastMacsRemote)
92                         .where(extraMac.getUuidColumn().getSchema().opEqual(macEntryUUID))
93                         .build());
94                 transaction.add(op.comment("McastMacRemote: Updating " + macEntryUUID));
95             } else {
96                 LOG.warn("Unable to update remoteMcastMacs {} because uuid not found in the operational store",
97                                 mac.getMacEntryKey().getValue());
98             }
99         }
100     }
101
102     private void setLogicalSwitch(McastMacsRemote mcastMacsRemote, RemoteMcastMacs inputMac) {
103         if (inputMac.getLogicalSwitchRef() != null) {
104             @SuppressWarnings("unchecked")
105             InstanceIdentifier<LogicalSwitches> lswitchIid = (InstanceIdentifier<LogicalSwitches>) inputMac.getLogicalSwitchRef().getValue();
106             Optional<LogicalSwitches> operationalSwitchOptional =
107                     getOperationalState().getLogicalSwitches(lswitchIid);
108             if (operationalSwitchOptional.isPresent()) {
109                 Uuid logicalSwitchUuid = operationalSwitchOptional.get().getLogicalSwitchUuid();
110                 UUID logicalSwitchUUID = new UUID(logicalSwitchUuid.getValue());
111                 mcastMacsRemote.setLogicalSwitch(logicalSwitchUUID);
112             } else {
113                 mcastMacsRemote.setLogicalSwitch(TransactUtils.getLogicalSwitchUUID(lswitchIid));
114             }
115         }
116     }
117
118     private void setLocatorSet(TransactionBuilder transaction, McastMacsRemote mcastMacsRemote, RemoteMcastMacs inputMac) {
119         if (inputMac.getLocatorSet() != null && !inputMac.getLocatorSet().isEmpty()) {
120             UUID locatorSetUuid = TransactUtils.createPhysicalLocatorSet(getOperationalState(), transaction, inputMac.getLocatorSet());
121             mcastMacsRemote.setLocatorSet(locatorSetUuid);
122         }
123     }
124
125     private void setIpAddress(McastMacsRemote mcastMacsRemote, RemoteMcastMacs inputMac) {
126         if (inputMac.getIpaddr() != null) {
127             mcastMacsRemote.setIpAddress(inputMac.getIpaddr().getIpv4Address().getValue());
128         }
129     }
130
131     private void setMac(McastMacsRemote mcastMacsRemote, RemoteMcastMacs inputMac,
132             Optional<RemoteMcastMacs> inputSwitchOptional) {
133         if (inputMac.getMacEntryKey() != null) {
134             if (inputMac.getMacEntryKey().equals(HwvtepSouthboundConstants.UNKNOWN_DST_MAC)) {
135                 mcastMacsRemote.setMac(HwvtepSouthboundConstants.UNKNOWN_DST_STRING);
136             } else {
137                 mcastMacsRemote.setMac(inputMac.getMacEntryKey().getValue());
138             }
139         } else if (inputSwitchOptional.isPresent() && inputSwitchOptional.get().getMacEntryKey() != null) {
140             mcastMacsRemote.setMac(inputSwitchOptional.get().getMacEntryKey().getValue());
141         }
142     }
143
144     private Map<InstanceIdentifier<Node>, List<RemoteMcastMacs>> extractCreated(
145             Collection<DataTreeModification<Node>> changes, Class<RemoteMcastMacs> class1) {
146         Map<InstanceIdentifier<Node>, List<RemoteMcastMacs>> result
147             = new HashMap<InstanceIdentifier<Node>, List<RemoteMcastMacs>>();
148         if (changes != null && !changes.isEmpty()) {
149             for (DataTreeModification<Node> change : changes) {
150                 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
151                 final DataObjectModification<Node> mod = change.getRootNode();
152                 Node created = TransactUtils.getCreated(mod);
153                 if (created != null) {
154                     List<RemoteMcastMacs> macListUpdated = null;
155                     HwvtepGlobalAugmentation hgAugmentation = created.getAugmentation(HwvtepGlobalAugmentation.class);
156                     if (hgAugmentation != null) {
157                         macListUpdated = hgAugmentation.getRemoteMcastMacs();
158                     }
159                     if (macListUpdated != null) {
160                         result.put(key, macListUpdated);
161                     }
162                 }
163             }
164         }
165         return result;
166     }
167
168     private Map<InstanceIdentifier<Node>, List<RemoteMcastMacs>> extractUpdated(
169             Collection<DataTreeModification<Node>> changes, Class<RemoteMcastMacs> class1) {
170         Map<InstanceIdentifier<Node>, List<RemoteMcastMacs>> result
171             = new HashMap<InstanceIdentifier<Node>, List<RemoteMcastMacs>>();
172         if (changes != null && !changes.isEmpty()) {
173             for (DataTreeModification<Node> change : changes) {
174                 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
175                 final DataObjectModification<Node> mod = change.getRootNode();
176                 Node updated = TransactUtils.getUpdated(mod);
177                 Node before = mod.getDataBefore();
178                 if (updated != null && before != null) {
179                     List<RemoteMcastMacs> macListUpdated = null;
180                     List<RemoteMcastMacs> macListBefore = null;
181                     HwvtepGlobalAugmentation hgUpdated = updated.getAugmentation(HwvtepGlobalAugmentation.class);
182                     if (hgUpdated != null) {
183                         macListUpdated = hgUpdated.getRemoteMcastMacs();
184                     }
185                     HwvtepGlobalAugmentation hgBefore = before.getAugmentation(HwvtepGlobalAugmentation.class);
186                     if (hgBefore != null) {
187                         macListBefore = hgBefore.getRemoteMcastMacs();
188                     }
189                     if (macListUpdated != null) {
190                         if (macListBefore != null) {
191                             macListUpdated.removeAll(macListBefore);
192                         }
193                         result.put(key, macListUpdated);
194                     }
195                 }
196             }
197         }
198         return result;
199     }
200
201     static class McastMacUnMetDependencyGetter extends UnMetDependencyGetter<RemoteMcastMacs> {
202
203         public List<InstanceIdentifier<?>> getLogicalSwitchDependencies(RemoteMcastMacs data) {
204             if (data == null) {
205                 return Collections.EMPTY_LIST;
206             }
207             return Lists.newArrayList(data.getLogicalSwitchRef().getValue());
208         }
209
210         public List<InstanceIdentifier<?>> getTerminationPointDependencies(RemoteMcastMacs data) {
211             if (data == null || HwvtepSouthboundUtil.isEmpty(data.getLocatorSet())) {
212                 return Collections.EMPTY_LIST;
213             }
214             List<InstanceIdentifier<?>> locators = new ArrayList<>();
215             for (LocatorSet locator: data.getLocatorSet()) {
216                 locators.add(locator.getLocatorRef().getValue());
217             }
218             return locators;
219         }
220     }
221 }