bug 6579 removed boilerplate code
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / McastMacsRemoteUpdateCommand.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 static org.opendaylight.ovsdb.lib.operations.Operations.op;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Map.Entry;
20 import java.util.Objects;
21
22 import com.google.common.collect.Lists;
23 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
24 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundConstants;
25 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundUtil;
26 import org.opendaylight.ovsdb.lib.notation.UUID;
27 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
28 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
29 import org.opendaylight.ovsdb.schema.hardwarevtep.McastMacsRemote;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.locator.set.attributes.LocatorSet;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
41
42 public class McastMacsRemoteUpdateCommand extends AbstractTransactCommand<RemoteMcastMacs, HwvtepGlobalAugmentation> {
43     private static final Logger LOG = LoggerFactory.getLogger(McastMacsRemoteUpdateCommand.class);
44     private static final McastMacUnMetDependencyGetter MCAST_MAC_DATA_VALIDATOR = new McastMacUnMetDependencyGetter();
45
46     public McastMacsRemoteUpdateCommand(HwvtepOperationalState state,
47             Collection<DataTreeModification<Node>> changes) {
48         super(state, changes);
49     }
50
51     @Override
52     public void execute(TransactionBuilder transaction) {
53         Map<InstanceIdentifier<Node>, List<RemoteMcastMacs>> updateds =
54                 extractUpdated(getChanges(),RemoteMcastMacs.class);
55         if (!updateds.isEmpty()) {
56             for (Entry<InstanceIdentifier<Node>, List<RemoteMcastMacs>> updated:
57                 updateds.entrySet()) {
58                 updateMcastMacRemote(transaction,  updated.getKey(), updated.getValue());
59             }
60         }
61     }
62
63     private void updateMcastMacRemote(TransactionBuilder transaction,
64             InstanceIdentifier<Node> instanceIdentifier, List<RemoteMcastMacs> macList) {
65         for (RemoteMcastMacs mac: macList) {
66             //add / update only if locator set got changed
67             if (!HwvtepSouthboundUtil.isEmpty(mac.getLocatorSet())) {
68                 onConfigUpdate(transaction, instanceIdentifier, mac, null);
69             }
70         }
71     }
72
73     @Override
74     public void onConfigUpdate(TransactionBuilder transaction,
75                                   InstanceIdentifier<Node> nodeIid,
76                                   RemoteMcastMacs remoteMcastMac,
77                                   InstanceIdentifier macKey,
78                                   Object... extraData) {
79         InstanceIdentifier<RemoteMcastMacs> macIid = nodeIid.augmentation(HwvtepGlobalAugmentation.class).
80                 child(RemoteMcastMacs.class, remoteMcastMac.getKey());
81         processDependencies(MCAST_MAC_DATA_VALIDATOR, transaction, nodeIid, macIid, remoteMcastMac);
82     }
83
84     @Override
85     public void doDeviceTransaction(TransactionBuilder transaction,
86                                        InstanceIdentifier<Node> instanceIdentifier,
87                                        RemoteMcastMacs mac,
88                                        InstanceIdentifier macKey,
89                                        Object... extraData) {
90             LOG.debug("Creating remoteMcastMacs, mac address: {}", mac.getMacEntryKey().getValue());
91             Optional<RemoteMcastMacs> operationalMacOptional =
92                     getOperationalState().getRemoteMcastMacs(instanceIdentifier, mac.getKey());
93             McastMacsRemote mcastMacsRemote = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), McastMacsRemote.class);
94             setIpAddress(mcastMacsRemote, mac);
95             setLocatorSet(transaction, mcastMacsRemote, mac);
96             setLogicalSwitch(mcastMacsRemote, mac);
97             if (!operationalMacOptional.isPresent()) {
98                 setMac(mcastMacsRemote, mac, operationalMacOptional);
99                 LOG.trace("execute: create RemoteMcastMac entry: {}", mcastMacsRemote);
100                 transaction.add(op.insert(mcastMacsRemote));
101                 transaction.add(op.comment("McastMacRemote: Creating " + mac.getMacEntryKey().getValue()));
102             } else if (operationalMacOptional.get().getMacEntryUuid() != null) {
103                 UUID macEntryUUID = new UUID(operationalMacOptional.get().getMacEntryUuid().getValue());
104                 McastMacsRemote extraMac = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
105                                 McastMacsRemote.class, null);
106                 extraMac.getUuidColumn().setData(macEntryUUID);
107                 LOG.trace("execute: update RemoteMcastMac entry: {}", mcastMacsRemote);
108                 transaction.add(op.update(mcastMacsRemote)
109                         .where(extraMac.getUuidColumn().getSchema().opEqual(macEntryUUID))
110                         .build());
111                 transaction.add(op.comment("McastMacRemote: Updating " + macEntryUUID));
112             } else {
113                 LOG.warn("Unable to update remoteMcastMacs {} because uuid not found in the operational store",
114                                 mac.getMacEntryKey().getValue());
115             }
116     }
117
118     private void setLogicalSwitch(McastMacsRemote mcastMacsRemote, RemoteMcastMacs inputMac) {
119         if (inputMac.getLogicalSwitchRef() != null) {
120             @SuppressWarnings("unchecked")
121             InstanceIdentifier<LogicalSwitches> lswitchIid = (InstanceIdentifier<LogicalSwitches>) inputMac.getLogicalSwitchRef().getValue();
122             Optional<LogicalSwitches> operationalSwitchOptional =
123                     getOperationalState().getLogicalSwitches(lswitchIid);
124             if (operationalSwitchOptional.isPresent()) {
125                 Uuid logicalSwitchUuid = operationalSwitchOptional.get().getLogicalSwitchUuid();
126                 UUID logicalSwitchUUID = new UUID(logicalSwitchUuid.getValue());
127                 mcastMacsRemote.setLogicalSwitch(logicalSwitchUUID);
128             } else {
129                 mcastMacsRemote.setLogicalSwitch(TransactUtils.getLogicalSwitchUUID(lswitchIid));
130             }
131         }
132     }
133
134     private void setLocatorSet(TransactionBuilder transaction, McastMacsRemote mcastMacsRemote, RemoteMcastMacs inputMac) {
135         if (inputMac.getLocatorSet() != null && !inputMac.getLocatorSet().isEmpty()) {
136             UUID locatorSetUuid = TransactUtils.createPhysicalLocatorSet(getOperationalState(), transaction, inputMac.getLocatorSet());
137             mcastMacsRemote.setLocatorSet(locatorSetUuid);
138         }
139     }
140
141     private void setIpAddress(McastMacsRemote mcastMacsRemote, RemoteMcastMacs inputMac) {
142         if (inputMac.getIpaddr() != null) {
143             mcastMacsRemote.setIpAddress(inputMac.getIpaddr().getIpv4Address().getValue());
144         }
145     }
146
147     private void setMac(McastMacsRemote mcastMacsRemote, RemoteMcastMacs inputMac,
148             Optional<RemoteMcastMacs> inputSwitchOptional) {
149         if (inputMac.getMacEntryKey() != null) {
150             if (inputMac.getMacEntryKey().equals(HwvtepSouthboundConstants.UNKNOWN_DST_MAC)) {
151                 mcastMacsRemote.setMac(HwvtepSouthboundConstants.UNKNOWN_DST_STRING);
152             } else {
153                 mcastMacsRemote.setMac(inputMac.getMacEntryKey().getValue());
154             }
155         } else if (inputSwitchOptional.isPresent() && inputSwitchOptional.get().getMacEntryKey() != null) {
156             mcastMacsRemote.setMac(inputSwitchOptional.get().getMacEntryKey().getValue());
157         }
158     }
159
160     @Override
161     protected List<RemoteMcastMacs> getData(HwvtepGlobalAugmentation augmentation) {
162         return augmentation.getRemoteMcastMacs();
163     }
164
165     @Override
166     protected boolean areEqual(RemoteMcastMacs a, RemoteMcastMacs b) {
167         return a.getKey().equals(b.getKey()) && Objects.equals(a.getLocatorSet(), b.getLocatorSet());
168     }
169
170     static class McastMacUnMetDependencyGetter extends UnMetDependencyGetter<RemoteMcastMacs> {
171
172         public List<InstanceIdentifier<?>> getLogicalSwitchDependencies(RemoteMcastMacs data) {
173             if (data == null) {
174                 return Collections.EMPTY_LIST;
175             }
176             return Lists.newArrayList(data.getLogicalSwitchRef().getValue());
177         }
178
179         public List<InstanceIdentifier<?>> getTerminationPointDependencies(RemoteMcastMacs data) {
180             if (data == null || HwvtepSouthboundUtil.isEmpty(data.getLocatorSet())) {
181                 return Collections.EMPTY_LIST;
182             }
183             List<InstanceIdentifier<?>> locators = new ArrayList<>();
184             for (LocatorSet locator: data.getLocatorSet()) {
185                 locators.add(locator.getLocatorRef().getValue());
186             }
187             return locators;
188         }
189     }
190 }