Create UcastMacs by Listening DS Changes
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / UcastMacsLocalUpdateCommand.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.UcastMacsLocal;
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.LocalUcastMacs;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitchesKey;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
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 UcastMacsLocalUpdateCommand extends AbstractTransactCommand {
41     private static final Logger LOG = LoggerFactory.getLogger(PhysicalPortRemoveCommand.class);
42
43     public UcastMacsLocalUpdateCommand(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<LocalUcastMacs>> createds =
51                 extractCreated(getChanges(),LocalUcastMacs.class);
52         if (!createds.isEmpty()) {
53             for (Entry<InstanceIdentifier<Node>, List<LocalUcastMacs>> created:
54                 createds.entrySet()) {
55                 updateUcastMacsLocal(transaction,  created.getKey(), created.getValue());
56             }
57         }
58         Map<InstanceIdentifier<Node>, List<LocalUcastMacs>> updateds =
59                 extractUpdated(getChanges(),LocalUcastMacs.class);
60         if (!updateds.isEmpty()) {
61             for (Entry<InstanceIdentifier<Node>, List<LocalUcastMacs>> updated:
62                 updateds.entrySet()) {
63                 updateUcastMacsLocal(transaction,  updated.getKey(), updated.getValue());
64             }
65         }
66     }
67
68     private void updateUcastMacsLocal(TransactionBuilder transaction,
69             InstanceIdentifier<Node> instanceIdentifier, List<LocalUcastMacs> localUcastMacs) {
70         for (LocalUcastMacs localUcastMac: localUcastMacs) {
71             LOG.debug("Creating localUcastMacs, mac address: {}", localUcastMac.getMacEntryKey().getValue());
72             Optional<LocalUcastMacs> operationalMacOptional =
73                     getOperationalState().getLocalUcastMacs(instanceIdentifier, localUcastMac.getKey());
74             UcastMacsLocal ucastMacsLocal = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), UcastMacsLocal.class);
75             setIpAddress(ucastMacsLocal, localUcastMac);
76             setLocator(transaction, ucastMacsLocal, localUcastMac);
77             setLogicalSwitch(instanceIdentifier, ucastMacsLocal, localUcastMac);
78             if (!operationalMacOptional.isPresent()) {
79                 setMac(ucastMacsLocal, localUcastMac, operationalMacOptional);
80                 transaction.add(op.insert(ucastMacsLocal));
81             } else {
82                 LocalUcastMacs updatedMac = operationalMacOptional.get();
83                 String existingMac = updatedMac.getMacEntryKey().getValue();
84                 UcastMacsLocal extraMac = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), UcastMacsLocal.class);
85                 extraMac.setMac("");;
86                 transaction.add(op.update(ucastMacsLocal)
87                         .where(extraMac.getMacColumn().getSchema().opEqual(existingMac))
88                         .build());
89             }
90         }
91     }
92
93     private void setLogicalSwitch(InstanceIdentifier<Node> iid, UcastMacsLocal ucastMacsLocal, LocalUcastMacs 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                 ucastMacsLocal.setLogicalSwitch(logicalSwitchUUID);
102             } else {
103                 LOG.warn("Create or update localUcastMacs: No logical switch named {} found in operational datastore!",
104                         lswitchName);
105             }
106         }
107     }
108
109     private void setLocator(TransactionBuilder transaction, UcastMacsLocal ucastMacsLocal, LocalUcastMacs 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                 //if no, get it from config DS and create id
124                 Optional<TerminationPoint> configLocatorOptional =
125                         TransactUtils.readNodeFromConfig(getOperationalState().getReadWriteTransaction(), iid);
126                 if (configLocatorOptional.isPresent()) {
127                     HwvtepPhysicalLocatorAugmentation locatorAugmentation =
128                             configLocatorOptional.get().getAugmentation(HwvtepPhysicalLocatorAugmentation.class);
129                     locatorUuid = TransactUtils.createPhysicalLocator(transaction, locatorAugmentation);
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 void setIpAddress(UcastMacsLocal ucastMacsLocal, LocalUcastMacs inputMac) {
142         if (inputMac.getIpaddr() != null) {
143             ucastMacsLocal.setIpAddress(inputMac.getIpaddr().getIpv4Address().getValue());
144         }
145     }
146
147     private void setMac(UcastMacsLocal ucastMacsLocal, LocalUcastMacs inputMac,
148             Optional<LocalUcastMacs> inputSwitchOptional) {
149         if (inputMac.getMacEntryKey() != null) {
150             ucastMacsLocal.setMac(inputMac.getMacEntryKey().getValue());
151         } else if (inputSwitchOptional.isPresent() && inputSwitchOptional.get().getMacEntryKey() != null) {
152             ucastMacsLocal.setMac(inputSwitchOptional.get().getMacEntryKey().getValue());
153         }
154     }
155
156     private Map<InstanceIdentifier<Node>, List<LocalUcastMacs>> extractCreated(
157             Collection<DataTreeModification<Node>> changes, Class<LocalUcastMacs> class1) {
158         Map<InstanceIdentifier<Node>, List<LocalUcastMacs>> result
159             = new HashMap<InstanceIdentifier<Node>, List<LocalUcastMacs>>();
160         if (changes != null && !changes.isEmpty()) {
161             for (DataTreeModification<Node> change : changes) {
162                 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
163                 final DataObjectModification<Node> mod = change.getRootNode();
164                 Node created = TransactUtils.getCreated(mod);
165                 if (created != null) {
166                     List<LocalUcastMacs> macListUpdated = null;
167                     HwvtepGlobalAugmentation hgAugmentation = created.getAugmentation(HwvtepGlobalAugmentation.class);
168                     if (hgAugmentation != null) {
169                         macListUpdated = hgAugmentation.getLocalUcastMacs();
170                     }
171                     if (macListUpdated != null) {
172                         result.put(key, macListUpdated);
173                     }
174                 }
175             }
176         }
177         return result;
178     }
179
180     private Map<InstanceIdentifier<Node>, List<LocalUcastMacs>> extractUpdated(
181             Collection<DataTreeModification<Node>> changes, Class<LocalUcastMacs> class1) {
182         Map<InstanceIdentifier<Node>, List<LocalUcastMacs>> result
183             = new HashMap<InstanceIdentifier<Node>, List<LocalUcastMacs>>();
184         if (changes != null && !changes.isEmpty()) {
185             for (DataTreeModification<Node> change : changes) {
186                 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
187                 final DataObjectModification<Node> mod = change.getRootNode();
188                 Node updated = TransactUtils.getUpdated(mod);
189                 Node before = mod.getDataBefore();
190                 if (updated != null && before != null) {
191                     List<LocalUcastMacs> macListUpdated = null;
192                     List<LocalUcastMacs> macListBefore = null;
193                     HwvtepGlobalAugmentation hgUpdated = updated.getAugmentation(HwvtepGlobalAugmentation.class);
194                     if (hgUpdated != null) {
195                         macListUpdated = hgUpdated.getLocalUcastMacs();
196                     }
197                     HwvtepGlobalAugmentation hgBefore = before.getAugmentation(HwvtepGlobalAugmentation.class);
198                     if (hgBefore != null) {
199                         macListBefore = hgBefore.getLocalUcastMacs();
200                     }
201                     if (macListUpdated != null) {
202                         if (macListBefore != null) {
203                             macListUpdated.removeAll(macListBefore);
204                         }
205                         result.put(key, macListUpdated);
206                     }
207                 }
208             }
209         }
210         return result;
211     }
212 }