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