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