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