253410e493c4678b61c3d68522e63e950b4b1701
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / main / java / org / openecomp / sdc / translator / services / heattotosca / impl / functiontranslation / FunctionTranslationGetFileImpl.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.openecomp.sdc.translator.services.heattotosca.impl.functiontranslation;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22 import org.onap.sdc.tosca.datatypes.model.ArtifactDefinition;
23 import org.onap.sdc.tosca.datatypes.model.NodeTemplate;
24 import org.openecomp.sdc.tosca.datatypes.ToscaArtifactType;
25 import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
26 import org.openecomp.sdc.tosca.services.ToscaConstants;
27 import org.openecomp.sdc.tosca.services.ToscaFileOutputService;
28 import org.openecomp.sdc.tosca.services.impl.ToscaFileOutputServiceCsarImpl;
29 import org.openecomp.sdc.translator.services.heattotosca.FunctionTranslation;
30
31 public class FunctionTranslationGetFileImpl implements FunctionTranslation {
32
33     private static ArtifactDefinition createArtifactDefinition(Object function, ToscaFileOutputService toscaFileOutputService) {
34         ArtifactDefinition artifactDefinition = new ArtifactDefinition();
35         artifactDefinition.setType(ToscaArtifactType.NATIVE_DEPLOYMENT);
36         artifactDefinition.setFile("../" + toscaFileOutputService.getArtifactsFolderName() + "/" + function);
37         return artifactDefinition;
38     }
39
40     @Override
41     public Object translateFunction(FunctionTranslator functionTranslator) {
42         String file = ((String) functionTranslator.getFunctionValue()).replace("file:///", "");
43         final String artifactId = file.split("\\.")[0];
44         Map<String, Object> returnValue = new HashMap<>();
45         List<String> artifactParameters = new ArrayList<>();
46         artifactParameters.add(ToscaConstants.MODELABLE_ENTITY_NAME_SELF);
47         returnValue.put(ToscaFunctions.GET_ARTIFACT.getFunctionName(), artifactParameters);
48         artifactParameters.add(artifactId);
49         ToscaFileOutputService toscaFileOutputService = new ToscaFileOutputServiceCsarImpl();
50         if (functionTranslator.getToscaTemplate() instanceof NodeTemplate) {
51             NodeTemplate nodeTemplate = (NodeTemplate) functionTranslator.getToscaTemplate();
52             ArtifactDefinition artifactDefinition = createArtifactDefinition(file, toscaFileOutputService);
53             if (nodeTemplate.getArtifacts() == null) {
54                 nodeTemplate.setArtifacts(new HashMap<>());
55             }
56             nodeTemplate.getArtifacts().put(artifactId, artifactDefinition);
57         }
58         return returnValue;
59     }
60 }