Update release in docs/conf.yaml
[transportpce.git] / networkmodel / src / main / java / org / opendaylight / transportpce / networkmodel / listeners / ServiceHandlerListener.java
1 /*
2  * Copyright © 2020 Orange, Inc. 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
9 package org.opendaylight.transportpce.networkmodel.listeners;
10
11 import java.util.Set;
12 import org.opendaylight.mdsal.binding.api.NotificationService.CompositeListener;
13 import org.opendaylight.transportpce.networkmodel.service.FrequenciesService;
14 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.servicehandler.rev201125.ServiceRpcResultSh;
15 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev230526.ServiceNotificationTypes;
16 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev220118.RpcStatusEx;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 public class ServiceHandlerListener {
21     private static final Logger LOG = LoggerFactory.getLogger(ServiceHandlerListener.class);
22     private final FrequenciesService service;
23
24
25     public ServiceHandlerListener(FrequenciesService service) {
26         LOG.info("Init service handler listener for network");
27         this.service = service;
28     }
29
30     public CompositeListener getCompositeListener() {
31         return new CompositeListener(Set.of(
32             new CompositeListener.Component<>(ServiceRpcResultSh.class, this::onServiceRpcResultSh)));
33     }
34
35     public void onServiceRpcResultSh(ServiceRpcResultSh notification) {
36         if (notification.getStatus() != RpcStatusEx.Successful) {
37             LOG.info("RpcStatusEx of notification not equals successful. Nothing to do for notification {}",
38                     notification);
39             return;
40         }
41         ServiceNotificationTypes notificationType = notification.getNotificationType();
42         if (notificationType == null) {
43             LOG.warn("No information about the type of the notification for {}", notification);
44             return;
45         }
46         switch (notificationType) {
47             case ServiceCreateResult:
48             case ServiceReconfigureResult:
49             case ServiceRestorationResult:
50                 LOG.info("Service creation or reconfiguration or restoration notification received {}", notification);
51                 onServiceCreation(notification);
52                 break;
53             case ServiceDeleteResult:
54                 LOG.info("Service delete notification received {}", notification);
55                 onServiceDeletion(notification);
56                 break;
57             default:
58                 LOG.warn("This type of notification is not managed at this time {} for notification {}",
59                         notificationType, notification);
60                 break;
61
62         }
63     }
64
65     /**
66      * Allocate frequencies in topology.
67      * @param notification ServiceRpcResultSh
68      */
69     private void onServiceCreation(ServiceRpcResultSh notification) {
70         if (notification.getAToZDirection() != null || notification.getZToADirection() != null) {
71             LOG.info("Update topology with used frequency by service {}", notification.getServiceName());
72             service.allocateFrequencies(notification.getAToZDirection(), notification.getZToADirection());
73         }
74     }
75
76     /**
77      * Release frequencies in topology.
78      * @param notification ServiceRpcResultSh
79      */
80     private void onServiceDeletion(ServiceRpcResultSh notification) {
81         if (notification.getAToZDirection() != null || notification.getZToADirection() != null) {
82             LOG.info("Update topology with no more used frequency by deleted service {}",
83                     notification.getServiceName());
84             service.releaseFrequencies(notification.getAToZDirection(), notification.getZToADirection());
85         }
86     }
87
88 }