15446d83f9e77fafa48d81e10d3a0b5def605f76
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / HwvtepSouthboundProvider.java
1 /*
2  * Copyright © 2015, 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.ovsdb.hwvtepsouthbound;
9
10 import com.google.common.base.Optional;
11 import com.google.common.util.concurrent.CheckedFuture;
12 import java.util.Collection;
13 import java.util.concurrent.ExecutionException;
14 import java.util.concurrent.TimeUnit;
15 import java.util.concurrent.atomic.AtomicBoolean;
16 import javax.annotation.PostConstruct;
17 import javax.annotation.PreDestroy;
18 import javax.inject.Inject;
19 import javax.inject.Singleton;
20 import org.apache.aries.blueprint.annotation.service.Reference;
21 import org.apache.aries.blueprint.annotation.service.Service;
22 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
24 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
25 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
26 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
29 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
30 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
31 import org.opendaylight.mdsal.eos.binding.api.Entity;
32 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipCandidateRegistration;
33 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipChange;
34 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListener;
35 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListenerRegistration;
36 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
37 import org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException;
38 import org.opendaylight.ovsdb.hwvtepsouthbound.reconciliation.configuration.HwvtepReconciliationManager;
39 import org.opendaylight.ovsdb.hwvtepsouthbound.transactions.md.TransactionInvoker;
40 import org.opendaylight.ovsdb.hwvtepsouthbound.transactions.md.TransactionInvokerImpl;
41 import org.opendaylight.ovsdb.lib.OvsdbConnection;
42 import org.opendaylight.ovsdb.utils.mdsal.utils.Scheduler;
43 import org.opendaylight.ovsdb.utils.mdsal.utils.ShardStatusMonitor;
44 import org.opendaylight.serviceutils.upgrade.UpgradeState;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
49 import org.opendaylight.yangtools.concepts.ListenerRegistration;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54 @Singleton
55 @Service(classes = HwvtepSouthboundProvider.class) // only because HwvtepCacheDisplayCmd needs a @Reference to this
56 public class HwvtepSouthboundProvider implements ClusteredDataTreeChangeListener<Topology>, AutoCloseable {
57
58     private static final Logger LOG = LoggerFactory.getLogger(HwvtepSouthboundProvider.class);
59     private static final String ENTITY_TYPE = "ovsdb-hwvtepsouthbound-provider";
60
61     private final DataBroker dataBroker;
62     private final EntityOwnershipService entityOwnershipService;
63     private final OvsdbConnection ovsdbConnection;
64
65     private HwvtepConnectionManager cm;
66     private TransactionInvoker txInvoker;
67     private EntityOwnershipCandidateRegistration registration;
68     private HwvtepsbPluginInstanceEntityOwnershipListener providerOwnershipChangeListener;
69     private HwvtepDataChangeListener hwvtepDTListener;
70     private HwvtepReconciliationManager hwvtepReconciliationManager;
71     private final AtomicBoolean registered = new AtomicBoolean(false);
72     private ListenerRegistration<HwvtepSouthboundProvider> operTopologyRegistration;
73     private int shardStatusCheckRetryCount = 1000;
74     private UpgradeState upgradeState;
75
76     @Inject
77     public HwvtepSouthboundProvider(@Reference final DataBroker dataBroker,
78             @Reference final EntityOwnershipService entityOwnershipServiceDependency,
79             @Reference final OvsdbConnection ovsdbConnection,
80             @Reference final DOMSchemaService schemaService,
81             @Reference final BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer,
82             @Reference final UpgradeState upgradeState) {
83         this.dataBroker = dataBroker;
84         this.entityOwnershipService = entityOwnershipServiceDependency;
85         registration = null;
86         this.ovsdbConnection = ovsdbConnection;
87         this.upgradeState = upgradeState;
88         HwvtepSouthboundUtil.setInstanceIdentifierCodec(new InstanceIdentifierCodec(schemaService,
89                 bindingNormalizedNodeSerializer));
90         LOG.info("HwvtepSouthboundProvider ovsdbConnectionService: {}", ovsdbConnection);
91     }
92
93     /**
94      * Used by blueprint when starting the container.
95      */
96     @PostConstruct
97     public void init() {
98         boolean isDatastoreAvailable = false;
99         int retryCount = 0;
100         try {
101             while (retryCount < shardStatusCheckRetryCount) {
102                 isDatastoreAvailable = ShardStatusMonitor.getShardStatus(ShardStatusMonitor.TOPOLOGY_SHARDS);
103                 if (isDatastoreAvailable) {
104                     break;
105                 }
106                 LOG.warn("Hwvtep: retrying shard status check for the {} time", ++retryCount);
107                 Thread.sleep(2000);
108             }
109             if (isDatastoreAvailable) {
110                 LOG.info("Hwvtep is UP");
111                 init2();
112             }
113         } catch (InterruptedException e) {
114             LOG.error("Error in intializing the Hwvtep Southbound ", e);
115         }
116     }
117
118     private void init2() {
119         LOG.info("HwvtepSouthboundProvider Session Initiated");
120         txInvoker = new TransactionInvokerImpl(dataBroker);
121         cm = new HwvtepConnectionManager(dataBroker, txInvoker, entityOwnershipService, ovsdbConnection);
122         registerConfigListenerPostUpgrade();
123         //Register listener for entityOnwership changes
124         providerOwnershipChangeListener =
125                 new HwvtepsbPluginInstanceEntityOwnershipListener(this,this.entityOwnershipService);
126
127         //register instance entity to get the ownership of the provider
128         Entity instanceEntity = new Entity(ENTITY_TYPE, ENTITY_TYPE);
129         try {
130             registration = entityOwnershipService.registerCandidate(instanceEntity);
131         } catch (CandidateAlreadyRegisteredException e) {
132             LOG.warn("HWVTEP Southbound Provider instance entity {} was already "
133                     + "registered for ownership", instanceEntity, e);
134         }
135         InstanceIdentifier<Topology> path = InstanceIdentifier
136                 .create(NetworkTopology.class)
137                 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID));
138         DataTreeIdentifier<Topology> treeId =
139                 new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, path);
140
141         LOG.trace("Registering listener for path {}", treeId);
142         operTopologyRegistration = dataBroker.registerDataTreeChangeListener(treeId, this);
143         Scheduler.getScheduledExecutorService().schedule(() -> {
144             if (!registered.get()) {
145                 openOvsdbPort();
146                 LOG.error("Timed out to get eos notification opening the port now");
147             }
148         }, HwvtepSouthboundConstants.PORT_OPEN_MAX_DELAY_IN_MINS, TimeUnit.MINUTES);
149     }
150
151     private void registerConfigListenerPostUpgrade() {
152         if (upgradeState.isUpgradeInProgress()) {
153             LOG.error("Upgrade is in progress delay config data change listener registration");
154             Scheduler.getScheduledExecutorService().schedule(() -> registerConfigListenerPostUpgrade(),
155                     60, TimeUnit.SECONDS);
156             return;
157         }
158         hwvtepDTListener = new HwvtepDataChangeListener(dataBroker, cm);
159         hwvtepReconciliationManager = new HwvtepReconciliationManager(dataBroker, cm);
160     }
161
162     @Override
163     @PreDestroy
164     @SuppressWarnings("checkstyle:IllegalCatch")
165     public void close() throws Exception {
166         LOG.info("HwvtepSouthboundProvider Closed");
167         if (txInvoker != null) {
168             try {
169                 txInvoker.close();
170                 txInvoker = null;
171             } catch (Exception e) {
172                 LOG.error("HWVTEP Southbound Provider failed to close TransactionInvoker", e);
173             }
174         }
175         if (cm != null) {
176             cm.close();
177             cm = null;
178         }
179         if (registration != null) {
180             registration.close();
181             registration = null;
182         }
183         if (providerOwnershipChangeListener != null) {
184             providerOwnershipChangeListener.close();
185             providerOwnershipChangeListener = null;
186         }
187         if (hwvtepDTListener != null) {
188             hwvtepDTListener.close();
189             hwvtepDTListener = null;
190         }
191         if (operTopologyRegistration != null) {
192             operTopologyRegistration.close();
193             operTopologyRegistration = null;
194         }
195     }
196
197     private void initializeHwvtepTopology(final LogicalDatastoreType type) {
198         InstanceIdentifier<Topology> path = InstanceIdentifier
199                 .create(NetworkTopology.class)
200                 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID));
201         ReadWriteTransaction transaction = dataBroker.newReadWriteTransaction();
202         CheckedFuture<Optional<Topology>, ReadFailedException> hwvtepTp = transaction.read(type, path);
203         try {
204             if (!hwvtepTp.get().isPresent()) {
205                 TopologyBuilder tpb = new TopologyBuilder();
206                 tpb.setTopologyId(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID);
207                 transaction.put(type, path, tpb.build(), true);
208                 transaction.submit();
209             } else {
210                 transaction.cancel();
211             }
212         } catch (InterruptedException | ExecutionException e) {
213             LOG.error("Error initializing hwvtep topology", e);
214         }
215     }
216
217     public void handleOwnershipChange(final EntityOwnershipChange ownershipChange) {
218         if (ownershipChange.getState().isOwner()) {
219             LOG.info("*This* instance of HWVTEP southbound provider is set as a MASTER instance");
220             LOG.info("Initialize HWVTEP topology {} in operational and config data store if not already present",
221                     HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID);
222             initializeHwvtepTopology(LogicalDatastoreType.OPERATIONAL);
223             initializeHwvtepTopology(LogicalDatastoreType.CONFIGURATION);
224         } else {
225             LOG.info("*This* instance of HWVTEP southbound provider is set as a SLAVE instance");
226         }
227     }
228
229
230     @Override
231     public void onDataTreeChanged(final Collection<DataTreeModification<Topology>> collection) {
232         openOvsdbPort();
233
234         if (operTopologyRegistration != null) {
235             operTopologyRegistration.close();
236             operTopologyRegistration = null;
237         }
238     }
239
240     private void openOvsdbPort() {
241         if (!registered.getAndSet(true)) {
242             LOG.info("Starting the ovsdb port");
243             ovsdbConnection.registerConnectionListener(cm);
244             ovsdbConnection.startOvsdbManager();
245         }
246     }
247
248     public void setShardStatusCheckRetryCount(int retryCount) {
249         this.shardStatusCheckRetryCount = retryCount;
250     }
251
252     private static class HwvtepsbPluginInstanceEntityOwnershipListener implements EntityOwnershipListener {
253         private final HwvtepSouthboundProvider hsp;
254         private final EntityOwnershipListenerRegistration listenerRegistration;
255
256         HwvtepsbPluginInstanceEntityOwnershipListener(final HwvtepSouthboundProvider hsp,
257                 final EntityOwnershipService entityOwnershipService) {
258             this.hsp = hsp;
259             listenerRegistration = entityOwnershipService.registerListener(ENTITY_TYPE, this);
260         }
261
262         public void close() {
263             this.listenerRegistration.close();
264         }
265
266         @Override
267         public void ownershipChanged(final EntityOwnershipChange ownershipChange) {
268             hsp.handleOwnershipChange(ownershipChange);
269         }
270     }
271
272     public HwvtepConnectionManager getHwvtepConnectionManager() {
273         return cm;
274     }
275 }