re base code
[sdc.git] / common / onap-sdc-artifact-generator-lib / onap-sdc-artifact-generator-core / src / test / java / org / onap / sdc / generator / ArtifactGenerationServiceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.sdc.generator;
22
23 import org.apache.commons.io.FileUtils;
24 import org.onap.sdc.generator.aai.tosca.GroupDefinition;
25 import org.onap.sdc.generator.data.AdditionalParams;
26 import org.onap.sdc.generator.data.GenerationData;
27 import org.onap.sdc.generator.data.GeneratorConstants;
28 import org.onap.sdc.generator.impl.ArtifactGenerationServiceImpl;
29 import org.onap.sdc.generator.aai.model.Resource;
30 import org.onap.sdc.generator.aai.model.Service;
31 import org.onap.sdc.generator.aai.model.Widget;
32 import org.onap.sdc.generator.aai.tosca.NodeTemplate;
33 import org.onap.sdc.generator.aai.tosca.ToscaTemplate;
34 import org.onap.sdc.generator.aai.xml.Model;
35 import org.onap.sdc.generator.aai.xml.ModelElement;
36 import org.onap.sdc.generator.aai.xml.ModelElements;
37 import org.onap.sdc.generator.aai.xml.ModelVer;
38 import org.onap.sdc.generator.aai.xml.Relationship;
39 import org.onap.sdc.generator.aai.xml.RelationshipData;
40 import org.onap.sdc.generator.data.Artifact;
41 import org.onap.sdc.generator.data.ArtifactType;
42 import org.onap.sdc.generator.data.GeneratorUtil;
43 import org.onap.sdc.generator.data.GroupType;
44 import org.testng.Assert;
45 import org.testng.annotations.BeforeSuite;
46 import org.testng.annotations.Test;
47
48 import java.io.ByteArrayInputStream;
49 import java.io.File;
50 import java.io.FileInputStream;
51 import java.io.FileOutputStream;
52 import java.io.IOException;
53 import java.io.InputStream;
54 import java.io.OutputStream;
55
56 import java.net.URL;
57 import java.util.*;
58 import javax.xml.bind.JAXBContext;
59 import javax.xml.bind.JAXBException;
60 import javax.xml.bind.Unmarshaller;
61
62 @SuppressWarnings("Duplicates")
63 public class ArtifactGenerationServiceTest {
64
65   private static final String aaiArtifactType = ArtifactType.AAI.name();
66   private static final String aaiArtifactGroupType = GroupType.DEPLOYMENT.name();
67   private static final String generatorConfig = "{\"artifactTypes\": [\"OTHER\",\"AAI\"]}";
68   private final Properties properties = new Properties();
69   private final Map<String, String> additionalParams = new HashMap<>();
70   private final Map<String, String> resourcesVersion = new HashMap<>();
71
72   @BeforeSuite
73   public void loadProperties() throws Exception{
74     loadConfigFromClasspath(properties);
75     additionalParams.put(AdditionalParams.ServiceVersion.getName(), "1.0");
76   }
77
78   @Test
79   public void testArtifactGeneration() {
80     // Sunny day scenario service with VF anf vfmodule
81     try {
82
83       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
84       List<ToscaTemplate> toscas = new LinkedList<>();
85       String aaiResourceBasePaths = "aai/";
86       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
87       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
88       if (data.getErrorData().isEmpty()) {
89         for (Artifact inputArtifact : inputArtifacts) {
90           toscas.add(getToscaModel(inputArtifact));
91         }
92         List<Artifact> resultData = data.getResultData();
93
94         /*for( int i = 0 ; i < resultData.size() ; i++) {
95           Artifact artifact = resultData.get(i);
96           String fileName = artifact.getName();
97           while(fileName.contains(":")){
98             fileName = fileName.replace(":","");
99           }
100           File targetFile =new File("src/test/resources/"+fileName);
101           OutputStream outStream = new FileOutputStream(targetFile);
102           outStream.write(Base64.getDecoder().decode(artifact.getPayload()));
103         }*/
104
105         Assert.assertEquals(resultData.size(),5);  //  1-service,1-VF-resource,1-vfmodule and 2
106         // others
107         Map<String, Model> outputArtifactMap = populateAAIGeneratedModelStore(resultData);
108         testServiceTosca(toscas, outputArtifactMap);
109         testResourceTosca(toscas.iterator(), outputArtifactMap);
110       } else {
111         Assert.fail("error encountered : " + data.getErrorData().get("AAI"));
112       }
113
114     } catch (Exception e) {
115       Assert.fail(e.getMessage());
116     }
117   }
118
119   @Test
120   public void testWidgetinServiceTosca() {
121     // Sunny day scenario service with VF and extra widget like CP anf vf has vfmodule without
122     // member
123     try {
124       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
125       List<ToscaTemplate> toscas = new LinkedList<>();
126       String aaiResourceBasePaths = "testWidgetinServiceTosca/";
127       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
128       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
129       if (data.getErrorData().isEmpty()) {
130         for (Artifact inputArtifact : inputArtifacts) {
131           toscas.add(getToscaModel(inputArtifact));
132         }
133         List<Artifact> resultData = data.getResultData();
134         Assert.assertEquals(resultData.size(),5);  //  1-service,1-VF-resource,1-vfmodule and 2
135         // others
136         Map<String, Model> outputArtifactMap = populateAAIGeneratedModelStore(resultData);
137         testServiceTosca(toscas, outputArtifactMap);
138         testResourceTosca(toscas.iterator(), outputArtifactMap);
139       } else {
140         Assert.fail("error encountered : " + data.getErrorData().get("AAI"));
141       }
142
143     } catch (Exception e) {
144       Assert.fail(e.getMessage());
145     }
146   }
147
148   @Test
149   public void testSameVLdifferentVersion() {
150     // Sunny day scenario service with VF and extra widget like CP anf vf has vfmodule without
151     // member
152     try {
153       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
154       List<ToscaTemplate> toscas = new LinkedList<>();
155       String aaiResourceBasePaths = "testSameVLdifferentVersion/";
156       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
157       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
158       if (data.getErrorData().isEmpty()) {
159         for (Artifact inputArtifact : inputArtifacts) {
160           toscas.add(getToscaModel(inputArtifact));
161         }
162         List<Artifact> resultData = data.getResultData();
163         Assert.assertEquals(resultData.size(),8);
164         // others
165         Map<String, Model> outputArtifactMap = populateAAIGeneratedModelStore(resultData);
166         testServiceTosca(toscas, outputArtifactMap);
167         testResourceTosca(toscas.iterator(), outputArtifactMap);
168       } else {
169         Assert.fail("error encountered : " + data.getErrorData().get("AAI"));
170       }
171
172     } catch (Exception e) {
173       Assert.fail(e.getMessage());
174     }
175   }
176
177   @Test
178   public void testArtifactGenerationAllottedResourceAndL3Network() {
179     // Sunny day scenario service with allotted resource and L3-network
180     try {
181       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
182       List<ToscaTemplate> toscas = new LinkedList<>();
183       String aaiResourceBasePaths = "aai2/";
184       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
185       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
186       if (data.getErrorData().isEmpty()) {
187         for (Artifact inputArtifact : inputArtifacts) {
188           toscas.add(getToscaModel(inputArtifact));
189         }
190         List<Artifact> resultData = data.getResultData();
191
192         Assert.assertEquals(resultData.size(),5);
193         // and 2
194         // others
195         Map<String, Model> outputArtifactMap = populateAAIGeneratedModelStore(resultData);
196         testServiceTosca(toscas, outputArtifactMap);
197         testResourceTosca(toscas.iterator(), outputArtifactMap);
198       } else {
199         Assert.fail("error encountered : " + data.getErrorData().get("AAI"));
200       }
201
202     } catch (Exception e) {
203       Assert.fail(e.getMessage());
204     }
205   }
206
207   @Test(dependsOnMethods = {"testArtifactGeneration"})
208   public void testWhenMissingTosca() {
209     try {
210       //Missing Service tosca test case
211       String aaiResourceBasePaths = "testArtifactGeneration2/";
212       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
213       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
214       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
215       Assert.assertEquals(data.getErrorData().get("AAI").get(0),"Service tosca missing from list of input artifacts");
216     } catch (Exception e) {
217       Assert.fail(e.getMessage());
218     }
219   }
220
221   @Test(dependsOnMethods = {"testWhenMissingTosca"})
222   public void testWhenInvaildConfig() {
223     try {
224       //Invalid config test case
225       String generatorConfig1 = "{\"artifactTypes\": [\"ABC\"]}";
226       String aaiResourceBasePaths = "testArtifactGeneration2/";
227       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
228       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
229       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig1, additionalParams);
230       Assert.assertEquals(data.getErrorData().get("ARTIFACT_GENERATOR_INVOCATION_ERROR").get(0),"Invalid Client Configuration");
231     } catch (Exception e) {
232       Assert.fail(e.getMessage());
233     }
234   }
235
236   @Test(dependsOnMethods = {"testArtifactGeneration"})
237   public void testWhenOnlyServToscaNoResTosca() {
238     try {
239       //Testing only service tosca no resource Tosca
240       String aaiResourceBasePaths = "testArtifactGeneration4/";
241       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
242       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
243       List<ToscaTemplate> toscas = new LinkedList<>();
244       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
245       List<Artifact> resultData = data.getResultData();
246       if (data.getErrorData().isEmpty()) {
247         for (Artifact inputArtifact : inputArtifacts) {
248           toscas.add(getToscaModel(inputArtifact));
249         }
250       }
251       Assert.assertEquals(resultData.size(),3);  //  1-service and 2-Others
252       Map<String, Model> outputArtifactMap = populateAAIGeneratedModelStore(resultData);
253       testServiceTosca(toscas, outputArtifactMap);
254     } catch (Exception e) {
255       Assert.fail(e.getMessage());
256     }
257   }
258
259   @Test(dependsOnMethods = {"testArtifactGeneration"})
260   public void testWhenInvaildYaml() {
261     try {
262       //Invalid Yaml file test case
263       String aaiResourceBasePaths = "testArtifactGeneration5/";
264       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
265       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
266       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
267       Assert.assertEquals(data.getErrorData().get("AAI").get(0),"Invalid format for Tosca YML  : " + inputArtifacts.get(0).getName());
268     } catch (Exception e) {
269       Assert.fail(e.getMessage());
270     }
271   }
272
273   @Test(dependsOnMethods = {"testArtifactGeneration"})
274   public void testWhenExtraResToscaNotPartOfServ() {
275     try {
276       // Valid scenario with extra resource tosca which is not part of Service
277       String aaiResourceBasePaths = "testArtifactGeneration6/";
278       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
279       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
280       List<ToscaTemplate> toscas = new LinkedList<>();
281       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
282       List<Artifact> resultData = data.getResultData();
283       if (data.getErrorData().isEmpty()) {
284         for (Artifact inputArtifact : inputArtifacts) {
285           toscas.add(getToscaModel(inputArtifact));
286         }
287       }
288       Assert.assertEquals(resultData.size(),3);  //  1-service and 2 Others
289       Map<String, Model> outputArtifactMap = populateAAIGeneratedModelStore(resultData);
290       testServiceTosca(toscas, outputArtifactMap);
291     } catch (Exception e) {
292       Assert.fail(e.getMessage());
293     }
294   }
295
296
297   @Test
298   public void testWhenInvUuIdAttrMissing() {
299     try {
300       // mandatory attribute <invariantUUID> missing
301       String aaiResourceBasePaths = "testArtifactGeneration8/";
302       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
303       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
304       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
305       Assert.assertEquals(
306           data.getErrorData().get("AAI").get(0),"Invalid Service/Resource definition mandatory attribute <invariantUUID> missing in Artifact: <" +
307               inputArtifacts.get(0).getName() + ">");
308
309     } catch (Exception e) {
310       Assert.fail(e.getMessage());
311     }
312   }
313
314   @Test
315   public void testErrorWhenInvalidInvId() {
316     try {
317       //Invariant Id in service tosca of length not 36
318       String aaiResourceBasePaths = "testErrorWhenInvalidInvId/";
319       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
320       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
321       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
322       Assert.assertEquals(
323           data.getErrorData().get("AAI").get(0),String.format(
324               GeneratorConstants.GENERATOR_AAI_ERROR_INVALID_ID,
325               "invariantUUID",inputArtifacts.get(0).getName()));
326
327     } catch (Exception e) {
328       Assert.fail(e.getMessage());
329     }
330   }
331
332   @Test
333   public void testWhenUuIdAttrMissing() {
334     try {
335       //mandatory attribute <UUID> missing
336       String aaiResourceBasePaths = "testArtifactGeneration9/";
337       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
338       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
339       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
340       Assert.assertEquals(
341           data.getErrorData().get("AAI").get(0),"Invalid Service/Resource definition mandatory attribute <UUID> missing in Artifact: <" +
342               inputArtifacts.get(0).getName() + ">");
343
344     } catch (Exception e) {
345       Assert.fail(e.getMessage());
346     }
347   }
348
349   @Test
350   public void testErrorWhenInvalidUuId() {
351     try {
352       //UUID Id in service tosca of length not 36
353       String aaiResourceBasePaths = "testErrorWhenInvalidUuId/";
354       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
355       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
356       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
357       Assert.assertEquals(
358           data.getErrorData().get("AAI").get(0),String.format(
359               GeneratorConstants.GENERATOR_AAI_ERROR_INVALID_ID,
360               "UUID",inputArtifacts.get(0).getName()));
361
362     } catch (Exception e) {
363       Assert.fail(e.getMessage());
364     }
365   }
366
367   @Test
368   public void testWhenNameAttrMissing() {
369     try {
370       //mandatory attribute <name> missing
371       String aaiResourceBasePaths = "testArtifactGeneration10/";
372       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
373       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
374       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
375       Assert.assertEquals(
376           data.getErrorData().get("AAI").get(0),"Invalid Service/Resource definition mandatory attribute <name> missing in Artifact: <" +
377               inputArtifacts.get(0).getName() + ">");
378
379     } catch (Exception e) {
380       Assert.fail(e.getMessage());
381     }
382   }
383
384   @Test //(dependsOnMethods = {"testArtifactGeneration"})
385   public void testWhenVfModInvUuIdAttrMissing() {
386     try {
387       //mandatory attribute <vfModuleModelInvariantUUID> missing
388       List<Artifact> inputArtifacts = new ArrayList<>();
389       readPayloadFromResource(inputArtifacts, "service_vmme_template_ModInvUUID.yml");
390       readPayloadFromResource(inputArtifacts, "vf_vmme_template_ModInvUUID.yml");
391       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
392       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
393       Assert.assertEquals(
394           data.getErrorData().get("AAI").get(0),"Invalid Service/Resource definition mandatory attribute <vfModuleModelInvariantUUID> missing in Artifact: <" +
395               inputArtifacts.get(1).getName() + ">");
396
397     } catch (Exception e) {
398       Assert.fail(e.getMessage());
399     }
400   }
401
402   public static void readPayload(List<Artifact> inputArtifacts, InputStream fis, String fileName) throws
403       IOException {
404     byte[] payload = new byte[fis.available()];
405     fis.read(payload);
406     String checksum = GeneratorUtil.checkSum(payload);
407     byte[] encodedPayload = GeneratorUtil.encode(payload);
408     Artifact artifact = new Artifact(aaiArtifactType, aaiArtifactGroupType, checksum, encodedPayload);
409     artifact.setName(fileName);
410     artifact.setLabel(fileName);
411     artifact.setDescription(fileName);
412     artifact.setVersion("1.0");
413     System.out.println(artifact.getName());
414     inputArtifacts.add(artifact);
415   }
416
417   @Test
418   public void testWhenInvalidVfModInvUuIdAttr() {
419     try {
420       //invalid id since not of length 36 for  <vfModuleModelInvariantUUID>
421       List<Artifact> inputArtifacts = new ArrayList<>();
422       readPayloadFromResource(inputArtifacts, "service_vmme_template_InvalidVfModInvUuIdAttr.yml");
423
424       readPayloadFromResource(inputArtifacts, "vf_vmme_template_InvalidVfModInvUuIdAttr.yml");
425       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
426       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
427       Assert.assertEquals(
428           data.getErrorData().get("AAI").get(0),String.format(
429               GeneratorConstants.GENERATOR_AAI_ERROR_INVALID_ID,
430               "vfModuleModelInvariantUUID", inputArtifacts.get(1).getName() ));
431
432     } catch (Exception e) {
433       Assert.fail(e.getMessage());
434     }
435   }
436
437   @Test //(dependsOnMethods = {"testArtifactGeneration"})
438   public void testWhenVfModNameAttrMissing() {
439     try {
440       //mandatory attribute <vfModuleModelName> missing
441       List<Artifact> inputArtifacts = new ArrayList<>();
442       readPayloadFromResource(inputArtifacts, "service_vmme_template_ModelName.yml");
443
444       readPayloadFromResource(inputArtifacts, "vf_vmme_template_ModelName.yml");
445       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
446       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
447       Assert.assertEquals(
448           data.getErrorData().get("AAI").get(0),"Invalid Service/Resource definition mandatory attribute <vfModuleModelName> missing in Artifact: <" +
449               inputArtifacts.get(1).getName() + ">");
450
451     } catch (Exception e) {
452       Assert.fail(e.getMessage());
453     }
454   }
455
456   /*public static void readPayload(List<Artifact> inputArtifacts,InputStream fis, String fileName)
457       throws
458       IOException {
459     byte[] payload = new byte[fis.available()];
460     fis.read(payload);
461     String checksum = GeneratorUtil.checkSum(payload);
462     byte[] encodedPayload = GeneratorUtil.encode(payload);
463     Artifact artifact = new Artifact(aaiArtifactType, aaiArtifactGroupType, checksum, encodedPayload);
464     artifact.setName(fileName);
465     artifact.setLabel(fileName);
466     artifact.setDescription(fileName);
467     artifact.setVersion("1.0");
468     System.out.println(artifact.getName());
469     inputArtifacts.add(artifact);
470   }*/
471
472   @Test //(dependsOnMethods = {"testArtifactGeneration"})
473   public void testWhenVfModUuIdAttrMissing() {
474     try {
475       //mandatory attribute <vfModuleModelUUID> missing in Artifact
476       List<Artifact> inputArtifacts = new ArrayList<>();
477       readPayloadFromResource(inputArtifacts, "service_vmme_template_ModelUUID.yml");
478
479       readPayloadFromResource(inputArtifacts, "vf_vmme_template_ModelUUID.yml");
480       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
481       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
482       Assert.assertEquals(
483           data.getErrorData().get("AAI").get(0),"Invalid Service/Resource definition mandatory " +
484               "attribute <vfModuleModelUUID> missing in Artifact: <" +
485               inputArtifacts.get(1).getName() + ">");
486
487     } catch (Exception e) {
488       Assert.fail(e.getMessage());
489     }
490   }
491
492   @Test
493   public void testWhenInvalidVfModUuIdAttr() {
494     try {
495       //invalid id since not of length 36 for  <vfModuleModelUUID>
496       List<Artifact> inputArtifacts = new ArrayList<>();
497       readPayloadFromResource(inputArtifacts, "service_vmme_template_InvalidVfModUuIdAttr.yml");
498
499       readPayloadFromResource(inputArtifacts, "vf_vmme_template_InvalidVfModUuIdAttr.yml");
500       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
501       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
502       Assert.assertEquals(
503           data.getErrorData().get("AAI").get(0),String.format(
504               GeneratorConstants.GENERATOR_AAI_ERROR_INVALID_ID,
505               "vfModuleModelUUID", inputArtifacts.get(1).getName() ));
506
507     } catch (Exception e) {
508       Assert.fail(e.getMessage());
509     }
510   }
511
512   @Test //(dependsOnMethods = {"testArtifactGeneration"})
513   public void testWhenVfModVersionAttrMissing() {
514     try {
515       //mandatory attribute <vfModuleModelVersion> missing
516       List<Artifact> inputArtifacts = new ArrayList<>();
517       readPayloadFromResource(inputArtifacts, "service_vmme_template_ModelVersion.yml");
518
519       readPayloadFromResource(inputArtifacts, "vf_vmme_template_ModelVersion.yml");
520       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
521       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
522       Assert.assertEquals(
523           data.getErrorData().get("AAI").get(0),"Invalid Service/Resource definition mandatory attribute <vfModuleModelVersion> missing in Artifact: <" +
524               inputArtifacts.get(1).getName() + ">");
525
526     } catch (Exception e) {
527       Assert.fail(e.getMessage());
528     }
529   }
530
531   @Test
532   public void testErrorWhenNoSystemPropConfigured() throws Exception  {
533     String configLoc = System.getProperty("artifactgenerator.config");
534     try {
535       System.clearProperty("artifactgenerator.config");
536       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
537       String aaiResourceBasePaths = "aai/";
538       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
539       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
540       Assert.assertEquals(false,data.getErrorData().isEmpty());
541       Assert.assertEquals(data.getErrorData().
542           get("AAI").get(0), GeneratorConstants.GENERATOR_AAI_CONFIGLOCATION_NOT_FOUND);
543     } catch (Exception e) {
544       Assert.fail(e.getMessage());
545     }
546     finally{
547       System.setProperty("artifactgenerator.config",configLoc);
548     }
549   }
550
551   @Test
552   public void testErrorWhenNoWidgetInConfig() throws Exception  {
553     String configLoc = System.getProperty("artifactgenerator.config");
554     final File configFile = new File(configLoc);
555     String configDir = configLoc.substring(0, configLoc.lastIndexOf(File.separator));
556     final File tempFile = new File(configDir + File.separator + "temp.properties");
557     try {
558       //copy orignal Artifact-Generator.properties to temp.properties for backup
559       FileUtils.copyFile(configFile, tempFile);
560
561       String serviceWidgetName = ArtifactType.AAI.name()+".model-version-id."+Widget.getWidget
562           (Widget.Type.SERVICE)
563           .getName();
564       String assertMsg = ArtifactType.AAI.name() + ".model-version-id." +Widget.getWidget
565           (Widget.Type.SERVICE).getName();
566
567       //Remove property from Artifact-Generator.properties
568       properties.remove(serviceWidgetName);
569       try (OutputStream fos = new FileOutputStream(new File(configLoc))) {
570         properties.store(fos,null);
571       }
572
573       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
574       String aaiResourceBasePaths = "aai/";
575       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
576       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
577       Assert.assertEquals(false,data.getErrorData().isEmpty());
578       String errMsg = String.format(GeneratorConstants.GENERATOR_AAI_CONFIGLPROP_NOT_FOUND,assertMsg);
579       Assert.assertEquals(data.getErrorData().get("AAI").get(0),errMsg);
580     } catch (Exception e) {
581       Assert.fail(e.getMessage());
582     }
583     finally{
584       if(tempFile.exists()){
585         //Revert the changes
586         FileUtils.copyFile(tempFile, configFile);
587         loadConfigFromClasspath(properties);
588         tempFile.delete();
589       }
590     }
591   }
592
593   @Test
594   public void testErrorWhenNoFileAtConfigLocation() throws Exception  {
595     String configLoc = System.getProperty("artifactgenerator.config");
596     try {
597       System.setProperty("artifactgenerator.config",configLoc + File.separator + "testErrorWhenNoFileAtConfigLocation");
598       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
599       String aaiResourceBasePaths = "aai/";
600       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
601       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
602       Assert.assertEquals(data.getErrorData().isEmpty(),false);
603       Assert.assertEquals(data.getErrorData().get("AAI").get(0),String.format(
604           GeneratorConstants.GENERATOR_AAI_CONFIGFILE_NOT_FOUND,System.getProperty
605           ("artifactgenerator.config")));
606     } catch (Exception e) {
607       Assert.fail(e.getMessage());
608     }
609     finally{
610       System.setProperty("artifactgenerator.config",configLoc);
611     }
612   }
613
614   @Test
615   public void testErrorWhenNoServiceVersion() {
616     //  scenario service with VF anf vfmodule but no service version in additional parameter
617     try {
618       additionalParams.clear();
619       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
620       String aaiResourceBasePaths = "aai/";
621       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
622       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
623       Assert.assertEquals(data.getErrorData().isEmpty(),false);
624       Assert.assertEquals(data.getErrorData().get("AAI").get(0),
625           GeneratorConstants.GENERATOR_AAI_ERROR_MISSING_SERVICE_VERSION);
626     } catch (Exception e) {
627       Assert.fail(e.getMessage());
628     }
629     finally{
630       additionalParams.put(AdditionalParams.ServiceVersion.getName(),"1.0");
631     }
632   }
633
634   @Test
635   public void testArtifactGenerationWithServiceVersion() {
636     // Sunny day scenario service with VF anf vfmodule and service version as adiitional parameter
637     try {
638       additionalParams.put(AdditionalParams.ServiceVersion.getName(),"9.0");
639       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
640       List<ToscaTemplate> toscas = new LinkedList<>();
641       String aaiResourceBasePaths = "aai/";
642       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
643       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
644       if (data.getErrorData().isEmpty()) {
645         for (Artifact inputArtifact : inputArtifacts) {
646           toscas.add(getToscaModel(inputArtifact));
647         }
648         List<Artifact> resultData = data.getResultData();
649
650         /*for( int i = 0 ; i < resultData.size() ; i++) {
651           Artifact artifact = resultData.get(i);
652           String fileName = artifact.getName();
653           while(fileName.contains(":")){
654             fileName = fileName.replace(":","");
655           }
656           File targetFile =new File("src/test/resources/"+fileName);
657           OutputStream outStream = new FileOutputStream(targetFile);
658           outStream.write(Base64.getDecoder().decode(artifact.getPayload()));
659         }*/
660
661         Assert.assertEquals(resultData.size(),5);  //  1-service,1-VF-resource,1-vfmodule and 2
662         // others
663         Map<String, Model> outputArtifactMap = populateAAIGeneratedModelStore(resultData);
664         testServiceTosca(toscas, outputArtifactMap);
665         testResourceTosca(toscas.iterator(), outputArtifactMap);
666       } else {
667         Assert.fail("error encountered : " + data.getErrorData().get("AAI"));
668       }
669
670     } catch (Exception e) {
671       Assert.fail(e.getMessage());
672     }
673     finally{
674       additionalParams.put(AdditionalParams.ServiceVersion.getName(),"1.0");
675     }
676   }
677
678   @Test
679   public void testErrorWhenInvalidServiceVersion() {
680     //  scenario service with VF anf vfmodule but invalid service version in additional parameter
681     try {
682       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
683       String aaiResourceBasePaths = "aai/";
684       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
685
686       additionalParams.put(AdditionalParams.ServiceVersion.getName(),"1");
687       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
688       Assert.assertEquals(data.getErrorData().isEmpty(),false);
689       Assert.assertEquals(data.getErrorData().get("AAI").get(0),
690           GeneratorConstants.GENERATOR_AAI_INVALID_SERVICE_VERSION);
691
692       additionalParams.put(AdditionalParams.ServiceVersion.getName(),"0.1");
693       data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
694       Assert.assertEquals(data.getErrorData().isEmpty(),false);
695       Assert.assertEquals(data.getErrorData().get("AAI").get(0),
696           GeneratorConstants.GENERATOR_AAI_INVALID_SERVICE_VERSION);
697
698       additionalParams.put(AdditionalParams.ServiceVersion.getName(),"0.0");
699       data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
700       Assert.assertEquals(data.getErrorData().isEmpty(),false);
701       Assert.assertEquals(data.getErrorData().get("AAI").get(0),
702           GeneratorConstants.GENERATOR_AAI_INVALID_SERVICE_VERSION);
703
704     } catch (Exception e) {
705       Assert.fail(e.getMessage());
706     }
707     finally{
708       additionalParams.put(AdditionalParams.ServiceVersion.getName(),"1.0");
709     }
710   }
711
712   @Test
713   public void testMissingResourceTosca() {
714     try {
715       //Service with resource but seperate resource tosca not coming as input.
716       String aaiResourceBasePaths = "missingResourceTosca/";
717       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
718       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
719       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
720       Assert.assertEquals(data.getErrorData().get("AAI").get(0),"Cannot generate artifacts. Resource Tosca missing for resource with UUID: <b020ed1e-4bc7-4fc0-ba7e-cc7af6da7ffc>");
721     } catch (Exception e) {
722       Assert.fail(e.getMessage());
723     }
724   }
725   @Test
726   public void testMissingVLTosca() {
727     try {
728       //Service with VL but seperate VL tosca not coming as input artifact.
729       String aaiResourceBasePaths = "missingVLTosca/";
730       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
731       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
732       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
733       Assert.assertEquals(data.getErrorData().get("AAI").get(0),"Cannot generate artifacts. Resource Tosca missing for resource with UUID: <3f8fa4d2-2b86-4b36-bbc8-ffb8f9f57468>");
734     } catch (Exception e) {
735       Assert.fail(e.getMessage());
736     }
737   }
738
739   @Test
740   public void testErrorWhenNoResourceVersion() {
741     //  scenario service with VF but missing resource version in service tosca
742     try {
743       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
744       String aaiResourceBasePaths = "testErrorWhenNoResourceVersion/";
745       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
746       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
747       Assert.assertEquals(data.getErrorData().isEmpty(),false);
748       Assert.assertEquals(data.getErrorData().get("AAI").get(0),
749           "Invalid Service definition mandatory attribute version missing for resource with UUID: <b020ed1e-4bc7-4fc0-ba7e-cc7af6da7ffc>");
750     } catch (Exception e) {
751       Assert.fail(e.getMessage());
752     }
753   }
754
755   @Test
756   public void testErrorWhenInvalidResourceVersion1() {
757     //  scenario service with VF but invalid resource version 0.0 in service tosca
758     try {
759       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
760       String aaiResourceBasePaths = "testErrorWhenInvalidResourceVersion1/";
761       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
762       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
763       Assert.assertEquals(data.getErrorData().isEmpty(),false);
764       Assert.assertEquals(data.getErrorData().get("AAI").get(0),
765           "Cannot generate artifacts. Invalid Resource version in Service tosca for resource with UUID: <b020ed1e-4bc7-4fc0-ba7e-cc7af6da7ffc>");
766     } catch (Exception e) {
767       Assert.fail(e.getMessage());
768     }
769   }
770
771   @Test
772   public void testErrorWhenInvalidResourceVersion2() {
773     //  scenario service with VF but invalid resource version 1 in service tosca
774     try {
775       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
776       String aaiResourceBasePaths = "testErrorWhenInvalidResourceVersion2/";
777       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
778       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
779       Assert.assertEquals(data.getErrorData().isEmpty(),false);
780       Assert.assertEquals(data.getErrorData().get("AAI").get(0),
781           "Cannot generate artifacts. Invalid Resource version in Service tosca for resource with UUID: <b020ed1e-4bc7-4fc0-ba7e-cc7af6da7ffc>");
782     } catch (Exception e) {
783       Assert.fail(e.getMessage());
784     }
785   }
786
787   @Test
788   public void testArtifactGenerationAllottedResourceWithIpMuxAndTunnelXConn() {
789     try {
790       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
791       List<ToscaTemplate> toscas = new LinkedList<>();
792       String aaiResourceBasePaths = "testArtifactGeneration15/";
793       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
794       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
795       if (data.getErrorData().isEmpty()) {
796         for (Artifact inputArtifact : inputArtifacts) {
797           toscas.add(getToscaModel(inputArtifact));
798         }
799         List<Artifact> resultData = data.getResultData();
800
801         Assert.assertEquals(resultData.size(),5);
802         Map<String, Model> outputArtifactMap = populateAAIGeneratedModelStore(resultData);
803         testServiceTosca(toscas, outputArtifactMap);
804         testResourceTosca(toscas.iterator(), outputArtifactMap);
805       } else {
806         Assert.fail("error encountered : " + data.getErrorData().get("AAI"));
807       }
808
809     } catch (Exception e) {
810       Assert.fail(e.getMessage());
811     }
812   }
813
814 //  @Test
815   public void testErrorWhenAllottedResourceWithOutProvidingServiceId() {
816     try {
817       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
818       String aaiResourceBasePaths = "testErrorWhenAllottedResourceWithOutDependingServiceId/";
819       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
820       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
821       Assert.assertEquals(data.getErrorData().isEmpty(),false);
822       Assert.assertEquals(data.getErrorData().get("AAI").get(0),String.format
823               (GeneratorConstants.GENERATOR_AAI_PROVIDING_SERVICE_METADATA_MISSING, "707b2850-e830-4b00-9902-879f44ac05a4"));
824     } catch (Exception e) {
825       Assert.fail(e.getMessage());
826     }
827   }
828
829   @Test
830   public void testArtifactGenerationWithoutAllottedResource() {
831     try {
832       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
833       String aaiResourceBasePaths = "testArtifactGeneration16/";
834       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
835       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
836       Assert.assertEquals(data.getErrorData().isEmpty(),false);
837       Assert.assertEquals(data.getErrorData().get("AAI").get(0),String.format
838           (GeneratorConstants.GENERATOR_AAI_PROVIDING_SERVICE_MISSING, "a54a5235-b69d-4f8a-838b-d011e6783fa5"));
839     } catch (Exception e) {
840       Assert.fail(e.getMessage());
841     }
842   }
843
844   @Test
845   public void testArtifactGenerationAllottedResourceIpmuxSameInvariantDiffVersion() {
846     try {
847       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
848       List<ToscaTemplate> toscas = new LinkedList<>();
849       String aaiResourceBasePaths = "testArtifactGeneration17/";
850       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
851       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
852       if (data.getErrorData().isEmpty()) {
853         for (Artifact inputArtifact : inputArtifacts) {
854           toscas.add(getToscaModel(inputArtifact));
855         }
856         List<Artifact> resultData = data.getResultData();
857
858         Assert.assertEquals(resultData.size(),5);
859         Map<String, Model> outputArtifactMap = populateAAIGeneratedModelStore(resultData);
860         testServiceTosca(toscas, outputArtifactMap);
861         testResourceTosca(toscas.iterator(), outputArtifactMap);
862       } else {
863         Assert.fail("error encountered : " + data.getErrorData().get("AAI"));
864       }
865
866     } catch (Exception e) {
867       Assert.fail(e.getMessage());
868     }
869   }
870
871   @Test
872   public void testArtifactGenerationAllottedResourceIpmuxSameInvariantSameVersion() {
873     try {
874       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
875       List<ToscaTemplate> toscas = new LinkedList<>();
876       String aaiResourceBasePaths = "testArtifactGeneration18/";
877       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
878       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
879       if (data.getErrorData().isEmpty()) {
880         for (Artifact inputArtifact : inputArtifacts) {
881           toscas.add(getToscaModel(inputArtifact));
882         }
883         List<Artifact> resultData = data.getResultData();
884
885
886         Assert.assertEquals(resultData.size(),4);
887         Map<String, Model> outputArtifactMap = populateAAIGeneratedModelStore(resultData);
888         testServiceTosca(toscas, outputArtifactMap);
889         testResourceTosca(toscas.iterator(), outputArtifactMap);
890       } else {
891         Assert.fail("error encountered : " + data.getErrorData().get("AAI"));
892       }
893
894     } catch (Exception e) {
895       Assert.fail(e.getMessage());
896     }
897   }
898
899   @Test
900   public void testArtifactGenerationAllottedResourceIpmuxWithGroups() {
901     try {
902       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
903       List<ToscaTemplate> toscas = new LinkedList<>();
904       String aaiResourceBasePaths = "testAllotedResourceWithDependingSerWithGroups/";
905       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
906       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
907       if (data.getErrorData().isEmpty()) {
908         for (Artifact inputArtifact : inputArtifacts) {
909           toscas.add(getToscaModel(inputArtifact));
910         }
911         List<Artifact> resultData = data.getResultData();
912
913         Assert.assertEquals(resultData.size(),5);
914         Map<String, Model> outputArtifactMap = populateAAIGeneratedModelStore(resultData);
915         testServiceTosca(toscas, outputArtifactMap);
916         testResourceTosca(toscas.iterator(), outputArtifactMap);
917       } else {
918         Assert.fail("error encountered : " + data.getErrorData().get("AAI"));
919       }
920
921     } catch (Exception e) {
922       Assert.fail(e.getMessage());
923     }
924   }
925
926   @Test
927   public void testArtifactGenerationAllottedResourceWithVF() {
928     try {
929       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
930       List<ToscaTemplate> toscas = new LinkedList<>();
931       String aaiResourceBasePaths = "testArtifactGenerationAllottedResourceWithVF/";
932       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
933       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
934       if (data.getErrorData().isEmpty()) {
935         for (Artifact inputArtifact : inputArtifacts) {
936           toscas.add(getToscaModel(inputArtifact));
937         }
938         List<Artifact> resultData = data.getResultData();
939
940         Assert.assertEquals(resultData.size(),7);
941         Map<String, Model> outputArtifactMap = populateAAIGeneratedModelStore(resultData);
942         testServiceTosca(toscas, outputArtifactMap);
943         testResourceTosca(toscas.iterator(), outputArtifactMap);
944       } else {
945         Assert.fail("error encountered : " + data.getErrorData().get("AAI"));
946       }
947
948     } catch (Exception e) {
949       Assert.fail(e.getMessage());
950     }
951   }
952
953
954   //@Test
955   public void testServiceTosca(List<ToscaTemplate> toscas, Map<String, Model> outputArtifactMap) {
956     try {
957       ToscaTemplate serviceTosca = getServiceTosca(toscas);
958       if (serviceTosca == null) {
959         Assert.fail("Service Tosca not found");
960       }
961       serviceTosca.getMetadata().put("version", "1.0");
962       Service service = new Service();
963       service.populateModelIdentificationInformation(serviceTosca.getMetadata());
964       String serviceNameVersionId = service.getModelNameVersionId();
965       Model serviceAAIModel = getAAIModelByNameVersionId(serviceNameVersionId, outputArtifactMap);
966       validateServiceModelMetadata(service, serviceAAIModel);
967       //Validate Service instance base widget
968       ModelVer modelVersion =  serviceAAIModel.getModelVers().getModelVer().get(0);
969
970       List<ModelElement> matchedServiceBaseWidgetElements =
971           getModelElementbyRelationshipValue( modelVersion.getModelElements(),
972               Widget.getWidget(Widget.Type.SERVICE).getId());
973       validateMatchedModelElementsInService(matchedServiceBaseWidgetElements,
974           Widget.getWidget(Widget.Type.SERVICE).getName());
975
976       validateWidgetIds(matchedServiceBaseWidgetElements, Widget.getWidget(Widget.Type.SERVICE).getName(),
977           Widget.getWidget(Widget.Type.SERVICE).getWidgetId());
978
979       ModelElements baseServiceWidgetModelElements =
980           matchedServiceBaseWidgetElements.get(0).getModelElements();
981
982
983       Map<String, String> nodeTemplateIdTypeStore = getNodeTemplateTypeStore(serviceTosca);
984       if (nodeTemplateIdTypeStore != null) {
985         for (String key : nodeTemplateIdTypeStore.keySet()) {
986           if (nodeTemplateIdTypeStore.get(key).contains("org.openecomp.resource.vf")) {
987             List<ModelElement> matchedResourceElements =
988                 getModelElementbyRelationshipValue(baseServiceWidgetModelElements, key);
989             if (nodeTemplateIdTypeStore.get(key).contains("org.openecomp.resource.vf.allottedResource")){
990               validateMatchedModelElementsInService(matchedResourceElements,
991                   Widget.getWidget(Widget.Type.ALLOTTED_RESOURCE).getName());
992             }else {
993               validateMatchedModelElementsInService(matchedResourceElements,
994                   Widget.getWidget(Widget.Type.VF).getName());
995             }
996
997             //Validate uuid and invariantuuid are populated in model-ver.model-version-id and model.model-invariant-id
998             Assert.assertEquals(matchedResourceElements.get(0).getRelationshipList()
999                 .getRelationship().get(0)
1000                 .getRelationshipData().get(0).getRelationshipValue(),key);
1001
1002             Assert.assertEquals(matchedResourceElements.get(0).getRelationshipList().getRelationship().get(0)
1003                 .getRelationshipData().get(1).getRelationshipValue(), nodeTemplateIdTypeStore
1004                 .get(key+"-INV_UID"));
1005           } else if(nodeTemplateIdTypeStore.get(key).contains("org.openecomp.resource.vl")){
1006             //validate l3-network in service tosca
1007             List<ModelElement> matchedResourceElements =
1008                 getModelElementbyRelationshipValue(baseServiceWidgetModelElements, key);
1009             validateMatchedModelElementsInService(matchedResourceElements,
1010                 Widget.getWidget(Widget.Type.L3_NET).getName());
1011             //Validate uuid and invariantuuid are populated in model-ver.model-version-id and model.model-invariant-id
1012             Assert.assertEquals(matchedResourceElements.get(0).getRelationshipList()
1013                 .getRelationship().get(0)
1014                 .getRelationshipData().get(0).getRelationshipValue(),key);
1015
1016             Assert.assertEquals(matchedResourceElements.get(0).getRelationshipList().getRelationship().get(0)
1017                 .getRelationshipData().get(1).getRelationshipValue(), nodeTemplateIdTypeStore
1018                 .get(key+"-INV_UID"));
1019           }
1020         }
1021
1022
1023       System.out.println();
1024
1025       }
1026     } catch (IllegalArgumentException e) {
1027       Assert.fail(e.getMessage());    //Can come while populating metadata
1028     }
1029   }
1030
1031   private void validateWidgetIds(List<ModelElement> matchedServiceBaseWidgetElements,
1032                                  String widgetName, String widgetInvUuId) {
1033     Assert.assertEquals(matchedServiceBaseWidgetElements.get(0).getRelationshipList().getRelationship().get(0)
1034         .getRelationshipData().get(0).getRelationshipValue(), properties.getProperty(ArtifactType.AAI.name()
1035         + ".model-version-id."+ widgetName));
1036
1037     Assert.assertEquals(matchedServiceBaseWidgetElements.get(0).getRelationshipList().getRelationship().get(0)
1038         .getRelationshipData().get(1).getRelationshipValue(), widgetInvUuId);
1039   }
1040
1041
1042   public void testL3NetworkResourceTosca(Map<String, Model> outputArtifactMap , ToscaTemplate
1043       resourceTosca) {
1044     try {
1045       if (resourceTosca != null) {
1046         Resource resource = new Resource();
1047         resource.populateModelIdentificationInformation(resourceTosca.getMetadata());
1048         String resourceNameVersionId = resource.getModelNameVersionId();
1049         Model resourceAAIModel =
1050             getAAIModelByNameVersionId(resourceNameVersionId, outputArtifactMap);
1051         if (resourceAAIModel != null) {
1052           validateResourceModelMetadata(resource, resourceAAIModel);
1053           //Validate Resource instance base widget
1054
1055           ModelVer modelVersion = resourceAAIModel.getModelVers().getModelVer().get(0);
1056
1057           List<ModelElement> matchedVFBaseWidgetElements =
1058               getModelElementbyRelationshipValue(modelVersion.getModelElements(),
1059                   Widget.getWidget(Widget.Type.L3_NET).getId());
1060           validateMatchedModelElementsInService(matchedVFBaseWidgetElements,
1061               Widget.getWidget(Widget.Type.L3_NET).getName());
1062
1063           validateWidgetIds(matchedVFBaseWidgetElements, Widget.getWidget(Widget.Type.L3_NET).getName(),
1064               Widget.getWidget(Widget.Type.L3_NET).getWidgetId());
1065
1066         }else {
1067           System.out.println("Resource mapping not found for " + resourceNameVersionId);
1068         }
1069       }
1070
1071     }catch (IllegalArgumentException e) {
1072       Assert.fail(e.getMessage());    //Can come while populating metadata
1073     }
1074
1075   }
1076
1077   public void testAllottedResourceTosca(Map<String, Model> outputArtifactMap , ToscaTemplate
1078       resourceTosca) {
1079     try {
1080       if (resourceTosca != null) {
1081         Resource resource = new Resource();
1082         resource.populateModelIdentificationInformation(resourceTosca.getMetadata());
1083         String resourceNameVersionId = resource.getModelNameVersionId();
1084         Model resourceAAIModel =
1085             getAAIModelByNameVersionId(resourceNameVersionId, outputArtifactMap);
1086         if (resourceAAIModel != null) {
1087           validateResourceModelMetadata(resource, resourceAAIModel);
1088           //Validate Resource instance base widget
1089
1090           ModelVer modelVersion = resourceAAIModel.getModelVers().getModelVer().get(0);
1091
1092           List<ModelElement> matchedVFBaseWidgetElements =
1093               getModelElementbyRelationshipValue(modelVersion.getModelElements(),
1094                   Widget.getWidget(Widget.Type.ALLOTTED_RESOURCE).getId());
1095           validateMatchedModelElementsInService(matchedVFBaseWidgetElements,
1096               Widget.getWidget(Widget.Type.ALLOTTED_RESOURCE).getName());
1097
1098           validateWidgetIds(matchedVFBaseWidgetElements, Widget.getWidget(Widget.Type.ALLOTTED_RESOURCE).getName(),
1099               Widget.getWidget(Widget.Type.ALLOTTED_RESOURCE).getWidgetId());
1100
1101           Map<String, Object> dependingServiceDetails = getProvidingServiceDetails(resourceTosca);
1102
1103           ModelElements containedModelElements = modelVersion.getModelElements().getModelElement().
1104                   get(0).getModelElements();
1105           Assert.assertEquals( containedModelElements.getModelElement().get(0).getRelationshipList()
1106                 .getRelationship().get(0).getRelationshipData().get(0).getRelationshipValue(),
1107                 dependingServiceDetails.get("providing_service_uuid"));
1108
1109           Assert.assertEquals(containedModelElements.getModelElement().get(0).getRelationshipList()
1110                 .getRelationship().get(0).getRelationshipData().get(1).getRelationshipValue(),
1111                 dependingServiceDetails.get("providing_service_invariant_uuid"));
1112
1113           if("Allotted Resource".equals(resourceTosca.getMetadata().get("category")) &&
1114                   "Tunnel XConnect".equals(resourceTosca.getMetadata().get("subcategory"))) {
1115
1116             List<ModelElement> matchedTunnelXConnectWidgetElements =
1117                     getModelElementbyRelationshipValue(containedModelElements,
1118                             Widget.getWidget(Widget.Type.TUNNEL_XCONNECT).getId());
1119             validateMatchedModelElementsInService(matchedTunnelXConnectWidgetElements,
1120                     Widget.getWidget(Widget.Type.TUNNEL_XCONNECT).getName());
1121
1122             validateWidgetIds(matchedTunnelXConnectWidgetElements, Widget.getWidget(Widget.Type.TUNNEL_XCONNECT).getName(),
1123                     Widget.getWidget(Widget.Type.TUNNEL_XCONNECT).getWidgetId());
1124           }
1125
1126         }else {
1127           System.out.println("Resource mapping not found for " + resourceNameVersionId);
1128         }
1129       }
1130
1131     }catch (IllegalArgumentException e) {
1132       Assert.fail(e.getMessage());    //Can come while populating metadata
1133     }
1134
1135   }
1136
1137   public Map<String, Object> getProvidingServiceDetails(ToscaTemplate resourceTemplate) {
1138 Set<String> keys = resourceTemplate.getTopology_template().getNode_templates().keySet();
1139
1140 Map<String, Object> nodeProperties =null;
1141 for(String key : keys) {
1142 NodeTemplate node = resourceTemplate.getTopology_template().getNode_templates().get(key);
1143 if(node.getType().equals("org.openecomp.resource.vfc.AllottedResource")) {
1144 nodeProperties = node.getProperties();
1145   }
1146 }
1147
1148   return nodeProperties;
1149   }
1150
1151   public void testVfTosca(Map<String, Model> outputArtifactMap , ToscaTemplate resourceTosca) {
1152     try {
1153       //ToscaTemplate resourceTosca = getResourceTosca(toscas);
1154       //resourceTosca.getTopology_template().getGroups().
1155       if (resourceTosca != null) {
1156         Resource resource = new Resource();
1157         resource.populateModelIdentificationInformation(resourceTosca.getMetadata());
1158         String resourceNameVersionId = resource.getModelNameVersionId();
1159         Model resourceAAIModel =
1160             getAAIModelByNameVersionId(resourceNameVersionId, outputArtifactMap);
1161         if (resourceAAIModel != null) {
1162           validateResourceModelMetadata(resource, resourceAAIModel);
1163           //Validate Resource instance base widget
1164
1165           ModelVer modelVersion = resourceAAIModel.getModelVers().getModelVer().get(0);
1166
1167           List<ModelElement> matchedVFBaseWidgetElements =
1168               getModelElementbyRelationshipValue(modelVersion.getModelElements(),
1169                   Widget.getWidget(Widget.Type.VF).getId());
1170           validateMatchedModelElementsInService(matchedVFBaseWidgetElements,
1171               Widget.getWidget(Widget.Type.VF).getName());
1172
1173           validateWidgetIds(matchedVFBaseWidgetElements, Widget.getWidget(Widget.Type.VF).getName(),
1174                 Widget.getWidget(Widget.Type.VF).getWidgetId());
1175
1176           ModelElements baseResourceWidgetModelElements =
1177               matchedVFBaseWidgetElements.get(0).getModelElements();
1178           if (resourceTosca.getTopology_template() != null) {
1179             Map<String, String> groupIdTypeStore = getGroupsTypeStore(resourceTosca);
1180
1181             if (baseResourceWidgetModelElements.getModelElement().size() !=
1182                 groupIdTypeStore.size()) {
1183               Assert.fail("Missing VFModule in VF model.xml");
1184             }
1185
1186             for (String key : groupIdTypeStore.keySet()) {
1187
1188               List<ModelElement> matchedResourceElements =
1189                   getModelElementbyRelationshipValue(baseResourceWidgetModelElements, key);
1190               validateMatchedModelElementsInService(matchedResourceElements,
1191                   Widget.getWidget(Widget.Type.VFMODULE).getName());
1192               Model resourceAAIVFModel = getAAIModelByNameVersionId(key, outputArtifactMap);
1193               Map<String, String> vfModuleModelMetadata =
1194                   getVFModuleMetadataTosca(resourceTosca, key);
1195               Map<String, Object> vfModuleMembers = getVFModuleMembersTosca(resourceTosca, key);
1196
1197               validateVFModelMetadata(vfModuleModelMetadata, resourceAAIVFModel);
1198
1199
1200               ModelVer modelVfVersion = resourceAAIVFModel.getModelVers().getModelVer().get(0);
1201
1202               List<ModelElement> matchedVFModuleBaseWidgetElements =
1203                   getModelElementbyRelationshipValue(modelVfVersion.getModelElements(),
1204                       Widget.getWidget(Widget.Type.VFMODULE).getId());
1205               validateMatchedModelElementsInService(matchedVFModuleBaseWidgetElements,
1206                   Widget.getWidget(Widget.Type.VFMODULE).getName());
1207               validateWidgetIds(matchedVFModuleBaseWidgetElements, Widget.getWidget(Widget.Type.VFMODULE)
1208                   .getName(), Widget.getWidget(Widget.Type.VFMODULE).getWidgetId());
1209
1210               ModelElements baseResourceVFModuleWidgetModelElements =
1211                   matchedVFModuleBaseWidgetElements.get(0).getModelElements();
1212              if (vfModuleMembers.containsKey("l3-network")) {
1213                 //Validate l3
1214                 List<ModelElement> matchedL3NetworkElements =
1215                     getModelElementbyRelationshipValue(baseResourceVFModuleWidgetModelElements,
1216                         Widget.getWidget(Widget.Type.L3_NET).getId());
1217                 validateMatchedModelElementsInService(matchedL3NetworkElements,
1218                     Widget.getWidget(Widget.Type.L3_NET).getName());
1219                 validateWidgetIds(matchedL3NetworkElements, Widget.getWidget(Widget.Type.L3_NET)
1220                     .getName(), Widget.getWidget(Widget.Type.L3_NET).getWidgetId());
1221               }
1222               if (vfModuleMembers.containsKey("vserver")) {
1223                 //Validate vserver
1224                 List<ModelElement> matchedVserverElements =
1225                     getModelElementbyRelationshipValue(baseResourceVFModuleWidgetModelElements,
1226                         Widget.getWidget(Widget.Type.VSERVER).getId());
1227                 validateMatchedModelElementsInService(matchedVserverElements,
1228                     Widget.getWidget(Widget.Type.VSERVER).getName());
1229                 ModelElements vserverWidgetModelElements =
1230                     matchedVserverElements.get(0).getModelElements();
1231
1232                 validateWidgetIds(matchedVserverElements, Widget.getWidget(Widget.Type.VSERVER)
1233                     .getName(), Widget.getWidget(Widget.Type.VSERVER).getWidgetId());
1234
1235
1236                 //Validate vserver->vfc
1237                 List<ModelElement> matchedVfcElements =
1238                     getModelElementbyRelationshipValue(vserverWidgetModelElements,
1239                         Widget.getWidget(Widget.Type.VFC).getId());
1240                 validateMatchedModelElementsInService(matchedVfcElements,
1241                     Widget.getWidget(Widget.Type.VFC).getName());
1242                 validateWidgetIds(matchedVfcElements, Widget.getWidget(Widget.Type.VFC).getName(),
1243                     Widget.getWidget(Widget.Type.VFC).getWidgetId());
1244
1245                 //Validate vserver->Image
1246                 List<ModelElement> matchedImageElements =
1247                     getModelElementbyRelationshipValue(vserverWidgetModelElements,
1248                         Widget.getWidget(Widget.Type.IMAGE).getId());
1249                 validateMatchedModelElementsInService(matchedImageElements,
1250                     Widget.getWidget(Widget.Type.IMAGE).getName());
1251                 validateWidgetIds(matchedImageElements, Widget.getWidget(Widget.Type.IMAGE)
1252                     .getName(), Widget.getWidget(Widget.Type.IMAGE).getWidgetId());
1253
1254
1255                 //Validate vserver->Flavor
1256                 List<ModelElement> matchedFlavorElements =
1257                     getModelElementbyRelationshipValue(vserverWidgetModelElements,
1258                         Widget.getWidget(Widget.Type.FLAVOR).getId());
1259                 validateMatchedModelElementsInService(matchedFlavorElements,
1260                     Widget.getWidget(Widget.Type.FLAVOR).getName());
1261                 validateWidgetIds(matchedFlavorElements, Widget.getWidget(Widget.Type.FLAVOR).getName(),
1262                     Widget.getWidget(Widget.Type.FLAVOR).getWidgetId());
1263
1264                 //Validate vserver->Tenant
1265                 List<ModelElement> matchedTenantElements =
1266                     getModelElementbyRelationshipValue(vserverWidgetModelElements,
1267                         Widget.getWidget(Widget.Type.TENANT).getId());
1268                 validateMatchedModelElementsInService(matchedTenantElements,
1269                     Widget.getWidget(Widget.Type.TENANT).getName());
1270                 validateWidgetIds(matchedTenantElements, Widget.getWidget(Widget.Type.TENANT).getName(),
1271                     Widget.getWidget(Widget.Type.TENANT).getWidgetId());
1272
1273                 //Validate vserver->l-interface
1274                 if (vfModuleMembers.containsKey("l-interface")) {
1275                   List<ModelElement> matchedLinterfaceElements =
1276                       getModelElementbyRelationshipValue(vserverWidgetModelElements,
1277                           Widget.getWidget(Widget.Type.LINT).getId());
1278                   validateMatchedModelElementsInService(matchedLinterfaceElements,
1279                       Widget.getWidget(Widget.Type.LINT).getName());
1280                   validateWidgetIds(matchedLinterfaceElements, Widget.getWidget(Widget.Type.LINT).getName(),
1281                       Widget.getWidget(Widget.Type.LINT).getWidgetId());
1282                 }
1283                 //Validate vserver->volume
1284                 if (vfModuleMembers.containsKey("volume")) {
1285                   List<ModelElement> matchedVolumeElements =
1286                       getModelElementbyRelationshipValue(vserverWidgetModelElements,
1287                           Widget.getWidget(Widget.Type.VOLUME).getId());
1288                   validateMatchedModelElementsInService(matchedVolumeElements,
1289                       Widget.getWidget(Widget.Type.VOLUME).getName());
1290                   validateWidgetIds(matchedVolumeElements, Widget.getWidget(Widget.Type.VOLUME).getName(),
1291                       Widget.getWidget(Widget.Type.VOLUME).getWidgetId());
1292                 }
1293               }
1294             }
1295           }
1296         } else {
1297           System.out.println("Resource mapping not found for " + resourceNameVersionId);
1298         }
1299       }
1300
1301     } catch (IllegalArgumentException e) {
1302       Assert.fail(e.getMessage());    //Can come while populating metadata
1303     }
1304
1305   }
1306
1307   private void validateMatchedModelElementsInService(List<ModelElement> matchedModelElements,
1308                                                      String modelType) {
1309     if (matchedModelElements.isEmpty()) {
1310       Assert.fail(modelType + " not present ");
1311     }
1312     if (matchedModelElements.size() > 1) {
1313       Assert.fail("More than one " + modelType + " present ");
1314     }
1315   }
1316
1317   private Map<String, String> getNodeTemplateTypeStore(ToscaTemplate toscaTemplate) {
1318     if (toscaTemplate.getTopology_template() != null) {
1319       Map<String, NodeTemplate> nodeTemplateMap =
1320           toscaTemplate.getTopology_template().getNode_templates();
1321       Map<String, String> nodeTemplateIdTypeStore = new LinkedHashMap<>();
1322       if (nodeTemplateMap != null) {
1323         for (Map.Entry<String, NodeTemplate> e : nodeTemplateMap.entrySet()) {
1324           String uuid = e.getValue().getMetadata().get("resourceUUID");
1325           if (GeneratorUtil.isEmpty(uuid)) {
1326             uuid = e.getValue().getMetadata().get("UUID");
1327             if (GeneratorUtil.isEmpty(uuid)) {
1328               Assert.fail("UUID Not found");
1329             }
1330           }
1331           if(e.getValue().getType().contains("org.openecomp.resource.vf.")&& (e.getValue()
1332               .getMetadata().get("category").equals("Allotted Resource")))
1333           {
1334             e.getValue().setType("org.openecomp.resource.vf.allottedResource");
1335           }
1336           nodeTemplateIdTypeStore.put(uuid, e.getValue().getType());
1337           resourcesVersion.put(uuid,e.getValue().getMetadata().get
1338               ("version"));
1339           //Populate invraintUuId for V9
1340           String invUuId = e.getValue().getMetadata().get("invariantUUID");
1341           nodeTemplateIdTypeStore.put(uuid+"-INV_UID" , invUuId);
1342         }
1343       }
1344       return nodeTemplateIdTypeStore;
1345     } else {
1346       return null;
1347     }
1348   }
1349
1350   private Map<String, String> getGroupsTypeStore(ToscaTemplate toscaTemplate) {
1351     if (toscaTemplate.getTopology_template() != null) {
1352       Map<String, GroupDefinition> groupDefinitionMap =
1353           toscaTemplate.getTopology_template().getGroups();
1354       Map<String, String> groupDefinitionIdTypeStore = new LinkedHashMap<>();
1355       if (groupDefinitionMap != null) {
1356         for (Map.Entry<String, GroupDefinition> e : groupDefinitionMap.entrySet()) {
1357           if (e.getValue().getType().contains("org.openecomp.groups.VfModule")) {
1358             String uuid = e.getValue().getMetadata().get("vfModuleModelUUID");
1359             if (GeneratorUtil.isEmpty(uuid)) {
1360               uuid = e.getValue().getMetadata().get("UUID");
1361               if (GeneratorUtil.isEmpty(uuid)) {
1362                 Assert.fail("UUID Not found");
1363               }
1364             }
1365             groupDefinitionIdTypeStore.put(uuid, e.getValue().getType());
1366           }
1367         }
1368       }
1369       return groupDefinitionIdTypeStore;
1370     } else {
1371       return null;
1372     }
1373
1374   }
1375
1376   private void validateServiceModelMetadata(Service serviceToscaModel, Model generatedAAIModel) {
1377     ModelVer modelVersion =  generatedAAIModel.getModelVers().getModelVer().get(0);
1378     Assert.assertEquals(serviceToscaModel.getModelNameVersionId(),
1379         modelVersion.getModelVersionId());
1380     Assert.assertEquals(serviceToscaModel.getModelId(), generatedAAIModel.getModelInvariantId());
1381     Assert.assertEquals(serviceToscaModel.getModelName(), modelVersion.getModelName());
1382     Assert.assertEquals(additionalParams.get(AdditionalParams.ServiceVersion.getName()), modelVersion
1383         .getModelVersion());
1384     Assert.assertEquals(serviceToscaModel.getModelDescription(),
1385         modelVersion.getModelDescription());
1386
1387   }
1388
1389   private void validateResourceModelMetadata(Resource resouerceToscaModel,
1390                                              Model generatedAAIModel) {
1391     ModelVer modelVersion =  generatedAAIModel.getModelVers().getModelVer().get(0);
1392     Assert.assertEquals(resouerceToscaModel.getModelNameVersionId(),
1393         modelVersion.getModelVersionId());
1394     Assert.assertEquals(resouerceToscaModel.getModelId(), generatedAAIModel.getModelInvariantId());
1395     Assert.assertEquals(resouerceToscaModel.getModelName(), modelVersion.getModelName());
1396     Assert
1397         .assertEquals(resouerceToscaModel.getModelVersion(), modelVersion.getModelVersion());
1398     Assert.assertEquals(resouerceToscaModel.getModelDescription(),
1399         modelVersion.getModelDescription());
1400
1401   }
1402
1403   private void validateVFModelMetadata(Map<String, String> vfModuleModelMetadata,
1404                                        Model generatedAAIModel) {
1405     ModelVer modelVersion =  generatedAAIModel.getModelVers().getModelVer().get(0);
1406     Assert.assertEquals(vfModuleModelMetadata.get("vfModuleModelUUID"),
1407         modelVersion.getModelVersionId());
1408     Assert.assertEquals(vfModuleModelMetadata.get("vfModuleModelInvariantUUID"),
1409         generatedAAIModel.getModelInvariantId());
1410     Assert.assertEquals(vfModuleModelMetadata.get("vfModuleModelName"),
1411         modelVersion.getModelName());
1412     Assert.assertEquals(vfModuleModelMetadata.get("vfModuleModelVersion"),
1413         modelVersion.getModelVersion());
1414     Assert.assertEquals(vfModuleModelMetadata.get("vf_module_description"),
1415         modelVersion.getModelDescription());
1416   }
1417
1418   private Model getAAIModelByNameVersionId(String nameVersionId,
1419                                            Map<String, Model> outputArtifactMap) {
1420     return outputArtifactMap.get(nameVersionId);
1421   }
1422
1423   private List<ModelElement> getModelElementbyRelationshipValue(ModelElements modelElements,
1424                                                                 String relationshipValue) {
1425     List<ModelElement> matchedModelElements = new ArrayList<>();
1426     if (modelElements != null) {
1427       List<ModelElement> modelElementList = modelElements.getModelElement();
1428       for (ModelElement element : modelElementList) {
1429         List<Relationship> relationshipList = element.getRelationshipList().getRelationship();
1430         for (Relationship r : relationshipList) {
1431           List<RelationshipData> relationshipDataList = r.getRelationshipData();
1432           for (RelationshipData relationshipData : relationshipDataList) {
1433             if (relationshipData.getRelationshipValue().equals(relationshipValue)) {
1434               matchedModelElements.add(element);
1435             }
1436           }
1437         }
1438       }
1439     }
1440     return matchedModelElements;
1441   }
1442
1443   private Map<String, Model> populateAAIGeneratedModelStore(List<Artifact> resultData) throws IOException {
1444     Map<String, Model> outputArtifactMap = new HashMap<>();
1445     for (Artifact outputArtifact : resultData) {
1446       if (outputArtifact.getType().equals(ArtifactType.MODEL_INVENTORY_PROFILE.name())) {
1447         byte[] decodedPayload = GeneratorUtil.decoder(outputArtifact.getPayload());
1448         Model aaiModel = getUnmarshalledArtifactModel(new String(decodedPayload));
1449         List<ModelVer> modelVersions =  aaiModel.getModelVers().getModelVer();
1450         outputArtifactMap.put(modelVersions.get(0).getModelVersionId(), aaiModel);
1451       }
1452     }
1453     return outputArtifactMap;
1454   }
1455
1456   private Model getUnmarshalledArtifactModel(String aaiModel) throws IOException {
1457
1458     try {
1459
1460       JAXBContext jaxbContext = JAXBContext.newInstance(Model.class);
1461       Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
1462
1463       try (InputStream aaiModelStream = new ByteArrayInputStream(aaiModel.getBytes())) {
1464         return (Model) unmarshaller.unmarshal(aaiModelStream);
1465       }
1466
1467     } catch (JAXBException e) {
1468       e.printStackTrace();
1469     }
1470
1471     return null;
1472   }
1473
1474   /**
1475    * Get the tosca java model from the tosca input artifact
1476    *
1477    * @param input Input tosca file and its metadata information as {@link Artifact} object
1478    * @return Translated {@link ToscaTemplate tosca} object
1479    */
1480   private ToscaTemplate getToscaModel(Artifact input) throws SecurityException {
1481     byte[] decodedInput = GeneratorUtil.decoder(input.getPayload());
1482     String checksum = GeneratorUtil.checkSum(decodedInput);
1483     if (checksum.equals(input.getChecksum())) {
1484       try {
1485         return GeneratorUtil.translateTosca(new String(decodedInput), ToscaTemplate.class);
1486       } catch (Exception e) {
1487         e.printStackTrace();
1488         throw new IllegalArgumentException(
1489             String.format(GeneratorConstants.GENERATOR_AAI_ERROR_INVALID_TOSCA, input.getName()));
1490       }
1491     } else {
1492       throw new SecurityException(
1493           String.format(GeneratorConstants.GENERATOR_AAI_ERROR_CHECKSUM_MISMATCH, input.getName()));
1494     }
1495   }
1496
1497   /**
1498    * Identify the service tosca artifact from the list of translated tosca inputs
1499    *
1500    * @param input List of translated {@link ToscaTemplate tosca} object models
1501    * @return Identified service {@link ToscaTemplate tosca}
1502    */
1503   private ToscaTemplate getServiceTosca(List<ToscaTemplate> input) {
1504     Iterator<ToscaTemplate> iter = input.iterator();
1505     while (iter.hasNext()) {
1506       ToscaTemplate tosca = iter.next();
1507       if (tosca.isService()) {
1508         iter.remove();
1509         return tosca;
1510       }
1511     }
1512     return null;
1513   }
1514
1515   private Map<String, String> getVFModuleMetadataTosca(ToscaTemplate toscaTemplate,
1516                                                        String vfModuleModelUUID) {
1517     Map<String, GroupDefinition> groupDefinitionMap =
1518         toscaTemplate.getTopology_template().getGroups();
1519     Map<String, String> vfModuleModelMetadata = new LinkedHashMap<>();
1520     for (Map.Entry<String, GroupDefinition> e : groupDefinitionMap.entrySet()) {
1521       if (e.getValue().getType().contains("org.openecomp.groups.VfModule")) {
1522         String uuid = e.getValue().getMetadata().get("vfModuleModelUUID");
1523         if (Objects.equals(uuid, vfModuleModelUUID)) {
1524           vfModuleModelMetadata = e.getValue().getMetadata();
1525           vfModuleModelMetadata.put("vf_module_description",
1526               (String) e.getValue().getProperties().get("vf_module_description"));
1527         }
1528       }
1529     }
1530     return vfModuleModelMetadata;
1531   }
1532
1533   private Map<String, Object> getVFModuleMembersTosca(ToscaTemplate toscaTemplate,
1534                                                       String vfModuleModelUUID) {
1535     Map<String, GroupDefinition> groupDefinitionMap =
1536         toscaTemplate.getTopology_template().getGroups();
1537     Map<String, NodeTemplate> nodeTemplateMaps =
1538         toscaTemplate.getTopology_template().getNode_templates();
1539     Map<String, Object> vfModuleMembers = new LinkedHashMap<>();
1540     List<String> vfModuleModelMetadata;
1541     for (Map.Entry<String, GroupDefinition> e : groupDefinitionMap.entrySet()) {
1542       if (e.getValue().getType().contains("org.openecomp.groups.VfModule")) {
1543         String uuid = e.getValue().getMetadata().get("vfModuleModelUUID");
1544         if (Objects.equals(uuid, vfModuleModelUUID)) {
1545           vfModuleModelMetadata = e.getValue().getMembers();
1546           if (vfModuleModelMetadata !=null) {
1547             for (Object key : vfModuleModelMetadata) {
1548               NodeTemplate nodeTemplate = nodeTemplateMaps.get(key);
1549               String nodetype = null;
1550               if (nodeTemplate != null) {
1551                 nodetype = nodeTemplate.getType();
1552               }
1553               if (nodetype != null) {
1554                 String widgetType = membersType(nodetype);
1555                 if (widgetType != null) {
1556                   vfModuleMembers.put(widgetType, key);
1557                 }
1558               }
1559             }
1560           }
1561         }
1562       }
1563     }
1564
1565     return vfModuleMembers;
1566   }
1567
1568
1569   private String membersType(String toscaType) {
1570     String modelToBeReturned = null;
1571     while (toscaType != null && toscaType.lastIndexOf(".") != -1 && modelToBeReturned == null) {
1572
1573       switch (toscaType) {
1574         case "org.openecomp.resource.vf.allottedResource":
1575           modelToBeReturned = "allotted-resource";
1576           break;
1577         case "org.openecomp.resource.vfc":
1578           modelToBeReturned = "vserver";
1579           break;
1580         case "org.openecomp.resource.cp":
1581         case "org.openecomp.cp":
1582           modelToBeReturned = "l-interface";
1583           break;
1584         case "org.openecomp.resource.vl":
1585           modelToBeReturned = "l3-network";
1586           break;
1587         case "org.openecomp.resource.vf":
1588           modelToBeReturned = "generic-vnf";
1589           break;
1590         case "org.openecomp.groups.VfModule":
1591           modelToBeReturned = "vf-module";
1592           break;
1593         case "org.openecomp.resource.vfc.nodes.heat.cinder":
1594           modelToBeReturned = "volume";
1595           break;
1596         default:
1597           modelToBeReturned = null;
1598           break;
1599       }
1600
1601       toscaType = toscaType.substring(0, toscaType.lastIndexOf("."));
1602     }
1603     return modelToBeReturned;
1604   }
1605
1606   private List<Artifact> init(String aaiResourceBasePaths) {
1607     List<Artifact> inputArtifacts1 = new ArrayList<>();
1608     try {
1609
1610       String[] resourceFileList = {};
1611       URL resourceDirUrl = this.getClass().getClassLoader().getResource(aaiResourceBasePaths);
1612       if (resourceDirUrl != null && resourceDirUrl.getProtocol().equals("file")) {
1613         resourceFileList = new File(resourceDirUrl.toURI()).list();
1614       } else {
1615         Assert.fail("Invalid resource directory");
1616       }
1617
1618       for (String aResourceFileList : resourceFileList) {
1619         File resourceFile = new File(
1620                 this.getClass().getClassLoader().getResource(aaiResourceBasePaths + aResourceFileList)
1621                         .getPath());
1622
1623         //convert service tosca file into array of bytes
1624         byte[] payload = new byte[(int) resourceFile.length()];
1625         try (FileInputStream fileInputStream = new FileInputStream(resourceFile)) {
1626           fileInputStream.read(payload);
1627         }
1628
1629         String checksum = GeneratorUtil.checkSum(payload);
1630         byte[] encodedPayload = GeneratorUtil.encode(payload);
1631         Artifact artifact =
1632                 new Artifact(aaiArtifactType, aaiArtifactGroupType, checksum, encodedPayload);
1633         artifact.setName(aResourceFileList);
1634         artifact.setLabel(aResourceFileList);
1635         artifact.setDescription(aResourceFileList);
1636         artifact.setVersion("1.0");
1637         inputArtifacts1.add(artifact);
1638
1639       }
1640     } catch (Exception e) {
1641       //e.printStackTrace();
1642       Assert.fail(e.getMessage());
1643     }
1644     return inputArtifacts1;
1645   }
1646
1647   private void loadConfigFromClasspath(Properties properties) throws IOException {
1648     String configLocation = System.getProperty("artifactgenerator.config");
1649     if (configLocation != null) {
1650       File file = new File(configLocation);
1651       if (file.exists()) {
1652         properties.load(new FileInputStream(file));
1653       }
1654     }
1655   }
1656
1657   public  void testResourceTosca(Iterator<ToscaTemplate> itr, Map<String, Model>
1658       outputArtifactMap) {
1659     while(itr.hasNext()){
1660       ToscaTemplate toscaTemplate = itr.next();
1661       String resourceVersion=resourcesVersion.get(toscaTemplate.getMetadata().get("UUID"));
1662       toscaTemplate.getMetadata().put("version", resourceVersion);
1663       if("VF".equals(toscaTemplate.getMetadata().get("type")) && !("Allotted Resource".equals
1664           (toscaTemplate.getMetadata().get("category"))) ){
1665         testVfTosca(outputArtifactMap, toscaTemplate);
1666       } else if("VF".equals(toscaTemplate.getMetadata().get("type")) && ("Allotted Resource".equals
1667           (toscaTemplate.getMetadata().get("category"))) ){
1668         testAllottedResourceTosca(outputArtifactMap, toscaTemplate);
1669       } else if("VL".equals(toscaTemplate.getMetadata().get("type"))){
1670         testL3NetworkResourceTosca(outputArtifactMap, toscaTemplate);
1671       }
1672     }
1673   }
1674
1675   private void readPayloadFromResource(List<Artifact> inputArtifacts, String file) throws IOException {
1676     try (InputStream stream = ArtifactGenerationServiceTest.class.getResourceAsStream("/" + file)) {
1677       readPayload(inputArtifacts, stream, file);
1678     }
1679   }
1680 }