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