Update odlux
[ccsdk/features.git] / sdnr / wt / devicemanager / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / impl / util / NetconfNotification.java
1 /**
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.util;
19
20 import java.util.Optional;
21 import javax.annotation.Nonnull;
22 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.DeviceManagerImpl;
23 import org.opendaylight.mdsal.binding.api.MountPoint;
24 import org.opendaylight.mdsal.binding.api.RpcConsumerRegistry;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInput;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.NotificationsService;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class NetconfNotification {
33
34     private static final Logger log = LoggerFactory.getLogger(DeviceManagerImpl.class);
35
36     /**
37      * Do the stream creation for the device.
38      * @param nodeId node-id of device
39      * @param mountpoint information
40      * @param streamName to register
41      */
42     public static void registerNotificationStream(String nodeId, MountPoint mountpoint, String streamName) {
43
44         final Optional<RpcConsumerRegistry> optionalRpcConsumerService =
45                 mountpoint.getService(RpcConsumerRegistry.class);
46         if (optionalRpcConsumerService.isPresent()) {
47             final RpcConsumerRegistry rpcConsumerRegitry = optionalRpcConsumerService.get();
48             @Nonnull
49             final NotificationsService rpcService = rpcConsumerRegitry.getRpcService(NotificationsService.class);
50
51             final CreateSubscriptionInputBuilder createSubscriptionInputBuilder = new CreateSubscriptionInputBuilder();
52             createSubscriptionInputBuilder.setStream(new StreamNameType(streamName));
53             log.info("Event listener triggering notification stream {} for node {}", streamName, nodeId);
54             try {
55                 CreateSubscriptionInput createSubscriptionInput = createSubscriptionInputBuilder.build();
56                 if (createSubscriptionInput == null) {
57                     log.warn("createSubscriptionInput is null for mountpoint {}", nodeId);
58                 } else {
59                     rpcService.createSubscription(createSubscriptionInput);
60                 }
61             } catch (NullPointerException e) {
62                 log.warn("createSubscription failed");
63             }
64         } else {
65             log.warn("No RpcConsumerRegistry avaialble.");
66         }
67
68     }
69
70 }