Bug9016:Using Single Transaction during NAPT SwitchOver
[netvirt.git] / vpnservice / natservice / natservice-impl / src / main / java / org / opendaylight / netvirt / natservice / internal / NAPTSwitchSelector.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.netvirt.natservice.internal;
9
10 import com.google.common.base.Optional;
11 import java.math.BigInteger;
12 import java.util.HashMap;
13 import java.util.Iterator;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Set;
17 import java.util.TreeSet;
18 import javax.annotation.Nonnull;
19 import javax.inject.Inject;
20 import javax.inject.Singleton;
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.genius.mdsalutil.MDSALUtil;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.NaptSwitches;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.napt.switches.RouterToNaptSwitch;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.napt.switches.RouterToNaptSwitchBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.napt.switches.RouterToNaptSwitchKey;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 @Singleton
33 public class NAPTSwitchSelector {
34     private static final Logger LOG = LoggerFactory.getLogger(NAPTSwitchSelector.class);
35     private final DataBroker dataBroker;
36
37     @Inject
38     public NAPTSwitchSelector(final DataBroker dataBroker) {
39         this.dataBroker = dataBroker;
40     }
41
42     BigInteger selectNewNAPTSwitch(String routerName) {
43         LOG.info("selectNewNAPTSwitch : Select a new NAPT switch for router {}", routerName);
44         Map<BigInteger, Integer> naptSwitchWeights = constructNAPTSwitches();
45         List<BigInteger> routerSwitches = getDpnsForVpn(routerName);
46         if (routerSwitches == null || routerSwitches.isEmpty()) {
47             LOG.warn("selectNewNAPTSwitch : Delaying NAPT switch selection due to no dpns scenario for router {}",
48                     routerName);
49             return BigInteger.ZERO;
50         }
51
52         Set<SwitchWeight> switchWeights = new TreeSet<>();
53         for (BigInteger dpn : routerSwitches) {
54             if (naptSwitchWeights.get(dpn) != null) {
55                 switchWeights.add(new SwitchWeight(dpn, naptSwitchWeights.get(dpn)));
56             } else {
57                 switchWeights.add(new SwitchWeight(dpn, 0));
58             }
59         }
60
61         BigInteger primarySwitch;
62
63         if (!switchWeights.isEmpty()) {
64
65             LOG.debug("selectNewNAPTSwitch : Current switch weights for router {} - {}", routerName, switchWeights);
66
67             Iterator<SwitchWeight> it = switchWeights.iterator();
68             RouterToNaptSwitchBuilder routerToNaptSwitchBuilder =
69                 new RouterToNaptSwitchBuilder().setRouterName(routerName);
70             if (switchWeights.size() == 1) {
71                 SwitchWeight singleSwitchWeight = null;
72                 while (it.hasNext()) {
73                     singleSwitchWeight = it.next();
74                 }
75                 primarySwitch = singleSwitchWeight.getSwitch();
76                 RouterToNaptSwitch id = routerToNaptSwitchBuilder.setPrimarySwitchId(primarySwitch).build();
77
78                 MDSALUtil.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION,
79                     getNaptSwitchesIdentifier(routerName), id);
80
81                 LOG.debug("selectNewNAPTSwitch : successful addition of RouterToNaptSwitch to napt-switches container "
82                     + "for single switch");
83                 return primarySwitch;
84             } else {
85                 SwitchWeight firstSwitchWeight = null;
86                 while (it.hasNext()) {
87                     firstSwitchWeight = it.next();
88                 }
89                 primarySwitch = firstSwitchWeight.getSwitch();
90                 RouterToNaptSwitch id = routerToNaptSwitchBuilder.setPrimarySwitchId(primarySwitch).build();
91
92                 MDSALUtil.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION,
93                     getNaptSwitchesIdentifier(routerName), id);
94
95                 LOG.debug("selectNewNAPTSwitch : successful addition of RouterToNaptSwitch to napt-switches container");
96                 return primarySwitch;
97             }
98         } else {
99             primarySwitch = BigInteger.ZERO;
100
101             LOG.debug("selectNewNAPTSwitch : switchWeights empty, primarySwitch: {} ", primarySwitch);
102             return primarySwitch;
103         }
104     }
105
106     private Map<BigInteger, Integer> constructNAPTSwitches() {
107         Optional<NaptSwitches> optNaptSwitches =
108             MDSALUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION, getNaptSwitchesIdentifier());
109         Map<BigInteger, Integer> switchWeights = new HashMap<>();
110
111         if (optNaptSwitches.isPresent()) {
112             NaptSwitches naptSwitches = optNaptSwitches.get();
113             List<RouterToNaptSwitch> routerToNaptSwitches = naptSwitches.getRouterToNaptSwitch();
114
115             for (RouterToNaptSwitch naptSwitch : routerToNaptSwitches) {
116                 BigInteger primarySwitch = naptSwitch.getPrimarySwitchId();
117                 //update weight
118                 Integer weight = switchWeights.get(primarySwitch);
119                 if (weight == null) {
120                     switchWeights.put(primarySwitch, 1);
121                 } else {
122                     switchWeights.put(primarySwitch, ++weight);
123                 }
124             }
125         }
126         return switchWeights;
127     }
128
129     private InstanceIdentifier<NaptSwitches> getNaptSwitchesIdentifier() {
130         return InstanceIdentifier.create(NaptSwitches.class);
131     }
132
133     private InstanceIdentifier<RouterToNaptSwitch> getNaptSwitchesIdentifier(String routerName) {
134         return InstanceIdentifier.builder(NaptSwitches.class)
135             .child(RouterToNaptSwitch.class, new RouterToNaptSwitchKey(routerName)).build();
136     }
137
138     public List<BigInteger> getDpnsForVpn(String routerName) {
139         LOG.debug("getDpnsForVpn: called for RouterName {}", routerName);
140         long bgpVpnId = NatUtil.getBgpVpnId(dataBroker, routerName);
141         // TODO Why?
142         if (bgpVpnId != NatConstants.INVALID_ID) {
143             return NatUtil.getDpnsForRouter(dataBroker, routerName);
144         }
145         return NatUtil.getDpnsForRouter(dataBroker, routerName);
146     }
147
148     private static class SwitchWeight implements Comparable<SwitchWeight> {
149         private BigInteger swich;
150         private int weight;
151
152         SwitchWeight(BigInteger swich, int weight) {
153             this.swich = swich;
154             this.weight = weight;
155         }
156
157         @Override
158         public int hashCode() {
159             final int prime = 31;
160             int result = 1;
161             result = prime * result + ((swich == null) ? 0 : swich.hashCode());
162             return result;
163         }
164
165         @Override
166         public boolean equals(Object obj) {
167             if (this == obj) {
168                 return true;
169             }
170             if (obj == null) {
171                 return false;
172             }
173             if (getClass() != obj.getClass()) {
174                 return false;
175             }
176             SwitchWeight other = (SwitchWeight) obj;
177             if (swich == null) {
178                 if (other.swich != null) {
179                     return false;
180                 }
181             } else if (!swich.equals(other.swich)) {
182                 return false;
183             }
184             return true;
185         }
186
187         public BigInteger getSwitch() {
188             return swich;
189         }
190
191         public int getWeight() {
192             return weight;
193         }
194
195         public void incrementWeight() {
196             ++weight;
197         }
198
199         @Override
200         public int compareTo(@Nonnull SwitchWeight switchWeight) {
201             return switchWeight.getWeight() - weight;
202         }
203     }
204 }