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