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