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