Merge "Bug 4789 - allowed address pair doesn't have port id"
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / McastMacsRemoteUpdateCommand.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.hwvtepsouthbound.HwvtepSouthboundConstants;
22 import org.opendaylight.ovsdb.lib.notation.UUID;
23 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
24 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
25 import org.opendaylight.ovsdb.schema.hardwarevtep.McastMacsRemote;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
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.RemoteMcastMacs;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import com.google.common.base.Optional;
38
39 public class McastMacsRemoteUpdateCommand extends AbstractTransactCommand {
40     private static final Logger LOG = LoggerFactory.getLogger(PhysicalPortRemoveCommand.class);
41
42     public McastMacsRemoteUpdateCommand(HwvtepOperationalState state,
43             Collection<DataTreeModification<Node>> changes) {
44         super(state, changes);
45     }
46
47     @Override
48     public void execute(TransactionBuilder transaction) {
49         Map<InstanceIdentifier<Node>, List<RemoteMcastMacs>> createds =
50                 extractCreated(getChanges(),RemoteMcastMacs.class);
51         if (!createds.isEmpty()) {
52             for (Entry<InstanceIdentifier<Node>, List<RemoteMcastMacs>> created:
53                 createds.entrySet()) {
54                 updateMcastMacRemote(transaction,  created.getKey(), created.getValue());
55             }
56         }
57         Map<InstanceIdentifier<Node>, List<RemoteMcastMacs>> updateds =
58                 extractUpdated(getChanges(),RemoteMcastMacs.class);
59         if (!updateds.isEmpty()) {
60             for (Entry<InstanceIdentifier<Node>, List<RemoteMcastMacs>> updated:
61                 updateds.entrySet()) {
62                 updateMcastMacRemote(transaction,  updated.getKey(), updated.getValue());
63             }
64         }
65     }
66
67     private void updateMcastMacRemote(TransactionBuilder transaction,
68             InstanceIdentifier<Node> instanceIdentifier, List<RemoteMcastMacs> macList) {
69         for (RemoteMcastMacs mac: macList) {
70             LOG.debug("Creating remoteMcastMacs, mac address: {}", mac.getMacEntryKey().getValue());
71             Optional<RemoteMcastMacs> operationalMacOptional =
72                     getOperationalState().getRemoteMcastMacs(instanceIdentifier, mac.getKey());
73             McastMacsRemote mcastMacsRemote = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), McastMacsRemote.class);
74             setIpAddress(mcastMacsRemote, mac);
75             setLocatorSet(transaction, mcastMacsRemote, mac);
76             setLogicalSwitch(instanceIdentifier, mcastMacsRemote, mac);
77             if (!operationalMacOptional.isPresent()) {
78                 setMac(mcastMacsRemote, mac, operationalMacOptional);
79                 transaction.add(op.insert(mcastMacsRemote));
80             } else {
81                 RemoteMcastMacs updatedMac = operationalMacOptional.get();
82                 String existingMac = updatedMac.getMacEntryKey().getValue();
83                 McastMacsRemote extraMac = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), McastMacsRemote.class);
84                 extraMac.setMac("");;
85                 transaction.add(op.update(mcastMacsRemote)
86                         .where(extraMac.getMacColumn().getSchema().opEqual(existingMac))
87                         .build());
88             }
89         }
90     }
91
92     private void setLogicalSwitch(InstanceIdentifier<Node> iid, McastMacsRemote mcastMacsRemote, RemoteMcastMacs inputMac) {
93         if (inputMac.getLogicalSwitchRef() != null) {
94             HwvtepNodeName lswitchName = new HwvtepNodeName(inputMac.getLogicalSwitchRef().getValue());
95             Optional<LogicalSwitches> operationalSwitchOptional =
96                     getOperationalState().getLogicalSwitches(iid, new LogicalSwitchesKey(lswitchName));
97             if (operationalSwitchOptional.isPresent()) {
98                 Uuid logicalSwitchUuid = operationalSwitchOptional.get().getLogicalSwitchUuid();
99                 UUID logicalSwitchUUID = new UUID(logicalSwitchUuid.getValue());
100                 mcastMacsRemote.setLogicalSwitch(logicalSwitchUUID);
101             } else {
102                 LOG.warn("Create or update remoteMcastMac: NO logical switch named {} found in operational datastore!",
103                         lswitchName);
104             }
105         }
106     }
107
108     private void setLocatorSet(TransactionBuilder transaction, McastMacsRemote mcastMacsRemote, RemoteMcastMacs inputMac) {
109         if (inputMac.getLocatorSet() != null && !inputMac.getLocatorSet().isEmpty()) {
110             UUID locatorSetUuid = TransactUtils.createPhysicalLocatorSet(getOperationalState(), transaction, inputMac.getLocatorSet());
111             mcastMacsRemote.setLocatorSet(locatorSetUuid);
112         }
113     }
114
115     private void setIpAddress(McastMacsRemote mcastMacsRemote, RemoteMcastMacs inputMac) {
116         if (inputMac.getIpaddr() != null) {
117             mcastMacsRemote.setIpAddress(inputMac.getIpaddr().getIpv4Address().getValue());
118         }
119     }
120
121     private void setMac(McastMacsRemote mcastMacsRemote, RemoteMcastMacs inputMac,
122             Optional<RemoteMcastMacs> inputSwitchOptional) {
123         if (inputMac.getMacEntryKey() != null) {
124             if (inputMac.getMacEntryKey().equals(HwvtepSouthboundConstants.UNKNOWN_DST_MAC)) {
125                 mcastMacsRemote.setMac(HwvtepSouthboundConstants.UNKNOWN_DST_STRING);
126             } else {
127                 mcastMacsRemote.setMac(inputMac.getMacEntryKey().getValue());
128             }
129         } else if (inputSwitchOptional.isPresent() && inputSwitchOptional.get().getMacEntryKey() != null) {
130             mcastMacsRemote.setMac(inputSwitchOptional.get().getMacEntryKey().getValue());
131         }
132     }
133
134     private Map<InstanceIdentifier<Node>, List<RemoteMcastMacs>> extractCreated(
135             Collection<DataTreeModification<Node>> changes, Class<RemoteMcastMacs> class1) {
136         Map<InstanceIdentifier<Node>, List<RemoteMcastMacs>> result
137             = new HashMap<InstanceIdentifier<Node>, List<RemoteMcastMacs>>();
138         if (changes != null && !changes.isEmpty()) {
139             for (DataTreeModification<Node> change : changes) {
140                 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
141                 final DataObjectModification<Node> mod = change.getRootNode();
142                 Node created = TransactUtils.getCreated(mod);
143                 if (created != null) {
144                     List<RemoteMcastMacs> macListUpdated = null;
145                     HwvtepGlobalAugmentation hgAugmentation = created.getAugmentation(HwvtepGlobalAugmentation.class);
146                     if (hgAugmentation != null) {
147                         macListUpdated = hgAugmentation.getRemoteMcastMacs();
148                     }
149                     if (macListUpdated != null) {
150                         result.put(key, macListUpdated);
151                     }
152                 }
153             }
154         }
155         return result;
156     }
157
158     private Map<InstanceIdentifier<Node>, List<RemoteMcastMacs>> extractUpdated(
159             Collection<DataTreeModification<Node>> changes, Class<RemoteMcastMacs> class1) {
160         Map<InstanceIdentifier<Node>, List<RemoteMcastMacs>> result
161             = new HashMap<InstanceIdentifier<Node>, List<RemoteMcastMacs>>();
162         if (changes != null && !changes.isEmpty()) {
163             for (DataTreeModification<Node> change : changes) {
164                 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
165                 final DataObjectModification<Node> mod = change.getRootNode();
166                 Node updated = TransactUtils.getUpdated(mod);
167                 Node before = mod.getDataBefore();
168                 if (updated != null && before != null) {
169                     List<RemoteMcastMacs> macListUpdated = null;
170                     List<RemoteMcastMacs> macListBefore = null;
171                     HwvtepGlobalAugmentation hgUpdated = updated.getAugmentation(HwvtepGlobalAugmentation.class);
172                     if (hgUpdated != null) {
173                         macListUpdated = hgUpdated.getRemoteMcastMacs();
174                     }
175                     HwvtepGlobalAugmentation hgBefore = before.getAugmentation(HwvtepGlobalAugmentation.class);
176                     if (hgBefore != null) {
177                         macListBefore = hgBefore.getRemoteMcastMacs();
178                     }
179                     if (macListUpdated != null) {
180                         if (macListBefore != null) {
181                             macListUpdated.removeAll(macListBefore);
182                         }
183                         result.put(key, macListUpdated);
184                     }
185                 }
186             }
187         }
188         return result;
189     }
190 }