11bce326bb075e43a13496be77c24ee1acda93e5
[policy/distribution.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Intel. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.distribution.reception.decoding.pdpx;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.fail;
25
26 import com.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
28
29 import java.io.IOException;
30 import java.util.Collection;
31
32 import org.junit.Test;
33 import org.onap.policy.distribution.model.Csar;
34
35 /**
36  * Class to perform unit test of {@link PolicyDecoderCsarPdpx}.
37  *
38  */
39 public class TestPolicyDecoderCsarPdpx {
40
41     @Test
42     public void testHpaPolicy2Vnf() throws IOException {
43         Csar csar = new Csar("src/test/resources/service-TestNs8-csar.csar");
44         PolicyDecoderCsarPdpx policyDecoderCsarPdpx = new PolicyDecoderCsarPdpx();
45         try {
46             Collection<PdpxPolicy> ret = policyDecoderCsarPdpx.decode(csar);
47             assertEquals(2, ret.size());
48             PdpxPolicy pdpxPolicy = (PdpxPolicy) ret.toArray()[0];
49             assertEquals("Optimization", pdpxPolicy.getContent().getPolicyType());
50             assertEquals(1, pdpxPolicy.getContent().getFlavorFeatures().size());
51
52             FlavorFeature flavorFeature = pdpxPolicy.getContent().getFlavorFeatures().get(0);
53             assertEquals("vdu_vnf_1", flavorFeature.getId());
54             assertEquals("tosca.node.nfv.Vdu.Compute", flavorFeature.getType());
55             assertEquals(1, flavorFeature.getDirectives().size());
56             Directive directive = flavorFeature.getDirectives().get(0);
57             assertEquals("flavor_directive", directive.getType());            
58             assertEquals(1, directive.getAttributes().size());
59             assertEquals("flavorName", directive.getAttributes().get(0).getAttributeName());
60             assertEquals("", directive.getAttributes().get(0).getAttributeValue());
61             assertEquals(2, flavorFeature.getFlavorProperties().size());
62             FlavorProperty flavorProperty = flavorFeature.getFlavorProperties().get(0);
63             assertEquals("BasicCapabilities", flavorProperty.getHpaFeature());
64             assertEquals("true", flavorProperty.getMandatory());
65             assertEquals("generic", flavorProperty.getArchitecture());
66             assertEquals("v1", flavorProperty.getHpaVersion());
67             assertEquals(0, flavorProperty.getDirectives().size());
68             assertEquals(1, flavorProperty.getHpaFeatureAttributes().size());
69             HpaFeatureAttribute hpaFeatreAttribute = flavorProperty.getHpaFeatureAttributes().get(0);
70             assertEquals("virtualMemSize",hpaFeatreAttribute.getHpaAttributeKey());
71             assertEquals("4096",hpaFeatreAttribute.getHpaAttributeValue());
72             assertEquals("",hpaFeatreAttribute.getOperator());
73             assertEquals("MB",hpaFeatreAttribute.getUnit());
74             
75         } catch (Exception e) {
76             fail("test should not thrown an exception here: " + e.getMessage());
77         }
78     }
79
80     @Test
81     public void testHpaPolicySriov() throws IOException {
82         Csar csar = new Csar("src/test/resources/hpaPolicySriov.csar");
83         PolicyDecoderCsarPdpx policyDecoderCsarPdpx = new PolicyDecoderCsarPdpx();
84         try {
85             Collection<PdpxPolicy> ret = policyDecoderCsarPdpx.decode(csar);
86             assertEquals(2, ret.size());
87             PdpxPolicy pdpxPolicy = (PdpxPolicy) ret.toArray()[0];
88             assertEquals("Optimization", pdpxPolicy.getContent().getPolicyType());
89             assertEquals(1, pdpxPolicy.getContent().getFlavorFeatures().size());
90
91             FlavorFeature flavorFeature = pdpxPolicy.getContent().getFlavorFeatures().get(0);
92             assertEquals("vdu_vnf_1", flavorFeature.getId());
93             assertEquals("tosca.node.nfv.Vdu.Compute", flavorFeature.getType());
94             assertEquals(1, flavorFeature.getDirectives().size());
95             Directive directive = flavorFeature.getDirectives().get(0);
96             assertEquals("flavor_directive", directive.getType());            
97             assertEquals(1, directive.getAttributes().size());
98             assertEquals("flavorName", directive.getAttributes().get(0).getAttributeName());
99             assertEquals("", directive.getAttributes().get(0).getAttributeValue());
100             assertEquals(4, flavorFeature.getFlavorProperties().size());
101             FlavorProperty flavorProperty = flavorFeature.getFlavorProperties().get(3);
102             assertEquals("SriovNICNetwork", flavorProperty.getHpaFeature());
103             assertEquals("true", flavorProperty.getMandatory());
104             assertEquals("generic", flavorProperty.getArchitecture());
105             assertEquals("v1", flavorProperty.getHpaVersion());
106             assertEquals(0, flavorProperty.getDirectives().size());
107             assertEquals(3, flavorProperty.getHpaFeatureAttributes().size());
108
109             HpaFeatureAttribute pciVendorId = flavorProperty.getHpaFeatureAttributes().get(0);
110             assertEquals("pciVendorId",pciVendorId.getHpaAttributeKey());
111             assertEquals("1234",pciVendorId.getHpaAttributeValue());
112             assertEquals("",pciVendorId.getOperator());
113             assertEquals("",pciVendorId.getUnit());
114             HpaFeatureAttribute pciDeviceId = flavorProperty.getHpaFeatureAttributes().get(1);
115             assertEquals("pciDeviceId",pciDeviceId.getHpaAttributeKey());
116             assertEquals("5678",pciDeviceId.getHpaAttributeValue());
117             assertEquals("",pciDeviceId.getOperator());
118             assertEquals("",pciDeviceId.getUnit());
119             HpaFeatureAttribute pciNumDevices = flavorProperty.getHpaFeatureAttributes().get(2);
120             assertEquals("pciNumDevices",pciNumDevices.getHpaAttributeKey());
121             assertEquals("1",pciNumDevices.getHpaAttributeValue());
122             assertEquals("",pciNumDevices.getOperator());
123             assertEquals("",pciNumDevices.getUnit());
124         } catch (Exception e) {
125             fail("test should not thrown an exception here: " + e.getMessage());
126         }
127     }
128
129     @Test
130     public void testHpaPolicyPciePassthrough() throws IOException {
131         Csar csar = new Csar("src/test/resources/hpaPolicyPciePassthrough.csar");
132         PolicyDecoderCsarPdpx policyDecoderCsarPdpx = new PolicyDecoderCsarPdpx();
133         try {
134             Collection<PdpxPolicy> ret = policyDecoderCsarPdpx.decode(csar);
135             assertEquals(2, ret.size());
136             PdpxPolicy pdpxPolicy = (PdpxPolicy) ret.toArray()[0];
137             assertEquals("Optimization", pdpxPolicy.getContent().getPolicyType());
138             assertEquals(1, pdpxPolicy.getContent().getFlavorFeatures().size());
139
140             FlavorFeature flavorFeature = pdpxPolicy.getContent().getFlavorFeatures().get(0);
141             assertEquals("vdu_vnf_1", flavorFeature.getId());
142             assertEquals("tosca.node.nfv.Vdu.Compute", flavorFeature.getType());
143             assertEquals(1, flavorFeature.getDirectives().size());
144             Directive directive = flavorFeature.getDirectives().get(0);
145             assertEquals("flavor_directive", directive.getType());            
146             assertEquals(1, directive.getAttributes().size());
147             assertEquals("flavorName", directive.getAttributes().get(0).getAttributeName());
148             assertEquals("", directive.getAttributes().get(0).getAttributeValue());
149             assertEquals(4, flavorFeature.getFlavorProperties().size());
150             FlavorProperty flavorProperty = flavorFeature.getFlavorProperties().get(3);
151             assertEquals("pciePassthrough", flavorProperty.getHpaFeature());
152             assertEquals("true", flavorProperty.getMandatory());
153             assertEquals("generic", flavorProperty.getArchitecture());
154             assertEquals("v1", flavorProperty.getHpaVersion());
155             assertEquals(0, flavorProperty.getDirectives().size());
156             assertEquals(3, flavorProperty.getHpaFeatureAttributes().size());
157
158             HpaFeatureAttribute pciVendorId = flavorProperty.getHpaFeatureAttributes().get(0);
159             assertEquals("pciVendorId",pciVendorId.getHpaAttributeKey());
160             assertEquals("1234",pciVendorId.getHpaAttributeValue());
161             assertEquals("",pciVendorId.getOperator());
162             assertEquals("",pciVendorId.getUnit());
163             HpaFeatureAttribute pciDeviceId = flavorProperty.getHpaFeatureAttributes().get(1);
164             assertEquals("pciDeviceId",pciDeviceId.getHpaAttributeKey());
165             assertEquals("5678",pciDeviceId.getHpaAttributeValue());
166             assertEquals("",pciDeviceId.getOperator());
167             assertEquals("",pciDeviceId.getUnit());
168             HpaFeatureAttribute pciNumDevices = flavorProperty.getHpaFeatureAttributes().get(2);
169             assertEquals("pciNumDevices",pciNumDevices.getHpaAttributeKey());
170             assertEquals("1",pciNumDevices.getHpaAttributeValue());
171             assertEquals("",pciNumDevices.getOperator());
172             assertEquals("",pciNumDevices.getUnit());
173         } catch (Exception e) {
174             fail("test should not thrown an exception here: " + e.getMessage());
175         }
176     }
177
178     @Test
179     public void testHpaPolicyHugePage() throws IOException {
180         Csar csar = new Csar("src/test/resources/hpaPolicyHugePage.csar");
181         PolicyDecoderCsarPdpx policyDecoderCsarPdpx = new PolicyDecoderCsarPdpx();
182         try {
183             Collection<PdpxPolicy> ret = policyDecoderCsarPdpx.decode(csar);
184             assertEquals(2, ret.size());
185             PdpxPolicy pdpxPolicy = (PdpxPolicy) ret.toArray()[0];
186             assertEquals("Optimization", pdpxPolicy.getContent().getPolicyType());
187             assertEquals(1, pdpxPolicy.getContent().getFlavorFeatures().size());
188
189             FlavorFeature flavorFeature = pdpxPolicy.getContent().getFlavorFeatures().get(0);
190             assertEquals("vdu_vnf_1", flavorFeature.getId());
191             assertEquals("tosca.node.nfv.Vdu.Compute", flavorFeature.getType());
192             assertEquals(1, flavorFeature.getDirectives().size());
193             Directive directive = flavorFeature.getDirectives().get(0);
194             assertEquals("flavor_directive", directive.getType());            
195             assertEquals(1, directive.getAttributes().size());
196             assertEquals("flavorName", directive.getAttributes().get(0).getAttributeName());
197             assertEquals("", directive.getAttributes().get(0).getAttributeValue());
198             assertEquals(3, flavorFeature.getFlavorProperties().size());
199             FlavorProperty flavorProperty = flavorFeature.getFlavorProperties().get(2);
200             assertEquals("hugePages", flavorProperty.getHpaFeature());
201             assertEquals("false", flavorProperty.getMandatory());
202             assertEquals("generic", flavorProperty.getArchitecture());
203             assertEquals("v1", flavorProperty.getHpaVersion());
204             assertEquals(0, flavorProperty.getDirectives().size());
205             assertEquals(1, flavorProperty.getHpaFeatureAttributes().size());
206             HpaFeatureAttribute hpaFeatreAttribute = flavorProperty.getHpaFeatureAttributes().get(0);
207             assertEquals("memoryPageSize",hpaFeatreAttribute.getHpaAttributeKey());
208             assertEquals("2",hpaFeatreAttribute.getHpaAttributeValue());
209             assertEquals("",hpaFeatreAttribute.getOperator());
210             assertEquals("MB",hpaFeatreAttribute.getUnit());
211         } catch (Exception e) {
212             fail("test should not thrown an exception here: " + e.getMessage());
213         }
214     }
215 }