Bug 8055: remove all unused imports
[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 org.opendaylight.controller.md.sal.binding.api.DataBroker;
11 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
12 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
13 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
14 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
15 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipChange;
16 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
17 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
18 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
19 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
22 import org.opendaylight.controller.sal.core.api.model.SchemaService;
23 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
24 import org.opendaylight.ovsdb.hwvtepsouthbound.reconciliation.configuration.HwvtepReconciliationManager;
25 import org.opendaylight.ovsdb.hwvtepsouthbound.transactions.md.TransactionInvoker;
26 import org.opendaylight.ovsdb.hwvtepsouthbound.transactions.md.TransactionInvokerImpl;
27 import org.opendaylight.ovsdb.lib.OvsdbConnection;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import com.google.common.base.Optional;
37 import com.google.common.util.concurrent.CheckedFuture;
38
39 public class HwvtepSouthboundProvider implements AutoCloseable {
40
41     private static final Logger LOG = LoggerFactory.getLogger(HwvtepSouthboundProvider.class);
42     private static final String ENTITY_TYPE = "ovsdb-hwvtepsouthbound-provider";
43
44     public static DataBroker getDb() {
45         return db;
46     }
47
48     private static DataBroker db;
49     private final EntityOwnershipService entityOwnershipService;
50     private final OvsdbConnection ovsdbConnection;
51
52     private HwvtepConnectionManager cm;
53     private TransactionInvoker txInvoker;
54     private EntityOwnershipCandidateRegistration registration;
55     private HwvtepsbPluginInstanceEntityOwnershipListener providerOwnershipChangeListener;
56     private HwvtepDataChangeListener hwvtepDTListener;
57     private HwvtepReconciliationManager hwvtepReconciliationManager;
58
59     public HwvtepSouthboundProvider(final DataBroker dataBroker,
60             final EntityOwnershipService entityOwnershipServiceDependency,
61             final OvsdbConnection ovsdbConnection,
62             final SchemaService schemaService,
63             final BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer) {
64         this.db = dataBroker;
65         this.entityOwnershipService = entityOwnershipServiceDependency;
66         registration = null;
67         this.ovsdbConnection = ovsdbConnection;
68         HwvtepSouthboundUtil.setInstanceIdentifierCodec(new InstanceIdentifierCodec(schemaService,
69                 bindingNormalizedNodeSerializer));
70         LOG.info("HwvtepSouthboundProvider ovsdbConnectionService: {}", ovsdbConnection);
71     }
72
73     /**
74      * Used by blueprint when starting the container.
75      */
76     public void init() {
77         LOG.info("HwvtepSouthboundProvider Session Initiated");
78         txInvoker = new TransactionInvokerImpl(db);
79         cm = new HwvtepConnectionManager(db, txInvoker, entityOwnershipService);
80         hwvtepDTListener = new HwvtepDataChangeListener(db, cm);
81         hwvtepReconciliationManager = new HwvtepReconciliationManager(db, cm);
82         //Register listener for entityOnwership changes
83         providerOwnershipChangeListener =
84                 new HwvtepsbPluginInstanceEntityOwnershipListener(this,this.entityOwnershipService);
85
86         //register instance entity to get the ownership of the provider
87         Entity instanceEntity = new Entity(ENTITY_TYPE, ENTITY_TYPE);
88         try {
89             Optional<EntityOwnershipState> ownershipStateOpt = entityOwnershipService.getOwnershipState(instanceEntity);
90             registration = entityOwnershipService.registerCandidate(instanceEntity);
91             if (ownershipStateOpt.isPresent()) {
92                 EntityOwnershipState ownershipState = ownershipStateOpt.get();
93                 if (ownershipState.hasOwner() && !ownershipState.isOwner()) {
94                     ovsdbConnection.registerConnectionListener(cm);
95                     ovsdbConnection.startOvsdbManager();
96                 }
97             }
98         } catch (CandidateAlreadyRegisteredException e) {
99             LOG.warn("HWVTEP Southbound Provider instance entity {} was already "
100                     + "registered for ownership", instanceEntity, e);
101         }
102     }
103
104     @Override
105     public void close() throws Exception {
106         LOG.info("HwvtepSouthboundProvider Closed");
107         if(cm != null){
108             cm.close();
109             cm = null;
110         }
111         if(registration != null) {
112             registration.close();
113             registration = null;
114         }
115         if(providerOwnershipChangeListener != null) {
116             providerOwnershipChangeListener.close();
117             providerOwnershipChangeListener = null;
118         }
119         if(hwvtepDTListener != null) {
120             hwvtepDTListener.close();
121             hwvtepDTListener = null;
122         }
123     }
124
125     private void initializeHwvtepTopology(LogicalDatastoreType type) {
126         InstanceIdentifier<Topology> path = InstanceIdentifier
127                 .create(NetworkTopology.class)
128                 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID));
129         ReadWriteTransaction transaction = db.newReadWriteTransaction();
130         CheckedFuture<Optional<Topology>, ReadFailedException> hwvtepTp = transaction.read(type, path);
131         try {
132             if (!hwvtepTp.get().isPresent()) {
133                 TopologyBuilder tpb = new TopologyBuilder();
134                 tpb.setTopologyId(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID);
135                 transaction.put(type, path, tpb.build(), true);
136                 transaction.submit();
137             } else {
138                 transaction.cancel();
139             }
140         } catch (Exception e) {
141             LOG.error("Error initializing hwvtep topology", e);
142         }
143     }
144
145     public void handleOwnershipChange(EntityOwnershipChange ownershipChange) {
146         if (ownershipChange.isOwner()) {
147             LOG.info("*This* instance of HWVTEP southbound provider is set as a MASTER instance");
148             LOG.info("Initialize HWVTEP topology {} in operational and config data store if not already present"
149                     ,HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID);
150             initializeHwvtepTopology(LogicalDatastoreType.OPERATIONAL);
151             initializeHwvtepTopology(LogicalDatastoreType.CONFIGURATION);
152         } else {
153             LOG.info("*This* instance of HWVTEP southbound provider is set as a SLAVE instance");
154         }
155         ovsdbConnection.registerConnectionListener(cm);
156         ovsdbConnection.startOvsdbManager();
157     }
158
159     private class HwvtepsbPluginInstanceEntityOwnershipListener implements EntityOwnershipListener {
160         private HwvtepSouthboundProvider hsp;
161         private EntityOwnershipListenerRegistration listenerRegistration;
162
163         HwvtepsbPluginInstanceEntityOwnershipListener(HwvtepSouthboundProvider hsp,
164                 EntityOwnershipService entityOwnershipService) {
165             this.hsp = hsp;
166             listenerRegistration = entityOwnershipService.registerListener(ENTITY_TYPE, this);
167         }
168
169         public void close() {
170             this.listenerRegistration.close();
171         }
172         @Override
173         public void ownershipChanged(EntityOwnershipChange ownershipChange) {
174             hsp.handleOwnershipChange(ownershipChange);
175         }
176     }
177
178 }