Merge "Use odlparent-lite as artifacts parent"
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transactions / md / UcastMacsRemoteUpdateCommand.java
1 /*
2  * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. 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.transactions.md;
10
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.Map.Entry;
15
16 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepConnectionInstance;
19 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundMapper;
20 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundUtil;
21 import org.opendaylight.ovsdb.lib.message.TableUpdates;
22 import org.opendaylight.ovsdb.lib.notation.UUID;
23 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
24 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
25 import org.opendaylight.ovsdb.schema.hardwarevtep.LogicalSwitch;
26 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalLocator;
27 import org.opendaylight.ovsdb.schema.hardwarevtep.UcastMacsRemote;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentationBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepLogicalSwitchRef;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorRef;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacsBuilder;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 import com.google.common.base.Optional;
44
45 public class UcastMacsRemoteUpdateCommand extends AbstractTransactionCommand {
46
47     private static final Logger LOG = LoggerFactory.getLogger(UcastMacsRemoteUpdateCommand.class);
48     private Map<UUID, UcastMacsRemote> updatedUMacsRemoteRows;
49     private Map<UUID, UcastMacsRemote> oldUMacsRemoteRows;
50     private Map<UUID, PhysicalLocator> updatedPLocRows;
51     private Map<UUID, LogicalSwitch> updatedLSRows;
52
53     public UcastMacsRemoteUpdateCommand(HwvtepConnectionInstance key, TableUpdates updates,
54             DatabaseSchema dbSchema) {
55         super(key, updates, dbSchema);
56         updatedUMacsRemoteRows = TyperUtils.extractRowsUpdated(UcastMacsRemote.class, getUpdates(),getDbSchema());
57         oldUMacsRemoteRows = TyperUtils.extractRowsOld(UcastMacsRemote.class, getUpdates(),getDbSchema());
58         updatedPLocRows = TyperUtils.extractRowsUpdated(PhysicalLocator.class, getUpdates(),getDbSchema());
59         updatedLSRows = TyperUtils.extractRowsUpdated(LogicalSwitch.class, getUpdates(),getDbSchema());
60     }
61
62     @Override
63     public void execute(ReadWriteTransaction transaction) {
64         if (updatedUMacsRemoteRows != null || !updatedUMacsRemoteRows.isEmpty()) {
65             for (Entry<UUID, UcastMacsRemote> umrUpdate : updatedUMacsRemoteRows.entrySet()) {
66                 updateUcastMacsRemote(transaction, umrUpdate.getValue());
67             }
68         }
69     }
70
71     private void updateUcastMacsRemote(ReadWriteTransaction transaction, UcastMacsRemote ucastMacsRemote) {
72         final InstanceIdentifier<Node> connectionIId = getOvsdbConnectionInstance().getInstanceIdentifier();
73         Optional<Node> connection = HwvtepSouthboundUtil.readNode(transaction, connectionIId);
74         if(connection.isPresent()) {
75             Node connectionNode = buildConnectionNode(ucastMacsRemote);
76             transaction.merge(LogicalDatastoreType.OPERATIONAL, connectionIId, connectionNode);
77             //TODO: Handle any deletes
78         }
79     }
80
81     private Node buildConnectionNode(UcastMacsRemote uMacRemote) {
82         NodeBuilder connectionNode = new NodeBuilder();
83         connectionNode.setNodeId(getOvsdbConnectionInstance().getNodeId());
84         InstanceIdentifier<Node> nodeIid = getOvsdbConnectionInstance().getInstanceIdentifier();
85         HwvtepGlobalAugmentationBuilder hgAugmentationBuilder = new HwvtepGlobalAugmentationBuilder();
86         List<RemoteUcastMacs> remoteUMacs = new ArrayList<>();
87         RemoteUcastMacsBuilder rumBuilder = new RemoteUcastMacsBuilder();
88         rumBuilder.setMacEntryKey(new MacAddress(uMacRemote.getMac()));
89         if(uMacRemote.getIpAddr() != null && !uMacRemote.getIpAddr().isEmpty()) {
90             rumBuilder.setIpaddr(new IpAddress(uMacRemote.getIpAddr().toCharArray()));
91         }
92         if(uMacRemote.getLocatorColumn() != null
93                         && uMacRemote.getLocatorColumn().getData() != null) {
94             UUID pLocUUID = uMacRemote.getLocatorColumn().getData();
95             if(updatedPLocRows.get(pLocUUID) != null) {
96                 InstanceIdentifier<TerminationPoint> plIid = HwvtepSouthboundMapper.createInstanceIdentifier(nodeIid, updatedPLocRows.get(pLocUUID));
97                 rumBuilder.setLocatorRef(new HwvtepPhysicalLocatorRef(plIid));
98             }
99         }
100         if(uMacRemote.getLogicalSwitchColumn() != null
101                         && uMacRemote.getLogicalSwitchColumn().getData() != null) {
102             UUID lsUUID = uMacRemote.getLogicalSwitchColumn().getData();
103             if(updatedLSRows.get(lsUUID) != null) {
104                 rumBuilder.setLogicalSwitchRef(new HwvtepLogicalSwitchRef(updatedLSRows.get(lsUUID).getName()));
105             }
106         }
107         remoteUMacs.add(rumBuilder.build());
108         hgAugmentationBuilder.setRemoteUcastMacs(remoteUMacs );
109         connectionNode.addAugmentation(HwvtepGlobalAugmentation.class, hgAugmentationBuilder.build());
110         return connectionNode.build();
111     }
112
113 }