Honeynode test tool
[transportpce.git] / tests / honeynode / netconf / src / main / java / io / fd / honeycomb / northbound / netconf / HoneycombNotification2NetconfProvider.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.northbound.netconf;
18
19 import com.google.inject.Inject;
20 import io.fd.honeycomb.binding.init.ProviderTrait;
21 import io.fd.honeycomb.northbound.NetconfConfiguration;
22 import io.fd.honeycomb.notification.NotificationCollector;
23 import io.fd.honeycomb.notification.impl.NotificationProducerRegistry;
24 import io.fd.honeycomb.notification.impl.TranslationUtil;
25 import java.util.List;
26 import java.util.stream.Collectors;
27 import javax.annotation.Nonnull;
28 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
29 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationListener;
30 import org.opendaylight.controller.md.sal.dom.broker.impl.DOMNotificationRouter;
31 import org.opendaylight.controller.sal.core.api.model.SchemaService;
32 import org.opendaylight.netconf.notifications.NetconfNotificationCollector;
33 import org.opendaylight.netconf.notifications.NotificationPublisherRegistration;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.StreamBuilder;
36 import org.opendaylight.yangtools.concepts.ListenerRegistration;
37 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 public final class HoneycombNotification2NetconfProvider
42         extends ProviderTrait<HoneycombNotification2NetconfProvider.HoneycombNotification2Netconf> {
43
44     private static final Logger LOG = LoggerFactory.getLogger(HoneycombNotification2NetconfProvider.class);
45
46     @Inject
47     private DOMNotificationRouter notificationRouter;
48     @Inject
49     private SchemaService schemaService;
50     @Inject
51     private NetconfConfiguration cfgAttributes;
52     @Inject
53     private NotificationCollector hcNotificationCollector;
54     @Inject
55     private NetconfNotificationCollector netconfNotificationCollector;
56
57     @Override
58     protected HoneycombNotification2Netconf create() {
59         final StreamNameType streamType = new StreamNameType(cfgAttributes.netconfNotificationStreamName.get());
60
61         // Register as HONEYCOMB_NETCONF notification publisher under configured name
62         final NotificationPublisherRegistration netconfNotifReg = netconfNotificationCollector
63                 .registerNotificationPublisher(new StreamBuilder().setName(streamType).setReplaySupport(false)
64                         .setDescription(cfgAttributes.netconfNotificationStreamName.get()).build());
65
66         // Notification Translator, get notification from HC producers and put into HONEYCOMB_NETCONF notification collector
67         final DOMNotificationListener domNotificationListener =
68                 new TranslatingNotificationListener(netconfNotifReg, streamType, schemaService);
69
70         // NotificationManager is used to provide list of available notifications (which are all of the notifications registered)
71         // TODO HONEYCOMB-165 make available notifications configurable here so that any number of notification streams for netconf
72         // can be configured on top of a single notification manager
73         LOG.debug("Current notifications to be exposed over HONEYCOMB_NETCONF: {}",
74                 hcNotificationCollector.getNotificationTypes());
75         List<SchemaPath> currentNotificationSchemaPaths = hcNotificationCollector.getNotificationTypes().stream()
76                 .map(notifType -> SchemaPath.create(true, NotificationProducerRegistry.getQName(notifType)))
77                 .collect(Collectors.toList());
78
79         // Register as listener to HC'OPERATIONAL DOM notification service
80         // TODO HONEYCOMB-166 This should only be triggered when HONEYCOMB_NETCONF notifications are activated
81         // Because this way we actually start all notification producers
82         // final Collection<QName> notificationQNames =
83         ListenerRegistration<DOMNotificationListener> domNotifListenerReg = notificationRouter
84                 .registerNotificationListener(domNotificationListener, currentNotificationSchemaPaths);
85
86         LOG.info("Exposing HONEYCOMB_NETCONF notification stream: {}", streamType.getValue());
87         return new HoneycombNotification2Netconf(domNotifListenerReg, netconfNotifReg);
88     }
89
90     public static final class HoneycombNotification2Netconf {
91         private final ListenerRegistration<DOMNotificationListener> domNotifListenerReg;
92         private final NotificationPublisherRegistration netconfNotifReg;
93
94         public HoneycombNotification2Netconf(final ListenerRegistration<DOMNotificationListener> domNotifListenerReg,
95                                              final NotificationPublisherRegistration netconfNotifReg) {
96             this.domNotifListenerReg = domNotifListenerReg;
97             this.netconfNotifReg = netconfNotifReg;
98         }
99
100         public ListenerRegistration<DOMNotificationListener> getDomNotifListenerReg() {
101             return domNotifListenerReg;
102         }
103
104         public NotificationPublisherRegistration getNetconfNotifReg() {
105             return netconfNotifReg;
106         }
107     }
108
109     private static final class TranslatingNotificationListener implements DOMNotificationListener {
110
111         private static final Logger LOG = LoggerFactory.getLogger(TranslatingNotificationListener.class);
112
113         private final NotificationPublisherRegistration netconfNotifReg;
114         private final StreamNameType streamType;
115         private final SchemaService schemaService;
116
117         TranslatingNotificationListener(final NotificationPublisherRegistration netconfNotifReg,
118                                                final StreamNameType streamType, final SchemaService schemaService) {
119             this.netconfNotifReg = netconfNotifReg;
120             this.streamType = streamType;
121             this.schemaService = schemaService;
122         }
123
124         @Override
125         public void onNotification(@Nonnull final DOMNotification notif) {
126             LOG.debug("Propagating notification: {} into HONEYCOMB_NETCONF", notif.getType());
127             netconfNotifReg.onNotification(streamType, TranslationUtil.notificationToXml(notif, schemaService.getGlobalContext()));
128         }
129     }
130 }