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.ArrayList;
12 import java.util.HashSet;
13 import java.util.List;
16 import java.util.stream.Collectors;
17 import org.opendaylight.mdsal.binding.api.DataTreeModification;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentationBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalMcastMacs;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalMcastMacsKey;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacsKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitchesKey;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 public final class GlobalConfigOperationalChangeGetter {
32 private GlobalConfigOperationalChangeGetter() {
35 public static DataTreeModification<Node> getModification(final InstanceIdentifier<Node> nodeId,
36 final Node configNode, final Node opNode) {
38 NodeBuilder newNodeBuilder = getNodeBuilderFromNode(configNode);
39 NodeBuilder oldNodeBuilder = getNodeBuilderFromNode(opNode);
41 HwvtepGlobalAugmentationBuilder newAugmentation = augmentationFromNode(configNode);
42 HwvtepGlobalAugmentationBuilder oldAugmentation = augmentationFromNode(opNode);
44 //fire removal of local ucast macs so that logical switches will be deleted
45 fillLocalMacsToBeRemoved(oldAugmentation, configNode, opNode);
47 newNodeBuilder.addAugmentation(HwvtepGlobalAugmentation.class, newAugmentation.build());
48 oldNodeBuilder.addAugmentation(HwvtepGlobalAugmentation.class, oldAugmentation.build());
50 return new DataTreeModificationImpl<>(nodeId, newNodeBuilder.build(), oldNodeBuilder.build());
53 static void fillLocalMacsToBeRemoved(final HwvtepGlobalAugmentationBuilder oldAugmentation, final Node configNode,
55 Set<String> logicalSwitchNamesToBeRemoved = getLogicalSwitchesToBeRemoved(configNode, opNode);
56 List<LocalUcastMacs> localUcastMacsToBeRemoved = getLocalUcastMacsToBeRemoved(opNode,
57 logicalSwitchNamesToBeRemoved);
58 List<LocalMcastMacs> localMcastMacsToBeRemoved = getLocalMcastMacsToBeRemoved(opNode,
59 logicalSwitchNamesToBeRemoved);
61 oldAugmentation.setLocalUcastMacs(localUcastMacsToBeRemoved);
62 oldAugmentation.setLocalMcastMacs(localMcastMacsToBeRemoved);
65 static List<LocalUcastMacs> getLocalUcastMacsToBeRemoved(final Node opNode, final Set<String> removedSwitchNames) {
66 if (opNode == null || opNode.augmentation(HwvtepGlobalAugmentation.class) == null) {
69 Map<LocalUcastMacsKey, LocalUcastMacs> localUcastMacs = opNode.augmentation(HwvtepGlobalAugmentation.class)
71 if (localUcastMacs == null) {
74 return localUcastMacs.values().stream()
75 .filter(mac -> removedSwitchNames.contains(
76 mac.getLogicalSwitchRef().getValue().firstKeyOf(
77 LogicalSwitches.class).getHwvtepNodeName().getValue()))
78 .collect(Collectors.toList());
81 static List<LocalMcastMacs> getLocalMcastMacsToBeRemoved(final Node opNode, final Set<String> removedSwitchNames) {
82 if (opNode == null || opNode.augmentation(HwvtepGlobalAugmentation.class) == null) {
85 Map<LocalMcastMacsKey, LocalMcastMacs> localMcastMacs = opNode.augmentation(HwvtepGlobalAugmentation.class)
87 if (localMcastMacs == null) {
90 return localMcastMacs.values().stream()
91 .filter(mac -> removedSwitchNames.contains(
92 mac.getLogicalSwitchRef().getValue().firstKeyOf(
93 LogicalSwitches.class).getHwvtepNodeName().getValue()))
94 .collect(Collectors.toList());
97 static Set<String> getLogicalSwitchesToBeRemoved(final Node configNode, final Node opNode) {
98 Set<String> opSwitchNames = new HashSet<>();
99 Set<String> cfgSwitchNames = new HashSet<>();
100 Map<LogicalSwitchesKey, LogicalSwitches> cfgLogicalSwitches = null;
101 Map<LogicalSwitchesKey, LogicalSwitches> opLogicalSwitches = null;
103 if (opNode != null && opNode.augmentation(HwvtepGlobalAugmentation.class) != null) {
104 opLogicalSwitches = opNode.augmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
106 if (configNode != null && configNode.augmentation(HwvtepGlobalAugmentation.class) != null) {
107 cfgLogicalSwitches = configNode.augmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
109 if (opLogicalSwitches != null) {
110 for (LogicalSwitches ls : opLogicalSwitches.values()) {
111 opSwitchNames.add(ls.getHwvtepNodeName().getValue());
114 if (cfgLogicalSwitches != null) {
115 for (LogicalSwitches ls : cfgLogicalSwitches.values()) {
116 cfgSwitchNames.add(ls.getHwvtepNodeName().getValue());
119 final Set<String> removedSwitchNames = Sets.difference(opSwitchNames, cfgSwitchNames);
120 return removedSwitchNames;
123 static HwvtepGlobalAugmentationBuilder augmentationFromNode(final Node node) {
125 return new HwvtepGlobalAugmentationBuilder();
127 HwvtepGlobalAugmentation src = node.augmentation(HwvtepGlobalAugmentation.class);
128 HwvtepGlobalAugmentationBuilder builder = new HwvtepGlobalAugmentationBuilder();
130 builder.setLogicalSwitches(src.getLogicalSwitches());
131 builder.setRemoteMcastMacs(src.getRemoteMcastMacs());
132 builder.setRemoteUcastMacs(src.getRemoteUcastMacs());
137 static NodeBuilder getNodeBuilderFromNode(final Node node) {
138 NodeBuilder newNodeBuilder;
140 newNodeBuilder = new NodeBuilder(node);
141 newNodeBuilder.removeAugmentation(HwvtepGlobalAugmentation.class);
143 newNodeBuilder = new NodeBuilder();
145 newNodeBuilder.setTerminationPoint(new ArrayList<>());
146 return newNodeBuilder;