fahadnaeemkhan | 7182724 | 2017-09-21 15:10:07 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.drivers.ciena; |
| 17 | |
| 18 | import org.onlab.util.Frequency; |
| 19 | import org.onosproject.net.DeviceId; |
| 20 | import org.onosproject.net.OchSignal; |
| 21 | import org.onosproject.net.PortNumber; |
| 22 | import org.onosproject.net.driver.DriverHandler; |
| 23 | import org.onosproject.protocol.rest.RestSBController; |
| 24 | import org.slf4j.Logger; |
| 25 | |
| 26 | import javax.ws.rs.core.MediaType; |
| 27 | import javax.ws.rs.core.Response; |
| 28 | import java.io.ByteArrayInputStream; |
| 29 | import java.io.InputStream; |
| 30 | import java.nio.charset.StandardCharsets; |
| 31 | |
| 32 | import static com.google.common.base.Preconditions.checkNotNull; |
| 33 | import static org.slf4j.LoggerFactory.getLogger; |
| 34 | |
| 35 | public class CienaRestDevice { |
| 36 | private DeviceId deviceId; |
| 37 | private RestSBController controller; |
| 38 | |
| 39 | private final Logger log = getLogger(getClass()); |
| 40 | private static final String ENABLED = "enabled"; |
| 41 | private static final String DISABLED = "disabled"; |
| 42 | private static final Frequency BASE_FREQUENCY = Frequency.ofGHz(193_950); |
| 43 | private static final int MULTIPLIER_OFFSET = 80; |
| 44 | |
| 45 | //URIs |
| 46 | private static final String PORT_URI = "ws-ptps/ptps/%s"; |
| 47 | private static final String TRANSMITTER_URI = PORT_URI + "/properties/transmitter"; |
| 48 | private static final String PORT_STATE_URI = PORT_URI + "/state"; |
| 49 | private static final String WAVELENGTH_URI = TRANSMITTER_URI + "/wavelength"; |
| 50 | private static final String FREQUENCY_URI = TRANSMITTER_URI + "/ciena-ws-ptp-modem:frequency"; |
| 51 | private static final String CHANNEL_URI = TRANSMITTER_URI + "/ciena-ws-ptp-modem:line-system-channel-number"; |
| 52 | |
| 53 | public CienaRestDevice(DriverHandler handler) throws NullPointerException { |
| 54 | deviceId = handler.data().deviceId(); |
| 55 | controller = checkNotNull(handler.get(RestSBController.class)); |
| 56 | } |
| 57 | |
| 58 | private final String genPortStateRequest(String state) { |
| 59 | String request = "{\n" + |
| 60 | "\"state\": {\n" + |
| 61 | "\"admin-state\": \"" + state + "\"\n}\n}"; |
| 62 | log.debug("generated request: \n{}", request); |
| 63 | return request; |
| 64 | } |
| 65 | |
| 66 | private String genWavelengthChangeRequest(String wavelength) { |
| 67 | String request = "{\n" + |
| 68 | "\"wavelength\": {\n" + |
| 69 | "\"value\": " + wavelength + "\n" + |
| 70 | "}\n" + |
| 71 | "}"; |
| 72 | log.debug("request:\n{}", request); |
| 73 | return request; |
| 74 | |
| 75 | } |
| 76 | |
| 77 | private String genFrequencyChangeRequest(long wavelength) { |
| 78 | String request = "{\n" + |
| 79 | "\"ciena-ws-ptp-modem:frequency\": {\n" + |
| 80 | "\"value\": " + Long.toString(wavelength) + "\n" + |
| 81 | "}\n" + |
| 82 | "}"; |
| 83 | log.debug("request:\n{}", request); |
| 84 | return request; |
| 85 | |
| 86 | } |
| 87 | |
| 88 | private String genChannelChangeRequest(int channel) { |
| 89 | String request = "{\n" + |
| 90 | "\"ciena-ws-ptp-modem:line-system-channel-number\": " + |
| 91 | Integer.toString(channel) + "\n}"; |
| 92 | log.debug("request:\n{}", request); |
| 93 | return request; |
| 94 | |
| 95 | } |
| 96 | |
| 97 | |
| 98 | private final String genUri(String uriFormat, PortNumber port) { |
| 99 | return String.format(uriFormat, port.name()); |
| 100 | } |
| 101 | |
| 102 | private boolean changePortState(PortNumber number, String state) { |
| 103 | log.debug("changing the port {} state to {}", number, state); |
| 104 | String uri = genUri(PORT_STATE_URI, number); |
| 105 | String request = genPortStateRequest(state); |
| 106 | return putNoReply(uri, request); |
| 107 | } |
| 108 | |
| 109 | public boolean disablePort(PortNumber number) { |
| 110 | return changePortState(number, DISABLED); |
| 111 | } |
| 112 | |
| 113 | public boolean enablePort(PortNumber number) { |
| 114 | return changePortState(number, ENABLED); |
| 115 | } |
| 116 | |
| 117 | public final boolean changeFrequency(OchSignal signal, PortNumber outPort) { |
| 118 | String uri = genUri(FREQUENCY_URI, outPort); |
| 119 | long frequency = toFrequency(signal); |
| 120 | String request = genFrequencyChangeRequest(frequency); |
| 121 | return putNoReply(uri, request); |
| 122 | } |
| 123 | |
| 124 | public final boolean changeChannel(OchSignal signal, PortNumber outPort) { |
| 125 | String uri = genUri(CHANNEL_URI, outPort); |
| 126 | int channel = signal.spacingMultiplier() + MULTIPLIER_OFFSET; |
| 127 | log.debug("channel is {} for port {} on device {}", channel, outPort.name(), deviceId); |
| 128 | String request = genChannelChangeRequest(channel); |
| 129 | return putNoReply(uri, request); |
| 130 | } |
| 131 | |
| 132 | private final long toFrequency(OchSignal signal) { |
| 133 | double frequency = BASE_FREQUENCY.asGHz() + |
| 134 | (signal.channelSpacing().frequency().asGHz() * (double) signal.slotGranularity()); |
| 135 | return Double.valueOf(frequency).longValue(); |
| 136 | } |
| 137 | |
| 138 | public static int getMultiplierOffset() { |
| 139 | return MULTIPLIER_OFFSET; |
| 140 | } |
| 141 | |
| 142 | private int put(String uri, String request) { |
| 143 | InputStream payload = new ByteArrayInputStream(request.getBytes(StandardCharsets.UTF_8)); |
| 144 | int response = controller.put(deviceId, uri, payload, MediaType.valueOf(MediaType.APPLICATION_JSON)); |
| 145 | log.debug("response: {}", response); |
| 146 | return response; |
| 147 | } |
| 148 | |
| 149 | private boolean putNoReply(String uri, String request) { |
| 150 | return put(uri, request) == Response.Status.NO_CONTENT.getStatusCode(); |
| 151 | } |
| 152 | |
| 153 | } |