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