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