Improve segmented journal actor metrics
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / OpsManager.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 import static java.util.Objects.requireNonNull;
12
13 import akka.actor.ActorRef;
14 import akka.actor.OneForOneStrategy;
15 import akka.actor.Props;
16 import akka.actor.SupervisorStrategy;
17 import java.util.concurrent.TimeUnit;
18 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActor;
19 import org.opendaylight.controller.remote.rpc.registry.ActionRegistry;
20 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry;
21 import org.opendaylight.mdsal.dom.api.DOMActionProviderService;
22 import org.opendaylight.mdsal.dom.api.DOMActionService;
23 import org.opendaylight.mdsal.dom.api.DOMRpcProviderService;
24 import org.opendaylight.mdsal.dom.api.DOMRpcService;
25 import org.opendaylight.yangtools.concepts.Registration;
26 import scala.concurrent.duration.FiniteDuration;
27
28 /**
29  * This class acts as a supervisor, creates all the actors, resumes them, if an exception is thrown. It also registers
30  * {@link OpsListener} with the local {@link DOMRpcService}.
31  */
32 public class OpsManager extends AbstractUntypedActor {
33     private final DOMRpcProviderService rpcProvisionRegistry;
34     private final RemoteOpsProviderConfig config;
35     private final DOMRpcService rpcServices;
36     private final DOMActionProviderService actionProvisionRegistry;
37     private final DOMActionService actionService;
38
39     private Registration listenerReg;
40     private ActorRef opsInvoker;
41     private ActorRef actionRegistry;
42     private ActorRef rpcRegistry;
43     private ActorRef opsRegistrar;
44
45     OpsManager(final DOMRpcProviderService rpcProvisionRegistry, final DOMRpcService rpcServices,
46                final RemoteOpsProviderConfig config, final DOMActionProviderService actionProviderService,
47                final DOMActionService actionService) {
48         this.rpcProvisionRegistry = requireNonNull(rpcProvisionRegistry);
49         this.rpcServices = requireNonNull(rpcServices);
50         this.config = requireNonNull(config);
51         actionProvisionRegistry = requireNonNull(actionProviderService);
52         this.actionService = requireNonNull(actionService);
53     }
54
55     public static Props props(final DOMRpcProviderService rpcProvisionRegistry, final DOMRpcService rpcServices,
56                               final RemoteOpsProviderConfig config,
57                               final DOMActionProviderService actionProviderService,
58                               final DOMActionService actionService) {
59         requireNonNull(rpcProvisionRegistry, "RpcProviderService can not be null!");
60         requireNonNull(rpcServices, "RpcService can not be null!");
61         requireNonNull(config, "RemoteOpsProviderConfig can not be null!");
62         requireNonNull(actionProviderService, "ActionProviderService can not be null!");
63         requireNonNull(actionService, "ActionService can not be null!");
64         return Props.create(OpsManager.class, rpcProvisionRegistry, rpcServices, config,
65                 actionProviderService, actionService);
66     }
67
68     @Override
69     public void preStart() throws Exception {
70         super.preStart();
71
72         opsInvoker = getContext().actorOf(OpsInvoker.props(rpcServices, actionService)
73                 .withMailbox(config.getMailBoxName()), config.getRpcBrokerName());
74         LOG.debug("Listening for RPC invocation requests with {}", opsInvoker);
75
76         opsRegistrar = getContext().actorOf(OpsRegistrar.props(config, rpcProvisionRegistry, actionProvisionRegistry)
77                 .withMailbox(config.getMailBoxName()), config.getRpcRegistrarName());
78         LOG.debug("Registering remote RPCs with {}", opsRegistrar);
79
80         rpcRegistry = getContext().actorOf(RpcRegistry.props(config, opsInvoker, opsRegistrar)
81                 .withMailbox(config.getMailBoxName()), config.getRpcRegistryName());
82         LOG.debug("Propagating RPC information with {}", rpcRegistry);
83
84         actionRegistry = getContext().actorOf(ActionRegistry.props(config, opsInvoker, opsRegistrar)
85                 .withMailbox(config.getMailBoxName()), config.getActionRegistryName());
86         LOG.debug("Propagating action information with {}", actionRegistry);
87
88         final OpsListener opsListener = new OpsListener(rpcRegistry, actionRegistry);
89         LOG.debug("Registering local availability listener {}", opsListener);
90         listenerReg = rpcServices.registerRpcListener(opsListener);
91     }
92
93     @Override
94     public void postStop() throws Exception {
95         if (listenerReg != null) {
96             listenerReg.close();
97             listenerReg = null;
98         }
99
100         super.postStop();
101     }
102
103     @Override
104     protected void handleReceive(final Object message) {
105         unknownMessage(message);
106     }
107
108     @Override
109     public SupervisorStrategy supervisorStrategy() {
110         return new OneForOneStrategy(10, FiniteDuration.create(1, TimeUnit.MINUTES), t -> {
111             LOG.error("An exception happened actor will be resumed", t);
112             return SupervisorStrategy.resume();
113         });
114     }
115 }