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 org.opendaylight.yangtools.yang.binding.RpcService;
11 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
12 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
13 import org.opendaylight.controller.sal.binding.api.rpc.RpcRouter;
14 import org.opendaylight.controller.sal.binding.api.rpc.RpcRoutingTable;
15 import org.opendaylight.controller.sal.binding.codegen.RuntimeCodeHelper;
16 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
17 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
19 import static org.opendaylight.controller.sal.binding.codegen.RuntimeCodeHelper.*;
23 import java.util.HashMap;
25 import org.opendaylight.yangtools.yang.binding.DataContainer;
26 import org.opendaylight.yangtools.yang.binding.RpcImplementation;
27 import org.opendaylight.controller.md.sal.common.api.routing.MutableRoutingTable;
28 import org.opendaylight.controller.md.sal.common.api.routing.RouteChange;
29 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener;
30 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
31 import org.opendaylight.yangtools.concepts.ListenerRegistration;
32 import org.opendaylight.yangtools.concepts.util.ListenerRegistry;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import com.google.common.collect.ImmutableMap;
37 import com.google.common.collect.ImmutableSet;
39 public class RpcRouterCodegenInstance<T extends RpcService> implements //
40 RpcRouter<T>, RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>> {
42 private static final Logger LOG = LoggerFactory.getLogger(RpcRouterCodegenInstance.class);
44 private T defaultService;
46 private final Class<T> serviceType;
48 private final T invocationProxy;
50 private final Set<Class<? extends BaseIdentity>> contexts;
52 private final ListenerRegistry<RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> listeners;
54 private final Map<Class<? extends BaseIdentity>, RpcRoutingTableImpl<? extends BaseIdentity, T>> routingTables;
56 private final String name;
58 @SuppressWarnings("unchecked")
59 public RpcRouterCodegenInstance(String name,Class<T> type, T routerImpl, Set<Class<? extends BaseIdentity>> contexts,
60 Set<Class<? extends DataContainer>> inputs) {
62 this.listeners = ListenerRegistry.create();
63 this.serviceType = type;
64 this.invocationProxy = routerImpl;
65 this.contexts = ImmutableSet.copyOf(contexts);
66 Map<Class<? extends BaseIdentity>, RpcRoutingTableImpl<? extends BaseIdentity, T>> mutableRoutingTables = new HashMap<>();
67 for (Class<? extends BaseIdentity> ctx : contexts) {
68 RpcRoutingTableImpl<? extends BaseIdentity, T> table = new RpcRoutingTableImpl<>(name,ctx,type);
70 @SuppressWarnings("rawtypes")
71 Map invokerView = table.getRoutes();
73 setRoutingTable((RpcService) invocationProxy, ctx, invokerView);
74 mutableRoutingTables.put(ctx, table);
75 table.registerRouteChangeListener(this);
77 this.routingTables = ImmutableMap.copyOf(mutableRoutingTables);
81 public Class<T> getServiceType() {
86 public T getInvocationProxy() {
87 return invocationProxy;
91 @SuppressWarnings("unchecked")
92 public <C extends BaseIdentity> RpcRoutingTable<C, T> getRoutingTable(Class<C> routeContext) {
93 return (RpcRoutingTable<C, T>) routingTables.get(routeContext);
97 public T getDefaultService() {
98 return defaultService;
102 public Set<Class<? extends BaseIdentity>> getContexts() {
107 public <L extends RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
109 return listeners.registerWithType(listener);
113 public void onRouteChange(RouteChange<Class<? extends BaseIdentity>, InstanceIdentifier<?>> change) {
114 for (ListenerRegistration<RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> listener : listeners) {
116 listener.getInstance().onRouteChange(change);
117 } catch (Exception e) {
118 LOG.error("Error occured during invoker listener {}", listener.getInstance(), e);
124 public T getService(Class<? extends BaseIdentity> context, InstanceIdentifier<?> path) {
125 return routingTables.get(context).getRoute(path);
129 public RoutedRpcRegistration<T> addRoutedRpcImplementation(T service) {
130 return new RoutedRpcRegistrationImpl(service);
134 public RpcRegistration<T> registerDefaultService(T service) {
135 // TODO Auto-generated method stub
136 RuntimeCodeHelper.setDelegate(invocationProxy, service);
140 private class RoutedRpcRegistrationImpl extends AbstractObjectRegistration<T> implements RoutedRpcRegistration<T> {
142 public RoutedRpcRegistrationImpl(T instance) {
147 public Class<T> getServiceType() {
152 public void registerPath(Class<? extends BaseIdentity> context, InstanceIdentifier<?> path) {
153 routingTables.get(context).updateRoute(path, getInstance());
157 public void unregisterPath(Class<? extends BaseIdentity> context, InstanceIdentifier<?> path) {
158 routingTables.get(context).removeRoute(path, getInstance());
162 public void registerInstance(Class<? extends BaseIdentity> context, InstanceIdentifier<?> instance) {
163 registerPath(context, instance);
167 public void unregisterInstance(Class<? extends BaseIdentity> context, InstanceIdentifier<?> instance) {
168 unregisterPath(context, instance);
172 protected void removeRegistration() {