Olm Power Down Task
[transportpce.git] / renderer / src / main / java / org / opendaylight / transportpce / renderer / provisiondevice / tasks / OlmPowerTurnDownTask.java
1 /*
2  * Copyright © 2024 Smartoptics 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.renderer.provisiondevice.tasks;
10
11 import java.util.concurrent.Callable;
12 import java.util.concurrent.Future;
13 import org.opendaylight.transportpce.common.ResponseCodes;
14 import org.opendaylight.transportpce.renderer.ServicePathInputData;
15 import org.opendaylight.transportpce.renderer.provisiondevice.OLMRenderingResult;
16 import org.opendaylight.transportpce.renderer.provisiondevice.notification.Notification;
17 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerTurndownInputBuilder;
18 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerTurndownOutput;
19 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.TransportpceOlmService;
20 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev220118.RpcStatusEx;
21 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev220118.ServicePathNotificationTypes;
22 import org.opendaylight.yangtools.yang.common.RpcResult;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class OlmPowerTurnDownTask implements Callable<OLMRenderingResult> {
27
28     private final String serviceName;
29
30     private final String path;
31
32     private final TransportpceOlmService olmService;
33
34     private final ServicePathInputData servicePathInputData;
35
36     private final Notification notification;
37
38     private static final Logger LOG = LoggerFactory.getLogger(OlmPowerTurnDownTask.class);
39
40     /**
41      * Task used to power down OLM.
42      *
43      * <p>
44      * Intended to be used for parallel execution.
45      */
46     public OlmPowerTurnDownTask(String serviceName,
47                                 String path,
48                                 TransportpceOlmService olmService,
49                                 ServicePathInputData servicePathInputData,
50                                 Notification notification) {
51
52         this.serviceName = serviceName;
53         this.path = path;
54         this.olmService = olmService;
55         this.servicePathInputData = servicePathInputData;
56         this.notification = notification;
57     }
58
59     @Override
60     public OLMRenderingResult call() throws Exception {
61
62         LOG.debug("Turning down power on {} path for service {}", path, serviceName);
63
64         Future<RpcResult<ServicePowerTurndownOutput>> fr = this.olmService.servicePowerTurndown(
65             new ServicePowerTurndownInputBuilder(
66                 servicePathInputData.getServicePathInput()
67             ).build());
68
69         notification.send(
70             ServicePathNotificationTypes.ServiceDelete,
71             serviceName,
72             RpcStatusEx.Pending,
73             String.format("Turning down power on %s path for service %s", path, serviceName)
74         );
75
76         RpcResult<ServicePowerTurndownOutput> result = fr.get();
77
78         if (result == null || !ResponseCodes.SUCCESS_RESULT.equals(result.getResult().getResult())) {
79             notification.send(
80                 ServicePathNotificationTypes.ServiceDelete,
81                 serviceName,
82                 RpcStatusEx.Failed,
83                 String.format("Service power turn down failed on %s path for service %s", path, serviceName)
84             );
85             return OLMRenderingResult.failed(
86                 String.format("Service power turn down failed on %s path for service %s", path, serviceName)
87             );
88         } else {
89             LOG.debug("OLM power turn down finished successfully on {} for service {}", path, serviceName);
90             return OLMRenderingResult.ok();
91         }
92     }
93 }