736e6471be4e627b63ab905d6fb7de29c5fd0b7c
[transportpce.git] / servicehandler / src / main / java / org / opendaylight / transportpce / servicehandler / listeners / ServiceListener.java
1 /*
2  * Copyright © 2021 Orange and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.transportpce.servicehandler.listeners;
9
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;
22
23 public class ServiceListener implements DataTreeChangeListener<Services> {
24
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;
29
30     public ServiceListener(final DataBroker dataBroker, NotificationPublishService notificationPublishService) {
31         this.dataBroker = dataBroker;
32         this.notificationPublishService = notificationPublishService;
33     }
34
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) {
40                 continue;
41             }
42             String serviceName = rootService.getDataBefore().key().getServiceName();
43             switch (rootService.getModificationType()) {
44                 case DELETE:
45                     LOG.info("Service {} correctly deleted from controller", serviceName);
46                     break;
47                 case WRITE:
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)
58                                 .build());
59                     }
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)
69                                 .build());
70                     }
71                     break;
72                 default:
73                     LOG.debug("Unknown modification type {}", rootService.getModificationType().name());
74                     break;
75             }
76         }
77     }
78
79     /**
80      * Send notification to NBI notification in order to publish message.
81      *
82      * @param service PublishNotificationAlarmService
83      */
84     private void sendNbiNotification(PublishNotificationAlarmService service) {
85         try {
86             notificationPublishService.putNotification(service);
87         } catch (InterruptedException e) {
88             LOG.warn("Cannot send notification to nbi", e);
89             Thread.currentThread().interrupt();
90         }
91     }
92 }