bug 6579 removed boilerplate code
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / reconciliation / configuration / GlobalConfigOperationalChangeGetter.java
1 /*
2  * Copyright (c) 2016 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.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;
23
24 import java.util.ArrayList;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Set;
28
29 public class GlobalConfigOperationalChangeGetter {
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 = getAugmentationFromNode(configNode);
38         HwvtepGlobalAugmentationBuilder oldAugmentation = getAugmentationFromNode(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<Node>(nodeId, newNodeBuilder.build(), oldNodeBuilder.build());
47     }
48
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);
55
56         oldAugmentation.setLocalUcastMacs(localUcastMacsToBeRemoved);
57         oldAugmentation.setLocalMcastMacs(localMcastMacsToBeRemoved);
58     }
59
60     static List<LocalUcastMacs> getLocalUcastMacsToBeRemoved(Node opNode, final Set<String> removedSwitchNames) {
61         if (opNode == null || opNode.getAugmentation(HwvtepGlobalAugmentation.class) == null) {
62             return null;
63         }
64         List<LocalUcastMacs> localUcastMacs = opNode.getAugmentation(HwvtepGlobalAugmentation.class).
65                 getLocalUcastMacs();
66         if (localUcastMacs == null) {
67             return null;
68         }
69         Iterable<LocalUcastMacs> removedLocalUcastMacs = Iterables.filter(localUcastMacs,
70                 new Predicate<LocalUcastMacs>() {
71                     @Override
72                     public boolean apply(LocalUcastMacs mac) {
73                         String ls = mac.getLogicalSwitchRef().getValue().firstKeyOf(LogicalSwitches.class).
74                                 getHwvtepNodeName().getValue();
75                         if (removedSwitchNames.contains(ls)) {
76                             return true;
77                         }
78                         return false;
79                     }
80                 });
81         return Lists.newArrayList(removedLocalUcastMacs);
82     }
83
84     static List<LocalMcastMacs> getLocalMcastMacsToBeRemoved(Node opNode, final Set<String> removedSwitchNames) {
85         if (opNode == null || opNode.getAugmentation(HwvtepGlobalAugmentation.class) == null) {
86             return null;
87         }
88         List<LocalMcastMacs> localMcastMacs = opNode.getAugmentation(HwvtepGlobalAugmentation.class).
89                 getLocalMcastMacs();
90         if (localMcastMacs == null) {
91             return null;
92         }
93         Iterable<LocalMcastMacs> removedLocalMcastMacs = Iterables.filter(localMcastMacs,
94                 new Predicate<LocalMcastMacs>() {
95                     @Override
96                     public boolean apply(LocalMcastMacs mac) {
97                         String ls = mac.getLogicalSwitchRef().getValue().firstKeyOf(LogicalSwitches.class).
98                                 getHwvtepNodeName().getValue();
99                         if (removedSwitchNames.contains(ls)) {
100                             return true;
101                         }
102                         return false;
103                     }
104                 });
105         return Lists.newArrayList(removedLocalMcastMacs);
106     }
107
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();
113
114         if (opNode != null && opNode.getAugmentation(HwvtepGlobalAugmentation.class) != null) {
115             opLogicalSwitches = opNode.getAugmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
116         }
117         if (configNode != null && configNode.getAugmentation(HwvtepGlobalAugmentation.class) != null) {
118             cfgLogicalSwitches = configNode.getAugmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
119         }
120         if (opLogicalSwitches != null) {
121             for (LogicalSwitches ls : opLogicalSwitches) {
122                 opSwitchNames.add(ls.getHwvtepNodeName().getValue());
123             }
124         }
125         if (cfgLogicalSwitches != null) {
126             for (LogicalSwitches ls : cfgLogicalSwitches) {
127                 cfgSwitchNames.add(ls.getHwvtepNodeName().getValue());
128             }
129         }
130         final Set<String> removedSwitchNames = Sets.difference(opSwitchNames, cfgSwitchNames);
131         return removedSwitchNames;
132     }
133
134     static HwvtepGlobalAugmentationBuilder getAugmentationFromNode(Node node ) {
135         if (node == null) {
136             return new HwvtepGlobalAugmentationBuilder();
137         }
138         HwvtepGlobalAugmentation src = node.getAugmentation(HwvtepGlobalAugmentation.class);
139         HwvtepGlobalAugmentationBuilder builder = new HwvtepGlobalAugmentationBuilder();
140         if (src != null) {
141             builder.setLogicalSwitches(src.getLogicalSwitches());
142             builder.setRemoteMcastMacs(src.getRemoteMcastMacs());
143             builder.setRemoteUcastMacs(src.getRemoteUcastMacs());
144         }
145         return builder;
146     }
147
148     static NodeBuilder getNodeBuilderFromNode(Node node) {
149         NodeBuilder newNodeBuilder;
150         if (node != null) {
151             newNodeBuilder = new NodeBuilder(node);
152             newNodeBuilder.removeAugmentation(HwvtepGlobalAugmentation.class);
153         } else {
154             newNodeBuilder = new NodeBuilder();
155         }
156         newNodeBuilder.setTerminationPoint(new ArrayList<>());
157         return newNodeBuilder;
158     }
159 }