2c2631b0bffe38b948c7dc86b3d18eb4328935e0
[honeycomb.git] / infra / minimal-distribution / src / main / java / io / fd / honeycomb / infra / distro / netconf / NetconfNotificationMapperProvider.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.md.sal.binding.api.DataBroker;
24 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
25 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
28 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
29 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
30 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactoryListener;
31 import org.opendaylight.netconf.mdsal.notification.NetconfNotificationOperationServiceFactory;
32 import org.opendaylight.netconf.notifications.BaseNotificationPublisherRegistration;
33 import org.opendaylight.netconf.notifications.NetconfNotificationCollector;
34 import org.opendaylight.netconf.notifications.NetconfNotificationRegistry;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Capabilities;
37 import org.opendaylight.yangtools.concepts.ListenerRegistration;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42
43 public class NetconfNotificationMapperProvider extends ProviderTrait<NetconfOperationServiceFactory> {
44
45     private static final Logger LOG = LoggerFactory.getLogger(NetconfNotificationMapperProvider.class);
46
47     public static final InstanceIdentifier<Capabilities> capabilitiesIdentifier =
48             InstanceIdentifier.create(NetconfState.class).child(Capabilities.class).builder().build();
49     @Inject
50     private NetconfNotificationCollector notificationCollector;
51     @Inject
52     private NetconfNotificationRegistry notificationRegistry;
53     @Inject
54     @Named(NetconfModule.HONEYCOMB_NETCONF)
55     private BindingAwareBroker bindingAwareBroker;
56     @Inject
57     @Named(NetconfModule.HONEYCOMB_NETCONF)
58     private DataBroker dataBroker;
59     @Inject
60     private NetconfOperationServiceFactoryListener aggregator;
61
62     @Override
63     protected NetconfNotificationOperationServiceFactory create() {
64         try {
65             final Class<?> notificationWriter = Class.forName(
66                     "org.opendaylight.controller.config.yang.netconf.mdsal.notification.NotificationToMdsalWriter");
67             Constructor<?> declaredConstructor =
68                     notificationWriter.getDeclaredConstructor(NetconfNotificationCollector.class);
69             declaredConstructor.setAccessible(true);
70             final BindingAwareProvider writer =
71                     (BindingAwareProvider) declaredConstructor.newInstance(notificationCollector);
72             bindingAwareBroker.registerProvider(writer);
73
74             final Class<?> notifPublisherCls = Class.forName(
75                     "org.opendaylight.controller.config.yang.netconf.mdsal.notification.BaseCapabilityChangeNotificationPublisher");
76             declaredConstructor =
77                     notifPublisherCls.getDeclaredConstructor(BaseNotificationPublisherRegistration.class);
78             declaredConstructor.setAccessible(true);
79             final DataChangeListener publisher = (DataChangeListener) declaredConstructor.newInstance(
80                     notificationCollector.registerBaseNotificationPublisher());
81
82             ListenerRegistration<DataChangeListener> capabilityChangeListenerRegistration = dataBroker
83                     .registerDataChangeListener(LogicalDatastoreType.OPERATIONAL, capabilitiesIdentifier,
84                             publisher, AsyncDataBroker.DataChangeScope.SUBTREE);
85             NetconfNotificationOperationServiceFactory netconfNotificationOperationServiceFactory =
86                     new NetconfNotificationOperationServiceFactory(notificationRegistry);
87             aggregator.onAddNetconfOperationServiceFactory(netconfNotificationOperationServiceFactory);
88
89             return netconfNotificationOperationServiceFactory;
90         } catch (final ReflectiveOperationException e) {
91             final String msg = "Unable to instantiate notification mapper using reflection";
92             LOG.error(msg, e);
93             throw new IllegalStateException(msg, e);
94         }
95     }
96 }