46a46ba478902d74ed5d5d2ee678deb2cfaf3c03
[honeycomb.git] / infra / minimal-distribution / src / main / java / io / fd / honeycomb / infra / distro / netconf / NetconfMonitoringMapperProvider.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.honeycomb.infra.distro.netconf;
18
19 import com.google.inject.Inject;
20 import com.google.inject.name.Named;
21 import io.fd.honeycomb.infra.distro.ProviderTrait;
22 import java.lang.reflect.Constructor;
23 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
24 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
25 import org.opendaylight.netconf.api.monitoring.NetconfMonitoringService;
26 import org.opendaylight.netconf.mapping.api.NetconfOperationService;
27 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
28 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactoryListener;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32
33 public final class NetconfMonitoringMapperProvider extends ProviderTrait<NetconfOperationServiceFactory> {
34
35     private static final Logger LOG = LoggerFactory.getLogger(NetconfMonitoringMapperProvider.class);
36
37     @Inject
38     @Named(NetconfModule.HONEYCOMB_NETCONF)
39     private BindingAwareBroker bindingAwareBroker;
40     @Inject
41     private NetconfOperationServiceFactoryListener aggregator;
42     @Inject
43     private NetconfMonitoringService monitoringService;
44
45     @Override
46     protected NetconfOperationServiceFactory create() {
47         try {
48             final Class<?> monitoringWriterCls = Class.forName(
49                     "org.opendaylight.controller.config.yang.netconf.mdsal.monitoring.MonitoringToMdsalWriter");
50             Constructor<?> declaredConstructor =
51                     monitoringWriterCls.getDeclaredConstructor(NetconfMonitoringService.class);
52             declaredConstructor.setAccessible(true);
53             final BindingAwareProvider o = (BindingAwareProvider) declaredConstructor.newInstance(monitoringService);
54             bindingAwareBroker.registerProvider(o);
55
56             final Class<?> monitoringMapperCls = Class.forName(
57                     "org.opendaylight.controller.config.yang.netconf.mdsal.monitoring.NetconfMdsalMonitoringMapperModule$MdsalMonitoringMapper");
58             declaredConstructor =
59                     monitoringMapperCls.getDeclaredConstructor(NetconfMonitoringService.class);
60             declaredConstructor.setAccessible(true);
61             final NetconfOperationService mdSalMonitoringMapper =
62                     (NetconfOperationService) declaredConstructor.newInstance(monitoringService);
63
64             final Class<?> monitoringMpperFactory = Class.forName(
65                     "org.opendaylight.controller.config.yang.netconf.mdsal.monitoring.NetconfMdsalMonitoringMapperModule$MdSalMonitoringMapperFactory");
66             declaredConstructor =
67                     monitoringMpperFactory.getDeclaredConstructor(NetconfOperationService.class);
68             declaredConstructor.setAccessible(true);
69             final NetconfOperationServiceFactory mdSalMonitoringMapperFactory =
70                     (NetconfOperationServiceFactory) declaredConstructor.newInstance(mdSalMonitoringMapper);
71             aggregator.onAddNetconfOperationServiceFactory(mdSalMonitoringMapperFactory);
72             return mdSalMonitoringMapperFactory;
73         } catch (final ReflectiveOperationException e) {
74             final String msg = "Unable to instantiate operation service factory using reflection";
75             LOG.error(msg, e);
76             throw new IllegalStateException(msg, e);
77         }
78     }
79 }