dd345879c99728794b3ed5adf29fbd6bcdaf13c5
[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 import java.util.Optional;
18 import org.opendaylight.mdsal.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.LocalMcastMacsKey;
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 public class McastMacsLocalUpdateCommand
34         extends AbstractTransactCommand<LocalMcastMacs, LocalMcastMacsKey, HwvtepGlobalAugmentation> {
35     private static final Logger LOG = LoggerFactory.getLogger(McastMacsLocalUpdateCommand.class);
36
37     public McastMacsLocalUpdateCommand(final HwvtepOperationalState state,
38             final Collection<DataTreeModification<Node>> changes) {
39         super(state, changes);
40     }
41
42     @Override
43     public void execute(final TransactionBuilder transaction) {
44         Map<InstanceIdentifier<Node>, List<LocalMcastMacs>> updateds =
45                 extractUpdated(getChanges(),LocalMcastMacs.class);
46         if (!updateds.isEmpty()) {
47             for (Entry<InstanceIdentifier<Node>, List<LocalMcastMacs>> updated:
48                 updateds.entrySet()) {
49                 updateMcastMacsLocal(transaction,  updated.getKey(), updated.getValue());
50             }
51         }
52     }
53
54     private void updateMcastMacsLocal(final TransactionBuilder transaction,
55             final InstanceIdentifier<Node> instanceIdentifier, final List<LocalMcastMacs> localMcastMacs) {
56         for (LocalMcastMacs localMcastMac: localMcastMacs) {
57             LOG.debug("Creating localMcastMac, mac address: {}", localMcastMac.getMacEntryKey().getValue());
58             final Optional<LocalMcastMacs> operationalMacOptional =
59                     getOperationalState().getLocalMcastMacs(instanceIdentifier, localMcastMac.key());
60             McastMacsLocal mcastMacsLocal = transaction.getTypedRowWrapper(McastMacsLocal.class);
61             setIpAddress(mcastMacsLocal, localMcastMac);
62             setLocatorSet(transaction, mcastMacsLocal, localMcastMac);
63             setLogicalSwitch(mcastMacsLocal, localMcastMac);
64             if (!operationalMacOptional.isPresent()) {
65                 setMac(mcastMacsLocal, localMcastMac, operationalMacOptional);
66                 LOG.trace("execute: create LocalMcastMac entry: {}", mcastMacsLocal);
67                 transaction.add(op.insert(mcastMacsLocal));
68                 transaction.add(op.comment("McastMacLocal: Creating " + localMcastMac.getMacEntryKey().getValue()));
69             } else if (operationalMacOptional.get().getMacEntryUuid() != null) {
70                 UUID macEntryUUID = new UUID(operationalMacOptional.get().getMacEntryUuid().getValue());
71                 McastMacsLocal extraMac = transaction.getTypedRowSchema(McastMacsLocal.class);
72                 extraMac.getUuidColumn().setData(macEntryUUID);
73                 LOG.trace("execute: update LocalMcastMac entry: {}", mcastMacsLocal);
74                 transaction.add(op.update(mcastMacsLocal)
75                         .where(extraMac.getUuidColumn().getSchema().opEqual(macEntryUUID))
76                         .build());
77                 transaction.add(op.comment("McastMacLocal: Updating " + macEntryUUID));
78             } else {
79                 LOG.warn("Unable to update localMcastMacs {} because uuid not found in the operational store",
80                                 localMcastMac.getMacEntryKey().getValue());
81             }
82         }
83     }
84
85     private void setLogicalSwitch(final McastMacsLocal mcastMacsLocal, final LocalMcastMacs inputMac) {
86         if (inputMac.getLogicalSwitchRef() != null) {
87             @SuppressWarnings("unchecked")
88             InstanceIdentifier<LogicalSwitches> lswitchIid =
89                    (InstanceIdentifier<LogicalSwitches>) inputMac.getLogicalSwitchRef().getValue();
90             Optional<LogicalSwitches> operationalSwitchOptional =
91                     getOperationalState().getLogicalSwitches(lswitchIid);
92             if (operationalSwitchOptional.isPresent()) {
93                 Uuid logicalSwitchUuid = operationalSwitchOptional.get().getLogicalSwitchUuid();
94                 UUID logicalSwitchUUID = new UUID(logicalSwitchUuid.getValue());
95                 mcastMacsLocal.setLogicalSwitch(logicalSwitchUUID);
96             } else {
97                 LOG.warn(
98                     "Create or update localMcastMac: No logical switch with iid {} found in operational datastore!",
99                     lswitchIid);
100             }
101         }
102     }
103
104     private void setLocatorSet(final TransactionBuilder transaction, final McastMacsLocal mcastMacsLocal,
105             final LocalMcastMacs inputMac) {
106         if (inputMac.getLocatorSet() != null && !inputMac.getLocatorSet().isEmpty()) {
107             UUID locatorSetUuid = TransactUtils.createPhysicalLocatorSet(getOperationalState(), transaction,
108                     inputMac.getLocatorSet());
109             mcastMacsLocal.setLocatorSet(locatorSetUuid);
110         }
111     }
112
113     private static void setIpAddress(final McastMacsLocal mcastMacsLocal, final LocalMcastMacs inputMac) {
114         if (inputMac.getIpaddr() != null) {
115             mcastMacsLocal.setIpAddress(inputMac.getIpaddr().getIpv4Address().getValue());
116         }
117     }
118
119     private static void setMac(final McastMacsLocal mcastMacsLocal, final LocalMcastMacs inputMac,
120             final Optional<LocalMcastMacs> inputSwitchOptional) {
121         if (inputMac.getMacEntryKey() != null) {
122             if (inputMac.getMacEntryKey().equals(HwvtepSouthboundConstants.UNKNOWN_DST_MAC)) {
123                 mcastMacsLocal.setMac(HwvtepSouthboundConstants.UNKNOWN_DST_STRING);
124             } else {
125                 mcastMacsLocal.setMac(inputMac.getMacEntryKey().getValue());
126             }
127         } else if (inputSwitchOptional.isPresent() && inputSwitchOptional.get().getMacEntryKey() != null) {
128             mcastMacsLocal.setMac(inputSwitchOptional.get().getMacEntryKey().getValue());
129         }
130     }
131
132     @Override
133     protected Map<LocalMcastMacsKey, LocalMcastMacs> getData(final HwvtepGlobalAugmentation augmentation) {
134         return augmentation.getLocalMcastMacs();
135     }
136
137     @Override
138     protected String getKeyStr(InstanceIdentifier<LocalMcastMacs> iid) {
139         return getLsKeyStr(iid.firstKeyOf(LocalMcastMacs.class).getLogicalSwitchRef().getValue());
140     }
141 }