unifying statistics manager api packages
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / yang / gen / v1 / urn / opendaylight / params / xml / ns / yang / openflow / common / config / impl / rev140326 / MsgSpyServiceModule.java
1 package org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.common.config.impl.rev140326;
2
3 import java.text.SimpleDateFormat;
4 import java.util.List;
5
6 import org.opendaylight.openflowplugin.openflow.md.core.sal.OpenflowPluginProvider;
7 import org.opendaylight.openflowplugin.api.openflow.statistics.MessageCountDumper;
8 import org.opendaylight.openflowplugin.api.openflow.statistics.MessageObservatory;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12 /**
13  * MsgSpyServiceModul implements and register own MsgSpyServiceRuntimeMXBean
14  * which is linked to {@link MessageObservatory} from {@link OpenflowPluginProvider}
15  *
16  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
17  *
18  */
19 public class MsgSpyServiceModule extends org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.common.config.impl.rev140326.AbstractMsgSpyServiceModule {
20     private static final Logger log = LoggerFactory.getLogger(MsgSpyServiceModule.class);
21
22     private static final SimpleDateFormat ft = new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
23
24     public MsgSpyServiceModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
25             final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
26
27         super(identifier, dependencyResolver);
28     }
29
30     public MsgSpyServiceModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
31             final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver,
32             final MsgSpyServiceModule oldModule, final java.lang.AutoCloseable oldInstance) {
33
34         super(identifier, dependencyResolver, oldModule, oldInstance);
35     }
36
37     @Override
38     public void customValidation() {
39         // No need to validate dependencies, since all dependencies are mandatory
40         // config-subsystem will perform the validation
41     }
42
43     @Override
44     public java.lang.AutoCloseable createInstance() {
45         final MessageCountDumper msg = getOpenflowPluginProviderDependency().getMessageCountDumper();
46
47         /* Internal MXBean implementation -> make statMsg from dumpMessageCounst only yet */
48         final MsgSpyServiceRuntimeMXBean msgSpyBean = new MsgSpyServiceRuntimeMXBean() {
49
50             @Override
51             public String makeMsgStatistics() {
52                 if (msg == null) {
53                     return "Message Spy Count Dumper is not avaliable.";
54                 }
55                 List<String> statList = msg.dumpMessageCounts();
56
57                 StringBuilder strBuilder = new StringBuilder(ft.format(System.currentTimeMillis()));
58                 for (String stat : statList) {
59                     strBuilder.append("\n").append(stat);
60                 }
61                 return strBuilder.toString();
62             }
63
64             @Override
65             public String getMsgStatistics() {
66                 return makeMsgStatistics();
67             }
68         };
69
70         /* MXBean registration */
71         final MsgSpyServiceRuntimeRegistration runtimeReg =
72                 getRootRuntimeBeanRegistratorWrapper().register(msgSpyBean);
73
74         /* Internal MsgSpyService implementation */
75         final class AutoClosableMsgSpyService implements MessageCountDumper, AutoCloseable {
76
77             @Override
78             public void close() {
79                 if (runtimeReg != null) {
80                     try {
81                         runtimeReg.close();
82                     }
83                     catch (Exception e) {
84                         String errMsg = "Error by stop MsgSpyService.";
85                         log.error(errMsg, e);
86                         throw new IllegalStateException(errMsg, e);
87                     }
88                 }
89                 log.info(" Msg Stat Service consumer (instance {} turn down.)", this);
90             }
91
92             @Override
93             public List<String> dumpMessageCounts() {
94                 return msg.dumpMessageCounts();
95             }
96         }
97
98         AutoCloseable ret = new AutoClosableMsgSpyService();
99         log.info("MsgStatService (instance {}) initialized.", ret);
100         return ret;
101     }
102
103 }