Initial OpenECOMP Demo commit
[demo.git] / vnfs / honeycomb_plugin / sample_plugin / sample-plugin-impl / src / main / java / io / fd / honeycomb / tutorial / notif / SampleNotificationProducer.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.tutorial.notif;
18
19 import io.fd.honeycomb.notification.ManagedNotificationProducer;
20 import io.fd.honeycomb.notification.NotificationCollector;
21 import java.util.Collection;
22 import java.util.Collections;
23 import javax.annotation.Nonnull;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.sample.plugin.rev160918.SampleNotification;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.sample.plugin.rev160918.SampleNotificationBuilder;
26 import org.opendaylight.yangtools.yang.binding.Notification;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Notification producer for sample plugin
32  */
33 public class SampleNotificationProducer implements ManagedNotificationProducer {
34
35     private static final Logger LOG = LoggerFactory.getLogger(SampleNotificationProducer.class);
36
37     private Thread thread;
38
39     @Override
40     public void start(@Nonnull final NotificationCollector collector) {
41         LOG.info("Starting notification stream for interfaces");
42
43         // Simulating notification producer
44         thread = new Thread(() -> {
45             while(true) {
46                 if (Thread.currentThread().isInterrupted()) {
47                     return;
48                 }
49
50                 try {
51                     Thread.sleep(2000);
52                 } catch (InterruptedException e) {
53                     Thread.currentThread().interrupt();
54                     break;
55                 }
56
57                 final SampleNotification notification = new SampleNotificationBuilder()
58                         .setContent("Hello world " + System.currentTimeMillis())
59                         .build();
60                 LOG.info("Emitting notification: {}", notification);
61                 collector.onNotification(notification);
62             }
63         }, "NotificationProducer");
64         thread.setDaemon(true);
65         thread.start();
66     }
67
68     @Override
69     public void stop() {
70         if(thread != null) {
71             thread.interrupt();
72         }
73     }
74
75     @Nonnull
76     @Override
77     public Collection<Class<? extends Notification>> getNotificationTypes() {
78         // Producing only this single type of notification
79         return Collections.singleton(SampleNotification.class);
80     }
81
82     @Override
83     public void close() throws Exception {
84         stop();
85     }
86 }