ovsdb enable checkstyle on error
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / SouthboundProvider.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, 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 package org.opendaylight.ovsdb.southbound;
9
10 import com.google.common.base.Optional;
11 import com.google.common.util.concurrent.CheckedFuture;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
14 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
15 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
16 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
17 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipChange;
18 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
19 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
20 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
21 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
24 import org.opendaylight.controller.sal.core.api.model.SchemaService;
25 import org.opendaylight.ovsdb.lib.OvsdbConnection;
26 import org.opendaylight.ovsdb.southbound.transactions.md.TransactionInvoker;
27 import org.opendaylight.ovsdb.southbound.transactions.md.TransactionInvokerImpl;
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 public class SouthboundProvider implements AutoCloseable {
39
40     private static final Logger LOG = LoggerFactory.getLogger(SouthboundProvider.class);
41     private static final String ENTITY_TYPE = "ovsdb-southbound-provider";
42
43     public static DataBroker getDb() {
44         return db;
45     }
46
47     private static DataBroker db;
48     private OvsdbConnectionManager cm;
49     private TransactionInvoker txInvoker;
50     private OvsdbDataTreeChangeListener ovsdbDataTreeChangeListener;
51     private EntityOwnershipService entityOwnershipService;
52     private EntityOwnershipCandidateRegistration registration;
53     private SouthboundPluginInstanceEntityOwnershipListener providerOwnershipChangeListener;
54     private OvsdbConnection ovsdbConnection;
55
56     public SouthboundProvider(final DataBroker dataBroker,
57             final EntityOwnershipService entityOwnershipServiceDependency,
58             final OvsdbConnection ovsdbConnection,
59             final SchemaService schemaService,
60             final BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer) {
61         this.db = dataBroker;
62         this.entityOwnershipService = entityOwnershipServiceDependency;
63         registration = null;
64         this.ovsdbConnection = ovsdbConnection;
65
66         SouthboundUtil.setInstanceIdentifierCodec(new InstanceIdentifierCodec(schemaService,
67                 bindingNormalizedNodeSerializer));
68         LOG.info("SouthboundProvider ovsdbConnectionService: {}", ovsdbConnection);
69     }
70
71     /**
72      * Used by blueprint when starting the container.
73      */
74     public void init() {
75         LOG.info("SouthboundProvider Session Initiated");
76         this.txInvoker = new TransactionInvokerImpl(db);
77         cm = new OvsdbConnectionManager(db,txInvoker,entityOwnershipService, ovsdbConnection);
78         ovsdbDataTreeChangeListener = new OvsdbDataTreeChangeListener(db, cm);
79
80         //Register listener for entityOnwership changes
81         providerOwnershipChangeListener =
82                 new SouthboundPluginInstanceEntityOwnershipListener(this,this.entityOwnershipService);
83         entityOwnershipService.registerListener(ENTITY_TYPE,providerOwnershipChangeListener);
84
85         //register instance entity to get the ownership of the provider
86         Entity instanceEntity = new Entity(ENTITY_TYPE, ENTITY_TYPE);
87         try {
88             Optional<EntityOwnershipState> ownershipStateOpt = entityOwnershipService.getOwnershipState(instanceEntity);
89             registration = entityOwnershipService.registerCandidate(instanceEntity);
90             if (ownershipStateOpt.isPresent()) {
91                 EntityOwnershipState ownershipState = ownershipStateOpt.get();
92                 if (ownershipState.hasOwner() && !ownershipState.isOwner()) {
93                     ovsdbConnection.registerConnectionListener(cm);
94                     ovsdbConnection.startOvsdbManager(SouthboundConstants.DEFAULT_OVSDB_PORT);
95                     LOG.info("*This* instance of OVSDB southbound provider is set as a SLAVE instance");
96                 }
97             }
98         } catch (CandidateAlreadyRegisteredException e) {
99             LOG.warn("OVSDB Southbound Provider instance entity {} was already "
100                     + "registered for ownership", instanceEntity, e);
101         }
102     }
103
104     @Override
105     public void close() {
106         LOG.info("SouthboundProvider Closed");
107         cm.close();
108         ovsdbDataTreeChangeListener.close();
109         registration.close();
110         providerOwnershipChangeListener.close();
111     }
112
113     private void initializeOvsdbTopology(LogicalDatastoreType type) {
114         InstanceIdentifier<Topology> path = InstanceIdentifier
115                 .create(NetworkTopology.class)
116                 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID));
117         initializeTopology(type);
118         ReadWriteTransaction transaction = db.newReadWriteTransaction();
119         CheckedFuture<Optional<Topology>, ReadFailedException> ovsdbTp = transaction.read(type, path);
120         try {
121             if (!ovsdbTp.get().isPresent()) {
122                 TopologyBuilder tpb = new TopologyBuilder();
123                 tpb.setTopologyId(SouthboundConstants.OVSDB_TOPOLOGY_ID);
124                 transaction.put(type, path, tpb.build());
125                 transaction.submit();
126             } else {
127                 transaction.cancel();
128             }
129         } catch (Exception e) {
130             LOG.error("Error initializing ovsdb topology", e);
131         }
132     }
133
134     private void initializeTopology(LogicalDatastoreType type) {
135         ReadWriteTransaction transaction = db.newReadWriteTransaction();
136         InstanceIdentifier<NetworkTopology> path = InstanceIdentifier.create(NetworkTopology.class);
137         CheckedFuture<Optional<NetworkTopology>, ReadFailedException> topology = transaction.read(type,path);
138         try {
139             if (!topology.get().isPresent()) {
140                 NetworkTopologyBuilder ntb = new NetworkTopologyBuilder();
141                 transaction.put(type,path,ntb.build());
142                 transaction.submit();
143             } else {
144                 transaction.cancel();
145             }
146         } catch (Exception e) {
147             LOG.error("Error initializing ovsdb topology", e);
148         }
149     }
150
151     public void handleOwnershipChange(EntityOwnershipChange ownershipChange) {
152         if (ownershipChange.isOwner()) {
153             LOG.info("*This* instance of OVSDB southbound provider is set as a MASTER instance");
154             LOG.info("Initialize OVSDB topology {} in operational and config data store if not already present",
155                     SouthboundConstants.OVSDB_TOPOLOGY_ID);
156             initializeOvsdbTopology(LogicalDatastoreType.OPERATIONAL);
157             initializeOvsdbTopology(LogicalDatastoreType.CONFIGURATION);
158         } else {
159             LOG.info("*This* instance of OVSDB southbound provider is set as a SLAVE instance");
160         }
161         ovsdbConnection.registerConnectionListener(cm);
162         ovsdbConnection.startOvsdbManager(SouthboundConstants.DEFAULT_OVSDB_PORT);
163     }
164
165     private class SouthboundPluginInstanceEntityOwnershipListener implements EntityOwnershipListener {
166         private SouthboundProvider sp;
167         private EntityOwnershipListenerRegistration listenerRegistration;
168
169         SouthboundPluginInstanceEntityOwnershipListener(SouthboundProvider sp,
170                 EntityOwnershipService entityOwnershipService) {
171             this.sp = sp;
172             listenerRegistration = entityOwnershipService.registerListener(ENTITY_TYPE, this);
173         }
174
175         public void close() {
176             this.listenerRegistration.close();
177         }
178
179         @Override
180         public void ownershipChanged(EntityOwnershipChange ownershipChange) {
181             sp.handleOwnershipChange(ownershipChange);
182         }
183     }
184
185 }