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 com.google.common.base.Optional;
11 import java.math.BigInteger;
12 import java.util.HashMap;
13 import java.util.List;
16 import java.util.TreeSet;
17 import javax.inject.Inject;
18 import javax.inject.Singleton;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
23 import org.opendaylight.genius.cloudscaler.api.TombstonedNodeManager;
24 import org.opendaylight.genius.mdsalutil.MDSALUtil;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.NaptSwitches;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.napt.switches.RouterToNaptSwitch;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.napt.switches.RouterToNaptSwitchBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.napt.switches.RouterToNaptSwitchKey;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 public class NAPTSwitchSelector {
35 private static final Logger LOG = LoggerFactory.getLogger(NAPTSwitchSelector.class);
36 private final DataBroker dataBroker;
37 private final TombstonedNodeManager tombstonedNodeManager;
40 public NAPTSwitchSelector(final DataBroker dataBroker, final TombstonedNodeManager tombstonedNodeManager) {
41 this.dataBroker = dataBroker;
42 this.tombstonedNodeManager = tombstonedNodeManager;
45 BigInteger selectNewNAPTSwitch(String routerName, List<BigInteger> excludeDpns) {
46 LOG.info("selectNewNAPTSwitch : Select a new NAPT switch for router {}", routerName);
47 Map<BigInteger, Integer> naptSwitchWeights = constructNAPTSwitches();
48 List<BigInteger> routerSwitches = getDpnsForVpn(routerName);
49 if (routerSwitches.isEmpty()) {
50 LOG.warn("selectNewNAPTSwitch : Delaying NAPT switch selection due to no dpns scenario for router {}",
52 return BigInteger.ZERO;
55 if (excludeDpns != null) {
56 routerSwitches.removeAll(excludeDpns);
58 LOG.debug("selectNewNAPTSwitch : routerSwitches before filtering : {}", routerSwitches);
59 routerSwitches = tombstonedNodeManager.filterTombStoned(routerSwitches);
60 LOG.debug("selectNewNAPTSwitch : routerSwitches after filtering : {}", routerSwitches);
61 } catch (ReadFailedException ex) {
62 LOG.error("selectNewNAPTSwitch : filterTombStoned Exception thrown", ex);
64 Set<SwitchWeight> switchWeights = new TreeSet<>();
65 for (BigInteger dpn : routerSwitches) {
66 if (naptSwitchWeights.get(dpn) != null) {
67 switchWeights.add(new SwitchWeight(dpn, naptSwitchWeights.get(dpn)));
69 switchWeights.add(new SwitchWeight(dpn, 0));
73 BigInteger primarySwitch;
75 if (!switchWeights.isEmpty()) {
77 LOG.debug("selectNewNAPTSwitch : Current switch weights for router {} - {}", routerName, switchWeights);
79 RouterToNaptSwitchBuilder routerToNaptSwitchBuilder =
80 new RouterToNaptSwitchBuilder().setRouterName(routerName);
81 SwitchWeight firstSwitchWeight = switchWeights.iterator().next();
82 primarySwitch = firstSwitchWeight.getSwitch();
83 RouterToNaptSwitch id = routerToNaptSwitchBuilder.setPrimarySwitchId(primarySwitch).build();
85 MDSALUtil.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION,
86 getNaptSwitchesIdentifier(routerName), id);
88 LOG.debug("selectNewNAPTSwitch : successful addition of RouterToNaptSwitch to napt-switches container");
91 primarySwitch = BigInteger.ZERO;
93 LOG.debug("selectNewNAPTSwitch : switchWeights empty, primarySwitch: {} ", primarySwitch);
98 private Map<BigInteger, Integer> constructNAPTSwitches() {
99 Optional<NaptSwitches> optNaptSwitches =
100 MDSALUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION, getNaptSwitchesIdentifier());
101 Map<BigInteger, Integer> switchWeights = new HashMap<>();
103 if (optNaptSwitches.isPresent()) {
104 NaptSwitches naptSwitches = optNaptSwitches.get();
106 for (RouterToNaptSwitch naptSwitch : naptSwitches.nonnullRouterToNaptSwitch()) {
107 BigInteger primarySwitch = naptSwitch.getPrimarySwitchId();
109 Integer weight = switchWeights.get(primarySwitch);
110 if (weight == null) {
111 switchWeights.put(primarySwitch, 1);
113 switchWeights.put(primarySwitch, ++weight);
117 return switchWeights;
120 private InstanceIdentifier<NaptSwitches> getNaptSwitchesIdentifier() {
121 return InstanceIdentifier.create(NaptSwitches.class);
124 private InstanceIdentifier<RouterToNaptSwitch> getNaptSwitchesIdentifier(String routerName) {
125 return InstanceIdentifier.builder(NaptSwitches.class)
126 .child(RouterToNaptSwitch.class, new RouterToNaptSwitchKey(routerName)).build();
130 public List<BigInteger> getDpnsForVpn(String routerName) {
131 LOG.debug("getDpnsForVpn: called for RouterName {}", routerName);
132 long bgpVpnId = NatUtil.getBgpVpnId(dataBroker, routerName);
134 if (bgpVpnId != NatConstants.INVALID_ID) {
135 return NatUtil.getDpnsForRouter(dataBroker, routerName);
137 return NatUtil.getDpnsForRouter(dataBroker, routerName);
140 private static class SwitchWeight implements Comparable<SwitchWeight> {
141 private final BigInteger swich;
144 SwitchWeight(BigInteger swich, int weight) {
146 this.weight = weight;
150 public int hashCode() {
151 final int prime = 31;
153 result = prime * result + (swich == null ? 0 : swich.hashCode());
158 public boolean equals(Object obj) {
165 if (getClass() != obj.getClass()) {
168 SwitchWeight other = (SwitchWeight) obj;
170 if (other.swich != null) {
173 } else if (!swich.equals(other.swich)) {
179 public BigInteger getSwitch() {
183 public int getWeight() {
188 public int compareTo(@NonNull SwitchWeight switchWeight) {
189 return weight - switchWeight.getWeight();