2 * Copyright (c) 2017 Red Hat, Inc. 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
9 package org.opendaylight.netvirt.natservice.ha;
11 import com.google.common.base.Optional;
13 import java.math.BigInteger;
14 import java.util.HashMap;
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;
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;
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;
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();
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()) {
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);
83 writeOperTxn.submit();
84 SingleTransactionDataBroker.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION,
85 getNaptSwitchesIdentifier(routerName), id);
86 switchWeightsMap.put(nextSwitchId,switchWeightsMap.get(nextSwitchId) + 1);
88 } catch (TransactionCommitFailedException e) {
89 // TODO Auto-generated catch block
96 public boolean releaseCentralizedSwitch(String routerName) {
97 BigInteger primarySwitchId = NatUtil.getPrimaryNaptfromRouterName(dataBroker, routerName);
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,
116 writeOperTxn.submit();
117 SingleTransactionDataBroker.syncDelete(dataBroker, LogicalDatastoreType.CONFIGURATION,
118 getNaptSwitchesIdentifier(routerName));
119 switchWeightsMap.put(primarySwitchId,switchWeightsMap.get(primarySwitchId) - 1);
120 } catch (TransactionCommitFailedException e) {
127 public boolean addSwitch(BigInteger dpnId) {
128 /* Initialize the switch in the map with weight 0 */
129 switchWeightsMap.put(dpnId, initialSwitchWeight);
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());
147 switchWeightsMap.remove(dpnId);
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();
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;
170 private InstanceIdentifier<RouterToNaptSwitch> getNaptSwitchesIdentifier(String routerName) {
171 return InstanceIdentifier.builder(NaptSwitches.class)
172 .child(RouterToNaptSwitch.class, new RouterToNaptSwitchKey(routerName)).build();
175 private InstanceIdentifier<Subnetmap> getSubnetMapIdentifier(Uuid subnetId) {
176 return InstanceIdentifier.builder(Subnetmaps.class).child(Subnetmap.class,
177 new SubnetmapKey(subnetId)).build();
181 public boolean getCentralizedSwitch(String routerName) {
182 // TODO Auto-generated method stub