Merge "Changed codec for Identityref in JSON transformation"
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / implementation / src / main / java / org / opendaylight / controller / sal / connector / remoterpc / RoutingTableProvider.java
1 /*
2  * Copyright (c) 2013 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
9 package org.opendaylight.controller.sal.connector.remoterpc;
10
11 import com.google.common.base.Optional;
12 import org.opendaylight.controller.sal.connector.remoterpc.api.RouteChangeListener;
13 import org.opendaylight.controller.sal.connector.remoterpc.api.RoutingTable;
14 import org.opendaylight.controller.sal.connector.remoterpc.impl.RoutingTableImpl;
15 import org.osgi.framework.BundleContext;
16 import org.osgi.util.tracker.ServiceTracker;
17
18 public class RoutingTableProvider implements AutoCloseable {
19
20     @SuppressWarnings("rawtypes")
21     final ServiceTracker<RoutingTable,RoutingTable> tracker;
22
23     private RoutingTableImpl routingTableImpl = null;
24
25     final private RouteChangeListener routeChangeListener;
26     
27     
28     public RoutingTableProvider(BundleContext ctx,RouteChangeListener rcl) {
29         @SuppressWarnings("rawtypes")
30         ServiceTracker<RoutingTable, RoutingTable> rawTracker = new ServiceTracker<>(ctx, RoutingTable.class, null);
31         tracker = rawTracker;
32         tracker.open();
33
34         routeChangeListener = rcl;
35     }
36     
37     public Optional<RoutingTable<String, String>> getRoutingTable() {
38         @SuppressWarnings("unchecked")
39         RoutingTable<String,String> tracked = tracker.getService();
40
41         if(tracked instanceof RoutingTableImpl){
42             if(routingTableImpl != tracked){
43              routingTableImpl= (RoutingTableImpl)tracked;
44              routingTableImpl.setRouteChangeListener(routeChangeListener);
45             }
46         }
47
48         return Optional.fromNullable(tracked);
49     }
50
51     @Override
52     public void close() throws Exception {
53         tracker.close();
54     }
55 }