BUG 2412 - remove CompositeNode from sal-remoterpc-connector
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / RoutedRpcListener.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
9 package org.opendaylight.controller.remote.rpc;
10
11
12 import akka.actor.ActorRef;
13 import com.google.common.base.Preconditions;
14 import java.util.ArrayList;
15 import java.util.HashSet;
16 import java.util.Map;
17 import java.util.Set;
18 import org.opendaylight.controller.md.sal.common.api.routing.RouteChange;
19 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener;
20 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry;
21 import org.opendaylight.controller.sal.connector.api.RpcRouter;
22 import org.opendaylight.controller.sal.core.api.RpcRoutingContext;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public class RoutedRpcListener implements RouteChangeListener<RpcRoutingContext, YangInstanceIdentifier>{
28     private static final Logger LOG = LoggerFactory.getLogger(RoutedRpcListener.class);
29     private final ActorRef rpcRegistry;
30
31     public RoutedRpcListener(final ActorRef rpcRegistry) {
32         Preconditions.checkNotNull(rpcRegistry, "rpc registry actor should not be null");
33         this.rpcRegistry = rpcRegistry;
34     }
35
36     @Override
37     public void onRouteChange(final RouteChange<RpcRoutingContext, YangInstanceIdentifier> routeChange) {
38         final Map<RpcRoutingContext, Set<YangInstanceIdentifier>> announcements = routeChange.getAnnouncements();
39         if(announcements != null && announcements.size() > 0){
40             announce(getRouteIdentifiers(announcements));
41         }
42
43         final Map<RpcRoutingContext, Set<YangInstanceIdentifier>> removals = routeChange.getRemovals();
44         if(removals != null && removals.size() > 0 ) {
45             remove(getRouteIdentifiers(removals));
46         }
47     }
48
49     /**
50      *
51      * @param announcements
52      */
53     private void announce(final Set<RpcRouter.RouteIdentifier<?, ?, ?>> announcements) {
54         if(LOG.isDebugEnabled()) {
55             LOG.debug("Announcing [{}]", announcements);
56         }
57         final RpcRegistry.Messages.AddOrUpdateRoutes addRpcMsg =
58                 new RpcRegistry.Messages.AddOrUpdateRoutes(new ArrayList<>(announcements));
59         rpcRegistry.tell(addRpcMsg, ActorRef.noSender());
60     }
61
62     /**
63      *
64      * @param removals
65      */
66     private void remove(final Set<RpcRouter.RouteIdentifier<?, ?, ?>> removals){
67         if(LOG.isDebugEnabled()) {
68             LOG.debug("Removing [{}]", removals);
69         }
70         final RpcRegistry.Messages.RemoveRoutes removeRpcMsg =
71                 new RpcRegistry.Messages.RemoveRoutes(new ArrayList<>(removals));
72         rpcRegistry.tell(removeRpcMsg, ActorRef.noSender());
73     }
74
75     /**
76      *
77      * @param changes
78      * @return
79      */
80     private Set<RpcRouter.RouteIdentifier<?, ?, ?>> getRouteIdentifiers(
81             final Map<RpcRoutingContext, Set<YangInstanceIdentifier>> changes) {
82
83         final Set<RpcRouter.RouteIdentifier<?, ?, ?>> routeIdSet = new HashSet<>();
84         for (final RpcRoutingContext context : changes.keySet()){
85             for (final YangInstanceIdentifier instanceId : changes.get(context)){
86                 final RouteIdentifierImpl routeId = new RouteIdentifierImpl(null, context.getRpc(), instanceId);
87                 routeIdSet.add(routeId);
88             }
89         }
90         return routeIdSet;
91     }
92 }