Cleanup warnings in hwvtepsouthbound-impl
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / reconciliation / configuration / GlobalConfigOperationalChangeGetter.java
1 /*
2  * Copyright © 2016, 2017 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 package org.opendaylight.ovsdb.hwvtepsouthbound.reconciliation.configuration;
9
10 import com.google.common.collect.Sets;
11 import java.util.HashSet;
12 import java.util.Map;
13 import java.util.Set;
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;
27
28 public final class GlobalConfigOperationalChangeGetter {
29
30     private GlobalConfigOperationalChangeGetter() {
31     }
32
33     public static DataTreeModification<Node> getModification(final InstanceIdentifier<Node> nodeId,
34                                                              final Node configNode, final Node opNode) {
35
36         NodeBuilder newNodeBuilder = getNodeBuilderFromNode(configNode);
37         NodeBuilder oldNodeBuilder = getNodeBuilderFromNode(opNode);
38
39         HwvtepGlobalAugmentationBuilder newAugmentation = augmentationFromNode(configNode);
40         HwvtepGlobalAugmentationBuilder oldAugmentation = augmentationFromNode(opNode);
41
42         //fire removal of local ucast macs so that logical switches will be deleted
43         fillLocalMacsToBeRemoved(oldAugmentation, configNode, opNode);
44
45         newNodeBuilder.addAugmentation(newAugmentation.build());
46         oldNodeBuilder.addAugmentation(oldAugmentation.build());
47
48         return new DataTreeModificationImpl<>(nodeId, newNodeBuilder.build(), oldNodeBuilder.build());
49     }
50
51     static void fillLocalMacsToBeRemoved(final HwvtepGlobalAugmentationBuilder oldAugmentation, final Node configNode,
52             final Node opNode) {
53         Set<String> logicalSwitchNamesToBeRemoved = getLogicalSwitchesToBeRemoved(configNode, opNode);
54         var localUcastMacsToBeRemoved = getLocalUcastMacsToBeRemoved(opNode, logicalSwitchNamesToBeRemoved);
55         var localMcastMacsToBeRemoved = getLocalMcastMacsToBeRemoved(opNode, logicalSwitchNamesToBeRemoved);
56
57         oldAugmentation.setLocalUcastMacs(localUcastMacsToBeRemoved);
58         oldAugmentation.setLocalMcastMacs(localMcastMacsToBeRemoved);
59     }
60
61     static Map<LocalUcastMacsKey, LocalUcastMacs> getLocalUcastMacsToBeRemoved(final Node opNode,
62             final Set<String> removedSwitchNames) {
63         if (opNode == null || opNode.augmentation(HwvtepGlobalAugmentation.class) == null) {
64             return null;
65         }
66         Map<LocalUcastMacsKey, LocalUcastMacs> localUcastMacs = opNode.augmentation(HwvtepGlobalAugmentation.class)
67                 .getLocalUcastMacs();
68         if (localUcastMacs == null) {
69             return null;
70         }
71         return localUcastMacs.values().stream()
72                 .filter(mac -> removedSwitchNames.contains(
73                         mac.getLogicalSwitchRef().getValue().firstKeyOf(
74                                 LogicalSwitches.class).getHwvtepNodeName().getValue()))
75                 .collect(BindingMap.toOrderedMap());
76     }
77
78     static Map<LocalMcastMacsKey, LocalMcastMacs> getLocalMcastMacsToBeRemoved(final Node opNode,
79             final Set<String> removedSwitchNames) {
80         if (opNode == null || opNode.augmentation(HwvtepGlobalAugmentation.class) == null) {
81             return null;
82         }
83         Map<LocalMcastMacsKey, LocalMcastMacs> localMcastMacs = opNode.augmentation(HwvtepGlobalAugmentation.class)
84                 .getLocalMcastMacs();
85         if (localMcastMacs == null) {
86             return null;
87         }
88         return localMcastMacs.values().stream()
89                 .filter(mac -> removedSwitchNames.contains(
90                         mac.getLogicalSwitchRef().getValue().firstKeyOf(
91                                 LogicalSwitches.class).getHwvtepNodeName().getValue()))
92                 .collect(BindingMap.toOrderedMap());
93     }
94
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;
100
101         if (opNode != null && opNode.augmentation(HwvtepGlobalAugmentation.class) != null) {
102             opLogicalSwitches = opNode.augmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
103         }
104         if (configNode != null && configNode.augmentation(HwvtepGlobalAugmentation.class) != null) {
105             cfgLogicalSwitches = configNode.augmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
106         }
107         if (opLogicalSwitches != null) {
108             for (LogicalSwitches ls : opLogicalSwitches.values()) {
109                 opSwitchNames.add(ls.getHwvtepNodeName().getValue());
110             }
111         }
112         if (cfgLogicalSwitches != null) {
113             for (LogicalSwitches ls : cfgLogicalSwitches.values()) {
114                 cfgSwitchNames.add(ls.getHwvtepNodeName().getValue());
115             }
116         }
117         final Set<String> removedSwitchNames = Sets.difference(opSwitchNames, cfgSwitchNames);
118         return removedSwitchNames;
119     }
120
121     static HwvtepGlobalAugmentationBuilder augmentationFromNode(final Node node) {
122         if (node == null) {
123             return new HwvtepGlobalAugmentationBuilder();
124         }
125         HwvtepGlobalAugmentation src = node.augmentation(HwvtepGlobalAugmentation.class);
126         HwvtepGlobalAugmentationBuilder builder = new HwvtepGlobalAugmentationBuilder();
127         if (src != null) {
128             builder.setLogicalSwitches(src.getLogicalSwitches());
129             builder.setRemoteMcastMacs(src.getRemoteMcastMacs());
130             builder.setRemoteUcastMacs(src.getRemoteUcastMacs());
131         }
132         return builder;
133     }
134
135     static NodeBuilder getNodeBuilderFromNode(final Node node) {
136         NodeBuilder newNodeBuilder;
137         if (node != null) {
138             newNodeBuilder = new NodeBuilder(node);
139             newNodeBuilder.removeAugmentation(HwvtepGlobalAugmentation.class);
140         } else {
141             newNodeBuilder = new NodeBuilder();
142         }
143         newNodeBuilder.setTerminationPoint(Map.of());
144         return newNodeBuilder;
145     }
146 }