0756378e0d10967be466b1379bbc66cf44874f00
[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 java.util.concurrent.ExecutionException;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
15 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
16 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
17 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
18 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipChange;
19 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
20 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
21 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
22 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
25 import org.opendaylight.controller.sal.core.api.model.SchemaService;
26 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
27 import org.opendaylight.ovsdb.lib.OvsdbConnection;
28 import org.opendaylight.ovsdb.southbound.transactions.md.TransactionInvoker;
29 import org.opendaylight.ovsdb.southbound.transactions.md.TransactionInvokerImpl;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
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     private final InstanceIdentifierCodec instanceIdentifierCodec;
56
57     public SouthboundProvider(final DataBroker dataBroker,
58             final EntityOwnershipService entityOwnershipServiceDependency,
59             final OvsdbConnection ovsdbConnection,
60             final SchemaService schemaService,
61             final BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer) {
62         this.db = dataBroker;
63         this.entityOwnershipService = entityOwnershipServiceDependency;
64         registration = null;
65         this.ovsdbConnection = ovsdbConnection;
66
67         this.instanceIdentifierCodec = new InstanceIdentifierCodec(schemaService,
68                 bindingNormalizedNodeSerializer);
69         LOG.info("SouthboundProvider ovsdbConnectionService Initialized");
70     }
71
72     /**
73      * Used by blueprint when starting the container.
74      */
75     public void init() {
76         LOG.info("SouthboundProvider Session Initiated");
77         this.txInvoker = new TransactionInvokerImpl(db);
78         cm = new OvsdbConnectionManager(db,txInvoker,entityOwnershipService, ovsdbConnection, instanceIdentifierCodec);
79         ovsdbDataTreeChangeListener = new OvsdbDataTreeChangeListener(db, cm, instanceIdentifierCodec);
80
81         //Register listener for entityOnwership changes
82         providerOwnershipChangeListener =
83                 new SouthboundPluginInstanceEntityOwnershipListener(this,this.entityOwnershipService);
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         ReadWriteTransaction transaction = db.newReadWriteTransaction();
118         CheckedFuture<Optional<Topology>, ReadFailedException> ovsdbTp = transaction.read(type, path);
119         try {
120             if (!ovsdbTp.get().isPresent()) {
121                 TopologyBuilder tpb = new TopologyBuilder();
122                 tpb.setTopologyId(SouthboundConstants.OVSDB_TOPOLOGY_ID);
123                 transaction.put(type, path, tpb.build(), true);
124                 transaction.submit();
125             } else {
126                 transaction.cancel();
127             }
128         } catch (InterruptedException | ExecutionException e) {
129             LOG.error("Error initializing ovsdb topology", e);
130         }
131     }
132
133     public void handleOwnershipChange(EntityOwnershipChange ownershipChange) {
134         if (ownershipChange.isOwner()) {
135             LOG.info("*This* instance of OVSDB southbound provider is set as a MASTER instance");
136             LOG.info("Initialize OVSDB topology {} in operational and config data store if not already present",
137                     SouthboundConstants.OVSDB_TOPOLOGY_ID);
138             initializeOvsdbTopology(LogicalDatastoreType.OPERATIONAL);
139             initializeOvsdbTopology(LogicalDatastoreType.CONFIGURATION);
140         } else {
141             LOG.info("*This* instance of OVSDB southbound provider is set as a SLAVE instance");
142         }
143         ovsdbConnection.registerConnectionListener(cm);
144         ovsdbConnection.startOvsdbManager(SouthboundConstants.DEFAULT_OVSDB_PORT);
145     }
146
147     private class SouthboundPluginInstanceEntityOwnershipListener implements EntityOwnershipListener {
148         private SouthboundProvider sp;
149         private EntityOwnershipListenerRegistration listenerRegistration;
150
151         SouthboundPluginInstanceEntityOwnershipListener(SouthboundProvider sp,
152                 EntityOwnershipService entityOwnershipService) {
153             this.sp = sp;
154             listenerRegistration = entityOwnershipService.registerListener(ENTITY_TYPE, this);
155         }
156
157         public void close() {
158             this.listenerRegistration.close();
159         }
160
161         @Override
162         public void ownershipChanged(EntityOwnershipChange ownershipChange) {
163             sp.handleOwnershipChange(ownershipChange);
164         }
165     }
166
167 }