Add initial version of code
[nonrtric/plt/rappmanager.git] / rapp-manager-acm / src / main / java / com / oransc / rappmanager / acm / service / AutomationCompositionLifeCycleManager.java
1 /*-
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2023 Nordix Foundation. All rights reserved.
4  * ===============================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END========================================================================
17  */
18
19 package com.oransc.rappmanager.acm.service;
20
21
22 import com.oransc.rappmanager.acm.configuration.ACMConfiguration;
23 import java.nio.file.Files;
24 import java.nio.file.Path;
25 import java.util.concurrent.TimeUnit;
26 import lombok.RequiredArgsConstructor;
27 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
28 import org.onap.policy.clamp.models.acm.messages.rest.commissioning.CommissioningResponse;
29 import org.onap.policy.clamp.models.acm.messages.rest.commissioning.PrimeOrder;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.context.SmartLifecycle;
33 import org.springframework.stereotype.Service;
34
35 @Service
36 @RequiredArgsConstructor
37 public class AutomationCompositionLifeCycleManager implements SmartLifecycle {
38
39     Logger logger = LoggerFactory.getLogger(AutomationCompositionLifeCycleManager.class);
40     private final ACMConfiguration acmConfiguration;
41     private final AcmDeployer acmDeployer;
42     private boolean running;
43
44     @Override
45     public void start() {
46         logger.info("Initializing automation Composition");
47         try {
48             String compositionPayload = Files.readString(Path.of(acmConfiguration.getCompositionDefinitionLocation()));
49             CommissioningResponse commissioningResponse =
50                     acmDeployer.createComposition(compositionPayload);
51             if (commissioningResponse != null && commissioningResponse.getCompositionId() != null) {
52                 logger.info("Priming automation Composition");
53                 acmDeployer.primeACMComposition(commissioningResponse.getCompositionId(),
54                         PrimeOrder.PRIME);
55                 for (int i = 0; i < acmConfiguration.getMaxRetries(); i++) {
56                     logger.debug("Composition priming check {}", i + 1);
57                     if (acmDeployer.isCompositionStateEquals(commissioningResponse.getCompositionId(),
58                             AcTypeState.PRIMED)) {
59                         logger.info("Composition {} is primed", commissioningResponse.getCompositionId());
60                         running = true;
61                         break;
62                     } else {
63                         TimeUnit.SECONDS.sleep(acmConfiguration.getRetryInterval());
64                     }
65                 }
66             } else {
67                 logger.error("Failed to create automation composition");
68             }
69         } catch (Exception e) {
70             logger.error("Failed to create automation composition", e);
71         }
72     }
73
74     @Override
75     public void stop() {
76         logger.info("Depriming automation Composition");
77         if (running) {
78             try {
79                 acmDeployer.primeACMComposition(acmDeployer.getCompositionId(),
80                         PrimeOrder.DEPRIME);
81                 for (int i = 0; i < acmConfiguration.getMaxRetries(); i++) {
82                     logger.debug("Composition depriming check {}", i + 1);
83                     if (acmDeployer.isCompositionStateEquals(
84                             acmDeployer.getCompositionId(), AcTypeState.COMMISSIONED)) {
85                         logger.info("Composition {} is deprimed", acmDeployer.getCompositionId());
86                         logger.info("Deleting automation Composition");
87                         acmDeployer.deleteComposition(acmDeployer.getCompositionId());
88                         running = false;
89                         break;
90                     } else {
91                         TimeUnit.SECONDS.sleep(acmConfiguration.getRetryInterval());
92                     }
93                 }
94             } catch (Exception e) {
95                 logger.error("Failed to cleanup automation composition");
96             }
97         }
98     }
99
100     @Override
101     public boolean isRunning() {
102         return running;
103     }
104 }