Migrate users of Optional.get()
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / UcastMacsLocalUpdateCommand.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 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.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.ovsdb.lib.notation.UUID;
20 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
21 import org.opendaylight.ovsdb.schema.hardwarevtep.UcastMacsLocal;
22 import org.opendaylight.ovsdb.utils.mdsal.utils.MdsalUtils;
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.HwvtepPhysicalLocatorAugmentation;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacsKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class UcastMacsLocalUpdateCommand
36         extends AbstractTransactCommand<LocalUcastMacs, LocalUcastMacsKey, HwvtepGlobalAugmentation> {
37     private static final Logger LOG = LoggerFactory.getLogger(UcastMacsLocalUpdateCommand.class);
38
39     public UcastMacsLocalUpdateCommand(final HwvtepOperationalState state,
40             final Collection<DataTreeModification<Node>> changes) {
41         super(state, changes);
42     }
43
44     @Override
45     public void execute(final TransactionBuilder transaction) {
46         Map<InstanceIdentifier<Node>, List<LocalUcastMacs>> updateds =
47                 extractUpdated(getChanges(),LocalUcastMacs.class);
48         if (!updateds.isEmpty()) {
49             for (Entry<InstanceIdentifier<Node>, List<LocalUcastMacs>> updated:
50                 updateds.entrySet()) {
51                 updateUcastMacsLocal(transaction,  updated.getKey(), updated.getValue());
52             }
53         }
54     }
55
56     private void updateUcastMacsLocal(final TransactionBuilder transaction,
57             final InstanceIdentifier<Node> instanceIdentifier, final List<LocalUcastMacs> localUcastMacs) {
58         for (LocalUcastMacs localUcastMac: localUcastMacs) {
59             LOG.debug("Creating localUcastMacs, mac address: {}", localUcastMac.getMacEntryKey().getValue());
60             final Optional<LocalUcastMacs> operationalMacOptional =
61                     getOperationalState().getLocalUcastMacs(instanceIdentifier, localUcastMac.key());
62             UcastMacsLocal ucastMacsLocal = transaction.getTypedRowWrapper(UcastMacsLocal.class);
63             setIpAddress(ucastMacsLocal, localUcastMac);
64             setLocator(transaction, ucastMacsLocal, localUcastMac);
65             setLogicalSwitch(ucastMacsLocal, localUcastMac);
66             if (operationalMacOptional.isEmpty()) {
67                 setMac(ucastMacsLocal, localUcastMac, operationalMacOptional);
68                 LOG.trace("execute: creating LocalUcastMac entry: {}", ucastMacsLocal);
69                 transaction.add(op.insert(ucastMacsLocal));
70                 transaction.add(op.comment("UcastMacLocal: Creating " + localUcastMac.getMacEntryKey().getValue()));
71             } else if (operationalMacOptional.orElseThrow().getMacEntryUuid() != null) {
72                 UUID macEntryUUID = new UUID(operationalMacOptional.orElseThrow().getMacEntryUuid().getValue());
73                 UcastMacsLocal extraMac = transaction.getTypedRowSchema(UcastMacsLocal.class);
74                 extraMac.getUuidColumn().setData(macEntryUUID);
75                 LOG.trace("execute: updating LocalUcastMac entry: {}", ucastMacsLocal);
76                 transaction.add(op.update(ucastMacsLocal)
77                         .where(extraMac.getUuidColumn().getSchema().opEqual(macEntryUUID))
78                         .build());
79                 transaction.add(op.comment("UcastMacLocal: Updating " + macEntryUUID));
80             } else {
81                 LOG.warn("Unable to update localUcastMacs {} because uuid not found in the operational store",
82                                 localUcastMac.getMacEntryKey().getValue());
83             }
84         }
85     }
86
87     private void setLogicalSwitch(final UcastMacsLocal ucastMacsLocal, final LocalUcastMacs inputMac) {
88         if (inputMac.getLogicalSwitchRef() != null) {
89             @SuppressWarnings("unchecked")
90             InstanceIdentifier<LogicalSwitches> lswitchIid =
91                     (InstanceIdentifier<LogicalSwitches>) inputMac.getLogicalSwitchRef().getValue();
92             Optional<LogicalSwitches> operationalSwitchOptional =
93                     getOperationalState().getLogicalSwitches(lswitchIid);
94             if (operationalSwitchOptional.isPresent()) {
95                 Uuid logicalSwitchUuid = operationalSwitchOptional.orElseThrow().getLogicalSwitchUuid();
96                 UUID logicalSwitchUUID = new UUID(logicalSwitchUuid.getValue());
97                 ucastMacsLocal.setLogicalSwitch(logicalSwitchUUID);
98             } else {
99                 LOG.warn(
100                     "Create or update localUcastMacs: No logical switch with iid {} found in operational datastore!",
101                     lswitchIid);
102             }
103         }
104     }
105
106     private void setLocator(final TransactionBuilder transaction, final UcastMacsLocal ucastMacsLocal,
107             final LocalUcastMacs inputMac) {
108         //get UUID by locatorRef
109         if (inputMac.getLocatorRef() != null) {
110             UUID locatorUuid = null;
111             @SuppressWarnings("unchecked")
112             InstanceIdentifier<TerminationPoint> iid =
113                     (InstanceIdentifier<TerminationPoint>) inputMac.getLocatorRef().getValue();
114             //try to find locator in operational DS
115             Optional<HwvtepPhysicalLocatorAugmentation> operationalLocatorOptional =
116                     getOperationalState().getPhysicalLocatorAugmentation(iid);
117             if (operationalLocatorOptional.isPresent()) {
118                 //if exist, get uuid
119                 HwvtepPhysicalLocatorAugmentation locatorAugmentation = operationalLocatorOptional.orElseThrow();
120                 locatorUuid = new UUID(locatorAugmentation.getPhysicalLocatorUuid().getValue());
121             } else {
122                 //if no, get it from config DS and create id
123                 Optional<TerminationPoint> configLocatorOptional = new MdsalUtils(
124                         getOperationalState().getDataBroker()).readOptional(LogicalDatastoreType.CONFIGURATION, iid);
125                 if (configLocatorOptional.isPresent()) {
126                     HwvtepPhysicalLocatorAugmentation locatorAugmentation =
127                             configLocatorOptional.orElseThrow().augmentation(HwvtepPhysicalLocatorAugmentation.class);
128                     locatorUuid = TransactUtils.createPhysicalLocator(transaction, locatorAugmentation,
129                             getOperationalState());
130                 } else {
131                     LOG.warn("Create or update localUcastMac: No physical locator found in operational datastore!"
132                             + "Its indentifier is {}", inputMac.getLocatorRef().getValue());
133                 }
134             }
135             if (locatorUuid != null) {
136                 ucastMacsLocal.setLocator(locatorUuid);
137             }
138         }
139     }
140
141     private static void setIpAddress(final UcastMacsLocal ucastMacsLocal, final LocalUcastMacs inputMac) {
142         if (inputMac.getIpaddr() != null) {
143             ucastMacsLocal.setIpAddress(inputMac.getIpaddr().getIpv4Address().getValue());
144         }
145     }
146
147     private static void setMac(final UcastMacsLocal ucastMacsLocal, final LocalUcastMacs inputMac,
148             final Optional<LocalUcastMacs> inputSwitchOptional) {
149         if (inputMac.getMacEntryKey() != null) {
150             ucastMacsLocal.setMac(inputMac.getMacEntryKey().getValue());
151         } else if (inputSwitchOptional.isPresent() && inputSwitchOptional.orElseThrow().getMacEntryKey() != null) {
152             ucastMacsLocal.setMac(inputSwitchOptional.orElseThrow().getMacEntryKey().getValue());
153         }
154     }
155
156     @Override
157     protected Map<LocalUcastMacsKey, LocalUcastMacs> getData(final HwvtepGlobalAugmentation augmentation) {
158         return augmentation.getLocalUcastMacs();
159     }
160 }