Scale-in/Scale-out for NAT.
[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 com.google.common.base.Optional;
11 import java.math.BigInteger;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Set;
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;
32
33 @Singleton
34 public class NAPTSwitchSelector {
35     private static final Logger LOG = LoggerFactory.getLogger(NAPTSwitchSelector.class);
36     private final DataBroker dataBroker;
37     private final TombstonedNodeManager tombstonedNodeManager;
38
39     @Inject
40     public NAPTSwitchSelector(final DataBroker dataBroker, final TombstonedNodeManager tombstonedNodeManager) {
41         this.dataBroker = dataBroker;
42         this.tombstonedNodeManager = tombstonedNodeManager;
43     }
44
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 {}",
51                     routerName);
52             return BigInteger.ZERO;
53         }
54         try {
55             if (excludeDpns != null) {
56                 routerSwitches.removeAll(excludeDpns);
57             }
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);
63         }
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)));
68             } else {
69                 switchWeights.add(new SwitchWeight(dpn, 0));
70             }
71         }
72
73         BigInteger primarySwitch;
74
75         if (!switchWeights.isEmpty()) {
76
77             LOG.debug("selectNewNAPTSwitch : Current switch weights for router {} - {}", routerName, switchWeights);
78
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();
84
85             MDSALUtil.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION,
86                 getNaptSwitchesIdentifier(routerName), id);
87
88             LOG.debug("selectNewNAPTSwitch : successful addition of RouterToNaptSwitch to napt-switches container");
89             return primarySwitch;
90         } else {
91             primarySwitch = BigInteger.ZERO;
92
93             LOG.debug("selectNewNAPTSwitch : switchWeights empty, primarySwitch: {} ", primarySwitch);
94             return primarySwitch;
95         }
96     }
97
98     private Map<BigInteger, Integer> constructNAPTSwitches() {
99         Optional<NaptSwitches> optNaptSwitches =
100             MDSALUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION, getNaptSwitchesIdentifier());
101         Map<BigInteger, Integer> switchWeights = new HashMap<>();
102
103         if (optNaptSwitches.isPresent()) {
104             NaptSwitches naptSwitches = optNaptSwitches.get();
105
106             for (RouterToNaptSwitch naptSwitch : naptSwitches.nonnullRouterToNaptSwitch()) {
107                 BigInteger primarySwitch = naptSwitch.getPrimarySwitchId();
108                 //update weight
109                 Integer weight = switchWeights.get(primarySwitch);
110                 if (weight == null) {
111                     switchWeights.put(primarySwitch, 1);
112                 } else {
113                     switchWeights.put(primarySwitch, ++weight);
114                 }
115             }
116         }
117         return switchWeights;
118     }
119
120     private InstanceIdentifier<NaptSwitches> getNaptSwitchesIdentifier() {
121         return InstanceIdentifier.create(NaptSwitches.class);
122     }
123
124     private InstanceIdentifier<RouterToNaptSwitch> getNaptSwitchesIdentifier(String routerName) {
125         return InstanceIdentifier.builder(NaptSwitches.class)
126             .child(RouterToNaptSwitch.class, new RouterToNaptSwitchKey(routerName)).build();
127     }
128
129     @NonNull
130     public List<BigInteger> getDpnsForVpn(String routerName) {
131         LOG.debug("getDpnsForVpn: called for RouterName {}", routerName);
132         long bgpVpnId = NatUtil.getBgpVpnId(dataBroker, routerName);
133         // TODO Why?
134         if (bgpVpnId != NatConstants.INVALID_ID) {
135             return NatUtil.getDpnsForRouter(dataBroker, routerName);
136         }
137         return NatUtil.getDpnsForRouter(dataBroker, routerName);
138     }
139
140     private static class SwitchWeight implements Comparable<SwitchWeight> {
141         private final BigInteger swich;
142         private int weight;
143
144         SwitchWeight(BigInteger swich, int weight) {
145             this.swich = swich;
146             this.weight = weight;
147         }
148
149         @Override
150         public int hashCode() {
151             final int prime = 31;
152             int result = 1;
153             result = prime * result + (swich == null ? 0 : swich.hashCode());
154             return result;
155         }
156
157         @Override
158         public boolean equals(Object obj) {
159             if (this == obj) {
160                 return true;
161             }
162             if (obj == null) {
163                 return false;
164             }
165             if (getClass() != obj.getClass()) {
166                 return false;
167             }
168             SwitchWeight other = (SwitchWeight) obj;
169             if (swich == null) {
170                 if (other.swich != null) {
171                     return false;
172                 }
173             } else if (!swich.equals(other.swich)) {
174                 return false;
175             }
176             return true;
177         }
178
179         public BigInteger getSwitch() {
180             return swich;
181         }
182
183         public int getWeight() {
184             return weight;
185         }
186
187         @Override
188         public int compareTo(@NonNull SwitchWeight switchWeight) {
189             return weight - switchWeight.getWeight();
190         }
191     }
192 }