Migrate to serviceutils/tools and serviceutils/srm
[netvirt.git] / natservice / impl / src / main / java / org / opendaylight / netvirt / natservice / internal / UpgradeStateListener.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.internal;
10
11 import java.util.List;
12 import javax.annotation.Nonnull;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
20 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
21 import org.opendaylight.netvirt.natservice.api.CentralizedSwitchScheduler;
22 import org.opendaylight.serviceutils.tools.mdsal.listener.AbstractClusteredSyncDataTreeChangeListener;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.config.rev170206.NatserviceConfig;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ExtRouters;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.Routers;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.routers.ExternalIps;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsalutil.rev170830.Config;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 @Singleton
33 public class UpgradeStateListener extends AbstractClusteredSyncDataTreeChangeListener<Config> {
34     private static final Logger LOG = LoggerFactory.getLogger(UpgradeStateListener.class);
35
36     private final DataBroker dataBroker;
37     private final CentralizedSwitchScheduler centralizedSwitchScheduler;
38     private final NatserviceConfig.NatMode natMode;
39
40     @Inject
41     public UpgradeStateListener(final DataBroker dataBroker,
42                                 final CentralizedSwitchScheduler centralizedSwitchScheduler,
43                                 final NatserviceConfig config) {
44         super(dataBroker, new DataTreeIdentifier<>(
45                 LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.create(Config.class)));
46         this.dataBroker = dataBroker;
47         this.centralizedSwitchScheduler = centralizedSwitchScheduler;
48         if (config != null) {
49             this.natMode = config.getNatMode();
50         } else {
51             this.natMode = NatserviceConfig.NatMode.Controller;
52         }
53         LOG.trace("UpgradeStateListener (nat) initialized");
54     }
55
56     @Override
57     public void add(@Nonnull Config newDataObject) {
58     }
59
60     @Override
61     public void remove(@Nonnull Config removedDataObject) {
62     }
63
64     @Override
65     public void update(@Nonnull Config original, Config updated) {
66         if (natMode != NatserviceConfig.NatMode.Conntrack) {
67             return;
68         }
69
70         LOG.info("UpgradeStateListener update from {} to {}", original, updated);
71         if (!(original.isUpgradeInProgress() && !updated.isUpgradeInProgress())) {
72             return;
73         }
74
75         SingleTransactionDataBroker reader = new SingleTransactionDataBroker(dataBroker);
76         ExtRouters routers;
77         try {
78             routers = reader.syncRead(LogicalDatastoreType.CONFIGURATION,
79                     InstanceIdentifier.create(ExtRouters.class));
80         } catch (ReadFailedException e) {
81             LOG.error("Error reading external routers", e);
82             return;
83         }
84
85         for (Routers router : routers.getRouters()) {
86             List<ExternalIps> externalIps = router.getExternalIps();
87             if (router.isEnableSnat() && externalIps != null && !externalIps.isEmpty()) {
88                 centralizedSwitchScheduler.scheduleCentralizedSwitch(router);
89             }
90         }
91     }
92 }