Make TransactionBuilder type-aware
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / McastMacsLocalUpdateCommand.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 static org.opendaylight.ovsdb.lib.operations.Operations.op;
12
13 import com.google.common.base.Optional;
14 import java.util.Collection;
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.HwvtepSouthboundConstants;
20 import org.opendaylight.ovsdb.lib.notation.UUID;
21 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
22 import org.opendaylight.ovsdb.schema.hardwarevtep.McastMacsLocal;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
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.LocalMcastMacs;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class McastMacsLocalUpdateCommand extends AbstractTransactCommand<LocalMcastMacs, HwvtepGlobalAugmentation> {
33     private static final Logger LOG = LoggerFactory.getLogger(McastMacsLocalUpdateCommand.class);
34
35     public McastMacsLocalUpdateCommand(final HwvtepOperationalState state,
36             final Collection<DataTreeModification<Node>> changes) {
37         super(state, changes);
38     }
39
40     @Override
41     public void execute(final TransactionBuilder transaction) {
42         Map<InstanceIdentifier<Node>, List<LocalMcastMacs>> updateds =
43                 extractUpdated(getChanges(),LocalMcastMacs.class);
44         if (!updateds.isEmpty()) {
45             for (Entry<InstanceIdentifier<Node>, List<LocalMcastMacs>> updated:
46                 updateds.entrySet()) {
47                 updateMcastMacsLocal(transaction,  updated.getKey(), updated.getValue());
48             }
49         }
50     }
51
52     private void updateMcastMacsLocal(final TransactionBuilder transaction,
53             final InstanceIdentifier<Node> instanceIdentifier, final List<LocalMcastMacs> localMcastMacs) {
54         for (LocalMcastMacs localMcastMac: localMcastMacs) {
55             LOG.debug("Creating localMcastMac, mac address: {}", localMcastMac.getMacEntryKey().getValue());
56             final Optional<LocalMcastMacs> operationalMacOptional =
57                     getOperationalState().getLocalMcastMacs(instanceIdentifier, localMcastMac.key());
58             McastMacsLocal mcastMacsLocal = transaction.getTypedRowWrapper(McastMacsLocal.class);
59             setIpAddress(mcastMacsLocal, localMcastMac);
60             setLocatorSet(transaction, mcastMacsLocal, localMcastMac);
61             setLogicalSwitch(mcastMacsLocal, localMcastMac);
62             if (!operationalMacOptional.isPresent()) {
63                 setMac(mcastMacsLocal, localMcastMac, operationalMacOptional);
64                 LOG.trace("execute: create LocalMcastMac entry: {}", mcastMacsLocal);
65                 transaction.add(op.insert(mcastMacsLocal));
66                 transaction.add(op.comment("McastMacLocal: Creating " + localMcastMac.getMacEntryKey().getValue()));
67             } else if (operationalMacOptional.get().getMacEntryUuid() != null) {
68                 UUID macEntryUUID = new UUID(operationalMacOptional.get().getMacEntryUuid().getValue());
69                 McastMacsLocal extraMac = transaction.getTypedRowSchema(McastMacsLocal.class);
70                 extraMac.getUuidColumn().setData(macEntryUUID);
71                 LOG.trace("execute: update LocalMcastMac entry: {}", mcastMacsLocal);
72                 transaction.add(op.update(mcastMacsLocal)
73                         .where(extraMac.getUuidColumn().getSchema().opEqual(macEntryUUID))
74                         .build());
75                 transaction.add(op.comment("McastMacLocal: Updating " + macEntryUUID));
76             } else {
77                 LOG.warn("Unable to update localMcastMacs {} because uuid not found in the operational store",
78                                 localMcastMac.getMacEntryKey().getValue());
79             }
80         }
81     }
82
83     private void setLogicalSwitch(final McastMacsLocal mcastMacsLocal, final LocalMcastMacs inputMac) {
84         if (inputMac.getLogicalSwitchRef() != null) {
85             @SuppressWarnings("unchecked")
86             InstanceIdentifier<LogicalSwitches> lswitchIid =
87                    (InstanceIdentifier<LogicalSwitches>) inputMac.getLogicalSwitchRef().getValue();
88             Optional<LogicalSwitches> operationalSwitchOptional =
89                     getOperationalState().getLogicalSwitches(lswitchIid);
90             if (operationalSwitchOptional.isPresent()) {
91                 Uuid logicalSwitchUuid = operationalSwitchOptional.get().getLogicalSwitchUuid();
92                 UUID logicalSwitchUUID = new UUID(logicalSwitchUuid.getValue());
93                 mcastMacsLocal.setLogicalSwitch(logicalSwitchUUID);
94             } else {
95                 LOG.warn(
96                     "Create or update localMcastMac: No logical switch with iid {} found in operational datastore!",
97                     lswitchIid);
98             }
99         }
100     }
101
102     private void setLocatorSet(final TransactionBuilder transaction, final McastMacsLocal mcastMacsLocal,
103             final LocalMcastMacs inputMac) {
104         if (inputMac.getLocatorSet() != null && !inputMac.getLocatorSet().isEmpty()) {
105             UUID locatorSetUuid = TransactUtils.createPhysicalLocatorSet(getOperationalState(), transaction,
106                     inputMac.getLocatorSet());
107             mcastMacsLocal.setLocatorSet(locatorSetUuid);
108         }
109     }
110
111     private static void setIpAddress(final McastMacsLocal mcastMacsLocal, final LocalMcastMacs inputMac) {
112         if (inputMac.getIpaddr() != null) {
113             mcastMacsLocal.setIpAddress(inputMac.getIpaddr().getIpv4Address().getValue());
114         }
115     }
116
117     private static void setMac(final McastMacsLocal mcastMacsLocal, final LocalMcastMacs inputMac,
118             final Optional<LocalMcastMacs> inputSwitchOptional) {
119         if (inputMac.getMacEntryKey() != null) {
120             if (inputMac.getMacEntryKey().equals(HwvtepSouthboundConstants.UNKNOWN_DST_MAC)) {
121                 mcastMacsLocal.setMac(HwvtepSouthboundConstants.UNKNOWN_DST_STRING);
122             } else {
123                 mcastMacsLocal.setMac(inputMac.getMacEntryKey().getValue());
124             }
125         } else if (inputSwitchOptional.isPresent() && inputSwitchOptional.get().getMacEntryKey() != null) {
126             mcastMacsLocal.setMac(inputSwitchOptional.get().getMacEntryKey().getValue());
127         }
128     }
129
130     @Override
131     protected List<LocalMcastMacs> getData(final HwvtepGlobalAugmentation augmentation) {
132         return augmentation.getLocalMcastMacs();
133     }
134 }