bug 6579 removed boilerplate code
[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 java.util.Collection;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Map.Entry;
17
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.lib.schema.typed.TyperUtils;
23 import org.opendaylight.ovsdb.schema.hardwarevtep.McastMacsLocal;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
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.hwvtep.global.attributes.LocalMcastMacs;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import com.google.common.base.Optional;
34
35 public class McastMacsLocalUpdateCommand extends AbstractTransactCommand<LocalMcastMacs, HwvtepGlobalAugmentation> {
36     private static final Logger LOG = LoggerFactory.getLogger(McastMacsLocalUpdateCommand.class);
37
38     public McastMacsLocalUpdateCommand(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<LocalMcastMacs>> updateds =
46                 extractUpdated(getChanges(),LocalMcastMacs.class);
47         if (!updateds.isEmpty()) {
48             for (Entry<InstanceIdentifier<Node>, List<LocalMcastMacs>> updated:
49                 updateds.entrySet()) {
50                 updateMcastMacsLocal(transaction,  updated.getKey(), updated.getValue());
51             }
52         }
53     }
54
55     private void updateMcastMacsLocal(TransactionBuilder transaction,
56             InstanceIdentifier<Node> instanceIdentifier, List<LocalMcastMacs> localMcastMacs) {
57         for (LocalMcastMacs localMcastMac: localMcastMacs) {
58             LOG.debug("Creating localMcastMac, mac address: {}", localMcastMac.getMacEntryKey().getValue());
59             Optional<LocalMcastMacs> operationalMacOptional =
60                     getOperationalState().getLocalMcastMacs(instanceIdentifier, localMcastMac.getKey());
61             McastMacsLocal mcastMacsLocal = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), McastMacsLocal.class);
62             setIpAddress(mcastMacsLocal, localMcastMac);
63             setLocatorSet(transaction, mcastMacsLocal, localMcastMac);
64             setLogicalSwitch(mcastMacsLocal, localMcastMac);
65             if (!operationalMacOptional.isPresent()) {
66                 setMac(mcastMacsLocal, localMcastMac, operationalMacOptional);
67                 LOG.trace("execute: create LocalMcastMac entry: {}", mcastMacsLocal);
68                 transaction.add(op.insert(mcastMacsLocal));
69                 transaction.add(op.comment("McastMacLocal: Creating " + localMcastMac.getMacEntryKey().getValue()));
70             } else if (operationalMacOptional.get().getMacEntryUuid() != null) {
71                 UUID macEntryUUID = new UUID(operationalMacOptional.get().getMacEntryUuid().getValue());
72                 McastMacsLocal extraMac = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
73                                 McastMacsLocal.class, null);
74                 extraMac.getUuidColumn().setData(macEntryUUID);
75                 LOG.trace("execute: update LocalMcastMac entry: {}", mcastMacsLocal);
76                 transaction.add(op.update(mcastMacsLocal)
77                         .where(extraMac.getUuidColumn().getSchema().opEqual(macEntryUUID))
78                         .build());
79                 transaction.add(op.comment("McastMacLocal: Updating " + macEntryUUID));
80             } else {
81                 LOG.warn("Unable to update localMcastMacs {} because uuid not found in the operational store",
82                                 localMcastMac.getMacEntryKey().getValue());
83             }
84         }
85     }
86
87     private void setLogicalSwitch(McastMacsLocal mcastMacsLocal, LocalMcastMacs inputMac) {
88         if (inputMac.getLogicalSwitchRef() != null) {
89             @SuppressWarnings("unchecked")
90             InstanceIdentifier<LogicalSwitches> lswitchIid = (InstanceIdentifier<LogicalSwitches>) inputMac.getLogicalSwitchRef().getValue();
91             Optional<LogicalSwitches> operationalSwitchOptional =
92                     getOperationalState().getLogicalSwitches(lswitchIid);
93             if (operationalSwitchOptional.isPresent()) {
94                 Uuid logicalSwitchUuid = operationalSwitchOptional.get().getLogicalSwitchUuid();
95                 UUID logicalSwitchUUID = new UUID(logicalSwitchUuid.getValue());
96                 mcastMacsLocal.setLogicalSwitch(logicalSwitchUUID);
97             } else {
98                 LOG.warn("Create or update localMcastMac: No logical switch with iid {} found in operational datastore!",
99                         lswitchIid);
100             }
101         }
102     }
103
104     private void setLocatorSet(TransactionBuilder transaction, McastMacsLocal mcastMacsLocal, LocalMcastMacs inputMac) {
105         if (inputMac.getLocatorSet() != null && !inputMac.getLocatorSet().isEmpty()) {
106             UUID locatorSetUuid = TransactUtils.createPhysicalLocatorSet(getOperationalState(), transaction, inputMac.getLocatorSet());
107             mcastMacsLocal.setLocatorSet(locatorSetUuid);
108         }
109     }
110
111     private void setIpAddress(McastMacsLocal mcastMacsLocal, LocalMcastMacs inputMac) {
112         if (inputMac.getIpaddr() != null) {
113             mcastMacsLocal.setIpAddress(inputMac.getIpaddr().getIpv4Address().getValue());
114         }
115     }
116
117     private void setMac(McastMacsLocal mcastMacsLocal, LocalMcastMacs inputMac,
118             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(HwvtepGlobalAugmentation augmentation) {
132         return augmentation.getLocalMcastMacs();
133     }
134 }