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