8a8ed9bb7f07378340406f92799860db7ba15558
[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.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 public class McastMacsLocalUpdateCommand extends AbstractTransactCommand<LocalMcastMacs, HwvtepGlobalAugmentation> {
34     private static final Logger LOG = LoggerFactory.getLogger(McastMacsLocalUpdateCommand.class);
35
36     public McastMacsLocalUpdateCommand(HwvtepOperationalState state,
37             Collection<DataTreeModification<Node>> changes) {
38         super(state, changes);
39     }
40
41     @Override
42     public void execute(TransactionBuilder transaction) {
43         Map<InstanceIdentifier<Node>, List<LocalMcastMacs>> updateds =
44                 extractUpdated(getChanges(),LocalMcastMacs.class);
45         if (!updateds.isEmpty()) {
46             for (Entry<InstanceIdentifier<Node>, List<LocalMcastMacs>> updated:
47                 updateds.entrySet()) {
48                 updateMcastMacsLocal(transaction,  updated.getKey(), updated.getValue());
49             }
50         }
51     }
52
53     private void updateMcastMacsLocal(TransactionBuilder transaction,
54             InstanceIdentifier<Node> instanceIdentifier, List<LocalMcastMacs> localMcastMacs) {
55         for (LocalMcastMacs localMcastMac: localMcastMacs) {
56             LOG.debug("Creating localMcastMac, mac address: {}", localMcastMac.getMacEntryKey().getValue());
57             final Optional<LocalMcastMacs> operationalMacOptional =
58                     getOperationalState().getLocalMcastMacs(instanceIdentifier, localMcastMac.key());
59             McastMacsLocal mcastMacsLocal = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
60                     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 = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
72                                 McastMacsLocal.class, null);
73                 extraMac.getUuidColumn().setData(macEntryUUID);
74                 LOG.trace("execute: update LocalMcastMac entry: {}", mcastMacsLocal);
75                 transaction.add(op.update(mcastMacsLocal)
76                         .where(extraMac.getUuidColumn().getSchema().opEqual(macEntryUUID))
77                         .build());
78                 transaction.add(op.comment("McastMacLocal: Updating " + macEntryUUID));
79             } else {
80                 LOG.warn("Unable to update localMcastMacs {} because uuid not found in the operational store",
81                                 localMcastMac.getMacEntryKey().getValue());
82             }
83         }
84     }
85
86     private void setLogicalSwitch(McastMacsLocal mcastMacsLocal, LocalMcastMacs inputMac) {
87         if (inputMac.getLogicalSwitchRef() != null) {
88             @SuppressWarnings("unchecked")
89             InstanceIdentifier<LogicalSwitches> lswitchIid =
90                    (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(
99                     "Create or update localMcastMac: No logical switch with iid {} found in operational datastore!",
100                     lswitchIid);
101             }
102         }
103     }
104
105     private void setLocatorSet(TransactionBuilder transaction, McastMacsLocal mcastMacsLocal, 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 void setIpAddress(McastMacsLocal mcastMacsLocal, LocalMcastMacs inputMac) {
114         if (inputMac.getIpaddr() != null) {
115             mcastMacsLocal.setIpAddress(inputMac.getIpaddr().getIpv4Address().getValue());
116         }
117     }
118
119     private void setMac(McastMacsLocal mcastMacsLocal, LocalMcastMacs inputMac,
120             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 List<LocalMcastMacs> getData(HwvtepGlobalAugmentation augmentation) {
134         return augmentation.getLocalMcastMacs();
135     }
136 }