3f4e720a0620c2052a606d73362890d3c181c327
[sdc/sdc-tosca.git] / sdc-tosca / src / test / java / org / onap / sdc / impl / GetEntityPortMirroringTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * sdc-tosca
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 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.impl;
22
23 import org.junit.jupiter.api.BeforeAll;
24 import org.junit.jupiter.api.Test;
25 import org.onap.sdc.tosca.parser.api.IEntityDetails;
26 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
27 import org.onap.sdc.tosca.parser.elements.queries.EntityQuery;
28 import org.onap.sdc.tosca.parser.elements.queries.TopologyTemplateQuery;
29 import org.onap.sdc.tosca.parser.enums.SdcTypes;
30 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
31 import org.onap.sdc.tosca.parser.impl.SdcPropertyNames;
32 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
33 import org.onap.sdc.toscaparser.api.CapabilityAssignment;
34 import org.onap.sdc.toscaparser.api.RequirementAssignment;
35
36 import java.net.URL;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.stream.Collectors;
40
41 import static org.junit.jupiter.api.Assertions.assertEquals;
42 import static org.junit.jupiter.api.Assertions.assertTrue;
43
44 public class GetEntityPortMirroringTest {
45
46     private final static String cap0 = "vepdg5afn0.vlb_1.port_mirroring_vlb_gn_vepdg_fn_5_RVMI";
47     private final static String cap1 = "radcomfnvlbavf11_3_3vepdg0.abstract_vlbagentbase_eph_aff.port_mirroring_vlbagentbase_eph_aff_vlbagentbase_eph_aff_int_pktmirror_1_port";
48     private static ISdcCsarHelper helper = null;
49
50     @BeforeAll
51     public static void setUpClass() {
52         try {
53             URL resource = GetEntityPortMirroringTest.class.getClassLoader()
54                     .getResource("csars/service-JennnyVepdgPortMirroringTest-csar.csar");
55             if (resource != null) {
56                 helper = SdcToscaParserFactory.getInstance().getSdcCsarHelper(resource.getFile());
57             }
58
59         } catch (SdcToscaParserException e) {
60             e.printStackTrace();
61         }
62     }
63
64     @Test
65     public void getServiceConfigurations() {
66         EntityQuery entityQuery = EntityQuery.newBuilder(SdcTypes.CONFIGURATION)
67                 .build();
68         TopologyTemplateQuery topologyTemplateQuery = TopologyTemplateQuery.newBuilder(SdcTypes.SERVICE)
69                 .build();
70
71         List<IEntityDetails> entities = helper.getEntity(entityQuery, topologyTemplateQuery, false);
72
73         assertEquals(1, entities.size());
74         assertEquals("54308d8b-21ca-40a1-bd3e-efde64791605", entities.get(0).getMetadata().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
75         assertEquals("Port Mirroring Configuration", entities.get(0).getMetadata().getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
76         assertEquals(2, entities.get(0).getRequirements().size());
77         assertTrue(entities.get(0).getProperties().isEmpty());
78         assertEquals(1, entities.get(0).getCapabilities().size());
79     }
80
81     @Test
82     public void getServiceProxyOnService() {
83         EntityQuery entityQuery = EntityQuery.newBuilder(SdcTypes.SERVICE_PROXY)
84                 .build();
85         TopologyTemplateQuery topologyTemplateQuery = TopologyTemplateQuery.newBuilder(SdcTypes.SERVICE)
86                 .build();
87
88         List<IEntityDetails> entities = helper.getEntity(entityQuery, topologyTemplateQuery, false);
89
90         assertEquals(2, entities.size());
91         assertTrue("vEPDG-5A-FN SVC Service Proxy".equals(entities.get(0).getMetadata().getValue(SdcPropertyNames.PROPERTY_NAME_NAME)) ||
92                         "vEPDG-5A-FN SVC Service Proxy".equals(entities.get(1).getMetadata().getValue(SdcPropertyNames.PROPERTY_NAME_NAME)));
93         assertTrue("Radcom FN vLBA SVC 11_3_3 vEPDG Service Proxy".equals(entities.get(0).getMetadata().getValue(SdcPropertyNames.PROPERTY_NAME_NAME)) ||
94                 "Radcom FN vLBA SVC 11_3_3 vEPDG Service Proxy".equals(entities.get(1).getMetadata().getValue(SdcPropertyNames.PROPERTY_NAME_NAME)));
95
96         assertTrue(entities.get(0).getRequirements().isEmpty());
97         assertTrue(entities.get(0).getProperties().isEmpty());
98         assertEquals(940, entities.get(0).getCapabilities().size());
99         assertEquals(197, entities.get(1).getCapabilities().size());
100     }
101
102     @Test
103     public void getCapabilityForCOnfigurationRequirementOnService() {
104         EntityQuery entityQueryC = EntityQuery.newBuilder(SdcTypes.CONFIGURATION)
105                 .build();
106
107         EntityQuery entityQueryS = EntityQuery.newBuilder(SdcTypes.SERVICE_PROXY)
108                 .build();
109         TopologyTemplateQuery topologyTemplateQuery = TopologyTemplateQuery.newBuilder(SdcTypes.SERVICE)
110                 .build();
111
112         List<IEntityDetails> configurationNodes = helper.getEntity(entityQueryC, topologyTemplateQuery, false);
113         List<IEntityDetails> proxyNodes = helper.getEntity(entityQueryS, topologyTemplateQuery, false);
114
115         assertEquals(2, proxyNodes.size());
116         assertEquals(1, configurationNodes.size());
117
118         List<RequirementAssignment> reqMap = configurationNodes.get(0).getRequirements();
119         assertEquals(cap0, reqMap.stream().filter(f -> f.getName().equals("source")).findAny().get().getCapabilityName());
120         assertEquals(cap1, reqMap.stream().filter(f -> f.getName().equals("collector")).findAny().get().getCapabilityName());
121
122         assertTrue(isCapabilityFound(proxyNodes.get(0), cap0));
123         assertTrue(isCapabilityFound(proxyNodes.get(1), cap1));
124     }
125
126     private boolean isCapabilityFound(IEntityDetails nodeDetails, String capName) {
127         List<CapabilityAssignment> assignments = nodeDetails.getCapabilities();
128         CapabilityAssignment capabilityAssignment = assignments.stream().filter(f -> f.getDefinition().getName().equals(capName)).findAny().orElse(null);
129         return capabilityAssignment != null;
130     }
131 }