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;
15 import java.util.stream.Collectors;
16 import org.opendaylight.mdsal.binding.api.DataTreeModification;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentationBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalMcastMacs;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
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.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 public final class GlobalConfigOperationalChangeGetter {
28 private GlobalConfigOperationalChangeGetter() {
31 public static DataTreeModification<Node> getModification(InstanceIdentifier<Node> nodeId, Node configNode,
34 NodeBuilder newNodeBuilder = getNodeBuilderFromNode(configNode);
35 NodeBuilder oldNodeBuilder = getNodeBuilderFromNode(opNode);
37 HwvtepGlobalAugmentationBuilder newAugmentation = augmentationFromNode(configNode);
38 HwvtepGlobalAugmentationBuilder oldAugmentation = augmentationFromNode(opNode);
40 //fire removal of local ucast macs so that logical switches will be deleted
41 fillLocalMacsToBeRemoved(oldAugmentation, configNode, opNode);
43 newNodeBuilder.addAugmentation(HwvtepGlobalAugmentation.class, newAugmentation.build());
44 oldNodeBuilder.addAugmentation(HwvtepGlobalAugmentation.class, oldAugmentation.build());
46 return new DataTreeModificationImpl<>(nodeId, newNodeBuilder.build(), oldNodeBuilder.build());
49 static void fillLocalMacsToBeRemoved(HwvtepGlobalAugmentationBuilder oldAugmentation, Node configNode,
51 Set<String> logicalSwitchNamesToBeRemoved = getLogicalSwitchesToBeRemoved(configNode, opNode);
52 List<LocalUcastMacs> localUcastMacsToBeRemoved = getLocalUcastMacsToBeRemoved(opNode,
53 logicalSwitchNamesToBeRemoved);
54 List<LocalMcastMacs> localMcastMacsToBeRemoved = getLocalMcastMacsToBeRemoved(opNode,
55 logicalSwitchNamesToBeRemoved);
57 oldAugmentation.setLocalUcastMacs(localUcastMacsToBeRemoved);
58 oldAugmentation.setLocalMcastMacs(localMcastMacsToBeRemoved);
61 static List<LocalUcastMacs> getLocalUcastMacsToBeRemoved(Node opNode, final Set<String> removedSwitchNames) {
62 if (opNode == null || opNode.augmentation(HwvtepGlobalAugmentation.class) == null) {
65 List<LocalUcastMacs> localUcastMacs = opNode.augmentation(HwvtepGlobalAugmentation.class)
67 if (localUcastMacs == null) {
70 return localUcastMacs.stream()
71 .filter(mac -> removedSwitchNames.contains(
72 mac.getLogicalSwitchRef().getValue().firstKeyOf(
73 LogicalSwitches.class).getHwvtepNodeName().getValue()))
74 .collect(Collectors.toList());
77 static List<LocalMcastMacs> getLocalMcastMacsToBeRemoved(Node opNode, final Set<String> removedSwitchNames) {
78 if (opNode == null || opNode.augmentation(HwvtepGlobalAugmentation.class) == null) {
81 List<LocalMcastMacs> localMcastMacs = opNode.augmentation(HwvtepGlobalAugmentation.class)
83 if (localMcastMacs == null) {
86 return localMcastMacs.stream()
87 .filter(mac -> removedSwitchNames.contains(
88 mac.getLogicalSwitchRef().getValue().firstKeyOf(
89 LogicalSwitches.class).getHwvtepNodeName().getValue()))
90 .collect(Collectors.toList());
93 static Set<String> getLogicalSwitchesToBeRemoved(Node configNode, Node opNode) {
94 Set<String> opSwitchNames = new HashSet<>();
95 Set<String> cfgSwitchNames = new HashSet<>();
96 List<LogicalSwitches> cfgLogicalSwitches = new ArrayList<>();
97 List<LogicalSwitches> opLogicalSwitches = new ArrayList<>();
99 if (opNode != null && opNode.augmentation(HwvtepGlobalAugmentation.class) != null) {
100 opLogicalSwitches = opNode.augmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
102 if (configNode != null && configNode.augmentation(HwvtepGlobalAugmentation.class) != null) {
103 cfgLogicalSwitches = configNode.augmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
105 if (opLogicalSwitches != null) {
106 for (LogicalSwitches ls : opLogicalSwitches) {
107 opSwitchNames.add(ls.getHwvtepNodeName().getValue());
110 if (cfgLogicalSwitches != null) {
111 for (LogicalSwitches ls : cfgLogicalSwitches) {
112 cfgSwitchNames.add(ls.getHwvtepNodeName().getValue());
115 final Set<String> removedSwitchNames = Sets.difference(opSwitchNames, cfgSwitchNames);
116 return removedSwitchNames;
119 static HwvtepGlobalAugmentationBuilder augmentationFromNode(Node node) {
121 return new HwvtepGlobalAugmentationBuilder();
123 HwvtepGlobalAugmentation src = node.augmentation(HwvtepGlobalAugmentation.class);
124 HwvtepGlobalAugmentationBuilder builder = new HwvtepGlobalAugmentationBuilder();
126 builder.setLogicalSwitches(src.getLogicalSwitches());
127 builder.setRemoteMcastMacs(src.getRemoteMcastMacs());
128 builder.setRemoteUcastMacs(src.getRemoteUcastMacs());
133 static NodeBuilder getNodeBuilderFromNode(Node node) {
134 NodeBuilder newNodeBuilder;
136 newNodeBuilder = new NodeBuilder(node);
137 newNodeBuilder.removeAugmentation(HwvtepGlobalAugmentation.class);
139 newNodeBuilder = new NodeBuilder();
141 newNodeBuilder.setTerminationPoint(new ArrayList<>());
142 return newNodeBuilder;