Bug 8853 - In conntrack SNAT , FIB flows are not created for existing
[netvirt.git] / vpnservice / natservice / natservice-impl / src / main / java / org / opendaylight / netvirt / natservice / ha / WeightedCentralizedSwitchScheduler.java
1 /*
2  * Copyright (c) 2017 Red Hat, Inc. 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
9 package org.opendaylight.netvirt.natservice.ha;
10
11 import com.google.common.base.Optional;
12
13 import java.math.BigInteger;
14 import java.util.HashMap;
15 import java.util.Map;
16
17 import javax.inject.Inject;
18 import javax.inject.Singleton;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
23 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
24 import org.opendaylight.netvirt.natservice.api.CentralizedSwitchScheduler;
25 import org.opendaylight.netvirt.natservice.internal.NatUtil;
26 import org.opendaylight.netvirt.vpnmanager.api.IVpnFootprintService;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.OdlInterfaceRpcService;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.NaptSwitches;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.Routers;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.napt.switches.RouterToNaptSwitch;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.napt.switches.RouterToNaptSwitchBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.napt.switches.RouterToNaptSwitchKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.Subnetmaps;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.subnetmaps.Subnetmap;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.subnetmaps.SubnetmapKey;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38
39 @Singleton
40 public class WeightedCentralizedSwitchScheduler implements CentralizedSwitchScheduler {
41     private final Map<BigInteger,Integer> switchWeightsMap = new HashMap<>();
42     private final DataBroker dataBroker;
43     private final NatDataUtil natDataUtil;
44     final OdlInterfaceRpcService interfaceManager;
45     private final int initialSwitchWeight = 0;
46     private final IVpnFootprintService vpnFootprintService;
47
48     @Inject
49     public WeightedCentralizedSwitchScheduler(DataBroker dataBroker, OdlInterfaceRpcService interfaceManager,
50             NatDataUtil natDataUtil, IVpnFootprintService vpnFootprintService) {
51         this.dataBroker = dataBroker;
52         this.interfaceManager = interfaceManager;
53         this.natDataUtil = natDataUtil;
54         this.vpnFootprintService = vpnFootprintService;
55     }
56
57     @Override
58     public boolean scheduleCentralizedSwitch(String routerName) {
59         BigInteger nextSwitchId = getSwitchWithLowestWeight();
60         RouterToNaptSwitchBuilder routerToNaptSwitchBuilder =
61                 new RouterToNaptSwitchBuilder().setRouterName(routerName);
62         RouterToNaptSwitch id = routerToNaptSwitchBuilder.setPrimarySwitchId(nextSwitchId).build();
63         try {
64             WriteTransaction writeOperTxn = dataBroker.newWriteOnlyTransaction();
65             Routers router = NatUtil.getRoutersFromConfigDS(dataBroker, routerName);
66             String vpnName = router.getRouterName();
67             long vpnId = NatUtil.getVpnId(dataBroker, routerName);
68             for (Uuid subnetUuid :router.getSubnetIds()) {
69                 Optional<Subnetmap> subnetMapEntry =
70                         SingleTransactionDataBroker.syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional(
71                                 dataBroker, LogicalDatastoreType.CONFIGURATION, getSubnetMapIdentifier(subnetUuid));
72                 if (subnetMapEntry.isPresent()) {
73
74                     Uuid routerPortUuid = subnetMapEntry.get().getRouterInterfacePortId();
75                     vpnFootprintService.updateVpnToDpnMapping(nextSwitchId, vpnName,
76                             routerPortUuid.getValue(), null, true);
77                     NatUtil.addToNeutronRouterDpnsMap(dataBroker, routerName, routerPortUuid.getValue(),
78                             nextSwitchId, writeOperTxn);
79                     NatUtil.addToDpnRoutersMap(dataBroker, routerName, routerPortUuid.getValue(),
80                             nextSwitchId, writeOperTxn);
81                 }
82             }
83             writeOperTxn.submit();
84             SingleTransactionDataBroker.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION,
85                     getNaptSwitchesIdentifier(routerName), id);
86             switchWeightsMap.put(nextSwitchId,switchWeightsMap.get(nextSwitchId) + 1);
87
88         } catch (TransactionCommitFailedException e) {
89             // TODO Auto-generated catch block
90         }
91         return true;
92
93     }
94
95     @Override
96     public boolean releaseCentralizedSwitch(String routerName) {
97         BigInteger primarySwitchId = NatUtil.getPrimaryNaptfromRouterName(dataBroker, routerName);
98         try {
99             WriteTransaction writeOperTxn = dataBroker.newWriteOnlyTransaction();
100             Routers router = natDataUtil.getRouter(routerName);
101             String vpnName = router.getRouterName();
102             long vpnId = NatUtil.getVpnId(dataBroker, vpnName);
103             for (Uuid subnetUuid :router.getSubnetIds()) {
104                 Optional<Subnetmap> subnetMapEntry =
105                         SingleTransactionDataBroker.syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional(
106                                 dataBroker, LogicalDatastoreType.CONFIGURATION, getSubnetMapIdentifier(subnetUuid));
107                 if (subnetMapEntry.isPresent()) {
108                     Uuid routerPortUuid = subnetMapEntry.get().getRouterInterfacePortId();
109                     vpnFootprintService.updateVpnToDpnMapping(primarySwitchId, vpnName,
110                             routerPortUuid.getValue(), null, false);
111                     NatUtil.removeFromNeutronRouterDpnsMap(dataBroker, routerName, primarySwitchId, writeOperTxn);
112                     NatUtil.removeFromDpnRoutersMap(dataBroker, routerName, routerName, interfaceManager,
113                             writeOperTxn);
114                 }
115             }
116             writeOperTxn.submit();
117             SingleTransactionDataBroker.syncDelete(dataBroker, LogicalDatastoreType.CONFIGURATION,
118                     getNaptSwitchesIdentifier(routerName));
119             switchWeightsMap.put(primarySwitchId,switchWeightsMap.get(primarySwitchId) - 1);
120         } catch (TransactionCommitFailedException e) {
121             return false;
122         }
123         return true;
124     }
125
126     @Override
127     public boolean addSwitch(BigInteger dpnId) {
128         /* Initialize the switch in the map with weight 0 */
129         switchWeightsMap.put(dpnId, initialSwitchWeight);
130         return true;
131
132     }
133
134     @Override
135     public boolean removeSwitch(BigInteger dpnId) {
136         if (switchWeightsMap.get(dpnId) != initialSwitchWeight) {
137             NaptSwitches naptSwitches = getNaptSwitches(dataBroker);
138             for (RouterToNaptSwitch routerToNaptSwitch : naptSwitches.getRouterToNaptSwitch()) {
139                 if (dpnId.equals(routerToNaptSwitch.getPrimarySwitchId())) {
140                     releaseCentralizedSwitch(routerToNaptSwitch.getRouterName());
141                     switchWeightsMap.remove(dpnId);
142                     scheduleCentralizedSwitch(routerToNaptSwitch.getRouterName());
143                     break;
144                 }
145             }
146         } else {
147             switchWeightsMap.remove(dpnId);
148         }
149         return true;
150     }
151
152     public static NaptSwitches getNaptSwitches(DataBroker dataBroker) {
153         InstanceIdentifier<NaptSwitches> id = InstanceIdentifier.builder(NaptSwitches.class).build();
154         return SingleTransactionDataBroker.syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional(dataBroker,
155                 LogicalDatastoreType.CONFIGURATION, id).orNull();
156     }
157
158     private BigInteger getSwitchWithLowestWeight() {
159         int lowestWeight = Integer.MAX_VALUE;
160         BigInteger nextSwitchId = BigInteger.valueOf(0);
161         for (BigInteger dpnId : switchWeightsMap.keySet()) {
162             if (lowestWeight > switchWeightsMap.get(dpnId)) {
163                 lowestWeight = switchWeightsMap.get(dpnId);
164                 nextSwitchId =  dpnId;
165             }
166         }
167         return nextSwitchId;
168     }
169
170     private InstanceIdentifier<RouterToNaptSwitch> getNaptSwitchesIdentifier(String routerName) {
171         return InstanceIdentifier.builder(NaptSwitches.class)
172             .child(RouterToNaptSwitch.class, new RouterToNaptSwitchKey(routerName)).build();
173     }
174
175     private InstanceIdentifier<Subnetmap> getSubnetMapIdentifier(Uuid subnetId) {
176         return InstanceIdentifier.builder(Subnetmaps.class).child(Subnetmap.class,
177                 new SubnetmapKey(subnetId)).build();
178     }
179
180     @Override
181     public boolean getCentralizedSwitch(String routerName) {
182         // TODO Auto-generated method stub
183         return false;
184     }
185 }