Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame^] | 1 | package org.onosproject.incubator.net.config.basics; |
| 2 | |
| 3 | import static org.junit.Assert.assertEquals; |
| 4 | import static org.junit.Assert.assertFalse; |
| 5 | import static org.onosproject.incubator.net.config.basics.OpticalPortConfig.TYPE; |
| 6 | import static org.onosproject.incubator.net.config.basics.OpticalPortConfig.NAME; |
| 7 | import static org.onosproject.incubator.net.config.basics.OpticalPortConfig.PORT; |
| 8 | |
| 9 | import java.io.IOException; |
| 10 | import java.util.Iterator; |
| 11 | import java.util.List; |
| 12 | |
| 13 | import org.junit.Before; |
| 14 | import org.junit.Test; |
| 15 | import org.onosproject.incubator.net.config.Config; |
| 16 | import org.onosproject.incubator.net.config.ConfigApplyDelegate; |
| 17 | import org.onosproject.net.AnnotationKeys; |
| 18 | import org.onosproject.net.ConnectPoint; |
| 19 | import org.onosproject.net.DeviceId; |
| 20 | import org.onosproject.net.Port; |
| 21 | import org.onosproject.net.PortNumber; |
| 22 | |
| 23 | import com.fasterxml.jackson.databind.JsonNode; |
| 24 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 25 | import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
| 26 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 27 | import com.google.common.collect.Lists; |
| 28 | |
| 29 | public class OpticalPortConfigTest { |
| 30 | private static final String FIELD = "ports"; |
| 31 | private static final String KEY = "opc-test"; |
| 32 | |
| 33 | private static final DeviceId DID = DeviceId.deviceId(KEY); |
| 34 | private static final PortNumber PN = PortNumber.portNumber(100); |
| 35 | private static final ConnectPoint CPT = new ConnectPoint(DID, PN); |
| 36 | private static final String DEMOTREE = "{" + |
| 37 | "\"ports\": [" + |
| 38 | // config entity 0 |
| 39 | "{" + |
| 40 | "\"name\": \"1-10-E1_WPORT\"," + |
| 41 | "\"type\": \"OMS\"" + |
| 42 | "}," + |
| 43 | // config entity 1 |
| 44 | "{" + |
| 45 | "\"type\": \"OCH\"," + |
| 46 | "\"speed\": 0," + |
| 47 | "\"port\": 10" + |
| 48 | "}," + |
| 49 | // config entity 2 |
| 50 | "{" + |
| 51 | "\"name\": \"1-1-E1_LPORT\"," + |
| 52 | "\"type\": \"OCH\"," + |
| 53 | "\"annotations\": {" + |
| 54 | "\"staticLambda\": 1," + |
| 55 | "\"staticPort\": \"1-22-E1_WPORT\"" + |
| 56 | "}" + |
| 57 | "}" + |
| 58 | "]" + |
| 59 | "}"; |
| 60 | |
| 61 | private final ConfigApplyDelegate delegate = new MockCfgDelegate(); |
| 62 | private final ObjectMapper mapper = new ObjectMapper(); |
| 63 | |
| 64 | // one OPC per port in DEMOTREE |
| 65 | private List<OpticalPortConfig> opcl = Lists.newArrayList(); |
| 66 | // JsonNodes representing each port. |
| 67 | private List<JsonNode> testNodes = Lists.newArrayList(); |
| 68 | |
| 69 | @Before |
| 70 | public void setUp() { |
| 71 | try { |
| 72 | JsonNode tree = new ObjectMapper().readTree(DEMOTREE); |
| 73 | Iterator<JsonNode> pitr = tree.get(FIELD).elements(); |
| 74 | while (pitr.hasNext()) { |
| 75 | // initialize a config entity, add to lists |
| 76 | JsonNode jn = pitr.next(); |
| 77 | OpticalPortConfig opc = new OpticalPortConfig(); |
| 78 | ObjectNode node = JsonNodeFactory.instance.objectNode(); |
| 79 | opc.init(CPT, KEY, node, mapper, delegate); |
| 80 | |
| 81 | testNodes.add(jn); |
| 82 | opcl.add(opc); |
| 83 | } |
| 84 | } catch (IOException e) { |
| 85 | e.printStackTrace(); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | @Test |
| 90 | public void testBaseAttrs() { |
| 91 | // configs 0 and 1 - port with and without alphanumeric names |
| 92 | OpticalPortConfig op0 = opcl.get(0); |
| 93 | OpticalPortConfig op1 = opcl.get(1); |
| 94 | // config 2 - no name |
| 95 | OpticalPortConfig op2 = opcl.get(2); |
| 96 | JsonNode jn0 = testNodes.get(0); |
| 97 | JsonNode jn1 = testNodes.get(1); |
| 98 | |
| 99 | op0.portType(Port.Type.valueOf(jn0.path(TYPE).asText())) |
| 100 | .portName(jn0.path(NAME).asText()); |
| 101 | op1.portType(Port.Type.valueOf(jn1.path(TYPE).asText())) |
| 102 | .portNumberName(jn1.path(PORT).asLong()); |
| 103 | |
| 104 | assertEquals(Port.Type.OMS, op0.type()); |
| 105 | assertEquals(jn0.path(NAME).asText(), op0.name()); |
| 106 | assertEquals(jn1.path(PORT).asText(), op1.name()); |
| 107 | assertEquals("", op2.name()); |
| 108 | } |
| 109 | |
| 110 | @Test |
| 111 | public void testAdditionalAttrs() { |
| 112 | // config 1 has no annotations, 2 has predefined ones |
| 113 | OpticalPortConfig op1 = opcl.get(1); |
| 114 | OpticalPortConfig op2 = opcl.get(2); |
| 115 | JsonNode jn2 = testNodes.get(2); |
| 116 | Long sl = 1L; |
| 117 | |
| 118 | // see config entity 2 in DEMOTREE |
| 119 | op2.staticLambda(jn2.path("annotations").path(AnnotationKeys.STATIC_LAMBDA).asLong()); |
| 120 | op2.staticPort(jn2.path("annotations").path(AnnotationKeys.STATIC_PORT).asText()); |
| 121 | |
| 122 | assertEquals(sl, op2.staticLambda().get()); |
| 123 | assertFalse(op1.staticLambda().isPresent()); |
| 124 | assertEquals("1-22-E1_WPORT", op2.staticPort()); |
| 125 | assertEquals("", op1.staticPort()); |
| 126 | |
| 127 | op2.staticLambda(null); |
| 128 | assertFalse(op2.staticLambda().isPresent()); |
| 129 | } |
| 130 | |
| 131 | private class MockCfgDelegate implements ConfigApplyDelegate { |
| 132 | |
| 133 | @Override |
| 134 | public void onApply(@SuppressWarnings("rawtypes") Config config) { |
| 135 | config.apply(); |
| 136 | } |
| 137 | |
| 138 | } |
| 139 | } |