2 * Copyright (c) 2016, 2017 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.netvirt.natservice.internal;
10 import static org.opendaylight.netvirt.natservice.internal.NatUtil.requireNonNullElse;
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;
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;
35 public class NAPTSwitchSelector {
36 private static final Logger LOG = LoggerFactory.getLogger(NAPTSwitchSelector.class);
37 private final DataBroker dataBroker;
40 public NAPTSwitchSelector(final DataBroker dataBroker) {
41 this.dataBroker = dataBroker;
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 {}",
51 return BigInteger.ZERO;
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)));
59 switchWeights.add(new SwitchWeight(dpn, 0));
63 BigInteger primarySwitch;
65 if (!switchWeights.isEmpty()) {
67 LOG.debug("selectNewNAPTSwitch : Current switch weights for router {} - {}", routerName, switchWeights);
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();
75 MDSALUtil.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION,
76 getNaptSwitchesIdentifier(routerName), id);
78 LOG.debug("selectNewNAPTSwitch : successful addition of RouterToNaptSwitch to napt-switches container");
81 primarySwitch = BigInteger.ZERO;
83 LOG.debug("selectNewNAPTSwitch : switchWeights empty, primarySwitch: {} ", primarySwitch);
88 private Map<BigInteger, Integer> constructNAPTSwitches() {
89 Optional<NaptSwitches> optNaptSwitches =
90 MDSALUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION, getNaptSwitchesIdentifier());
91 Map<BigInteger, Integer> switchWeights = new HashMap<>();
93 if (optNaptSwitches.isPresent()) {
94 NaptSwitches naptSwitches = optNaptSwitches.get();
96 for (RouterToNaptSwitch naptSwitch : requireNonNullElse(naptSwitches.getRouterToNaptSwitch(),
97 Collections.<RouterToNaptSwitch>emptyList())) {
98 BigInteger primarySwitch = naptSwitch.getPrimarySwitchId();
100 Integer weight = switchWeights.get(primarySwitch);
101 if (weight == null) {
102 switchWeights.put(primarySwitch, 1);
104 switchWeights.put(primarySwitch, ++weight);
108 return switchWeights;
111 private InstanceIdentifier<NaptSwitches> getNaptSwitchesIdentifier() {
112 return InstanceIdentifier.create(NaptSwitches.class);
115 private InstanceIdentifier<RouterToNaptSwitch> getNaptSwitchesIdentifier(String routerName) {
116 return InstanceIdentifier.builder(NaptSwitches.class)
117 .child(RouterToNaptSwitch.class, new RouterToNaptSwitchKey(routerName)).build();
121 public List<BigInteger> getDpnsForVpn(String routerName) {
122 LOG.debug("getDpnsForVpn: called for RouterName {}", routerName);
123 long bgpVpnId = NatUtil.getBgpVpnId(dataBroker, routerName);
125 if (bgpVpnId != NatConstants.INVALID_ID) {
126 return NatUtil.getDpnsForRouter(dataBroker, routerName);
128 return NatUtil.getDpnsForRouter(dataBroker, routerName);
131 private static class SwitchWeight implements Comparable<SwitchWeight> {
132 private final BigInteger swich;
135 SwitchWeight(BigInteger swich, int weight) {
137 this.weight = weight;
141 public int hashCode() {
142 final int prime = 31;
144 result = prime * result + (swich == null ? 0 : swich.hashCode());
149 public boolean equals(Object obj) {
156 if (getClass() != obj.getClass()) {
159 SwitchWeight other = (SwitchWeight) obj;
161 if (other.swich != null) {
164 } else if (!swich.equals(other.swich)) {
170 public BigInteger getSwitch() {
174 public int getWeight() {
178 public void incrementWeight() {
183 public int compareTo(@Nonnull SwitchWeight switchWeight) {
184 return weight - switchWeight.getWeight();