Make TransactionBuilder type-aware
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / UcastMacsRemoteUpdateCommand.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 static org.opendaylight.ovsdb.lib.operations.Operations.op;
11
12 import java.util.Collection;
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Map.Entry;
17 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
18 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepDeviceInfo;
19 import org.opendaylight.ovsdb.lib.notation.UUID;
20 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
21 import org.opendaylight.ovsdb.schema.hardwarevtep.UcastMacsRemote;
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.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.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class UcastMacsRemoteUpdateCommand extends AbstractTransactCommand<RemoteUcastMacs, HwvtepGlobalAugmentation> {
32     private static final Logger LOG = LoggerFactory.getLogger(UcastMacsRemoteUpdateCommand.class);
33     private static final UcastMacUnMetDependencyGetter UCAST_MAC_DATA_VALIDATOR = new UcastMacUnMetDependencyGetter();
34
35     public UcastMacsRemoteUpdateCommand(final HwvtepOperationalState state,
36             final Collection<DataTreeModification<Node>> changes) {
37         super(state, changes);
38     }
39
40     @Override
41     public void execute(final TransactionBuilder transaction) {
42         Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> updateds =
43                 extractUpdated(getChanges(),RemoteUcastMacs.class);
44         if (!updateds.isEmpty()) {
45             for (Entry<InstanceIdentifier<Node>, List<RemoteUcastMacs>> updated:
46                 updateds.entrySet()) {
47                 updateUcastMacsRemote(transaction,  updated.getKey(), updated.getValue());
48             }
49         }
50     }
51
52     private void updateUcastMacsRemote(final TransactionBuilder transaction,
53                                        final InstanceIdentifier<Node> instanceIdentifier,
54                                        final List<RemoteUcastMacs> remoteUcastMacs) {
55         if (remoteUcastMacs == null) {
56             return;
57         }
58         for (RemoteUcastMacs remoteUcastMac : remoteUcastMacs) {
59             onConfigUpdate(transaction, instanceIdentifier, remoteUcastMac, null);
60         }
61     }
62
63     @Override
64     public void onConfigUpdate(final TransactionBuilder transaction,
65                                final InstanceIdentifier<Node> nodeIid,
66                                final RemoteUcastMacs remoteUcastMacs,
67                                final InstanceIdentifier macKey,
68                                final Object... extraData) {
69         InstanceIdentifier<RemoteUcastMacs> macIid = nodeIid.augmentation(HwvtepGlobalAugmentation.class)
70                 .child(RemoteUcastMacs.class, remoteUcastMacs.key());
71         processDependencies(UCAST_MAC_DATA_VALIDATOR, transaction, nodeIid, macIid, remoteUcastMacs);
72     }
73
74     @Override
75     public void doDeviceTransaction(final TransactionBuilder transaction,
76                                     final InstanceIdentifier<Node> instanceIdentifier,
77                                     final RemoteUcastMacs remoteUcastMac,
78                                     final InstanceIdentifier macKey,
79                                     final Object... extraData) {
80         LOG.debug("Creating remoteUcastMacs, mac address: {}", remoteUcastMac.getMacEntryKey().getValue());
81         final HwvtepDeviceInfo.DeviceData deviceData =
82                 getOperationalState().getDeviceInfo().getDeviceOperData(RemoteUcastMacs.class, macKey);
83
84         UcastMacsRemote ucastMacsRemote = transaction.getTypedRowWrapper(UcastMacsRemote.class);
85         setIpAddress(ucastMacsRemote, remoteUcastMac);
86         setLocator(transaction, ucastMacsRemote, remoteUcastMac);
87         setLogicalSwitch(transaction, ucastMacsRemote, remoteUcastMac);
88         if (deviceData == null) {
89             setMac(ucastMacsRemote, remoteUcastMac);
90             LOG.trace("doDeviceTransaction: creating RemotUcastMac entry: {}", ucastMacsRemote);
91             transaction.add(op.insert(ucastMacsRemote));
92             getOperationalState().getDeviceInfo().markKeyAsInTransit(RemoteUcastMacs.class, macKey);
93             updateCurrentTxData(RemoteUcastMacs.class, macKey, new UUID("uuid"), remoteUcastMac);
94         } else if (deviceData.getUuid() != null) {
95             UUID macEntryUUID = deviceData.getUuid();
96             UcastMacsRemote extraMac = transaction.getTypedRowSchema(UcastMacsRemote.class);
97             extraMac.getUuidColumn().setData(macEntryUUID);
98             LOG.trace("doDeviceTransaction: updating RemotUcastMac entry: {}", ucastMacsRemote);
99             transaction.add(op.update(ucastMacsRemote)
100                     .where(extraMac.getUuidColumn().getSchema().opEqual(macEntryUUID))
101                     .build());
102         } else {
103             LOG.warn("Unable to update remoteMcastMacs {} because uuid not found in the operational store",
104                     remoteUcastMac.getMacEntryKey().getValue());
105         }
106     }
107
108     private void setLogicalSwitch(final TransactionBuilder transaction, final UcastMacsRemote ucastMacsRemote,
109             final RemoteUcastMacs inputMac) {
110         if (inputMac.getLogicalSwitchRef() != null) {
111             @SuppressWarnings("unchecked")
112             InstanceIdentifier<LogicalSwitches> lswitchIid =
113                     (InstanceIdentifier<LogicalSwitches>) inputMac.getLogicalSwitchRef().getValue();
114             ucastMacsRemote.setLogicalSwitch(TransactUtils.getLogicalSwitchUUID(
115                     transaction, getOperationalState(), lswitchIid));
116         }
117     }
118
119     private void setLocator(final TransactionBuilder transaction, final UcastMacsRemote ucastMacsRemote,
120             final RemoteUcastMacs inputMac) {
121         //get UUID by locatorRef
122         if (inputMac.getLocatorRef() != null) {
123             @SuppressWarnings("unchecked")
124             InstanceIdentifier<TerminationPoint> iid = (InstanceIdentifier<TerminationPoint>)
125                     inputMac.getLocatorRef().getValue();
126             UUID locatorUuid = TransactUtils.createPhysicalLocator(transaction, getOperationalState(), iid);
127             if (locatorUuid != null) {
128                 ucastMacsRemote.setLocator(locatorUuid);
129             }
130         }
131     }
132
133     private static void setIpAddress(final UcastMacsRemote ucastMacsRemote, final RemoteUcastMacs inputMac) {
134         if (inputMac.getIpaddr() != null) {
135             ucastMacsRemote.setIpAddress(inputMac.getIpaddr().getIpv4Address().getValue());
136         }
137     }
138
139     private static void setMac(final UcastMacsRemote ucastMacsRemote, final RemoteUcastMacs inputMac) {
140         if (inputMac.getMacEntryKey() != null) {
141             ucastMacsRemote.setMac(inputMac.getMacEntryKey().getValue());
142         }
143     }
144
145     @Override
146     protected List<RemoteUcastMacs> getData(final HwvtepGlobalAugmentation augmentation) {
147         return augmentation.getRemoteUcastMacs();
148     }
149
150     static class UcastMacUnMetDependencyGetter extends UnMetDependencyGetter<RemoteUcastMacs> {
151
152         @Override
153         public List<InstanceIdentifier<?>> getLogicalSwitchDependencies(final RemoteUcastMacs data) {
154             if (data == null) {
155                 return Collections.emptyList();
156             }
157             return Collections.singletonList(data.getLogicalSwitchRef().getValue());
158         }
159
160         @Override
161         public List<InstanceIdentifier<?>> getTerminationPointDependencies(final RemoteUcastMacs data) {
162             if (data == null) {
163                 return Collections.emptyList();
164             }
165             return Collections.singletonList(data.getLocatorRef().getValue());
166         }
167     }
168
169     @Override
170     public void onCommandSucceeded() {
171         for (MdsalUpdate mdsalUpdate : updates.get(getDeviceTransaction())) {
172             RemoteUcastMacs newMac = (RemoteUcastMacs) mdsalUpdate.getNewData();
173             InstanceIdentifier<RemoteUcastMacs> macIid = mdsalUpdate.getKey();
174             RemoteUcastMacs oldMac = (RemoteUcastMacs) mdsalUpdate.getOldData();
175             if (oldMac != null && !oldMac.equals(newMac)) {
176                 getDeviceInfo().decRefCount(macIid, oldMac.getLocatorRef().getValue());
177             }
178             getDeviceInfo().updateRemoteUcast(
179                     (InstanceIdentifier<LogicalSwitches>) newMac.getLogicalSwitchRef().getValue(), macIid, newMac);
180         }
181     }
182 }