Support for optional rApp and rApp instance parameters
[nonrtric/plt/rappmanager.git] / rapp-manager-dme / src / main / java / com / oransc / rappmanager / dme / service / DmeDeployer.java
1 /*-
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2023 Nordix Foundation. All rights reserved.
4  * Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved.
5  * ===============================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  * ============LICENSE_END========================================================================
18  */
19
20 package com.oransc.rappmanager.dme.service;
21
22 import com.fasterxml.jackson.databind.ObjectMapper;
23 import com.oransc.rappmanager.dme.data.ConsumerJob;
24 import com.oransc.rappmanager.dme.data.ProducerRegistrationInfo;
25 import com.oransc.rappmanager.dme.rest.DataProducerRegistrationApiClient;
26 import com.oransc.rappmanager.models.RappDeployer;
27 import com.oransc.rappmanager.models.csar.RappCsarConfigurationHandler;
28 import com.oransc.rappmanager.models.rapp.Rapp;
29 import com.oransc.rappmanager.models.rappinstance.RappInstance;
30 import java.util.HashSet;
31 import java.util.Set;
32 import lombok.RequiredArgsConstructor;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.stereotype.Service;
36
37 @Service
38 @RequiredArgsConstructor
39 public class DmeDeployer implements RappDeployer {
40
41     Logger logger = LoggerFactory.getLogger(DmeDeployer.class);
42
43     private final DataProducerRegistrationApiClient dataProducerRegistrationApiClient;
44
45     private final RappCsarConfigurationHandler rappCsarConfigurationHandler;
46
47     private final ObjectMapper objectMapper;
48
49     @Override
50     public boolean deployRappInstance(Rapp rapp, RappInstance rappInstance) {
51         logger.debug("DME instance deployment is handled as part of ACM injection for {}",
52                 rappInstance.getRappInstanceId());
53         return true;
54     }
55
56     @Override
57     public boolean undeployRappInstance(Rapp rapp, RappInstance rappInstance) {
58         logger.debug("DME instance undeployment is handled as part of ACM injection for {}",
59                 rappInstance.getRappInstanceId());
60         return true;
61     }
62
63     @Override
64     public boolean primeRapp(Rapp rapp) {
65         logger.debug("Priming DME functions for rApp {}", rapp.getRappId());
66         if (rapp.isDMEEnabled()) {
67             try {
68                 Set<String> requiredInfoTypes = new HashSet<>();
69                 for (String producerResourceName : rapp.getRappResources().getDme().getInfoProducers()) {
70                     String producerPayload =
71                             rappCsarConfigurationHandler.getDmeInfoProducerPayload(rapp, producerResourceName);
72                     ProducerRegistrationInfo producerRegistrationInfo =
73                             objectMapper.readValue(producerPayload, ProducerRegistrationInfo.class);
74                     requiredInfoTypes.addAll(producerRegistrationInfo.getSupportedInfoTypes());
75                 }
76                 for (String consumerResourceName : rapp.getRappResources().getDme().getInfoConsumers()) {
77                     String consumerPayload =
78                             rappCsarConfigurationHandler.getDmeInfoConsumerPayload(rapp, consumerResourceName);
79                     ConsumerJob consumerJob = objectMapper.readValue(consumerPayload, ConsumerJob.class);
80                     requiredInfoTypes.add(consumerJob.getInfoTypeId());
81                 }
82                 Set<String> allInfoTypes = new HashSet<>(rapp.getRappResources().getDme().getProducerInfoTypes());
83                 allInfoTypes.addAll(rapp.getRappResources().getDme().getConsumerInfoTypes());
84                 requiredInfoTypes.removeAll(allInfoTypes);
85                 if (!requiredInfoTypes.isEmpty()) {
86                     allInfoTypes.addAll(dataProducerRegistrationApiClient.getInfoTypdentifiers());
87                     requiredInfoTypes.removeAll(allInfoTypes);
88                     if (!requiredInfoTypes.isEmpty()) {
89                         rapp.setReason(
90                                 String.format("Invalid rapp package as the following info types cannot be found %s",
91                                         requiredInfoTypes));
92                     }
93                 }
94                 return true;
95             } catch (Exception e) {
96                 logger.warn("Failed to prime DME", e);
97                 rapp.setReason("Failed to prime DME");
98                 return false;
99             }
100         }
101         return true;
102     }
103
104     @Override
105     public boolean deprimeRapp(Rapp rapp) {
106         logger.debug("Depriming DME functions for rApp {}", rapp.getRappId());
107         return true;
108     }
109 }