f3ced2e3fb9a3e5b1da22271a86180f539cfa381
[honeycomb.git] /
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 groovy.transform.ToString
21 import groovy.util.logging.Slf4j
22 import io.fd.honeycomb.infra.distro.ProviderTrait
23 import io.fd.honeycomb.infra.distro.cfgattrs.HoneycombConfiguration
24 import io.fd.honeycomb.notification.NotificationCollector
25 import io.fd.honeycomb.notification.impl.NotificationProducerRegistry
26 import org.opendaylight.controller.md.sal.dom.broker.impl.DOMNotificationRouter
27 import org.opendaylight.controller.sal.core.api.model.SchemaService
28 import org.opendaylight.netconf.notifications.NetconfNotificationCollector
29 import io.fd.honeycomb.notification.impl.TranslationUtil
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.StreamBuilder
32 import org.opendaylight.yangtools.yang.model.api.SchemaPath
33
34 @Slf4j
35 @ToString
36 class HoneycombNotification2NetconfProvider extends ProviderTrait<HoneycombNotification2Netconf> {
37
38     @Inject
39     DOMNotificationRouter notificationRouter
40     @Inject
41     SchemaService schemaService
42     @Inject
43     HoneycombConfiguration cfgAttributes
44     @Inject
45     NotificationCollector hcNotificationCollector
46     @Inject
47     NetconfNotificationCollector netconfNotificationCollector
48
49     @Override
50     def create() {
51         def streamType = new StreamNameType(cfgAttributes.netconfNotificationStreamName.get());
52
53         // Register as HONEYCOMB_NETCONF notification publisher under configured name
54         def netconfNotifReg = netconfNotificationCollector.registerNotificationPublisher(new StreamBuilder()
55                         .setName(streamType)
56                         .setReplaySupport(false)
57                         .setDescription(cfgAttributes.netconfNotificationStreamName.get()).build());
58
59         // Notification Translator, get notification from HC producers and put into HONEYCOMB_NETCONF notification collector
60         def domNotificationListener = { notif ->
61                 log.debug "Propagating notification: {} into HONEYCOMB_NETCONF", notif.type
62                 netconfNotifReg.onNotification(streamType, TranslationUtil.notificationToXml(notif, schemaService.globalContext))
63         }
64
65         // NotificationManager is used to provide list of available notifications (which are all of the notifications registered)
66         // TODO make available notifications configurable here so that any number of notification streams for HONEYCOMB_NETCONF
67         // can be configured on top of a single notification manager
68         log.debug "Current notifications to be exposed over HONEYCOMB_NETCONF: {}", hcNotificationCollector.notificationTypes
69         def currentNotificationSchemaPaths = hcNotificationCollector.notificationTypes
70                 .collect {SchemaPath.create(true, NotificationProducerRegistry.getQName(it))}
71
72         // Register as listener to HC'OPERATIONAL DOM notification service
73         // TODO This should only be triggered when HONEYCOMB_NETCONF notifications are activated
74         // Because this way we actually start all notification producers
75         // final Collection<QName> notificationQNames =
76         def domNotifListenerReg = notificationRouter
77                 .registerNotificationListener(domNotificationListener, currentNotificationSchemaPaths);
78
79         log.info "Exposing HONEYCOMB_NETCONF notification stream: {}", streamType.value
80
81         new HoneycombNotification2Netconf(domNotifListenerReg: domNotifListenerReg, netconfNotifReg: netconfNotifReg)
82     }
83
84     static class HoneycombNotification2Netconf {
85         def domNotifListenerReg
86         def netconfNotifReg
87     }
88 }