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