2 * Copyright (C) 2014 Cisco Systems, Inc.
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
10 package org.opendaylight.groupbasedpolicy.renderer.opflex.mit;
12 import static org.junit.Assert.assertTrue;
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;
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;
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;
37 public class MitLibTest {
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";
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;
70 private ObjectMapper objectMapper;
71 private JsonNodeFactory jnf;
73 public static class TestMit implements OpflexMit {
75 private Map<String,PolicyClassInfo> metaDataMap = null;
76 private Map<Long, String> classIdMap = null;
80 PolicyPropertyInfo ppi;
83 PolicyPropertyInfoBuilder ppib;
84 PolicyClassInfoBuilder pcib;
85 List<PolicyPropertyInfo> ppil;
86 List<PolicyPropertyId> classKeys;
88 Map<String,PolicyClassInfo> metaData = new HashMap<String, PolicyClassInfo>();
89 Map<Long, String> classIds = new HashMap<Long, String>();
92 * Construct a test MIT tree
94 * TODO: test vectored values
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);
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);
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);
121 pcib = new PolicyClassInfoBuilder();
122 pcib.setClassId(TEST_CLASS_1_ID).
123 setClassName(TEST_CLASS_1_NAME).
124 setPolicyType(PolicyClassInfo.PolicyClassType.POLICY).
128 metaData.put(pci.getClassName(), pci);
129 classIds.put(new Long(pci.getClassId()), pci.getClassName());
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);
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)));
145 ppib = new PolicyPropertyInfoBuilder();
146 ppib.setPropId(new PolicyPropertyId(TEST_PROP_5_ID)).
147 setPropName(TEST_PROP_5_NAME).
148 setType(PolicyPropertyInfo.PropertyType.ENUM8).
150 setPropCardinality(PolicyPropertyInfo.PropertyCardinality.SCALAR);
153 pcib = new PolicyClassInfoBuilder();
154 pcib.setClassId(TEST_CLASS_2_ID).
155 setClassName(TEST_CLASS_2_NAME).
156 setPolicyType(PolicyClassInfo.PolicyClassType.POLICY).
160 metaData.put(pci.getClassName(), pci);
161 classIds.put(new Long(pci.getClassId()), pci.getClassName());
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);
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);
179 pcib = new PolicyClassInfoBuilder();
180 pcib.setClassId(TEST_CLASS_3_ID).
181 setClassName(TEST_CLASS_3_NAME).
182 setPolicyType(PolicyClassInfo.PolicyClassType.LOCAL_ONLY).
186 metaData.put(pci.getClassName(), pci);
187 classIds.put(new Long(pci.getClassId()), pci.getClassName());
189 metaDataMap = Collections.unmodifiableMap(metaData);
190 classIdMap = Collections.unmodifiableMap(classIds);
195 public PolicyClassInfo getClass(String name) {
196 return metaDataMap.get(name);
201 public PolicyClassInfo getClass(Long classId) {
202 String className = classIdMap.get(classId);
203 if (className == null) return null;
204 return metaDataMap.get(className);
209 private TestMit testMit;
210 private ManagedObject testMo;
214 public void setUp() throws Exception {
216 testMit = new TestMit();
217 objectMapper = new ObjectMapper();
218 objectMapper.configure(
219 DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
220 jnf = objectMapper.getNodeFactory();
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);
231 JsonNode jn = jnf.textNode(TEST_DATA_2_STRING);
232 prop = new Property();
233 prop.setName(TEST_PROP_2_NAME);
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);
243 mo.setProperties(propList);
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);
255 prop = new Property();
256 prop.setName(TEST_PROP_4_NAME);
257 JsonNode jn = jnf.textNode(TEST_DATA_4_STRING);
260 prop = new Property();
261 prop.setName(TEST_PROP_5_NAME);
262 jn = jnf.textNode(TEST_ENUM_VAL_2_NAME);
265 mo.setProperties(propList);
270 private ManagedObject constructClass3Mo() {
271 List<Property> propList = new ArrayList<Property>();
272 Property prop = null;
273 ManagedObject mo = new ManagedObject();
274 mo = new ManagedObject();
276 mo.setSubject(TEST_CLASS_3_NAME);
278 prop = new Property();
279 prop.setName(TEST_PROP_6_NAME);
280 JsonNode jn = jnf.numberNode(new BigInteger(TEST_DATA_6_STRING));
283 prop = new Property();
284 jn = jnf.numberNode(new BigInteger(TEST_DATA_7_STRING));
285 prop.setName(TEST_PROP_7_NAME);
288 mo.setProperties(propList);
294 public void testDeserializeMoProperties() throws Exception {
295 PolicyObjectInstance poi = null;
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
306 testMo = constructClass1Mo();
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));
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));
323 testMo = constructClass2Mo();
324 poi = lib.deserializeMoProperties(testMo, testMit);
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)));
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);
339 testMo = constructClass3Mo();
340 poi = lib.deserializeMoProperties(testMo, testMit);
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)));
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));
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;
366 * Construct the PolicyObjectInfo object by
367 * running it through the deserializer
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) {
378 if (prop.getName().equals(TEST_PROP_2_NAME)) {
379 assertTrue(prop.getData().asText().equals(TEST_DATA_2_STRING));
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));
394 assertTrue(prop2Found);
395 assertTrue(prop3Found);
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) {
405 if (prop.getName().equals(TEST_PROP_4_NAME)) {
406 assertTrue(prop.getData().asText().equals(TEST_DATA_4_STRING));
409 if (prop.getName().equals(TEST_PROP_5_NAME)) {
410 assertTrue(prop.getData().asText().equals(TEST_ENUM_VAL_2_NAME));
414 assertTrue(prop4Found);
415 assertTrue(prop5Found);
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));
429 if (prop.getName().equals(TEST_PROP_7_NAME)) {
430 assertTrue(prop.getData().asText().equals(TEST_DATA_7_STRING));
434 assertTrue(prop6Found);
435 assertTrue(prop7Found);