2 * Copyright © 2016, 2017 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.ovsdb.hwvtepsouthbound.reconciliation.configuration;
10 import com.google.common.collect.Sets;
11 import java.util.HashSet;
14 import org.opendaylight.mdsal.binding.api.DataTreeModification;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentationBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalMcastMacs;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalMcastMacsKey;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacsKey;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitchesKey;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.opendaylight.yangtools.yang.binding.util.BindingMap;
28 public final class GlobalConfigOperationalChangeGetter {
30 private GlobalConfigOperationalChangeGetter() {
33 public static DataTreeModification<Node> getModification(final InstanceIdentifier<Node> nodeId,
34 final Node configNode, final Node opNode) {
36 NodeBuilder newNodeBuilder = getNodeBuilderFromNode(configNode);
37 NodeBuilder oldNodeBuilder = getNodeBuilderFromNode(opNode);
39 HwvtepGlobalAugmentationBuilder newAugmentation = augmentationFromNode(configNode);
40 HwvtepGlobalAugmentationBuilder oldAugmentation = augmentationFromNode(opNode);
42 //fire removal of local ucast macs so that logical switches will be deleted
43 fillLocalMacsToBeRemoved(oldAugmentation, configNode, opNode);
45 newNodeBuilder.addAugmentation(newAugmentation.build());
46 oldNodeBuilder.addAugmentation(oldAugmentation.build());
48 return new DataTreeModificationImpl<>(nodeId, newNodeBuilder.build(), oldNodeBuilder.build());
51 static void fillLocalMacsToBeRemoved(final HwvtepGlobalAugmentationBuilder oldAugmentation, final Node configNode,
53 Set<String> logicalSwitchNamesToBeRemoved = getLogicalSwitchesToBeRemoved(configNode, opNode);
54 var localUcastMacsToBeRemoved = getLocalUcastMacsToBeRemoved(opNode, logicalSwitchNamesToBeRemoved);
55 var localMcastMacsToBeRemoved = getLocalMcastMacsToBeRemoved(opNode, logicalSwitchNamesToBeRemoved);
57 oldAugmentation.setLocalUcastMacs(localUcastMacsToBeRemoved);
58 oldAugmentation.setLocalMcastMacs(localMcastMacsToBeRemoved);
61 static Map<LocalUcastMacsKey, LocalUcastMacs> getLocalUcastMacsToBeRemoved(final Node opNode,
62 final Set<String> removedSwitchNames) {
63 if (opNode == null || opNode.augmentation(HwvtepGlobalAugmentation.class) == null) {
66 Map<LocalUcastMacsKey, LocalUcastMacs> localUcastMacs = opNode.augmentation(HwvtepGlobalAugmentation.class)
68 if (localUcastMacs == null) {
71 return localUcastMacs.values().stream()
72 .filter(mac -> removedSwitchNames.contains(
73 mac.getLogicalSwitchRef().getValue().firstKeyOf(
74 LogicalSwitches.class).getHwvtepNodeName().getValue()))
75 .collect(BindingMap.toOrderedMap());
78 static Map<LocalMcastMacsKey, LocalMcastMacs> getLocalMcastMacsToBeRemoved(final Node opNode,
79 final Set<String> removedSwitchNames) {
80 if (opNode == null || opNode.augmentation(HwvtepGlobalAugmentation.class) == null) {
83 Map<LocalMcastMacsKey, LocalMcastMacs> localMcastMacs = opNode.augmentation(HwvtepGlobalAugmentation.class)
85 if (localMcastMacs == null) {
88 return localMcastMacs.values().stream()
89 .filter(mac -> removedSwitchNames.contains(
90 mac.getLogicalSwitchRef().getValue().firstKeyOf(
91 LogicalSwitches.class).getHwvtepNodeName().getValue()))
92 .collect(BindingMap.toOrderedMap());
95 static Set<String> getLogicalSwitchesToBeRemoved(final Node configNode, final Node opNode) {
96 Set<String> opSwitchNames = new HashSet<>();
97 Set<String> cfgSwitchNames = new HashSet<>();
98 Map<LogicalSwitchesKey, LogicalSwitches> cfgLogicalSwitches = null;
99 Map<LogicalSwitchesKey, LogicalSwitches> opLogicalSwitches = null;
101 if (opNode != null && opNode.augmentation(HwvtepGlobalAugmentation.class) != null) {
102 opLogicalSwitches = opNode.augmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
104 if (configNode != null && configNode.augmentation(HwvtepGlobalAugmentation.class) != null) {
105 cfgLogicalSwitches = configNode.augmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
107 if (opLogicalSwitches != null) {
108 for (LogicalSwitches ls : opLogicalSwitches.values()) {
109 opSwitchNames.add(ls.getHwvtepNodeName().getValue());
112 if (cfgLogicalSwitches != null) {
113 for (LogicalSwitches ls : cfgLogicalSwitches.values()) {
114 cfgSwitchNames.add(ls.getHwvtepNodeName().getValue());
117 final Set<String> removedSwitchNames = Sets.difference(opSwitchNames, cfgSwitchNames);
118 return removedSwitchNames;
121 static HwvtepGlobalAugmentationBuilder augmentationFromNode(final Node node) {
123 return new HwvtepGlobalAugmentationBuilder();
125 HwvtepGlobalAugmentation src = node.augmentation(HwvtepGlobalAugmentation.class);
126 HwvtepGlobalAugmentationBuilder builder = new HwvtepGlobalAugmentationBuilder();
128 builder.setLogicalSwitches(src.getLogicalSwitches());
129 builder.setRemoteMcastMacs(src.getRemoteMcastMacs());
130 builder.setRemoteUcastMacs(src.getRemoteUcastMacs());
135 static NodeBuilder getNodeBuilderFromNode(final Node node) {
136 NodeBuilder newNodeBuilder;
138 newNodeBuilder = new NodeBuilder(node);
139 newNodeBuilder.removeAugmentation(HwvtepGlobalAugmentation.class);
141 newNodeBuilder = new NodeBuilder();
143 newNodeBuilder.setTerminationPoint(Map.of());
144 return newNodeBuilder;