bug 6579 removed boilerplate code
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / UcastMacsRemoteUpdateCommand.java
1 /*
2  * Copyright (c) 2015, 2016 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 com.google.common.base.Optional;
12 import com.google.common.collect.Lists;
13
14 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
15
16 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
17 import org.opendaylight.ovsdb.lib.notation.UUID;
18 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
19 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
20 import org.opendaylight.ovsdb.schema.hardwarevtep.UcastMacsRemote;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.EncapsulationTypeBase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.EncapsulationTypeVxlanOverIpv4;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentation;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentationBuilder;
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 import java.util.Collection;
37 import java.util.Collections;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Map.Entry;
42
43 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
44
45 public class UcastMacsRemoteUpdateCommand extends AbstractTransactCommand<RemoteUcastMacs, HwvtepGlobalAugmentation> {
46     private static final Logger LOG = LoggerFactory.getLogger(UcastMacsRemoteUpdateCommand.class);
47     private static final UcastMacUnMetDependencyGetter UCAST_MAC_DATA_VALIDATOR = new UcastMacUnMetDependencyGetter();
48
49     public UcastMacsRemoteUpdateCommand(HwvtepOperationalState state,
50             Collection<DataTreeModification<Node>> changes) {
51         super(state, changes);
52     }
53
54     @Override
55     public void execute(TransactionBuilder transaction) {
56         Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> updateds =
57                 extractUpdated(getChanges(),RemoteUcastMacs.class);
58         if (!updateds.isEmpty()) {
59             for (Entry<InstanceIdentifier<Node>, List<RemoteUcastMacs>> updated:
60                 updateds.entrySet()) {
61                 updateUcastMacsRemote(transaction,  updated.getKey(), updated.getValue());
62             }
63         }
64     }
65
66     private void updateUcastMacsRemote(TransactionBuilder transaction,
67                                        InstanceIdentifier<Node> instanceIdentifier,
68                                        List<RemoteUcastMacs> remoteUcastMacs) {
69         if (remoteUcastMacs == null) {
70             return;
71         }
72         for (RemoteUcastMacs remoteUcastMac : remoteUcastMacs) {
73             onConfigUpdate(transaction, instanceIdentifier, remoteUcastMac, null);
74         }
75     }
76
77     @Override
78     public void onConfigUpdate(TransactionBuilder transaction,
79                                   InstanceIdentifier<Node> nodeIid,
80                                   RemoteUcastMacs remoteUcastMacs,
81                                   InstanceIdentifier macKey,
82                                   Object... extraData) {
83         InstanceIdentifier<RemoteUcastMacs> macIid = nodeIid.augmentation(HwvtepGlobalAugmentation.class).
84                 child(RemoteUcastMacs.class, remoteUcastMacs.getKey());
85         processDependencies(UCAST_MAC_DATA_VALIDATOR, transaction, nodeIid, macIid, remoteUcastMacs);
86     }
87
88     @Override
89     public void doDeviceTransaction(TransactionBuilder transaction,
90                                    InstanceIdentifier<Node> instanceIdentifier,
91                                    RemoteUcastMacs remoteUcastMac,
92                                    InstanceIdentifier macKey,
93                                    Object... extraData) {
94             LOG.debug("Creating remoteUcastMacs, mac address: {}", remoteUcastMac.getMacEntryKey().getValue());
95             Optional<RemoteUcastMacs> operationalMacOptional =
96                     getOperationalState().getRemoteUcastMacs(instanceIdentifier, remoteUcastMac.getKey());
97             UcastMacsRemote ucastMacsRemote = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), UcastMacsRemote.class);
98             setIpAddress(ucastMacsRemote, remoteUcastMac);
99             setLocator(transaction, ucastMacsRemote, remoteUcastMac);
100             setLogicalSwitch(ucastMacsRemote, remoteUcastMac);
101             if (!operationalMacOptional.isPresent()) {
102                 setMac(ucastMacsRemote, remoteUcastMac, operationalMacOptional);
103                 LOG.trace("doDeviceTransaction: creating RemotUcastMac entry: {}", ucastMacsRemote);
104                 transaction.add(op.insert(ucastMacsRemote));
105                 transaction.add(op.comment("UcastMacRemote: Creating " + remoteUcastMac.getMacEntryKey().getValue()));
106             } else if (operationalMacOptional.get().getMacEntryUuid() != null) {
107                 UUID macEntryUUID = new UUID(operationalMacOptional.get().getMacEntryUuid().getValue());
108                 UcastMacsRemote extraMac = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
109                                 UcastMacsRemote.class, null);
110                 extraMac.getUuidColumn().setData(macEntryUUID);
111                 LOG.trace("doDeviceTransaction: updating RemotUcastMac entry: {}", ucastMacsRemote);
112                 transaction.add(op.update(ucastMacsRemote)
113                         .where(extraMac.getUuidColumn().getSchema().opEqual(macEntryUUID))
114                         .build());
115                 transaction.add(op.comment("UcastMacRemote: Updating " + remoteUcastMac.getMacEntryKey().getValue()));
116             } else {
117                 LOG.warn("Unable to update remoteMcastMacs {} because uuid not found in the operational store",
118                                 remoteUcastMac.getMacEntryKey().getValue());
119             }
120     }
121
122     private void setLogicalSwitch(UcastMacsRemote ucastMacsRemote, RemoteUcastMacs inputMac) {
123         if (inputMac.getLogicalSwitchRef() != null) {
124             @SuppressWarnings("unchecked")
125             InstanceIdentifier<LogicalSwitches> lswitchIid = (InstanceIdentifier<LogicalSwitches>) inputMac.getLogicalSwitchRef().getValue();
126             Optional<LogicalSwitches> operationalSwitchOptional =
127                     getOperationalState().getLogicalSwitches(lswitchIid);
128             if (operationalSwitchOptional.isPresent()) {
129                 Uuid logicalSwitchUuid = operationalSwitchOptional.get().getLogicalSwitchUuid();
130                 UUID logicalSwitchUUID = new UUID(logicalSwitchUuid.getValue());
131                 ucastMacsRemote.setLogicalSwitch(logicalSwitchUUID);
132             } else {
133                 ucastMacsRemote.setLogicalSwitch(TransactUtils.getLogicalSwitchUUID(lswitchIid));
134             }
135         }
136     }
137
138     private void setLocator(TransactionBuilder transaction, UcastMacsRemote ucastMacsRemote, RemoteUcastMacs inputMac) {
139         //get UUID by locatorRef
140         if (inputMac.getLocatorRef() != null) {
141             UUID locatorUuid = null;
142             @SuppressWarnings("unchecked")
143             InstanceIdentifier<TerminationPoint> iid = (InstanceIdentifier<TerminationPoint>) inputMac.getLocatorRef().getValue();
144             //try to find locator in operational DS
145             Optional<HwvtepPhysicalLocatorAugmentation> operationalLocatorOptional =
146                     getOperationalState().getPhysicalLocatorAugmentation(iid);
147             if (operationalLocatorOptional.isPresent()) {
148                 //if exist, get uuid
149                 HwvtepPhysicalLocatorAugmentation locatorAugmentation = operationalLocatorOptional.get();
150                 locatorUuid = new UUID(locatorAugmentation.getPhysicalLocatorUuid().getValue());
151             } else {
152                 locatorUuid = getOperationalState().getUUIDFromCurrentTx(TerminationPoint.class, iid);
153                 if (locatorUuid == null) {
154                     locatorUuid = TransactUtils.createPhysicalLocator(transaction, getOperationalState(),
155                             (InstanceIdentifier<TerminationPoint>) inputMac.getLocatorRef().getValue());
156                     updateCurrentTxData(TerminationPoint.class, iid, locatorUuid, null);
157                 }
158             }
159             if (locatorUuid != null) {
160                 ucastMacsRemote.setLocator(locatorUuid);
161             }
162         }
163     }
164
165     private void setIpAddress(UcastMacsRemote ucastMacsRemote, RemoteUcastMacs inputMac) {
166         if (inputMac.getIpaddr() != null) {
167             ucastMacsRemote.setIpAddress(inputMac.getIpaddr().getIpv4Address().getValue());
168         }
169     }
170
171     private void setMac(UcastMacsRemote ucastMacsRemote, RemoteUcastMacs inputMac,
172             Optional<RemoteUcastMacs> inputSwitchOptional) {
173         if (inputMac.getMacEntryKey() != null) {
174             ucastMacsRemote.setMac(inputMac.getMacEntryKey().getValue());
175         } else if (inputSwitchOptional.isPresent() && inputSwitchOptional.get().getMacEntryKey() != null) {
176             ucastMacsRemote.setMac(inputSwitchOptional.get().getMacEntryKey().getValue());
177         }
178     }
179
180     protected List<RemoteUcastMacs> getData(HwvtepGlobalAugmentation augmentation) {
181         return augmentation.getRemoteUcastMacs();
182     }
183
184     static class UcastMacUnMetDependencyGetter extends UnMetDependencyGetter<RemoteUcastMacs> {
185
186         public List<InstanceIdentifier<?>> getLogicalSwitchDependencies(RemoteUcastMacs data) {
187             if (data == null) {
188                 return Collections.EMPTY_LIST;
189             }
190             return Lists.newArrayList(data.getLogicalSwitchRef().getValue());
191         }
192
193         public List<InstanceIdentifier<?>> getTerminationPointDependencies(RemoteUcastMacs data) {
194             if (data == null) {
195                 return Collections.EMPTY_LIST;
196             }
197             return Lists.newArrayList(data.getLocatorRef().getValue());
198         }
199     }
200 }