3a2758d22c8b43b199fbd28cb47d624c33cf8823
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / UcastMacsRemoteUpdateCommand.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.HashMap;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Map.Entry;
18
19 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
20 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
21 import org.opendaylight.ovsdb.lib.notation.UUID;
22 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
23 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
24 import org.opendaylight.ovsdb.schema.hardwarevtep.UcastMacsRemote;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentation;
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.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
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.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import com.google.common.base.Optional;
37
38 public class UcastMacsRemoteUpdateCommand extends AbstractTransactCommand {
39     private static final Logger LOG = LoggerFactory.getLogger(UcastMacsRemoteUpdateCommand.class);
40
41     public UcastMacsRemoteUpdateCommand(HwvtepOperationalState state,
42             Collection<DataTreeModification<Node>> changes) {
43         super(state, changes);
44     }
45
46     @Override
47     public void execute(TransactionBuilder transaction) {
48         Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> createds =
49                 extractCreated(getChanges(),RemoteUcastMacs.class);
50         if (!createds.isEmpty()) {
51             for (Entry<InstanceIdentifier<Node>, List<RemoteUcastMacs>> created:
52                 createds.entrySet()) {
53                 updateUcastMacsRemote(transaction,  created.getKey(), created.getValue());
54             }
55         }
56         Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> updateds =
57                 extractUpdated(getChanges(),RemoteUcastMacs.class);
58         if (!updateds.isEmpty()) {
59             for (Entry<InstanceIdentifier<Node>, List<RemoteUcastMacs>> updated:
60                 updateds.entrySet()) {
61                 updateUcastMacsRemote(transaction,  updated.getKey(), updated.getValue());
62             }
63         }
64     }
65
66     private void updateUcastMacsRemote(TransactionBuilder transaction,
67             InstanceIdentifier<Node> instanceIdentifier, List<RemoteUcastMacs> remoteUcastMacs) {
68         for (RemoteUcastMacs remoteUcastMac: remoteUcastMacs) {
69             LOG.debug("Creating remoteUcastMacs, mac address: {}", remoteUcastMac.getMacEntryKey().getValue());
70             Optional<RemoteUcastMacs> operationalMacOptional =
71                     getOperationalState().getRemoteUcastMacs(instanceIdentifier, remoteUcastMac.getKey());
72             UcastMacsRemote ucastMacsRemote = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), UcastMacsRemote.class);
73             setIpAddress(ucastMacsRemote, remoteUcastMac);
74             setLocator(transaction, ucastMacsRemote, remoteUcastMac);
75             setLogicalSwitch(ucastMacsRemote, remoteUcastMac);
76             if (!operationalMacOptional.isPresent()) {
77                 setMac(ucastMacsRemote, remoteUcastMac, operationalMacOptional);
78                 LOG.trace("execute: creating RemotUcastMac entry: {}", ucastMacsRemote);
79                 transaction.add(op.insert(ucastMacsRemote));
80                 transaction.add(op.comment("UcastMacRemote: Creating " + remoteUcastMac.getMacEntryKey().getValue()));
81             } else if (operationalMacOptional.get().getMacEntryUuid() != null) {
82                 UUID macEntryUUID = new UUID(operationalMacOptional.get().getMacEntryUuid().getValue());
83                 UcastMacsRemote extraMac = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
84                                 UcastMacsRemote.class, null);
85                 extraMac.getUuidColumn().setData(macEntryUUID);
86                 LOG.trace("execute: updating RemotUcastMac entry: {}", ucastMacsRemote);
87                 transaction.add(op.update(ucastMacsRemote)
88                         .where(extraMac.getUuidColumn().getSchema().opEqual(macEntryUUID))
89                         .build());
90                 transaction.add(op.comment("UcastMacRemote: Updating " + remoteUcastMac.getMacEntryKey().getValue()));
91             } else {
92                 LOG.warn("Unable to update remoteMcastMacs {} because uuid not found in the operational store",
93                                 remoteUcastMac.getMacEntryKey().getValue());
94             }
95         }
96     }
97
98     private void setLogicalSwitch(UcastMacsRemote ucastMacsRemote, RemoteUcastMacs inputMac) {
99         if (inputMac.getLogicalSwitchRef() != null) {
100             @SuppressWarnings("unchecked")
101             InstanceIdentifier<LogicalSwitches> lswitchIid = (InstanceIdentifier<LogicalSwitches>) inputMac.getLogicalSwitchRef().getValue();
102             Optional<LogicalSwitches> operationalSwitchOptional =
103                     getOperationalState().getLogicalSwitches(lswitchIid);
104             if (operationalSwitchOptional.isPresent()) {
105                 Uuid logicalSwitchUuid = operationalSwitchOptional.get().getLogicalSwitchUuid();
106                 UUID logicalSwitchUUID = new UUID(logicalSwitchUuid.getValue());
107                 ucastMacsRemote.setLogicalSwitch(logicalSwitchUUID);
108             } else {
109                 ucastMacsRemote.setLogicalSwitch(TransactUtils.getLogicalSwitchUUID(lswitchIid));
110             }
111         }
112     }
113
114     private void setLocator(TransactionBuilder transaction, UcastMacsRemote ucastMacsRemote, RemoteUcastMacs inputMac) {
115         //get UUID by locatorRef
116         if (inputMac.getLocatorRef() != null) {
117             UUID locatorUuid = null;
118             @SuppressWarnings("unchecked")
119             InstanceIdentifier<TerminationPoint> iid = (InstanceIdentifier<TerminationPoint>) inputMac.getLocatorRef().getValue();
120             //try to find locator in operational DS
121             Optional<HwvtepPhysicalLocatorAugmentation> operationalLocatorOptional =
122                     getOperationalState().getPhysicalLocatorAugmentation(iid);
123             if (operationalLocatorOptional.isPresent()) {
124                 //if exist, get uuid
125                 HwvtepPhysicalLocatorAugmentation locatorAugmentation = operationalLocatorOptional.get();
126                 locatorUuid = new UUID(locatorAugmentation.getPhysicalLocatorUuid().getValue());
127             } else {
128                 //TODO: need to optimize by eliminating reading Configuration datastore
129                 //if no, get it from config DS and create id
130                 locatorUuid = getOperationalState().getPhysicalLocatorInFlight(iid);
131                 if (locatorUuid == null) {
132                     Optional<TerminationPoint> configLocatorOptional =
133                             TransactUtils.readNodeFromConfig(getOperationalState().getReadWriteTransaction(), iid);
134                     if (configLocatorOptional.isPresent()) {
135                         HwvtepPhysicalLocatorAugmentation locatorAugmentation =
136                                 configLocatorOptional.get().getAugmentation(HwvtepPhysicalLocatorAugmentation.class);
137                         locatorUuid = TransactUtils.createPhysicalLocator(transaction, locatorAugmentation);
138                         getOperationalState().setPhysicalLocatorInFlight(iid, locatorUuid);
139                     } else {
140                         LOG.warn("Create or update remoteUcastMac: No physical locator found in operational datastore!"
141                                 + "Its indentifier is {}", inputMac.getLocatorRef().getValue());
142                     }
143                 }
144             }
145             if (locatorUuid != null) {
146                 ucastMacsRemote.setLocator(locatorUuid);
147             }
148         }
149     }
150
151     private void setIpAddress(UcastMacsRemote ucastMacsRemote, RemoteUcastMacs inputMac) {
152         if (inputMac.getIpaddr() != null) {
153             ucastMacsRemote.setIpAddress(inputMac.getIpaddr().getIpv4Address().getValue());
154         }
155     }
156
157     private void setMac(UcastMacsRemote ucastMacsRemote, RemoteUcastMacs inputMac,
158             Optional<RemoteUcastMacs> inputSwitchOptional) {
159         if (inputMac.getMacEntryKey() != null) {
160             ucastMacsRemote.setMac(inputMac.getMacEntryKey().getValue());
161         } else if (inputSwitchOptional.isPresent() && inputSwitchOptional.get().getMacEntryKey() != null) {
162             ucastMacsRemote.setMac(inputSwitchOptional.get().getMacEntryKey().getValue());
163         }
164     }
165
166     private Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> extractCreated(
167             Collection<DataTreeModification<Node>> changes, Class<RemoteUcastMacs> class1) {
168         Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> result
169             = new HashMap<InstanceIdentifier<Node>, List<RemoteUcastMacs>>();
170         if (changes != null && !changes.isEmpty()) {
171             for (DataTreeModification<Node> change : changes) {
172                 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
173                 final DataObjectModification<Node> mod = change.getRootNode();
174                 Node created = TransactUtils.getCreated(mod);
175                 if (created != null) {
176                     List<RemoteUcastMacs> macListUpdated = null;
177                     HwvtepGlobalAugmentation hgAugmentation = created.getAugmentation(HwvtepGlobalAugmentation.class);
178                     if (hgAugmentation != null) {
179                         macListUpdated = hgAugmentation.getRemoteUcastMacs();
180                     }
181                     if (macListUpdated != null) {
182                         result.put(key, macListUpdated);
183                     }
184                 }
185             }
186         }
187         return result;
188     }
189
190     private Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> extractUpdated(
191             Collection<DataTreeModification<Node>> changes, Class<RemoteUcastMacs> class1) {
192         Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> result
193             = new HashMap<InstanceIdentifier<Node>, List<RemoteUcastMacs>>();
194         if (changes != null && !changes.isEmpty()) {
195             for (DataTreeModification<Node> change : changes) {
196                 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
197                 final DataObjectModification<Node> mod = change.getRootNode();
198                 Node updated = TransactUtils.getUpdated(mod);
199                 Node before = mod.getDataBefore();
200                 if (updated != null && before != null) {
201                     List<RemoteUcastMacs> macListUpdated = null;
202                     List<RemoteUcastMacs> macListBefore = null;
203                     HwvtepGlobalAugmentation hgUpdated = updated.getAugmentation(HwvtepGlobalAugmentation.class);
204                     if (hgUpdated != null) {
205                         macListUpdated = hgUpdated.getRemoteUcastMacs();
206                     }
207                     HwvtepGlobalAugmentation hgBefore = before.getAugmentation(HwvtepGlobalAugmentation.class);
208                     if (hgBefore != null) {
209                         macListBefore = hgBefore.getRemoteUcastMacs();
210                     }
211                     if (macListUpdated != null) {
212                         if (macListBefore != null) {
213                             macListUpdated.removeAll(macListBefore);
214                         }
215                         result.put(key, macListUpdated);
216                     }
217                 }
218             }
219         }
220         return result;
221     }
222 }