2 * Copyright © 2021 Orange and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.transportpce.servicehandler.listeners;
10 import java.util.Collection;
11 import org.opendaylight.mdsal.binding.api.DataBroker;
12 import org.opendaylight.mdsal.binding.api.DataObjectModification;
13 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
14 import org.opendaylight.mdsal.binding.api.DataTreeModification;
15 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
16 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.state.types.rev181130.State;
17 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.service.list.Services;
18 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210813.PublishNotificationAlarmService;
19 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210813.PublishNotificationAlarmServiceBuilder;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 public class ServiceListener implements DataTreeChangeListener<Services> {
25 private static final Logger LOG = LoggerFactory.getLogger(ServiceListener.class);
26 private static final String PUBLISHER = "ServiceListener";
27 private final DataBroker dataBroker;
28 private NotificationPublishService notificationPublishService;
30 public ServiceListener(final DataBroker dataBroker, NotificationPublishService notificationPublishService) {
31 this.dataBroker = dataBroker;
32 this.notificationPublishService = notificationPublishService;
35 public void onDataTreeChanged(Collection<DataTreeModification<Services>> changes) {
36 LOG.info("onDataTreeChanged - {}", this.getClass().getSimpleName());
37 for (DataTreeModification<Services> change : changes) {
38 DataObjectModification<Services> rootService = change.getRootNode();
39 if (rootService.getDataBefore() == null) {
42 String serviceName = rootService.getDataBefore().key().getServiceName();
43 switch (rootService.getModificationType()) {
45 LOG.info("Service {} correctly deleted from controller", serviceName);
48 Services input = rootService.getDataAfter();
49 if (rootService.getDataBefore().getOperationalState() == State.InService
50 && rootService.getDataAfter().getOperationalState() == State.OutOfService) {
51 LOG.info("Service {} is becoming outOfService", serviceName);
52 sendNbiNotification(new PublishNotificationAlarmServiceBuilder()
53 .setServiceName(input.getServiceName())
54 .setConnectionType(input.getConnectionType())
55 .setMessage("The service is now outOfService")
56 .setOperationalState(State.OutOfService)
57 .setPublisherName(PUBLISHER)
60 else if (rootService.getDataBefore().getOperationalState() == State.OutOfService
61 && rootService.getDataAfter().getOperationalState() == State.InService) {
62 LOG.info("Service {} is becoming InService", serviceName);
63 sendNbiNotification(new PublishNotificationAlarmServiceBuilder()
64 .setServiceName(input.getServiceName())
65 .setConnectionType(input.getConnectionType())
66 .setMessage("The service is now inService")
67 .setOperationalState(State.InService)
68 .setPublisherName(PUBLISHER)
73 LOG.debug("Unknown modification type {}", rootService.getModificationType().name());
80 * Send notification to NBI notification in order to publish message.
82 * @param service PublishNotificationAlarmService
84 private void sendNbiNotification(PublishNotificationAlarmService service) {
86 notificationPublishService.putNotification(service);
87 } catch (InterruptedException e) {
88 LOG.warn("Cannot send notification to nbi", e);
89 Thread.currentThread().interrupt();