Ayaka Koshibe
Committed by Gerrit Code Review

Config entities for Optical ports and unit test.

Conflicts:
	incubator/net/src/main/java/org/onosproject/incubator/net/config/impl/BasicNetworkConfigs.java

Change-Id: I6ed0b0a9b62b0f3a225514070523778a124f7564
package org.onosproject.incubator.net.config.basics;
import java.util.Optional;
import org.onosproject.incubator.net.config.Config;
import org.onosproject.net.AnnotationKeys;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.Port;
import com.fasterxml.jackson.databind.JsonNode;
/**
* Configurations for an optical port on a device.
*/
public class OpticalPortConfig extends Config<ConnectPoint> {
// optical type {OMS, OCH, ODUClt, fiber}
public static final String TYPE = "type";
// port name. "name" is the alphanumeric name of the port, but "port" refers
// to the port number used as a name string (i.e., for ports without
// alphanumeric names). this should be linked to ConnectPoint.
public static final String NAME = "name";
public static final String PORT = "port";
/**
* Returns the Enum value representing the type of port.
*
* @return the port type, or null if invalid or unset
*/
public Port.Type type() {
JsonNode type = node.path(TYPE);
if (type.isMissingNode()) {
return null;
}
return Port.Type.valueOf(type.asText());
}
/**
* Returns the port name associated with this port configuration. The Name
* may either be an alphanumeric string, or a string representation of the
* port number, falling back on the latter if the former doesn't exist.
*
* @return the name of this port, else, an empty string
*/
public String name() {
String name = getStringValue(NAME);
return name.isEmpty() ? getStringValue(PORT) : name;
}
/**
* Returns the string-representation of name of the output port. This is
* usually an OMS port for an OCH input ports, or an OCH port for ODU input
* ports.
*
* @return the name of this port, else, an empty string
*/
public String staticPort() {
return getStringValue(AnnotationKeys.STATIC_PORT);
}
private String getStringValue(String field) {
JsonNode name = node.path(field);
return name.isMissingNode() ? "" : name.asText();
}
/**
* Returns the output lambda configured for this port. The lambda value is
* expressed as a frequency value.
*
* @return an Optional that may contain a frequency value.
*/
public Optional<Long> staticLambda() {
JsonNode sl = node.path(AnnotationKeys.STATIC_LAMBDA);
if (sl.isMissingNode()) {
return Optional.empty();
}
return Optional.of(sl.asLong());
}
/**
* Sets the port type, or updates it if it's already set. A null argument removes
* this field.
*
* @param type the port type
* @return this OpticalPortConfig instance
*/
public OpticalPortConfig portType(Port.Type type) {
// if unspecified, ideally fall back on FIBER or PACKET.
String pt = (type == null) ? null : type.toString();
return (OpticalPortConfig) setOrClear(TYPE, pt);
}
/**
* Sets the port name, or updates it if already set. A null argument removes
* this field.
*
* @param name the port's name
* @return this OpticalPortConfig instance
*/
public OpticalPortConfig portName(String name) {
return (OpticalPortConfig) setOrClear(NAME, name);
}
/**
* Sets the port name from port number, or updates it if already set. A null
* argument removes this field.
*
* @param name the port number, to be used as name
* @return this OpticalPortConfig instance
*/
public OpticalPortConfig portNumberName(Long name) {
return (OpticalPortConfig) setOrClear(PORT, name);
}
/**
* Sets the output port name, or updates it if already set. A null argument
* removes this field.
*
* @param name the output port's name
* @return this OpticalPortConfig instance
*/
public OpticalPortConfig staticPort(String name) {
return (OpticalPortConfig) setOrClear(AnnotationKeys.STATIC_PORT, name);
}
/**
* Sets the output lambda index, or updates it if already set. A null argument
* removes this field.
*
* @param index the output lambda
* @return this OpticalPortConfig instance
*/
public OpticalPortConfig staticLambda(Long index) {
return (OpticalPortConfig) setOrClear(AnnotationKeys.STATIC_LAMBDA, index);
}
}
package org.onosproject.incubator.net.config.basics;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.onosproject.incubator.net.config.basics.OpticalPortConfig.TYPE;
import static org.onosproject.incubator.net.config.basics.OpticalPortConfig.NAME;
import static org.onosproject.incubator.net.config.basics.OpticalPortConfig.PORT;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.onosproject.incubator.net.config.Config;
import org.onosproject.incubator.net.config.ConfigApplyDelegate;
import org.onosproject.net.AnnotationKeys;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.DeviceId;
import org.onosproject.net.Port;
import org.onosproject.net.PortNumber;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Lists;
public class OpticalPortConfigTest {
private static final String FIELD = "ports";
private static final String KEY = "opc-test";
private static final DeviceId DID = DeviceId.deviceId(KEY);
private static final PortNumber PN = PortNumber.portNumber(100);
private static final ConnectPoint CPT = new ConnectPoint(DID, PN);
private static final String DEMOTREE = "{" +
"\"ports\": [" +
// config entity 0
"{" +
"\"name\": \"1-10-E1_WPORT\"," +
"\"type\": \"OMS\"" +
"}," +
// config entity 1
"{" +
"\"type\": \"OCH\"," +
"\"speed\": 0," +
"\"port\": 10" +
"}," +
// config entity 2
"{" +
"\"name\": \"1-1-E1_LPORT\"," +
"\"type\": \"OCH\"," +
"\"annotations\": {" +
"\"staticLambda\": 1," +
"\"staticPort\": \"1-22-E1_WPORT\"" +
"}" +
"}" +
"]" +
"}";
private final ConfigApplyDelegate delegate = new MockCfgDelegate();
private final ObjectMapper mapper = new ObjectMapper();
// one OPC per port in DEMOTREE
private List<OpticalPortConfig> opcl = Lists.newArrayList();
// JsonNodes representing each port.
private List<JsonNode> testNodes = Lists.newArrayList();
@Before
public void setUp() {
try {
JsonNode tree = new ObjectMapper().readTree(DEMOTREE);
Iterator<JsonNode> pitr = tree.get(FIELD).elements();
while (pitr.hasNext()) {
// initialize a config entity, add to lists
JsonNode jn = pitr.next();
OpticalPortConfig opc = new OpticalPortConfig();
ObjectNode node = JsonNodeFactory.instance.objectNode();
opc.init(CPT, KEY, node, mapper, delegate);
testNodes.add(jn);
opcl.add(opc);
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void testBaseAttrs() {
// configs 0 and 1 - port with and without alphanumeric names
OpticalPortConfig op0 = opcl.get(0);
OpticalPortConfig op1 = opcl.get(1);
// config 2 - no name
OpticalPortConfig op2 = opcl.get(2);
JsonNode jn0 = testNodes.get(0);
JsonNode jn1 = testNodes.get(1);
op0.portType(Port.Type.valueOf(jn0.path(TYPE).asText()))
.portName(jn0.path(NAME).asText());
op1.portType(Port.Type.valueOf(jn1.path(TYPE).asText()))
.portNumberName(jn1.path(PORT).asLong());
assertEquals(Port.Type.OMS, op0.type());
assertEquals(jn0.path(NAME).asText(), op0.name());
assertEquals(jn1.path(PORT).asText(), op1.name());
assertEquals("", op2.name());
}
@Test
public void testAdditionalAttrs() {
// config 1 has no annotations, 2 has predefined ones
OpticalPortConfig op1 = opcl.get(1);
OpticalPortConfig op2 = opcl.get(2);
JsonNode jn2 = testNodes.get(2);
Long sl = 1L;
// see config entity 2 in DEMOTREE
op2.staticLambda(jn2.path("annotations").path(AnnotationKeys.STATIC_LAMBDA).asLong());
op2.staticPort(jn2.path("annotations").path(AnnotationKeys.STATIC_PORT).asText());
assertEquals(sl, op2.staticLambda().get());
assertFalse(op1.staticLambda().isPresent());
assertEquals("1-22-E1_WPORT", op2.staticPort());
assertEquals("", op1.staticPort());
op2.staticLambda(null);
assertFalse(op2.staticLambda().isPresent());
}
private class MockCfgDelegate implements ConfigApplyDelegate {
@Override
public void onApply(@SuppressWarnings("rawtypes") Config config) {
config.apply();
}
}
}
......@@ -16,6 +16,7 @@
package org.onosproject.incubator.net.config.impl;
import com.google.common.collect.ImmutableSet;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
......@@ -28,6 +29,7 @@ import org.onosproject.incubator.net.config.basics.BasicDeviceConfig;
import org.onosproject.incubator.net.config.basics.BasicHostConfig;
import org.onosproject.incubator.net.config.basics.BasicLinkConfig;
import org.onosproject.incubator.net.config.basics.BasicPortConfig;
import org.onosproject.incubator.net.config.basics.OpticalPortConfig;
import org.onosproject.incubator.net.config.basics.SubjectFactories;
import org.onosproject.incubator.net.domain.IntentDomainConfig;
import org.onosproject.incubator.net.domain.IntentDomainId;
......@@ -90,6 +92,14 @@ public class BasicNetworkConfigs {
public IntentDomainConfig createConfig() {
return new IntentDomainConfig();
}
},
new ConfigFactory<ConnectPoint, OpticalPortConfig>(CONNECT_POINT_SUBJECT_FACTORY,
OpticalPortConfig.class,
"basic") {
@Override
public OpticalPortConfig createConfig() {
return new OpticalPortConfig();
}
}
);
......