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