refactor waveserver. Allow future drivers
Change-Id: I8ca4ed2435d42aaf5d58eb020aa3a6cfd17c6fa4
diff --git a/drivers/ciena/waveserver/src/main/java/org/onosproject/drivers/ciena/waveserver/rest/CienaDriversLoader.java b/drivers/ciena/waveserver/src/main/java/org/onosproject/drivers/ciena/waveserver/rest/CienaDriversLoader.java
new file mode 100644
index 0000000..11e3fc5
--- /dev/null
+++ b/drivers/ciena/waveserver/src/main/java/org/onosproject/drivers/ciena/waveserver/rest/CienaDriversLoader.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2016-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.drivers.ciena.waveserver.rest;
+
+import org.apache.felix.scr.annotations.Component;
+import org.onosproject.net.driver.AbstractDriverLoader;
+import org.onosproject.net.optical.OpticalDevice;
+
+/**
+ * Loader for Ciena device drivers.
+ */
+@Component(immediate = true)
+public class CienaDriversLoader extends AbstractDriverLoader {
+
+ // OSGI: help bundle plugin discover runtime package dependency.
+ @SuppressWarnings("unused")
+ private OpticalDevice optical;
+
+ public CienaDriversLoader() {
+ super("/ciena-drivers.xml");
+ }
+}
diff --git a/drivers/ciena/waveserver/src/main/java/org/onosproject/drivers/ciena/waveserver/rest/CienaFlowRuleProgrammable.java b/drivers/ciena/waveserver/src/main/java/org/onosproject/drivers/ciena/waveserver/rest/CienaFlowRuleProgrammable.java
new file mode 100644
index 0000000..6dc0400
--- /dev/null
+++ b/drivers/ciena/waveserver/src/main/java/org/onosproject/drivers/ciena/waveserver/rest/CienaFlowRuleProgrammable.java
@@ -0,0 +1,158 @@
+/*
+ * Copyright 2016-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.drivers.ciena.waveserver.rest;
+
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.OchSignal;
+import org.onosproject.net.PortNumber;
+import org.onosproject.driver.optical.flowrule.CrossConnectFlowRule;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.onosproject.net.flow.FlowEntry;
+import org.onosproject.net.flow.FlowRule;
+import org.onosproject.net.flow.FlowRuleProgrammable;
+
+import org.slf4j.Logger;
+
+
+import java.util.List;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Objects;
+import java.util.stream.Collectors;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+public class CienaFlowRuleProgrammable extends AbstractHandlerBehaviour implements FlowRuleProgrammable {
+ private CienaRestDevice restCiena;
+ private final Logger log = getLogger(getClass());
+
+ @Override
+ public Collection<FlowEntry> getFlowEntries() {
+ DeviceId deviceId = handler().data().deviceId();
+ log.debug("getting flow entries for device {}", deviceId);
+ try {
+ restCiena = new CienaRestDevice(handler());
+ } catch (NullPointerException e) {
+ log.error("unable to create CienaRestDevice:\n{}", e);
+ return Collections.emptyList();
+ }
+ return restCiena.getFlowEntries();
+ }
+
+ @Override
+ public Collection<FlowRule> applyFlowRules(Collection<FlowRule> rules) {
+ log.debug("installing flow rules: {}", rules);
+ try {
+ restCiena = new CienaRestDevice(handler());
+ } catch (NullPointerException e) {
+ log.error("unable to create CienaRestDevice:\n{}", e);
+ return Collections.emptyList();
+ }
+ // Apply the valid rules on the device
+ Collection<FlowRule> added = rules.stream()
+ .map(this::createCrossConnectFlowRule)
+ .filter(this::installCrossConnect)
+ .collect(Collectors.toList());
+ restCiena.setCrossConnectCache(added);
+ return added;
+ }
+
+ @Override
+ public Collection<FlowRule> removeFlowRules(Collection<FlowRule> rules) {
+ log.debug("removing flow rules: {}", rules);
+ try {
+ restCiena = new CienaRestDevice(handler());
+ } catch (NullPointerException e) {
+ log.error("unable to create CienaRestDevice:\n{}", e);
+ return Collections.emptyList();
+ }
+ Collection<FlowRule> removed = rules.stream()
+ .map(this::createCrossConnectFlowRule)
+ .filter(Objects::nonNull)
+ .collect(Collectors.toList());
+ restCiena.removeCrossConnectCache(removed);
+ return removed;
+ }
+
+ private CrossConnectFlowRule createCrossConnectFlowRule(FlowRule r) {
+ List<PortNumber> linePorts = CienaRestDevice.getLinesidePortId().stream()
+ .map(PortNumber::portNumber)
+ .collect(Collectors.toList());
+ try {
+ return new CrossConnectFlowRule(r, linePorts);
+ } catch (IllegalArgumentException e) {
+ log.debug("unable to create cross connect for rule:\n{}", r);
+ }
+ return null;
+ }
+
+ private boolean installCrossConnect(CrossConnectFlowRule xc) {
+ if (xc == null) {
+ return false;
+ }
+ // only handling lineside rule
+ if (xc.isAddRule()) {
+ PortNumber outPort = xc.addDrop();
+ OchSignal signal = xc.ochSignal();
+ return install(outPort, signal);
+ }
+ return false;
+ }
+
+ private boolean removeCrossConnect(CrossConnectFlowRule xc) {
+ //for now setting channel to 0 for remove rule
+ if (xc == null) {
+ return false;
+ }
+ // only handling lineside rule
+ if (xc.isAddRule()) {
+ PortNumber outPort = xc.addDrop();
+ OchSignal signal = OchSignal.newDwdmSlot(xc.ochSignal().channelSpacing(), 0);
+ return install(outPort, signal);
+ }
+ return false;
+ }
+
+ private boolean install(PortNumber outPort, OchSignal signal) {
+ /*
+ * rule is installed in three steps
+ * 1- disable port
+ * 2- change frequency
+ * 3- enable port
+ */
+ try {
+ restCiena = new CienaRestDevice(handler());
+ } catch (NullPointerException e) {
+ log.error("unable to create CienaRestDevice, {}", e);
+ return false;
+ }
+ //1- disable port
+ //blindly disabling port
+ if (!restCiena.disablePort(outPort)) {
+ return false;
+ }
+ //2- change frequency
+ if (!restCiena.changeFrequency(signal, outPort)) {
+ return false;
+ }
+ //3- enable port
+ if (!restCiena.enablePort(outPort)) {
+ return false;
+ }
+ return true;
+ }
+
+}
diff --git a/drivers/ciena/waveserver/src/main/java/org/onosproject/drivers/ciena/waveserver/rest/CienaRestDevice.java b/drivers/ciena/waveserver/src/main/java/org/onosproject/drivers/ciena/waveserver/rest/CienaRestDevice.java
new file mode 100644
index 0000000..ea02de3
--- /dev/null
+++ b/drivers/ciena/waveserver/src/main/java/org/onosproject/drivers/ciena/waveserver/rest/CienaRestDevice.java
@@ -0,0 +1,446 @@
+/*
+ * Copyright 2016-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.drivers.ciena.waveserver.rest;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.ObjectReader;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
+import org.apache.commons.lang3.tuple.Pair;
+import org.onlab.util.Frequency;
+import org.onlab.util.Spectrum;
+import org.onosproject.driver.optical.flowrule.CrossConnectCache;
+import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
+import org.onosproject.incubator.net.faultmanagement.alarm.AlarmEntityId;
+import org.onosproject.incubator.net.faultmanagement.alarm.AlarmId;
+import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm;
+import org.onosproject.net.ChannelSpacing;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.OchSignal;
+import org.onosproject.net.OchSignalType;
+import org.onosproject.net.Port;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.driver.DriverHandler;
+import org.onosproject.net.flow.DefaultFlowEntry;
+import org.onosproject.net.flow.DefaultFlowRule;
+import org.onosproject.net.flow.DefaultTrafficSelector;
+import org.onosproject.net.flow.DefaultTrafficTreatment;
+import org.onosproject.net.flow.FlowEntry;
+import org.onosproject.net.flow.FlowId;
+import org.onosproject.net.flow.FlowRule;
+import org.onosproject.net.flow.TrafficSelector;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.flow.criteria.Criteria;
+import org.onosproject.protocol.rest.RestSBController;
+import org.slf4j.Logger;
+
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
+import java.time.format.DateTimeParseException;
+import java.util.Collection;
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Collectors;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.slf4j.LoggerFactory.getLogger;
+
+public class CienaRestDevice {
+ private static final Frequency CENTER_FREQUENCY = Frequency.ofGHz(195_950);
+ private static final String ENABLED = "enabled";
+ private static final String DISABLED = "disabled";
+ private static final String VALUE = "value";
+ private static final String STATE = "state";
+ private static final String ADMIN_STATE = "admin-state";
+ private static final String WAVELENGTH = "wavelength";
+ private static final String DATA = "data";
+ private static final String ACTIVE = "active";
+ private static final String ACKNOWLEDGE = "acknowledged";
+ private static final String SEVERITY = "severity";
+ private static final String DESCRIPTION = "description";
+ private static final String INSTANCE = "instance";
+ private static final String PORT = "port";
+ private static final String PTP = "ptp";
+ private static final String UTC = "UTC";
+ private static final String OTHER = "other";
+ private static final String DATE_TIME_FORMAT = "EEE MMM [ ]d HH:mm:ss yyyy";
+ //keys
+ private static final String ALARM_KEY = "ws-alarms";
+ private static final String ALARM_INSTANCE_ID = "alarm-instance-id";
+ private static final String ALARM_TABLE_ID = "alarm-table-id";
+ private static final String ALARM_LOCAL_DATE_TIME = "local-date-time";
+ private static final String LINE_SYSTEM_CHANNEL_NUMBER = "ciena-ws-ptp-modem:line-system-channel-number";
+ private static final String FREQUENCY_KEY = "ciena-ws-ptp-modem:frequency";
+ //URIs
+ private static final String PORT_URI = "ws-ptps/ptps/%s";
+ private static final String TRANSMITTER_URI = PORT_URI + "/properties/transmitter";
+ private static final String PORT_STATE_URI = PORT_URI + "/" + STATE;
+ private static final String WAVELENGTH_URI = TRANSMITTER_URI + "/" + WAVELENGTH;
+ private static final String FREQUENCY_URI = TRANSMITTER_URI + "/" + FREQUENCY_KEY;
+ private static final String CHANNEL_URI = TRANSMITTER_URI + "/" + LINE_SYSTEM_CHANNEL_NUMBER;
+ private static final String ACTIVE_ALARMS_URL = ALARM_KEY + "/" + ACTIVE;
+ private static final List<String> LINESIDE_PORT_ID = ImmutableList.of("4", "48");
+ private static final ChannelSpacing CHANNEL_SPACING = ChannelSpacing.CHL_50GHZ;
+
+ private final Logger log = getLogger(getClass());
+
+ private DeviceId deviceId;
+ private RestSBController controller;
+ private CrossConnectCache crossConnectCache;
+ private DeviceService deviceService;
+
+
+ public CienaRestDevice(DriverHandler handler) throws NullPointerException {
+ deviceId = handler.data().deviceId();
+ controller = checkNotNull(handler.get(RestSBController.class));
+ crossConnectCache = checkNotNull(handler.get(CrossConnectCache.class));
+ deviceService = checkNotNull(handler.get(DeviceService.class));
+ }
+
+ /**
+ * return the Line side ports.
+ *
+ * @return List of Line side ports.
+ */
+ public static List<String> getLinesidePortId() {
+ return LINESIDE_PORT_ID;
+ }
+
+ /**
+ * add the given flow rules to cross connect-cache.
+ *
+ * @param flowRules flow rules that needs to be cached.
+ */
+ public void setCrossConnectCache(Collection<FlowRule> flowRules) {
+ flowRules.forEach(xc -> crossConnectCache.set(
+ Objects.hash(deviceId, xc.selector(), xc.treatment()),
+ xc.id(),
+ xc.priority()));
+ }
+
+ /**
+ * remove the given flow rules form the cross-connect cache.
+ *
+ * @param flowRules flow rules that needs to be removed from cache.
+ */
+ public void removeCrossConnectCache(Collection<FlowRule> flowRules) {
+ flowRules.forEach(xc -> crossConnectCache.remove(Objects.hash(deviceId, xc.selector(), xc.treatment())));
+ }
+
+ private final String genPortStateRequest(String state) {
+ String request = "{\n" +
+ "\"" + STATE + "\": {\n" +
+ "\"" + ADMIN_STATE + "\": \"" + state + "\"\n}\n}";
+ log.debug("generated request: \n{}", request);
+ return request;
+ }
+
+ private String genWavelengthChangeRequest(String wavelength) {
+ String request = "{\n" +
+ "\"" + WAVELENGTH + "\": {\n" +
+ "\"" + VALUE + "\": " + wavelength + "\n" +
+ "}\n" +
+ "}";
+ log.debug("request:\n{}", request);
+ return request;
+
+ }
+
+ private String genFrequencyChangeRequest(double frequency) {
+ String request = "{\n" +
+ "\"" + FREQUENCY_KEY + "\": {\n" +
+ "\"" + VALUE + "\": " + Double.toString(frequency) + "\n" +
+ "}\n" +
+ "}";
+ log.debug("request:\n{}", request);
+ return request;
+
+ }
+
+ private String genChannelChangeRequest(int channel) {
+ String request = "{\n" +
+ "\"" + LINE_SYSTEM_CHANNEL_NUMBER + "\": " +
+ Integer.toString(channel) + "\n}";
+ log.debug("request:\n{}", request);
+ return request;
+
+ }
+
+
+ private final String genUri(String uriFormat, PortNumber port) {
+ return String.format(uriFormat, port.name());
+ }
+
+ private boolean isPortState(PortNumber number, String state) {
+ log.debug("checking port {} state is {} or not on device {}", number, state, deviceId);
+ String uri = genUri(PORT_STATE_URI, number);
+ JsonNode jsonNode;
+ try {
+ jsonNode = get(uri);
+ } catch (IOException e) {
+ log.error("unable to get port state on device {}", deviceId);
+ return false;
+ }
+ return jsonNode.get(STATE).get(ADMIN_STATE).asText().equals(state);
+
+ }
+
+ private boolean confirmPortState(long timePeriodInMillis, int iterations, PortNumber number, String state) {
+ for (int i = 0; i < iterations; i++) {
+ log.debug("looping for port state with time period {}ms on device {}. try number {}/{}",
+ timePeriodInMillis, deviceId, i + 1, iterations);
+ if (isPortState(number, state)) {
+ return true;
+ }
+ try {
+ Thread.sleep(timePeriodInMillis);
+ } catch (InterruptedException e) {
+ log.error("unable to sleep thread for device {}\n", deviceId, e);
+ Thread.currentThread().interrupt();
+ return false;
+ }
+ }
+ return false;
+ }
+
+ private boolean changePortState(PortNumber number, String state) {
+ log.debug("changing the port {} on device {} state to {}", number, deviceId, state);
+ String uri = genUri(PORT_STATE_URI, number);
+ String request = genPortStateRequest(state);
+
+ boolean response = putNoReply(uri, request);
+ if (!response) {
+ log.error("unable to change port {} on device {} state to {}", number, deviceId, state);
+ }
+
+ // 5 tries with 2 sec delay
+ long timePeriod = 2000;
+ int iterations = 5;
+ return confirmPortState(timePeriod, iterations, number, state);
+ }
+
+ public boolean disablePort(PortNumber number) {
+ return changePortState(number, DISABLED);
+ }
+
+ public boolean enablePort(PortNumber number) {
+ return changePortState(number, ENABLED);
+ }
+
+ public final boolean changeFrequency(OchSignal signal, PortNumber outPort) {
+ String uri = genUri(FREQUENCY_URI, outPort);
+ double frequency = signal.centralFrequency().asGHz();
+ String request = genFrequencyChangeRequest(frequency);
+ boolean response = putNoReply(uri, request);
+ if (!response) {
+ log.error("unable to change frequency of port {} on device {}", outPort, deviceId);
+ }
+ return response;
+ }
+
+ public final boolean changeChannel(OchSignal signal, PortNumber outPort) {
+ String uri = genUri(CHANNEL_URI, outPort);
+ int channel = signal.spacingMultiplier();
+ log.debug("channel is {} for port {} on device {}", channel, outPort.name(), deviceId);
+ String request = genChannelChangeRequest(channel);
+ boolean response = putNoReply(uri, request);
+ if (!response) {
+ log.error("unable to change channel to {} for port {} on device {}",
+ channel, outPort.name(), deviceId);
+ }
+ return response;
+ }
+
+ private int getChannel(PortNumber port) {
+ try {
+ String uri = genUri(CHANNEL_URI, port);
+ JsonNode response = get(uri);
+ return response.get(LINE_SYSTEM_CHANNEL_NUMBER).asInt();
+ } catch (IOException e) {
+ // this is expected for client side ports as they don't contain channel data
+ log.error("unable to get channel for port {} on device {}:\n{}", port, deviceId, e);
+ return -1;
+ }
+
+ }
+
+ private int getChannelFromFrequency(Frequency frequency) {
+ return (int) frequency.subtract(Spectrum.CENTER_FREQUENCY)
+ .floorDivision(CHANNEL_SPACING.frequency().asHz()).asHz();
+
+ }
+
+ private Frequency getFrequency(PortNumber port) {
+ try {
+ String uri = genUri(FREQUENCY_URI, port);
+ JsonNode response = get(uri);
+ return Frequency.ofGHz(response.get(FREQUENCY_KEY).get(VALUE).asDouble());
+ } catch (IOException e) {
+ // this is expected for client side ports as they don't contain frequency data
+ log.error("unable to get frequency for port {} on device {}:\n{}", port, deviceId, e);
+ return null;
+ }
+
+ }
+
+ private AlarmEntityId getAlarmSource(String instance) {
+ AlarmEntityId source;
+ if (instance.contains(PORT)) {
+ source = AlarmEntityId.alarmEntityId(instance.replace("-", ":"));
+ } else if (instance.contains(PTP)) {
+ source = AlarmEntityId.alarmEntityId(instance.replace(PTP + "-", PORT + ":"));
+ } else {
+ source = AlarmEntityId.alarmEntityId(OTHER + ":" + instance);
+ }
+ return source;
+ }
+
+ private long parseAlarmTime(String time) {
+ /*
+ * expecting WaveServer time to be set to UTC.
+ */
+ try {
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_TIME_FORMAT);
+ LocalDateTime localDateTime = LocalDateTime.parse(time, formatter);
+ return localDateTime.atZone(ZoneId.of(UTC)).toInstant().toEpochMilli();
+ } catch (DateTimeParseException e2) {
+ log.error("unable to parse time {}, using system time", time);
+ return System.currentTimeMillis();
+ }
+ }
+
+ private Alarm newAlarmFromJsonNode(JsonNode jsonNode) {
+ try {
+ AlarmId alarmId = AlarmId.alarmId(checkNotNull(jsonNode.get(ALARM_INSTANCE_ID)).asText());
+ String time = checkNotNull(jsonNode.get(ALARM_LOCAL_DATE_TIME)).asText();
+ String instance = checkNotNull(jsonNode.get(INSTANCE).asText()).toLowerCase();
+ String description = checkNotNull(jsonNode.get(DESCRIPTION)).asText() + " - " + instance + " - " + time;
+ AlarmEntityId source = getAlarmSource(instance);
+ Alarm.SeverityLevel severity = Alarm.SeverityLevel.valueOf(checkNotNull(
+ jsonNode.get(SEVERITY)).asText().toUpperCase());
+
+ long timeRaised = parseAlarmTime(time);
+ boolean isAcknowledged = checkNotNull(jsonNode.get(ACKNOWLEDGE)).asBoolean();
+
+ return new DefaultAlarm.Builder(alarmId, deviceId, description, severity, timeRaised)
+ .withAcknowledged(isAcknowledged)
+ .forSource(source)
+ .build();
+
+ } catch (NullPointerException e) {
+ log.error("got exception while parsing alarm json node {} for device {}:\n", jsonNode, deviceId, e);
+ return null;
+ }
+
+ }
+
+ private List<Alarm> getActiveAlarms() {
+ log.debug("getting active alarms for device {}", deviceId);
+ try {
+ List<JsonNode> alarms = Lists.newArrayList(get(ACTIVE_ALARMS_URL).get(ACTIVE).elements());
+ return alarms.stream()
+ .map(this::newAlarmFromJsonNode)
+ .filter(Objects::nonNull)
+ .collect(Collectors.toList());
+ } catch (IOException e) {
+ log.error("unable to get active alarms for device {}:\n", deviceId, e);
+ return null;
+ }
+ }
+
+ public Collection<FlowEntry> getFlowEntries() {
+ List<Port> ports = deviceService.getPorts(deviceId);
+ //driver only handles lineSide ports
+ //TODO: handle client ports as well
+ return ports.stream()
+ .filter(p -> LINESIDE_PORT_ID.contains(p.number().name()))
+ .map(p -> fetchRule(p.number()))
+ .filter(Objects::nonNull)
+ .map(fr -> new DefaultFlowEntry(fr, FlowEntry.FlowEntryState.ADDED, 0, 0, 0))
+ .collect(Collectors.toList());
+ }
+
+ private FlowRule fetchRule(PortNumber port) {
+ Frequency frequency = getFrequency(port);
+ if (frequency == null) {
+ return null;
+ }
+ int channel = getChannelFromFrequency(frequency);
+ /*
+ * both inPort and outPort will be same as WaveServer only deal with same port ptp-indexes
+ * channel and spaceMultiplier are same.
+ * TODO: find a way to get both inPort and outPort for future when inPort may not be equal to outPort
+ */
+
+ TrafficSelector selector = DefaultTrafficSelector.builder()
+ .matchInPort(port)
+ .add(Criteria.matchOchSignalType(OchSignalType.FIXED_GRID))
+ .add(Criteria.matchLambda(OchSignal.newDwdmSlot(CHANNEL_SPACING, channel)))
+ .build();
+ TrafficTreatment treatment = DefaultTrafficTreatment.builder()
+ .setOutput(port)
+ .build();
+
+ int hash = Objects.hash(deviceId, selector, treatment);
+ Pair<FlowId, Integer> lookup = crossConnectCache.get(hash);
+ if (lookup == null) {
+ return null;
+ }
+
+ return DefaultFlowRule.builder()
+ .forDevice(deviceId)
+ .makePermanent()
+ .withSelector(selector)
+ .withTreatment(treatment)
+ .withPriority(lookup.getRight())
+ .withCookie(lookup.getLeft().value())
+ .build();
+ }
+
+ public List<Alarm> getAlarms() {
+ log.debug("getting alarms for device {}", deviceId);
+ return getActiveAlarms();
+ }
+
+ private JsonNode get(String uri) throws IOException {
+ InputStream response = controller.get(deviceId, uri, MediaType.valueOf(MediaType.APPLICATION_JSON));
+ ObjectMapper om = new ObjectMapper();
+ final ObjectReader reader = om.reader();
+ // all waveserver responses contain data node, which contains the requested data
+ return reader.readTree(response).get(DATA);
+ }
+
+ private int put(String uri, String request) {
+ InputStream payload = new ByteArrayInputStream(request.getBytes(StandardCharsets.UTF_8));
+ int response = controller.put(deviceId, uri, payload, MediaType.valueOf(MediaType.APPLICATION_JSON));
+ log.debug("response: {}", response);
+ return response;
+ }
+
+ private boolean putNoReply(String uri, String request) {
+ return put(uri, request) == Response.Status.NO_CONTENT.getStatusCode();
+ }
+
+}
diff --git a/drivers/ciena/waveserver/src/main/java/org/onosproject/drivers/ciena/waveserver/rest/CienaWaveServerAlarmConsumer.java b/drivers/ciena/waveserver/src/main/java/org/onosproject/drivers/ciena/waveserver/rest/CienaWaveServerAlarmConsumer.java
new file mode 100644
index 0000000..c1a6767
--- /dev/null
+++ b/drivers/ciena/waveserver/src/main/java/org/onosproject/drivers/ciena/waveserver/rest/CienaWaveServerAlarmConsumer.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.drivers.ciena.waveserver.rest;
+
+import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
+import org.onosproject.incubator.net.faultmanagement.alarm.AlarmConsumer;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.slf4j.Logger;
+
+import java.util.List;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Ciena Wave Server Alarm Consumer.
+ */
+public class CienaWaveServerAlarmConsumer extends AbstractHandlerBehaviour implements AlarmConsumer {
+ CienaRestDevice restCiena;
+
+ private final Logger log = getLogger(getClass());
+
+ @Override
+ public List<Alarm> consumeAlarms() {
+ try {
+ restCiena = new CienaRestDevice(handler());
+ } catch (NullPointerException e) {
+ log.error("unable to create CienaRestDevice:\n", e);
+ return null;
+ }
+ return restCiena.getAlarms();
+ }
+}
diff --git a/drivers/ciena/waveserver/src/main/java/org/onosproject/drivers/ciena/waveserver/rest/CienaWaveServerLambdaQuery.java b/drivers/ciena/waveserver/src/main/java/org/onosproject/drivers/ciena/waveserver/rest/CienaWaveServerLambdaQuery.java
new file mode 100644
index 0000000..cb5d452
--- /dev/null
+++ b/drivers/ciena/waveserver/src/main/java/org/onosproject/drivers/ciena/waveserver/rest/CienaWaveServerLambdaQuery.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.drivers.ciena.waveserver.rest;
+
+import org.onosproject.net.ChannelSpacing;
+import org.onosproject.net.OchSignal;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.behaviour.LambdaQuery;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+
+
+import com.google.common.collect.ImmutableSet;
+
+import java.util.Set;
+import java.util.stream.IntStream;
+
+/**
+ * Ciena WaveServer Lambda query.
+ * 88 50GHz flex grid channels with 6.25 slot width, starting from 0 to 87.
+ */
+public class CienaWaveServerLambdaQuery extends AbstractHandlerBehaviour implements LambdaQuery {
+
+ @Override
+ public Set<OchSignal> queryLambdas(PortNumber port) {
+ // 88 channels of 50 GHz with 6.25 GHz slothWidth
+ int slots = (int) (ChannelSpacing.CHL_50GHZ.frequency().asHz() /
+ ChannelSpacing.CHL_6P25GHZ.frequency().asHz());
+ int channels = 88;
+ // total lambdas are equal to: channels * slots
+ return IntStream.rangeClosed(0, channels * slots)
+ .mapToObj(OchSignal::newFlexGridSlot)
+ .collect(ImmutableSet.toImmutableSet());
+ }
+
+}
+
+
diff --git a/drivers/ciena/waveserver/src/main/java/org/onosproject/drivers/ciena/waveserver/rest/CienaWaveserverDeviceDescription.java b/drivers/ciena/waveserver/src/main/java/org/onosproject/drivers/ciena/waveserver/rest/CienaWaveserverDeviceDescription.java
new file mode 100644
index 0000000..0cd3b2b
--- /dev/null
+++ b/drivers/ciena/waveserver/src/main/java/org/onosproject/drivers/ciena/waveserver/rest/CienaWaveserverDeviceDescription.java
@@ -0,0 +1,190 @@
+/*
+ * Copyright 2016-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.drivers.ciena.waveserver.rest;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
+import org.apache.commons.configuration.HierarchicalConfiguration;
+import org.onlab.packet.ChassisId;
+import org.onosproject.drivers.utilities.XmlConfigParser;
+import org.onosproject.net.AnnotationKeys;
+import org.onosproject.net.ChannelSpacing;
+import org.onosproject.net.CltSignalType;
+import org.onosproject.net.DefaultAnnotations;
+import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.GridType;
+import org.onosproject.net.OchSignal;
+import org.onosproject.net.OduSignalType;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.SparseAnnotations;
+import org.onosproject.net.device.DefaultDeviceDescription;
+import org.onosproject.net.device.DeviceDescription;
+import org.onosproject.net.device.DeviceDescriptionDiscovery;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.device.PortDescription;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.onosproject.protocol.rest.RestSBController;
+import org.slf4j.Logger;
+
+import javax.ws.rs.core.MediaType;
+import java.util.List;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onosproject.net.optical.device.OchPortHelper.ochPortDescription;
+import static org.onosproject.net.optical.device.OduCltPortHelper.oduCltPortDescription;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Discovers the ports from a Ciena WaveServer Rest device.
+ */
+//TODO: Use CienaRestDevice
+public class CienaWaveserverDeviceDescription extends AbstractHandlerBehaviour
+ implements DeviceDescriptionDiscovery {
+
+ private final Logger log = getLogger(getClass());
+
+ private static final String PORT_ID = "ptp-index";
+ private static final String XML = "xml";
+ private static final String ENABLED = "enabled";
+ private static final String ADMIN_STATE = "state.admin-state";
+ private static final String PORTS = "ws-ptps.ptps";
+ private static final String PORT_IN = "properties.line-system.cmd.port-in";
+ private static final String PORT_OUT = "properties.line-system.cmd.port-out";
+
+ private static final String CHANNEL_ID =
+ "properties.transmitter.line-system-channel-number";
+
+ private static final String PORT_REQUEST =
+ "ciena-ws-ptp:ws-ptps?config=true&format=xml&depth=unbounded";
+
+ @Override
+ public DeviceDescription discoverDeviceDetails() {
+ log.debug("getting device description");
+ DeviceService deviceService = checkNotNull(handler().get(DeviceService.class));
+ DeviceId deviceId = handler().data().deviceId();
+ Device device = deviceService.getDevice(deviceId);
+
+ if (device == null) {
+ return new DefaultDeviceDescription(deviceId.uri(),
+ Device.Type.OTN,
+ "Ciena",
+ "WaveServer",
+ "Unknown",
+ "Unknown",
+ new ChassisId());
+ } else {
+ return new DefaultDeviceDescription(device.id().uri(),
+ Device.Type.OTN,
+ device.manufacturer(),
+ device.hwVersion(),
+ device.swVersion(),
+ device.serialNumber(),
+ device.chassisId());
+ }
+ }
+
+ @Override
+ public List<PortDescription> discoverPortDetails() {
+ return getPorts();
+ }
+
+ private List<PortDescription> getPorts() {
+ /*
+ * Relationship between ptp-index and port number shown in Ciena Wave Server
+ * CLI:
+ * ptp-index = 4 * port_number (without decimal) + decimal
+ * e.g
+ * if port_number is 5 then ptp-index = 5 * 4 + 0 = 20
+ * if port_number is 5.1 then ptp-index = 5 * 4 + 1 = 21
+ *
+ * Relationship between channelId and in/out port:
+ * in_port = channelId * 2
+ * out_port = channelId * 2 -1
+ */
+ List<PortDescription> ports = Lists.newArrayList();
+ RestSBController controller = checkNotNull(handler().get(RestSBController.class));
+ DeviceId deviceId = handler().data().deviceId();
+
+ HierarchicalConfiguration config = XmlConfigParser.
+ loadXml(controller.get(deviceId, PORT_REQUEST, MediaType.APPLICATION_XML_TYPE));
+ List<HierarchicalConfiguration> portsConfig =
+ parseWaveServerCienaPorts(config);
+ portsConfig.forEach(sub -> {
+ String portId = sub.getString(PORT_ID);
+ DefaultAnnotations.Builder annotations = DefaultAnnotations.builder();
+ if (CienaRestDevice.getLinesidePortId().contains(portId)) {
+ annotations.set(AnnotationKeys.CHANNEL_ID, sub.getString(CHANNEL_ID));
+ // TX/OUT and RX/IN ports
+ annotations.set(AnnotationKeys.PORT_OUT, sub.getString(PORT_OUT));
+ annotations.set(AnnotationKeys.PORT_IN, sub.getString(PORT_IN));
+ ports.add(parseWaveServerCienaOchPorts(
+ Long.valueOf(portId),
+ sub,
+ annotations.build()));
+
+ } else if (!portId.equals("5") && !portId.equals("49")) {
+ DefaultAnnotations.builder()
+ .set(AnnotationKeys.PORT_NAME, portId);
+ //FIXME change when all optical types have two way information methods, see jira tickets
+ ports.add(oduCltPortDescription(PortNumber.portNumber(sub.getLong(PORT_ID)),
+ sub.getString(ADMIN_STATE).equals(ENABLED),
+ CltSignalType.CLT_100GBE, annotations.build()));
+ }
+ });
+ return ImmutableList.copyOf(ports);
+ }
+
+ public static List<HierarchicalConfiguration> parseWaveServerCienaPorts(HierarchicalConfiguration cfg) {
+ return cfg.configurationsAt(PORTS);
+ }
+
+ public static PortDescription parseWaveServerCienaOchPorts(long portNumber,
+ HierarchicalConfiguration config,
+ SparseAnnotations annotations) {
+ final List<String> tunableType = Lists.newArrayList("performance-optimized", "accelerated");
+ final String flexGrid = "flex-grid";
+ final String state = "properties.transmitter.state";
+ final String tunable = "properties.modem.tx-tuning-mode";
+ final String spacing = "properties.line-system.wavelength-spacing";
+ final String frequency = "properties.transmitter.frequency.value";
+
+ boolean isEnabled = config.getString(state).equals(ENABLED);
+ boolean isTunable = tunableType.contains(config.getString(tunable));
+
+ GridType gridType = config.getString(spacing).equals(flexGrid) ? GridType.FLEX : null;
+ ChannelSpacing chSpacing = gridType == GridType.FLEX ? ChannelSpacing.CHL_6P25GHZ : null;
+
+ //Working in Ghz //(Nominal central frequency - 193.1)/channelSpacing = spacingMultiplier
+ final int baseFrequency = 193100;
+ long spacingFrequency = chSpacing == null ? baseFrequency : chSpacing.frequency().asHz();
+ int spacingMult = ((int) (toGbps(((int) config.getDouble(frequency) -
+ baseFrequency)) / toGbpsFromHz(spacingFrequency))); //FIXME is there a better way ?
+
+ return ochPortDescription(PortNumber.portNumber(portNumber), isEnabled, OduSignalType.ODU4, isTunable,
+ new OchSignal(gridType, chSpacing, spacingMult, 1), annotations);
+ }
+
+ //FIXME remove when all optical types have two way information methods, see jira tickets
+ private static long toGbps(long speed) {
+ return speed * 1000;
+ }
+
+ private static long toGbpsFromHz(long speed) {
+ return speed / 1000;
+ }
+}
+
diff --git a/drivers/ciena/waveserver/src/main/java/org/onosproject/drivers/ciena/waveserver/rest/CienaWaveserverPortAdmin.java b/drivers/ciena/waveserver/src/main/java/org/onosproject/drivers/ciena/waveserver/rest/CienaWaveserverPortAdmin.java
new file mode 100644
index 0000000..d3895c1
--- /dev/null
+++ b/drivers/ciena/waveserver/src/main/java/org/onosproject/drivers/ciena/waveserver/rest/CienaWaveserverPortAdmin.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2016-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.drivers.ciena.waveserver.rest;
+
+import org.onlab.util.Tools;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.behaviour.PortAdmin;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.slf4j.Logger;
+
+import java.util.concurrent.CompletableFuture;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+
+public class CienaWaveserverPortAdmin extends AbstractHandlerBehaviour
+ implements PortAdmin {
+ private CienaRestDevice restCiena;
+ private final Logger log = getLogger(getClass());
+
+ @Override
+ public CompletableFuture<Boolean> disable(PortNumber number) {
+ try {
+ restCiena = new CienaRestDevice(handler());
+ } catch (NullPointerException e) {
+ log.error("unable to create CienaRestDevice, {}", e);
+ return CompletableFuture.completedFuture(false);
+ }
+ return CompletableFuture.completedFuture(restCiena.disablePort(number));
+ }
+
+ @Override
+ public CompletableFuture<Boolean> enable(PortNumber number) {
+ try {
+ restCiena = new CienaRestDevice(handler());
+ } catch (NullPointerException e) {
+ log.error("unable to create CienaRestDevice, {}", e);
+ return CompletableFuture.completedFuture(false);
+ }
+ return CompletableFuture.completedFuture(restCiena.enablePort(number));
+ }
+
+ @Override
+ public CompletableFuture<Boolean> isEnabled(PortNumber number) {
+ return Tools.exceptionalFuture(
+ new UnsupportedOperationException("isEnabled is not supported"));
+
+ }
+}
diff --git a/drivers/ciena/waveserver/src/main/java/org/onosproject/drivers/ciena/waveserver/rest/package-info.java b/drivers/ciena/waveserver/src/main/java/org/onosproject/drivers/ciena/waveserver/rest/package-info.java
new file mode 100644
index 0000000..220063b
--- /dev/null
+++ b/drivers/ciena/waveserver/src/main/java/org/onosproject/drivers/ciena/waveserver/rest/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Package for Ciena device drivers.
+ */
+package org.onosproject.drivers.ciena.waveserver.rest;
\ No newline at end of file