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.base.Predicate;
11 import com.google.common.collect.Iterables;
12 import com.google.common.collect.Lists;
13 import com.google.common.collect.Sets;
14 import org.opendaylight.controller.md.sal.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.LocalUcastMacs;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import java.util.ArrayList;
25 import java.util.HashSet;
26 import java.util.List;
29 public class 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 = getAugmentationFromNode(configNode);
38 HwvtepGlobalAugmentationBuilder oldAugmentation = getAugmentationFromNode(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, Node opNode) {
50 Set<String> logicalSwitchNamesToBeRemoved = getLogicalSwitchesToBeRemoved(configNode, opNode);
51 List<LocalUcastMacs> localUcastMacsToBeRemoved = getLocalUcastMacsToBeRemoved(opNode,
52 logicalSwitchNamesToBeRemoved);
53 List<LocalMcastMacs> localMcastMacsToBeRemoved = getLocalMcastMacsToBeRemoved(opNode,
54 logicalSwitchNamesToBeRemoved);
56 oldAugmentation.setLocalUcastMacs(localUcastMacsToBeRemoved);
57 oldAugmentation.setLocalMcastMacs(localMcastMacsToBeRemoved);
60 static List<LocalUcastMacs> getLocalUcastMacsToBeRemoved(Node opNode, final Set<String> removedSwitchNames) {
61 if (opNode == null || opNode.getAugmentation(HwvtepGlobalAugmentation.class) == null) {
64 List<LocalUcastMacs> localUcastMacs = opNode.getAugmentation(HwvtepGlobalAugmentation.class).
66 if (localUcastMacs == null) {
69 Iterable<LocalUcastMacs> removedLocalUcastMacs = Iterables.filter(localUcastMacs,
70 new Predicate<LocalUcastMacs>() {
72 public boolean apply(LocalUcastMacs mac) {
73 String ls = mac.getLogicalSwitchRef().getValue().firstKeyOf(LogicalSwitches.class).
74 getHwvtepNodeName().getValue();
75 if (removedSwitchNames.contains(ls)) {
81 return Lists.newArrayList(removedLocalUcastMacs);
84 static List<LocalMcastMacs> getLocalMcastMacsToBeRemoved(Node opNode, final Set<String> removedSwitchNames) {
85 if (opNode == null || opNode.getAugmentation(HwvtepGlobalAugmentation.class) == null) {
88 List<LocalMcastMacs> localMcastMacs = opNode.getAugmentation(HwvtepGlobalAugmentation.class).
90 if (localMcastMacs == null) {
93 Iterable<LocalMcastMacs> removedLocalMcastMacs = Iterables.filter(localMcastMacs,
94 new Predicate<LocalMcastMacs>() {
96 public boolean apply(LocalMcastMacs mac) {
97 String ls = mac.getLogicalSwitchRef().getValue().firstKeyOf(LogicalSwitches.class).
98 getHwvtepNodeName().getValue();
99 if (removedSwitchNames.contains(ls)) {
105 return Lists.newArrayList(removedLocalMcastMacs);
108 static Set<String> getLogicalSwitchesToBeRemoved(Node configNode, Node opNode) {
109 Set<String> opSwitchNames = new HashSet<>();
110 Set<String> cfgSwitchNames = new HashSet<>();
111 List<LogicalSwitches> cfgLogicalSwitches = Lists.newArrayList();
112 List<LogicalSwitches> opLogicalSwitches = Lists.newArrayList();
114 if (opNode != null && opNode.getAugmentation(HwvtepGlobalAugmentation.class) != null) {
115 opLogicalSwitches = opNode.getAugmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
117 if (configNode != null && configNode.getAugmentation(HwvtepGlobalAugmentation.class) != null) {
118 cfgLogicalSwitches = configNode.getAugmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
120 if (opLogicalSwitches != null) {
121 for (LogicalSwitches ls : opLogicalSwitches) {
122 opSwitchNames.add(ls.getHwvtepNodeName().getValue());
125 if (cfgLogicalSwitches != null) {
126 for (LogicalSwitches ls : cfgLogicalSwitches) {
127 cfgSwitchNames.add(ls.getHwvtepNodeName().getValue());
130 final Set<String> removedSwitchNames = Sets.difference(opSwitchNames, cfgSwitchNames);
131 return removedSwitchNames;
134 static HwvtepGlobalAugmentationBuilder getAugmentationFromNode(Node node ) {
136 return new HwvtepGlobalAugmentationBuilder();
138 HwvtepGlobalAugmentation src = node.getAugmentation(HwvtepGlobalAugmentation.class);
139 HwvtepGlobalAugmentationBuilder builder = new HwvtepGlobalAugmentationBuilder();
141 builder.setLogicalSwitches(src.getLogicalSwitches());
142 builder.setRemoteMcastMacs(src.getRemoteMcastMacs());
143 builder.setRemoteUcastMacs(src.getRemoteUcastMacs());
148 static NodeBuilder getNodeBuilderFromNode(Node node) {
149 NodeBuilder newNodeBuilder;
151 newNodeBuilder = new NodeBuilder(node);
152 newNodeBuilder.removeAugmentation(HwvtepGlobalAugmentation.class);
154 newNodeBuilder = new NodeBuilder();
156 newNodeBuilder.setTerminationPoint(new ArrayList<>());
157 return newNodeBuilder;