Added range type to subject-feature-definition/parameter
[groupbasedpolicy.git] / renderers / opflex / src / test / java / org / opendaylight / groupbasedpolicy / renderer / opflex / mit / MitLibTest.java
1 /*
2  * Copyright (C) 2014 Cisco Systems, Inc.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  *
8  *  Authors : tbachman
9  */
10 package org.opendaylight.groupbasedpolicy.renderer.opflex.mit;
11
12 import static org.junit.Assert.assertTrue;
13
14 import java.math.BigInteger;
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.opendaylight.groupbasedpolicy.renderer.opflex.lib.messages.ManagedObject;
24 import org.opendaylight.groupbasedpolicy.renderer.opflex.lib.messages.ManagedObject.Property;
25 import org.opendaylight.groupbasedpolicy.renderer.opflex.mit.EnumInfo.EnumInfoBuilder;
26 import org.opendaylight.groupbasedpolicy.renderer.opflex.mit.PolicyClassInfo.PolicyClassInfoBuilder;
27 import org.opendaylight.groupbasedpolicy.renderer.opflex.mit.PolicyPropertyInfo.PolicyPropertyId;
28 import org.opendaylight.groupbasedpolicy.renderer.opflex.mit.PolicyPropertyInfo.PolicyPropertyInfoBuilder;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
30
31 import com.fasterxml.jackson.databind.DeserializationFeature;
32 import com.fasterxml.jackson.databind.JsonNode;
33 import com.fasterxml.jackson.databind.ObjectMapper;
34 import com.fasterxml.jackson.databind.node.JsonNodeFactory;
35 import com.fasterxml.jackson.databind.node.ObjectNode;
36
37 public class MitLibTest {
38
39         private static final String TEST_CLASS_1_NAME = "ClassBar";
40         private static final String TEST_CLASS_2_NAME = "ClassBee";
41         private static final String TEST_CLASS_3_NAME = "ClassZoo";
42         private static final String TEST_PROP_1_NAME = "PropBoo";
43         private static final String TEST_PROP_2_NAME = "PropFoo";
44         private static final String TEST_PROP_3_NAME = "PropGoo";
45         private static final String TEST_PROP_4_NAME = "PropFew";
46         private static final String TEST_PROP_5_NAME = "PropLou";
47         private static final String TEST_PROP_6_NAME = "PropDue";
48         private static final String TEST_PROP_7_NAME = "PropSue";
49         private static final String TEST_ENUM_1_NAME = "EnumBlue";
50         private static final String TEST_ENUM_VAL_1_NAME = "Hello";
51         private static final String TEST_ENUM_VAL_2_NAME = "World";
52         private static final String TEST_DATA_2_STRING = "FooToYou";
53         private static final String TEST_DATA_4_STRING = "00:01:02:03:04:05";
54         private static final String TEST_DATA_6_STRING = "2148040771";
55         private static final String TEST_DATA_7_STRING = "200";
56
57         private static final int TEST_PROP_1_ID = 101;
58         private static final int TEST_PROP_2_ID = 102;
59         private static final int TEST_PROP_3_ID = 103;
60         private static final int TEST_PROP_4_ID = 104;
61         private static final int TEST_PROP_5_ID = 105;
62         private static final int TEST_PROP_6_ID = 106;
63         private static final int TEST_PROP_7_ID = 106;
64         private static final int TEST_CLASS_1_ID = 201;
65         private static final int TEST_CLASS_2_ID = 202;
66         private static final int TEST_CLASS_3_ID = 203;
67         private static final int TEST_ENUM_VAL_1_VAL = 3;
68         private static final int TEST_ENUM_VAL_2_VAL = 4;
69
70     private ObjectMapper objectMapper;
71         private JsonNodeFactory jnf;
72
73         public static class TestMit implements OpflexMit {
74
75                 private Map<String,PolicyClassInfo> metaDataMap = null;
76                 private Map<Long, String> classIdMap = null;
77
78                 public TestMit() {
79                         EnumInfo ei;
80                         PolicyPropertyInfo ppi;
81                         PolicyClassInfo pci;
82                         EnumInfoBuilder eib;
83                         PolicyPropertyInfoBuilder ppib;
84                         PolicyClassInfoBuilder pcib;
85                         List<PolicyPropertyInfo> ppil;
86                         List<PolicyPropertyId> classKeys;
87
88                         Map<String,PolicyClassInfo> metaData = new HashMap<String, PolicyClassInfo>();
89                         Map<Long, String> classIds = new HashMap<Long, String>();
90
91                         /*
92                          * Construct a test MIT tree
93                          *
94                          * TODO: test vectored values
95                          */
96                         ppil = new ArrayList<PolicyPropertyInfo>();
97                 classKeys = new ArrayList<PolicyPropertyId>();
98                 ppib = new PolicyPropertyInfoBuilder();
99                 ppib.setPropId(new PolicyPropertyId(TEST_PROP_1_ID)).
100                      setPropName(TEST_PROP_1_NAME).
101                      setType(PolicyPropertyInfo.PropertyType.COMPOSITE).
102                      setClassId(TEST_CLASS_2_ID).
103                      setPropCardinality(PolicyPropertyInfo.PropertyCardinality.VECTOR);
104                 ppi = ppib.build();
105                 ppil.add(ppi);
106                 ppib = new PolicyPropertyInfoBuilder();
107                 ppib.setPropId(new PolicyPropertyId(TEST_PROP_2_ID)).
108                      setPropName(TEST_PROP_2_NAME).
109                      setType(PolicyPropertyInfo.PropertyType.STRING).
110                      setPropCardinality(PolicyPropertyInfo.PropertyCardinality.SCALAR);
111                 ppi = ppib.build();
112                 ppil.add(ppi);
113                 classKeys.add(ppi.getPropId());
114                 ppib = new PolicyPropertyInfoBuilder();
115                 ppib.setPropId(new PolicyPropertyId(TEST_PROP_3_ID)).
116                      setPropName(TEST_PROP_3_NAME).
117                      setType(PolicyPropertyInfo.PropertyType.REFERENCE).
118                      setPropCardinality(PolicyPropertyInfo.PropertyCardinality.SCALAR);
119                 ppi = ppib.build();
120                 ppil.add(ppi);
121                 pcib = new PolicyClassInfoBuilder();
122                 pcib.setClassId(TEST_CLASS_1_ID).
123                      setClassName(TEST_CLASS_1_NAME).
124                      setPolicyType(PolicyClassInfo.PolicyClassType.POLICY).
125                      setProperty(ppil).
126                      setKey(classKeys);
127                 pci = pcib.build();
128                 metaData.put(pci.getClassName(), pci);
129                 classIds.put(new Long(pci.getClassId()), pci.getClassName());
130
131                 ppil = new ArrayList<PolicyPropertyInfo>();
132                 classKeys = new ArrayList<PolicyPropertyId>();
133                 ppib = new PolicyPropertyInfoBuilder();
134                 ppib.setPropId(new PolicyPropertyId(TEST_PROP_4_ID)).
135                      setPropName(TEST_PROP_4_NAME).
136                      setType(PolicyPropertyInfo.PropertyType.MAC).
137                      setPropCardinality(PolicyPropertyInfo.PropertyCardinality.SCALAR);
138                 ppi = ppib.build();
139                 ppil.add(ppi);
140                 eib = new EnumInfoBuilder();
141                 eib.setName(TEST_ENUM_1_NAME);
142                 eib.setEnumValue(TEST_ENUM_VAL_1_NAME,new BigInteger(String.valueOf(TEST_ENUM_VAL_1_VAL)));
143                 eib.setEnumValue(TEST_ENUM_VAL_2_NAME,new BigInteger(String.valueOf(TEST_ENUM_VAL_2_VAL)));
144                 ei = eib.build();
145                 ppib = new PolicyPropertyInfoBuilder();
146                 ppib.setPropId(new PolicyPropertyId(TEST_PROP_5_ID)).
147                      setPropName(TEST_PROP_5_NAME).
148                      setType(PolicyPropertyInfo.PropertyType.ENUM8).
149                      setEnumInfo(ei).
150                      setPropCardinality(PolicyPropertyInfo.PropertyCardinality.SCALAR);
151                 ppi = ppib.build();
152                 ppil.add(ppi);
153                 pcib = new PolicyClassInfoBuilder();
154                 pcib.setClassId(TEST_CLASS_2_ID).
155                      setClassName(TEST_CLASS_2_NAME).
156                      setPolicyType(PolicyClassInfo.PolicyClassType.POLICY).
157                      setProperty(ppil).
158                      setKey(classKeys);
159                 pci = pcib.build();
160                 metaData.put(pci.getClassName(), pci);
161                 classIds.put(new Long(pci.getClassId()), pci.getClassName());
162
163                 ppil = new ArrayList<PolicyPropertyInfo>();
164                 classKeys = new ArrayList<PolicyPropertyId>();
165                 ppib = new PolicyPropertyInfoBuilder();
166                 ppib.setPropId(new PolicyPropertyId(TEST_PROP_6_ID)).
167                      setPropName(TEST_PROP_6_NAME).
168                      setType(PolicyPropertyInfo.PropertyType.U64).
169                      setPropCardinality(PolicyPropertyInfo.PropertyCardinality.SCALAR);
170                 ppi = ppib.build();
171                 ppil.add(ppi);
172                 ppib = new PolicyPropertyInfoBuilder();
173                 ppib.setPropId(new PolicyPropertyId(TEST_PROP_7_ID)).
174                      setPropName(TEST_PROP_7_NAME).
175                      setType(PolicyPropertyInfo.PropertyType.S64).
176                      setPropCardinality(PolicyPropertyInfo.PropertyCardinality.SCALAR);
177                 ppi = ppib.build();
178                 ppil.add(ppi);
179                 pcib = new PolicyClassInfoBuilder();
180                 pcib.setClassId(TEST_CLASS_3_ID).
181                      setClassName(TEST_CLASS_3_NAME).
182                      setPolicyType(PolicyClassInfo.PolicyClassType.LOCAL_ONLY).
183                      setProperty(ppil).
184                      setKey(classKeys);
185                 pci = pcib.build();
186                 metaData.put(pci.getClassName(), pci);
187                 classIds.put(new Long(pci.getClassId()), pci.getClassName());
188
189                 metaDataMap = Collections.unmodifiableMap(metaData);
190                 classIdMap = Collections.unmodifiableMap(classIds);
191
192                 }
193
194                 @Override
195                 public PolicyClassInfo getClass(String name) {
196                         return metaDataMap.get(name);
197
198                 }
199
200                 @Override
201                 public PolicyClassInfo getClass(Long classId) {
202                         String className = classIdMap.get(classId);
203                         if (className == null) return null;
204                         return metaDataMap.get(className);
205                 }
206
207         }
208
209         private TestMit testMit;
210         private ManagedObject testMo;
211         private MitLib lib;
212
213     @Before
214     public void setUp() throws Exception {
215         lib = new MitLib();
216         testMit  = new TestMit();
217                  objectMapper = new ObjectMapper();
218                  objectMapper.configure(
219                             DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
220                  jnf = objectMapper.getNodeFactory();
221
222     }
223
224     private ManagedObject constructClass1Mo() {
225         List<Property> propList = new ArrayList<Property>();
226         Property prop = null;
227         ManagedObject mo = new ManagedObject();
228         mo = new ManagedObject();
229         mo.setSubject(TEST_CLASS_1_NAME);
230
231         JsonNode jn = jnf.textNode(TEST_DATA_2_STRING);
232         prop = new Property();
233         prop.setName(TEST_PROP_2_NAME);
234         prop.setData(jn);
235         propList.add(prop);
236         prop = new Property();
237         prop.setName(TEST_PROP_3_NAME);
238         ObjectNode on = jnf.objectNode();
239         on.put(MitLib.REFERENCE_SUBJECT, TEST_CLASS_3_NAME);
240         on.put(MitLib.REFERENCE_URI, "/" + TEST_CLASS_3_NAME);
241         prop.setData(on);
242         propList.add(prop);
243         mo.setProperties(propList);
244         return mo;
245
246     }
247
248     private ManagedObject constructClass2Mo() {
249         List<Property> propList = new ArrayList<Property>();
250         Property prop = null;
251         ManagedObject mo = new ManagedObject();
252         mo = new ManagedObject();
253         mo.setSubject(TEST_CLASS_2_NAME);
254
255         prop = new Property();
256         prop.setName(TEST_PROP_4_NAME);
257         JsonNode jn = jnf.textNode(TEST_DATA_4_STRING);
258         prop.setData(jn);
259         propList.add(prop);
260         prop = new Property();
261         prop.setName(TEST_PROP_5_NAME);
262         jn = jnf.textNode(TEST_ENUM_VAL_2_NAME);
263         prop.setData(jn);
264         propList.add(prop);
265         mo.setProperties(propList);
266
267         return mo;
268     }
269
270     private ManagedObject constructClass3Mo() {
271         List<Property> propList = new ArrayList<Property>();
272         Property prop = null;
273         ManagedObject mo = new ManagedObject();
274         mo = new ManagedObject();
275
276         mo.setSubject(TEST_CLASS_3_NAME);
277
278         prop = new Property();
279         prop.setName(TEST_PROP_6_NAME);
280         JsonNode jn = jnf.numberNode(new BigInteger(TEST_DATA_6_STRING));
281         prop.setData(jn);
282         propList.add(prop);
283         prop = new Property();
284         jn = jnf.numberNode(new BigInteger(TEST_DATA_7_STRING));
285         prop.setName(TEST_PROP_7_NAME);
286         prop.setData(jn);
287         propList.add(prop);
288         mo.setProperties(propList);
289
290         return mo;
291     }
292
293     @Test
294     public void testDeserializeMoProperties() throws Exception {
295         PolicyObjectInstance poi = null;
296
297         /*
298          * Each MO is a class, with the properties of the class
299          * matching the properties of the MO. Construct the MO,
300          * run through the deserializer, and verify that the
301          * resulting object instance is of the right class and
302          * has all the properties with correct values
303          */
304
305         // Test MO/Class 1
306         testMo = constructClass1Mo();
307
308         poi = lib.deserializeMoProperties(testMo, testMit);
309         assertTrue(poi.getClassId() == TEST_CLASS_1_ID);
310         assertTrue(poi.isSet(new PolicyPropertyId(TEST_PROP_2_ID),
311                                      PolicyPropertyInfo.PropertyType.STRING,
312                                      PolicyPropertyInfo.PropertyCardinality.SCALAR));
313         assertTrue(poi.getString(new PolicyPropertyId(TEST_PROP_2_ID)).equals(TEST_DATA_2_STRING));
314
315         assertTrue(poi.isSet(new PolicyPropertyId(TEST_PROP_3_ID),
316                                      PolicyPropertyInfo.PropertyType.REFERENCE,
317                                      PolicyPropertyInfo.PropertyCardinality.SCALAR));
318         PolicyObjectInstance.PolicyReference ref = poi.getReference(new PolicyPropertyId(TEST_PROP_3_ID));
319         assertTrue(ref.getClassId() == TEST_CLASS_3_ID);
320         assertTrue(ref.getUri().getValue().equals("/" + TEST_CLASS_3_NAME));
321
322         // Test MO/Class 2
323         testMo = constructClass2Mo();
324         poi = lib.deserializeMoProperties(testMo, testMit);
325
326         assertTrue(poi.getClassId() == TEST_CLASS_2_ID);
327         assertTrue(poi.isSet(new PolicyPropertyId(TEST_PROP_4_ID),
328                                      PolicyPropertyInfo.PropertyType.MAC,
329                                      PolicyPropertyInfo.PropertyCardinality.SCALAR));
330         MacAddress mac = poi.getMacAddress(new PolicyPropertyId(TEST_PROP_4_ID));
331         assertTrue(mac.equals(new MacAddress(TEST_DATA_4_STRING)));
332
333         assertTrue(poi.isSet(new PolicyPropertyId(TEST_PROP_5_ID),
334                                      PolicyPropertyInfo.PropertyType.ENUM8,
335                                      PolicyPropertyInfo.PropertyCardinality.SCALAR));
336         assertTrue(poi.getUint64(new PolicyPropertyId(TEST_PROP_5_ID)).intValue() == TEST_ENUM_VAL_2_VAL);
337
338         // Test MO/Class 3
339         testMo = constructClass3Mo();
340         poi = lib.deserializeMoProperties(testMo, testMit);
341
342         assertTrue(poi.getClassId() == TEST_CLASS_3_ID);
343         assertTrue(poi.isSet(new PolicyPropertyId(TEST_PROP_6_ID),
344                                      PolicyPropertyInfo.PropertyType.U64,
345                                      PolicyPropertyInfo.PropertyCardinality.SCALAR));
346         assertTrue(poi.getUint64(new PolicyPropertyId(TEST_PROP_6_ID)).equals(new BigInteger(TEST_DATA_6_STRING)));
347         assertTrue(mac.equals(new MacAddress(TEST_DATA_4_STRING)));
348
349         assertTrue(poi.isSet(new PolicyPropertyId(TEST_PROP_7_ID),
350                                      PolicyPropertyInfo.PropertyType.S64,
351                                      PolicyPropertyInfo.PropertyCardinality.SCALAR));
352         assertTrue(poi.getInt64(new PolicyPropertyId(TEST_PROP_7_ID)) == new Long(TEST_DATA_7_STRING));
353
354
355     }
356
357     @Test
358     public void testSerializeMoProperties() throws Exception {
359         PolicyClassInfo pci = null;
360         PolicyObjectInstance poi = null;
361         boolean prop2Found = false, prop3Found = false,
362                         prop4Found = false, prop5Found = false,
363                         prop6Found = false, prop7Found = false;
364
365         /*
366          * Construct the PolicyObjectInfo object by
367          * running it through the deserializer
368          */
369
370         testMo = constructClass1Mo();
371         poi = lib.deserializeMoProperties(testMo,  testMit);
372         pci = testMit.getClass(TEST_CLASS_1_NAME);
373         lib.serializeMoProperties( pci, poi, testMo, testMit);
374         List<Property> props = testMo.getProperties();
375         assertTrue(props.size() == 2);
376         for (Property prop: props) {
377
378                 if (prop.getName().equals(TEST_PROP_2_NAME)) {
379                 assertTrue(prop.getData().asText().equals(TEST_DATA_2_STRING));
380                 prop2Found = true;
381                 }
382
383                 if (prop.getName().equals(TEST_PROP_3_NAME)) {
384                 assertTrue(prop.getName().equals(TEST_PROP_3_NAME));
385                 assertTrue(prop.getData().has(MitLib.REFERENCE_SUBJECT));
386                 JsonNode jn = prop.getData().findValue(MitLib.REFERENCE_SUBJECT);
387                 assertTrue(jn.asText().equals(TEST_CLASS_3_NAME));
388                 assertTrue(prop.getData().has(MitLib.REFERENCE_URI));
389                 jn = prop.getData().findValue(MitLib.REFERENCE_URI);
390                 assertTrue(jn.asText().equals("/" + TEST_CLASS_3_NAME));
391                 prop3Found = true;
392                 }
393         }
394         assertTrue(prop2Found);
395         assertTrue(prop3Found);
396
397         testMo = constructClass2Mo();
398         poi = lib.deserializeMoProperties(testMo,  testMit);
399         pci = testMit.getClass(TEST_CLASS_2_NAME);
400         lib.serializeMoProperties( pci, poi, testMo, testMit);
401         props = testMo.getProperties();
402         assertTrue(props.size() == 2);
403         for (Property prop: props) {
404
405                 if (prop.getName().equals(TEST_PROP_4_NAME)) {
406                 assertTrue(prop.getData().asText().equals(TEST_DATA_4_STRING));
407                 prop4Found = true;
408                 }
409                 if (prop.getName().equals(TEST_PROP_5_NAME)) {
410                 assertTrue(prop.getData().asText().equals(TEST_ENUM_VAL_2_NAME));
411                         prop5Found = true;
412                 }
413         }
414         assertTrue(prop4Found);
415         assertTrue(prop5Found);
416
417
418         testMo = constructClass3Mo();
419         poi = lib.deserializeMoProperties(testMo,  testMit);
420         pci = testMit.getClass(TEST_CLASS_3_NAME);
421         lib.serializeMoProperties( pci, poi, testMo, testMit);
422         props = testMo.getProperties();
423         assertTrue(props.size() == 2);
424         for (Property prop: props) {
425                 if (prop.getName().equals(TEST_PROP_6_NAME)) {
426                 assertTrue(prop.getData().asText().equals(TEST_DATA_6_STRING));
427                 prop6Found = true;
428                 }
429                 if (prop.getName().equals(TEST_PROP_7_NAME)) {
430                 assertTrue(prop.getData().asText().equals(TEST_DATA_7_STRING));
431                         prop7Found = true;
432                 }
433         }
434         assertTrue(prop6Found);
435         assertTrue(prop7Found);
436
437     }
438
439 }