Migrate users of Optional.get()
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / McastMacsLocalRemoveCommand.java
1 /*
2  * Copyright © 2015, 2017 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 package org.opendaylight.ovsdb.hwvtepsouthbound.transact;
9
10 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
11
12 import java.util.Collection;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Map.Entry;
16 import java.util.Optional;
17 import org.opendaylight.mdsal.binding.api.DataTreeModification;
18 import org.opendaylight.ovsdb.lib.notation.UUID;
19 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
20 import org.opendaylight.ovsdb.schema.hardwarevtep.McastMacsLocal;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalMcastMacs;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalMcastMacsKey;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class McastMacsLocalRemoveCommand
30         extends AbstractTransactCommand<LocalMcastMacs, LocalMcastMacsKey, HwvtepGlobalAugmentation> {
31     private static final Logger LOG = LoggerFactory.getLogger(McastMacsLocalRemoveCommand.class);
32
33     public McastMacsLocalRemoveCommand(final HwvtepOperationalState state,
34             final Collection<DataTreeModification<Node>> changes) {
35         super(state, changes);
36     }
37
38     @Override
39     public void execute(final TransactionBuilder transaction) {
40         Map<InstanceIdentifier<Node>, List<LocalMcastMacs>> removeds =
41                 extractRemoved(getChanges(),LocalMcastMacs.class);
42         if (!removeds.isEmpty()) {
43             for (Entry<InstanceIdentifier<Node>, List<LocalMcastMacs>> removed:
44                 removeds.entrySet()) {
45                 removeMcastMacLocal(transaction,  removed.getKey(), removed.getValue());
46             }
47         }
48     }
49
50     private void removeMcastMacLocal(final TransactionBuilder transaction,
51             final InstanceIdentifier<Node> instanceIdentifier, final List<LocalMcastMacs> macList) {
52         for (LocalMcastMacs mac: macList) {
53             LOG.debug("Removing localMcastMacs, mac address: {}", mac.getMacEntryKey().getValue());
54             Optional<LocalMcastMacs> operationalMacOptional =
55                     getOperationalState().getLocalMcastMacs(instanceIdentifier, mac.key());
56             McastMacsLocal mcastMacsLocal = transaction.getTypedRowSchema(McastMacsLocal.class);
57             if (operationalMacOptional.isPresent() && operationalMacOptional.orElseThrow().getMacEntryUuid() != null) {
58                 //when mac entry is deleted, its referenced locator set and locators are deleted automatically.
59                 //TODO: locator in config DS is not deleted
60                 UUID macEntryUUID = new UUID(operationalMacOptional.orElseThrow().getMacEntryUuid().getValue());
61                 mcastMacsLocal.getUuidColumn().setData(macEntryUUID);
62                 transaction.add(op.delete(mcastMacsLocal.getSchema())
63                         .where(mcastMacsLocal.getUuidColumn().getSchema().opEqual(macEntryUUID)).build());
64                 transaction.add(op.comment("McastMacLocal: Deleting " + mac.getMacEntryKey().getValue()));
65             } else {
66                 LOG.warn("Unable to delete localMcastMacs {} because it was not found in the operational store",
67                         mac.getMacEntryKey().getValue());
68             }
69         }
70     }
71
72     @Override
73     protected Map<LocalMcastMacsKey, LocalMcastMacs> getData(final HwvtepGlobalAugmentation augmentation) {
74         return augmentation.getLocalMcastMacs();
75     }
76
77     @Override
78     protected boolean cascadeDelete() {
79         return true;
80     }
81
82     @Override
83     protected UnMetDependencyGetter<LocalMcastMacs> getDependencyGetter() {
84         return MacDependencyGetter.INSTANCE;
85     }
86
87     @Override
88     protected boolean isDeleteCmd() {
89         return true;
90     }
91
92     @Override
93     protected String getKeyStr(InstanceIdentifier<LocalMcastMacs> iid) {
94         return getLsKeyStr(iid.firstKeyOf(LocalMcastMacs.class).getLogicalSwitchRef().getValue());
95     }
96
97     // FIXME: hide/move/make this final
98     public static class MacDependencyGetter extends UnMetDependencyGetter<LocalMcastMacs> {
99         static final MacDependencyGetter INSTANCE = new MacDependencyGetter();
100
101         protected MacDependencyGetter() {
102             // Hidden on purpose
103         }
104
105         @Override
106         public List<InstanceIdentifier<?>> getLogicalSwitchDependencies(final LocalMcastMacs data) {
107             return List.of(data.getLogicalSwitchRef().getValue());
108         }
109
110         @Override
111         public List<InstanceIdentifier<?>> getTerminationPointDependencies(final LocalMcastMacs data) {
112             return List.of();
113         }
114     }
115 }