2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.binding.codegen.impl;
10 import static org.opendaylight.controller.sal.binding.codegen.RuntimeCodeHelper.setRoutingTable;
12 import java.util.HashMap;
16 import org.opendaylight.controller.md.sal.common.api.routing.RouteChange;
17 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
20 import org.opendaylight.controller.sal.binding.api.rpc.RpcRouter;
21 import org.opendaylight.controller.sal.binding.api.rpc.RpcRoutingTable;
22 import org.opendaylight.controller.sal.binding.codegen.RuntimeCodeHelper;
23 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.concepts.util.ListenerRegistry;
26 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
27 import org.opendaylight.yangtools.yang.binding.DataContainer;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.opendaylight.yangtools.yang.binding.RpcService;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import com.google.common.collect.ImmutableMap;
34 import com.google.common.collect.ImmutableSet;
36 public class RpcRouterCodegenInstance<T extends RpcService> implements //
37 RpcRouter<T>, RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>> {
39 private static final Logger LOG = LoggerFactory.getLogger(RpcRouterCodegenInstance.class);
41 private T defaultService;
43 private final Class<T> serviceType;
45 private final T invocationProxy;
47 private final Set<Class<? extends BaseIdentity>> contexts;
49 private final ListenerRegistry<RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> listeners;
51 private final Map<Class<? extends BaseIdentity>, RpcRoutingTableImpl<? extends BaseIdentity, T>> routingTables;
53 private final String name;
55 @SuppressWarnings("unchecked")
56 public RpcRouterCodegenInstance(String name,Class<T> type, T routerImpl, Set<Class<? extends BaseIdentity>> contexts,
57 Set<Class<? extends DataContainer>> inputs) {
59 this.listeners = ListenerRegistry.create();
60 this.serviceType = type;
61 this.invocationProxy = routerImpl;
62 this.contexts = ImmutableSet.copyOf(contexts);
63 Map<Class<? extends BaseIdentity>, RpcRoutingTableImpl<? extends BaseIdentity, T>> mutableRoutingTables = new HashMap<>();
64 for (Class<? extends BaseIdentity> ctx : contexts) {
65 RpcRoutingTableImpl<? extends BaseIdentity, T> table = new RpcRoutingTableImpl<>(name,ctx,type);
67 @SuppressWarnings("rawtypes")
68 Map invokerView = table.getRoutes();
70 setRoutingTable(invocationProxy, ctx, invokerView);
71 mutableRoutingTables.put(ctx, table);
72 table.registerRouteChangeListener(this);
74 this.routingTables = ImmutableMap.copyOf(mutableRoutingTables);
78 public Class<T> getServiceType() {
83 public T getInvocationProxy() {
84 return invocationProxy;
88 @SuppressWarnings("unchecked")
89 public <C extends BaseIdentity> RpcRoutingTable<C, T> getRoutingTable(Class<C> routeContext) {
90 return (RpcRoutingTable<C, T>) routingTables.get(routeContext);
94 public T getDefaultService() {
95 return defaultService;
99 public Set<Class<? extends BaseIdentity>> getContexts() {
104 public <L extends RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
106 return listeners.registerWithType(listener);
110 public void onRouteChange(RouteChange<Class<? extends BaseIdentity>, InstanceIdentifier<?>> change) {
111 for (ListenerRegistration<RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> listener : listeners) {
113 listener.getInstance().onRouteChange(change);
114 } catch (Exception e) {
115 LOG.error("Error occured during invoker listener {}", listener.getInstance(), e);
121 public T getService(Class<? extends BaseIdentity> context, InstanceIdentifier<?> path) {
122 return routingTables.get(context).getRoute(path);
126 public RoutedRpcRegistration<T> addRoutedRpcImplementation(T service) {
127 return new RoutedRpcRegistrationImpl(service);
131 public RpcRegistration<T> registerDefaultService(T service) {
132 // TODO Auto-generated method stub
133 RuntimeCodeHelper.setDelegate(invocationProxy, service);
137 private class RoutedRpcRegistrationImpl extends AbstractObjectRegistration<T> implements RoutedRpcRegistration<T> {
139 public RoutedRpcRegistrationImpl(T instance) {
144 public Class<T> getServiceType() {
149 public void registerPath(Class<? extends BaseIdentity> context, InstanceIdentifier<?> path) {
150 routingTables.get(context).updateRoute(path, getInstance());
154 public void unregisterPath(Class<? extends BaseIdentity> context, InstanceIdentifier<?> path) {
155 routingTables.get(context).removeRoute(path, getInstance());
159 public void registerInstance(Class<? extends BaseIdentity> context, InstanceIdentifier<?> instance) {
160 registerPath(context, instance);
164 public void unregisterInstance(Class<? extends BaseIdentity> context, InstanceIdentifier<?> instance) {
165 unregisterPath(context, instance);
169 protected void removeRegistration() {