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.util.HashMap;
12 import java.util.List;
15 import java.util.TreeSet;
16 import javax.inject.Inject;
17 import javax.inject.Singleton;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
22 import org.opendaylight.genius.cloudscaler.api.TombstonedNodeManager;
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.opendaylight.yangtools.yang.common.Uint32;
30 import org.opendaylight.yangtools.yang.common.Uint64;
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;
38 private final TombstonedNodeManager tombstonedNodeManager;
41 public NAPTSwitchSelector(final DataBroker dataBroker, final TombstonedNodeManager tombstonedNodeManager) {
42 this.dataBroker = dataBroker;
43 this.tombstonedNodeManager = tombstonedNodeManager;
46 Uint64 selectNewNAPTSwitch(String routerName, List<Uint64> excludeDpns) {
47 LOG.info("selectNewNAPTSwitch : Select a new NAPT switch for router {}", routerName);
48 Map<Uint64, Integer> naptSwitchWeights = constructNAPTSwitches();
49 List<Uint64> routerSwitches = getDpnsForVpn(routerName);
50 if (routerSwitches.isEmpty()) {
51 LOG.warn("selectNewNAPTSwitch : Delaying NAPT switch selection due to no dpns scenario for router {}",
56 if (excludeDpns != null) {
57 routerSwitches.removeAll(excludeDpns);
59 LOG.debug("selectNewNAPTSwitch : routerSwitches before filtering : {}", routerSwitches);
60 routerSwitches = tombstonedNodeManager.filterTombStoned(routerSwitches);
61 LOG.debug("selectNewNAPTSwitch : routerSwitches after filtering : {}", routerSwitches);
62 } catch (ReadFailedException ex) {
63 LOG.error("selectNewNAPTSwitch : filterTombStoned Exception thrown", ex);
65 Set<SwitchWeight> switchWeights = new TreeSet<>();
66 for (Uint64 dpn : routerSwitches) {
67 if (naptSwitchWeights.get(dpn) != null) {
68 switchWeights.add(new SwitchWeight(dpn, naptSwitchWeights.get(dpn)));
70 switchWeights.add(new SwitchWeight(dpn, 0));
76 if (!switchWeights.isEmpty()) {
78 LOG.debug("selectNewNAPTSwitch : Current switch weights for router {} - {}", routerName, switchWeights);
80 RouterToNaptSwitchBuilder routerToNaptSwitchBuilder =
81 new RouterToNaptSwitchBuilder().setRouterName(routerName);
82 SwitchWeight firstSwitchWeight = switchWeights.iterator().next();
83 primarySwitch = firstSwitchWeight.getSwitch();
84 RouterToNaptSwitch id = routerToNaptSwitchBuilder.setPrimarySwitchId(primarySwitch).build();
86 MDSALUtil.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION,
87 getNaptSwitchesIdentifier(routerName), id);
89 LOG.debug("selectNewNAPTSwitch : successful addition of RouterToNaptSwitch to napt-switches container");
92 primarySwitch = Uint64.ZERO;
94 LOG.debug("selectNewNAPTSwitch : switchWeights empty, primarySwitch: {} ", primarySwitch);
99 private Map<Uint64, Integer> constructNAPTSwitches() {
100 Optional<NaptSwitches> optNaptSwitches =
101 MDSALUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION, getNaptSwitchesIdentifier());
102 Map<Uint64, Integer> switchWeights = new HashMap<>();
104 if (optNaptSwitches.isPresent()) {
105 NaptSwitches naptSwitches = optNaptSwitches.get();
107 for (RouterToNaptSwitch naptSwitch : naptSwitches.nonnullRouterToNaptSwitch()) {
108 Uint64 primarySwitch = naptSwitch.getPrimarySwitchId();
110 Integer weight = switchWeights.get(primarySwitch);
111 if (weight == null) {
112 switchWeights.put(primarySwitch, 1);
114 switchWeights.put(primarySwitch, ++weight);
118 return switchWeights;
121 private InstanceIdentifier<NaptSwitches> getNaptSwitchesIdentifier() {
122 return InstanceIdentifier.create(NaptSwitches.class);
125 private InstanceIdentifier<RouterToNaptSwitch> getNaptSwitchesIdentifier(String routerName) {
126 return InstanceIdentifier.builder(NaptSwitches.class)
127 .child(RouterToNaptSwitch.class, new RouterToNaptSwitchKey(routerName)).build();
131 public List<Uint64> getDpnsForVpn(String routerName) {
132 LOG.debug("getDpnsForVpn: called for RouterName {}", routerName);
133 Uint32 bgpVpnId = NatUtil.getBgpVpnId(dataBroker, routerName);
135 if (bgpVpnId != NatConstants.INVALID_ID) {
136 return NatUtil.getDpnsForRouter(dataBroker, routerName);
138 return NatUtil.getDpnsForRouter(dataBroker, routerName);
141 private static class SwitchWeight implements Comparable<SwitchWeight> {
142 private final Uint64 swich;
145 SwitchWeight(Uint64 swich, int weight) {
147 this.weight = weight;
151 public int hashCode() {
152 final int prime = 31;
154 result = prime * result + (swich == null ? 0 : swich.hashCode());
159 public boolean equals(Object obj) {
166 if (getClass() != obj.getClass()) {
169 SwitchWeight other = (SwitchWeight) obj;
171 if (other.swich != null) {
174 } else if (!swich.equals(other.swich)) {
180 public Uint64 getSwitch() {
184 public int getWeight() {
189 public int compareTo(@NonNull SwitchWeight switchWeight) {
190 return weight - switchWeight.getWeight();