f6e66b7e77591663d0236f8c687feb2c57024b64
[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 com.google.common.collect.Lists;
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 java.util.Objects;
19
20 import org.opendaylight.mdsal.binding.api.DataTreeModification;
21 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepDeviceInfo;
22 import org.opendaylight.ovsdb.lib.notation.UUID;
23 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
24 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
25 import org.opendaylight.ovsdb.schema.hardwarevtep.UcastMacsRemote;
26 import org.opendaylight.ovsdb.utils.mdsal.utils.TransactionType;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class UcastMacsRemoteUpdateCommand extends AbstractTransactCommand<RemoteUcastMacs, HwvtepGlobalAugmentation> {
37     private static final Logger LOG = LoggerFactory.getLogger(UcastMacsRemoteUpdateCommand.class);
38     private static final UcastMacUnMetDependencyGetter UCAST_MAC_DATA_VALIDATOR = new UcastMacUnMetDependencyGetter();
39
40     public UcastMacsRemoteUpdateCommand(final HwvtepOperationalState state,
41             final Collection<DataTreeModification<Node>> changes) {
42         super(state, changes);
43     }
44
45     @Override
46     public void execute(final TransactionBuilder transaction) {
47         Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> updateds =
48                 extractUpdated(getChanges(),RemoteUcastMacs.class);
49         if (!updateds.isEmpty()) {
50             for (Entry<InstanceIdentifier<Node>, List<RemoteUcastMacs>> updated:
51                 updateds.entrySet()) {
52                 updateUcastMacsRemote(transaction,  updated.getKey(), updated.getValue());
53             }
54         }
55     }
56
57     private void updateUcastMacsRemote(final TransactionBuilder transaction,
58                                        final InstanceIdentifier<Node> instanceIdentifier,
59                                        final List<RemoteUcastMacs> remoteUcastMacs) {
60         if (remoteUcastMacs == null) {
61             return;
62         }
63         for (RemoteUcastMacs remoteUcastMac : remoteUcastMacs) {
64             InstanceIdentifier<RemoteUcastMacs> macIid =
65                     instanceIdentifier.augmentation(HwvtepGlobalAugmentation.class)
66                             .child(RemoteUcastMacs.class, remoteUcastMac.key());
67             getDeviceInfo().updateConfigData(RemoteUcastMacs.class, macIid, remoteUcastMac);
68             onConfigUpdate(transaction, instanceIdentifier, remoteUcastMac, null);
69         }
70     }
71
72     @Override
73     public void onConfigUpdate(final TransactionBuilder transaction,
74                                final InstanceIdentifier<Node> nodeIid,
75                                final RemoteUcastMacs remoteUcastMacs,
76                                final InstanceIdentifier macKey,
77                                final Object... extraData) {
78         InstanceIdentifier<RemoteUcastMacs> macIid = nodeIid.augmentation(HwvtepGlobalAugmentation.class)
79                 .child(RemoteUcastMacs.class, remoteUcastMacs.key());
80         processDependencies(UCAST_MAC_DATA_VALIDATOR, transaction, nodeIid, macIid, remoteUcastMacs);
81     }
82
83     @Override
84     public void doDeviceTransaction(final TransactionBuilder transaction,
85                                     final InstanceIdentifier<Node> instanceIdentifier,
86                                     final RemoteUcastMacs remoteUcastMac,
87                                     final InstanceIdentifier macKey,
88                                     final Object... extraData) {
89         LOG.debug("DoDeviceTransaction remoteUcastMacs, mac address: {}", remoteUcastMac.getMacEntryKey().getValue());
90         updateConfigData(RemoteUcastMacs.class, macKey, remoteUcastMac);
91         HwvtepDeviceInfo.DeviceData deviceData = getDeviceOpData(RemoteUcastMacs.class, macKey);
92         UcastMacsRemote ucastMacsRemote = transaction.getTypedRowWrapper(UcastMacsRemote.class);
93         setIpAddress(ucastMacsRemote, remoteUcastMac);
94         setLogicalSwitch(transaction, ucastMacsRemote, remoteUcastMac);
95         //TODO handle multiple inserts
96         if (deviceData  == null) {
97             setLocator(transaction, ucastMacsRemote, remoteUcastMac);
98             setMac(ucastMacsRemote, remoteUcastMac);
99             LOG.trace("DoDeviceTransaction: creating RemotUcastMac entry: {} txId: {}", macKey,
100                     getOperationalState().getTransactionId());
101             transaction.add(op.insert(ucastMacsRemote));
102             updateCurrentTxData(RemoteUcastMacs.class, macKey, new UUID("uuid"), remoteUcastMac);
103             LOG.info("CONTROLLER - {} {}", TransactionType.ADD, ucastMacsRemote);
104             return;
105         } else if (deviceData.getUuid() != null) {
106             UUID newLocator = setLocator(transaction, ucastMacsRemote, remoteUcastMac);
107             if (deviceData.getData() != null) {
108                 UcastMacsRemote existing = (UcastMacsRemote) deviceData.getData();
109                 if (existing.getLocatorColumn() != null) {
110                     UUID oldLocatorUuid = existing.getLocatorColumn().getData();
111                     if (Objects.equals(newLocator, oldLocatorUuid)) {
112                         return;
113                     }
114                 }
115             }
116             setMac(ucastMacsRemote, remoteUcastMac);
117             UUID macEntryUUID = deviceData.getUuid();
118             UcastMacsRemote extraMac = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
119                     UcastMacsRemote.class, null);
120             extraMac.getUuidColumn().setData(macEntryUUID);
121             LOG.trace("doDeviceTransaction: updating RemotUcastMac entry: {} txId: {}", macKey,
122                     getOperationalState().getTransactionId());
123             transaction.add(op.update(ucastMacsRemote)
124                     .where(extraMac.getUuidColumn().getSchema().opEqual(macEntryUUID))
125                     .build());
126             LOG.info("CONTROLLER - {} {}", TransactionType.UPDATE, ucastMacsRemote);
127             addToUpdates(macKey, remoteUcastMac);
128         } else {
129             LOG.warn("Unable to update remoteUcastMacs {} because uuid not found in the operational store txId: {}",
130                     macKey, getOperationalState().getTransactionId());
131         }
132         return;
133     }
134
135     private void setLogicalSwitch(final TransactionBuilder transaction, final UcastMacsRemote ucastMacsRemote,
136             final RemoteUcastMacs inputMac) {
137         if (inputMac.getLogicalSwitchRef() != null) {
138             @SuppressWarnings("unchecked")
139             InstanceIdentifier<LogicalSwitches> lswitchIid =
140                     (InstanceIdentifier<LogicalSwitches>) inputMac.getLogicalSwitchRef().getValue();
141             UUID logicalSwitchUUID = TransactUtils.getLogicalSwitchUUID(transaction, getOperationalState(), lswitchIid);
142             if (logicalSwitchUUID != null) {
143                 ucastMacsRemote.setLogicalSwitch(TransactUtils.getLogicalSwitchUUID(transaction, getOperationalState(),
144                         lswitchIid));
145             }
146         }
147     }
148
149     private UUID setLocator(TransactionBuilder transaction, UcastMacsRemote ucastMacsRemote, RemoteUcastMacs inputMac) {
150         //get UUID by locatorRef
151         if (inputMac.getLocatorRef() != null) {
152             @SuppressWarnings("unchecked")
153             InstanceIdentifier<TerminationPoint> iid =
154                     (InstanceIdentifier<TerminationPoint>) inputMac.getLocatorRef().getValue();
155             UUID locatorUuid = TransactUtils.createPhysicalLocator(transaction, getOperationalState(), iid);
156             ucastMacsRemote.setLocator(locatorUuid);
157             return locatorUuid;
158         }
159         return null;
160     }
161
162     private static void setIpAddress(final UcastMacsRemote ucastMacsRemote, final RemoteUcastMacs inputMac) {
163         if (inputMac.getIpaddr() != null) {
164             ucastMacsRemote.setIpAddress(inputMac.getIpaddr().getIpv4Address().getValue());
165         }
166     }
167
168     private static void setMac(final UcastMacsRemote ucastMacsRemote, final RemoteUcastMacs inputMac) {
169         if (inputMac.getMacEntryKey() != null) {
170             ucastMacsRemote.setMac(inputMac.getMacEntryKey().getValue());
171         }
172     }
173
174     @Override
175     protected List<RemoteUcastMacs> getData(final HwvtepGlobalAugmentation augmentation) {
176         return augmentation.getRemoteUcastMacs();
177     }
178
179     static class UcastMacUnMetDependencyGetter extends UnMetDependencyGetter<RemoteUcastMacs> {
180
181         @Override
182         public List<InstanceIdentifier<?>> getLogicalSwitchDependencies(final RemoteUcastMacs data) {
183             if (data == null) {
184                 return Collections.EMPTY_LIST;
185             }
186             return Lists.newArrayList(data.getLogicalSwitchRef().getValue());
187         }
188
189         @Override
190         public List<InstanceIdentifier<?>> getTerminationPointDependencies(final RemoteUcastMacs data) {
191             if (data == null) {
192                 return Collections.EMPTY_LIST;
193             }
194             return Lists.newArrayList(data.getLocatorRef().getValue());
195         }
196     }
197
198     @Override
199     protected boolean areEqual(RemoteUcastMacs remoteUcastMacs1, RemoteUcastMacs remoteUcastMacs2) {
200         return Objects.equals(remoteUcastMacs1.key(), remoteUcastMacs2.key())
201                 && Objects.equals(remoteUcastMacs1.getLocatorRef(), remoteUcastMacs2.getLocatorRef());
202     }
203
204     @Override
205     public void onSuccess(TransactionBuilder tx) {
206         for (MdsalUpdate mdsalUpdate : updates) {
207             RemoteUcastMacs mac = (RemoteUcastMacs) mdsalUpdate.getNewData();
208             InstanceIdentifier<RemoteUcastMacs> macIid = mdsalUpdate.getKey();
209             getDeviceInfo().updateRemoteUcast(
210                     (InstanceIdentifier<LogicalSwitches>) mac.getLogicalSwitchRef().getValue(), macIid, mac);
211         }
212     }
213 }