Add installation mode to use snapshot image
[nonrtric/plt/rappmanager.git] / rapp-manager-dme / src / main / java / com / oransc / rappmanager / dme / service / DmeAcmInterceptor.java
1 /*-
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2023 OpenInfra Foundation Europe. 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.dme.service;
20
21 import com.google.gson.JsonParser;
22 import com.oransc.rappmanager.dme.models.DataConsumerEntity;
23 import com.oransc.rappmanager.dme.models.DataProducerEntity;
24 import com.oransc.rappmanager.dme.models.InfoTypeEntity;
25 import com.oransc.rappmanager.models.AcmInterceptor;
26 import com.oransc.rappmanager.models.csar.RappCsarConfigurationHandler;
27 import com.oransc.rappmanager.models.rapp.Rapp;
28 import com.oransc.rappmanager.models.rappinstance.RappInstance;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.UUID;
34 import lombok.RequiredArgsConstructor;
35 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeType;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaSchemaDefinition;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
43 import org.springframework.stereotype.Service;
44
45 @Service
46 @RequiredArgsConstructor
47 public class DmeAcmInterceptor implements AcmInterceptor {
48
49     private final RappCsarConfigurationHandler rappCsarConfigurationHandler;
50
51     String dmeInfoTypeEntity = "org.onap.datatypes.policy.clamp.acm.DMEAutomationCompositionElement.InfoTypeEntity";
52     String version100 = "1.0.0";
53     String version101 = "1.0.1";
54     String dmeDataProducerEntity =
55             "org.onap.datatypes.policy.clamp.acm.DMEAutomationCompositionElement.DataProducerEntity";
56     String dmeDataConsumerEntity =
57             "org.onap.datatypes.policy.clamp.acm.DMEAutomationCompositionElement.DataConsumerEntity";
58     String infoTypeEntitiesKey = "infoTypeEntities";
59     String dataProducerEntitiesKey = "dataProducerEntities";
60     String dataConsumerEntitiesKey = "dataConsumerEntities";
61     String toscaServiceTemplateDmeElement = "onap.policy.clamp.ac.element.DMEAutomationCompositionElement";
62     String toscaServiceTemplateDmeElementVersion = "1.2.3";
63     String toscaNodeTypeDmeElement = "org.onap.policy.clamp.acm.DMEAutomationCompositionElement";
64     String payload = "payload";
65
66     @Override
67     public void injectToscaServiceTemplate(ToscaServiceTemplate toscaServiceTemplate) {
68         injectToscaServiceTemplate(toscaServiceTemplate, toscaServiceTemplateDmeElement,
69                 toscaServiceTemplateDmeElementVersion);
70     }
71
72     @Override
73     public Map<String, ToscaNodeTemplate> getNodeTemplates() {
74         Map<String, ToscaNodeTemplate> toscaNodeTemplateMap = new HashMap<>();
75
76         ToscaNodeTemplate nodeTemplateParticipant = getNodeTemplateParticipant();
77         toscaNodeTemplateMap.put("org.onap.policy.clamp.acm.DMEAutomationCompositionParticipant",
78                 nodeTemplateParticipant);
79         ToscaNodeTemplate dmeAcmElement = getDmeAcmElement();
80         toscaNodeTemplateMap.put(toscaServiceTemplateDmeElement, dmeAcmElement);
81
82         return toscaNodeTemplateMap;
83     }
84
85     @Override
86     public Map<String, ToscaNodeType> getNodeTypes() {
87         return Map.of(toscaNodeTypeDmeElement, getNodeType());
88     }
89
90     @Override
91     public Map<String, ToscaDataType> getDataTypes() {
92         Map<String, ToscaDataType> toscaDataTypeMap = new HashMap<>();
93
94         ToscaDataType dmeInfoTypeDataType = getDmeInfoTypeDataType();
95         toscaDataTypeMap.put(dmeInfoTypeEntity, dmeInfoTypeDataType);
96
97         ToscaDataType dmeDataProducerDataType = getDmeDataProducerDataType();
98         toscaDataTypeMap.put(dmeDataProducerEntity, dmeDataProducerDataType);
99
100         ToscaDataType dmeDataConsumerDataType = getDmeDataConsumerDataType();
101         toscaDataTypeMap.put(dmeDataConsumerEntity, dmeDataConsumerDataType);
102
103         return toscaDataTypeMap;
104     }
105
106     @Override
107     public Map<UUID, AutomationCompositionElement> getInstantiationElement(Rapp rapp, RappInstance rappInstance) {
108         ToscaConceptIdentifier toscaConceptIdentifier = new ToscaConceptIdentifier();
109         toscaConceptIdentifier.setName(toscaServiceTemplateDmeElement);
110         toscaConceptIdentifier.setVersion(toscaServiceTemplateDmeElementVersion);
111
112         AutomationCompositionElement automationCompositionElement = new AutomationCompositionElement();
113         automationCompositionElement.setDefinition(toscaConceptIdentifier);
114
115         Map<String, Object> properties = new HashMap<>();
116         properties.put(infoTypeEntitiesKey, getInfoTypeEntities(rapp, rappInstance));
117         properties.put(dataProducerEntitiesKey, getDataProducerEntities(rapp, rappInstance));
118         properties.put(dataConsumerEntitiesKey, getDataConsumerEntities(rapp, rappInstance));
119
120         automationCompositionElement.setProperties(properties);
121
122         return Map.of(automationCompositionElement.getId(), automationCompositionElement);
123     }
124
125     private List<InfoTypeEntity> getInfoTypeEntities(Rapp rapp, RappInstance rappInstance) {
126         List<InfoTypeEntity> infoTypeEntityList = new ArrayList<>();
127         if (rappInstance.getDme().getInfoTypeConsumer() != null) {
128             String dmeConsumerInfoTypePayload = rappCsarConfigurationHandler.getDmeConsumerInfoTypePayload(rapp,
129                     rappInstance.getDme().getInfoTypeConsumer());
130             infoTypeEntityList.add(InfoTypeEntity.builder().infoTypeEntityId(
131                             new ToscaConceptIdentifier(rappInstance.getDme().getInfoTypeConsumer(), version101))
132                                            .infoTypeId(rappInstance.getDme().getInfoTypeConsumer())
133                                            .payload(JsonParser.parseString(dmeConsumerInfoTypePayload).toString())
134                                            .build());
135         }
136
137
138         if (rappInstance.getDme().getInfoTypesProducer() != null) {
139             rappInstance.getDme().getInfoTypesProducer().forEach(infoTypeProducer -> {
140                 if (!infoTypeProducer.equals(rappInstance.getDme().getInfoTypeConsumer())) {
141                     String dmeProducerInfoTypePayload =
142                             rappCsarConfigurationHandler.getDmeProducerInfoTypePayload(rapp, infoTypeProducer);
143                     infoTypeEntityList.add(InfoTypeEntity.builder().infoTypeEntityId(
144                                     new ToscaConceptIdentifier(infoTypeProducer, version101)).infoTypeId(infoTypeProducer)
145                                                    .payload(JsonParser.parseString(dmeProducerInfoTypePayload)
146                                                                     .toString()).build());
147                 }
148             });
149         }
150         return infoTypeEntityList;
151     }
152
153     List<DataProducerEntity> getDataProducerEntities(Rapp rapp, RappInstance rappInstance) {
154         List<DataProducerEntity> dataProducerEntityList = new ArrayList<>();
155         if (rappInstance.getDme().getInfoProducer() != null) {
156             String dmeInfoProducerPayload = rappCsarConfigurationHandler.getDmeInfoProducerPayload(rapp,
157                     rappInstance.getDme().getInfoProducer());
158             dataProducerEntityList.add(DataProducerEntity.builder().dataProducerEntityId(
159                             new ToscaConceptIdentifier(rappInstance.getDme().getInfoProducer(), version101))
160                                                .dataProducerId(rappInstance.getDme().getInfoProducer())
161                                                .payload(JsonParser.parseString(dmeInfoProducerPayload).toString())
162                                                .build());
163         }
164         return dataProducerEntityList;
165     }
166
167     List<DataConsumerEntity> getDataConsumerEntities(Rapp rapp, RappInstance rappInstance) {
168         List<DataConsumerEntity> dataConsumerEntityList = new ArrayList<>();
169         if (rappInstance.getDme().getInfoConsumer() != null) {
170             String dmeInfoConsumerPayload = rappCsarConfigurationHandler.getDmeInfoConsumerPayload(rapp,
171                     rappInstance.getDme().getInfoConsumer());
172             dataConsumerEntityList.add(DataConsumerEntity.builder().dataConsumerEntityId(
173                             new ToscaConceptIdentifier(rappInstance.getDme().getInfoConsumer(), version101))
174                                                .dataConsumerId(rappInstance.getDme().getInfoConsumer())
175                                                .payload(JsonParser.parseString(dmeInfoConsumerPayload).toString())
176                                                .build());
177         }
178         return dataConsumerEntityList;
179     }
180
181     ToscaNodeTemplate getDmeAcmElement() {
182         ToscaNodeTemplate toscaNodeTemplate = new ToscaNodeTemplate();
183         toscaNodeTemplate.setVersion("1.2.3");
184         toscaNodeTemplate.setType(toscaNodeTypeDmeElement);
185         toscaNodeTemplate.setTypeVersion(version101);
186         Map<String, Object> propertiesMap = new HashMap<>();
187         propertiesMap.put("provider", TEMPLATE_PROVIDER);
188         propertiesMap.put("participantType",
189                 new ToscaConceptIdentifier("org.onap.policy.clamp.acm.DMEParticipant", "2.3.4"));
190         toscaNodeTemplate.setProperties(propertiesMap);
191         return toscaNodeTemplate;
192     }
193
194     ToscaNodeTemplate getNodeTemplateParticipant() {
195         ToscaNodeTemplate toscaNodeTemplate = new ToscaNodeTemplate();
196         toscaNodeTemplate.setVersion("2.3.4");
197         toscaNodeTemplate.setType(AC_NODE_TEMPLATE_PARTICIPANT_TYPE);
198         toscaNodeTemplate.setTypeVersion(version101);
199         Map<String, Object> propertiesMap = new HashMap<>();
200         propertiesMap.put("provider", TEMPLATE_PROVIDER);
201         toscaNodeTemplate.setProperties(propertiesMap);
202         return toscaNodeTemplate;
203     }
204
205     ToscaDataType getDmeDataConsumerDataType() {
206         ToscaDataType toscaDataType = new ToscaDataType();
207         toscaDataType.setVersion(version100);
208         toscaDataType.setDerivedFrom(AC_TOSCA_DATA_TYPE_ROOT);
209
210         Map<String, ToscaProperty> propertyMap = new HashMap<>();
211         ToscaProperty dataConsumerEntityIdProperty = getToscaProperty(TOSCA_IDENTIFIER_KEY);
212         propertyMap.put("dataConsumerEntityId", dataConsumerEntityIdProperty);
213         ToscaProperty dataConsumerIdProperty = getToscaProperty(TOSCA_PROPERTY_TYPE_STRING);
214         propertyMap.put("dataConsumerId", dataConsumerIdProperty);
215         ToscaProperty payloadProperty = getToscaProperty(TOSCA_PROPERTY_TYPE_STRING);
216         propertyMap.put(payload, payloadProperty);
217         toscaDataType.setProperties(propertyMap);
218
219         return toscaDataType;
220     }
221
222
223     ToscaDataType getDmeDataProducerDataType() {
224         ToscaDataType toscaDataType = new ToscaDataType();
225         toscaDataType.setVersion(version100);
226         toscaDataType.setDerivedFrom(AC_TOSCA_DATA_TYPE_ROOT);
227
228         Map<String, ToscaProperty> propertyMap = new HashMap<>();
229         ToscaProperty dataProducerEntityIdProperty = getToscaProperty(TOSCA_IDENTIFIER_KEY);
230         propertyMap.put("dataProducerEntityId", dataProducerEntityIdProperty);
231         ToscaProperty dataProducerIdProperty = getToscaProperty(TOSCA_PROPERTY_TYPE_STRING);
232         propertyMap.put("dataProducerId", dataProducerIdProperty);
233         ToscaProperty payloadProperty = getToscaProperty(TOSCA_PROPERTY_TYPE_STRING);
234         propertyMap.put(payload, payloadProperty);
235         toscaDataType.setProperties(propertyMap);
236
237         return toscaDataType;
238     }
239
240     ToscaDataType getDmeInfoTypeDataType() {
241         ToscaDataType toscaDataType = new ToscaDataType();
242         toscaDataType.setVersion(version100);
243         toscaDataType.setDerivedFrom(AC_TOSCA_DATA_TYPE_ROOT);
244
245         Map<String, ToscaProperty> propertyMap = new HashMap<>();
246         ToscaProperty infoTypeEntityIdProperty = getToscaProperty(TOSCA_IDENTIFIER_KEY);
247         propertyMap.put("infoTypeEntityId", infoTypeEntityIdProperty);
248         ToscaProperty infoTypeIdProperty = getToscaProperty(TOSCA_PROPERTY_TYPE_STRING);
249         propertyMap.put("infoTypeId", infoTypeIdProperty);
250         ToscaProperty payloadProperty = getToscaProperty(TOSCA_PROPERTY_TYPE_STRING);
251         propertyMap.put(payload, payloadProperty);
252         toscaDataType.setProperties(propertyMap);
253
254         return toscaDataType;
255     }
256
257     ToscaProperty getToscaProperty(String type) {
258         ToscaProperty infoTypeEntityIdProperty = new ToscaProperty();
259         infoTypeEntityIdProperty.setType(type);
260         infoTypeEntityIdProperty.setRequired(true);
261         return infoTypeEntityIdProperty;
262     }
263
264     ToscaNodeType getNodeType() {
265         ToscaNodeType toscaNodeType = new ToscaNodeType();
266         toscaNodeType.setVersion(version101);
267         toscaNodeType.setDerivedFrom(AC_NODE_TYPE_ELEMENT_NAME);
268         toscaNodeType.setProperties(Map.of(infoTypeEntitiesKey, getInfoTypeProperties(), dataProducerEntitiesKey,
269                 getDataProducerProperties(), dataConsumerEntitiesKey, getDataConsumerProperties()));
270         return toscaNodeType;
271     }
272
273     ToscaProperty getInfoTypeProperties() {
274         return getToscoProperty(dmeInfoTypeEntity, version100);
275     }
276
277     ToscaProperty getDataProducerProperties() {
278         return getToscoProperty(dmeDataProducerEntity, version100);
279     }
280
281     ToscaProperty getDataConsumerProperties() {
282         return getToscoProperty(dmeDataConsumerEntity, version100);
283     }
284
285     ToscaProperty getToscoProperty(String schemaType, String schemaTypeVersion) {
286         ToscaProperty toscaProperty = new ToscaProperty();
287         toscaProperty.setType(TOSCA_PROPERTY_TYPE_LIST);
288         toscaProperty.setRequired(true);
289         ToscaSchemaDefinition toscaSchemaDefinition = new ToscaSchemaDefinition();
290         toscaSchemaDefinition.setType(schemaType);
291         toscaSchemaDefinition.setTypeVersion(schemaTypeVersion);
292         toscaProperty.setEntrySchema(toscaSchemaDefinition);
293         return toscaProperty;
294     }
295 }