fahadnaeemkhan | 482951f | 2017-08-24 16:35:17 -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 | |
| 17 | package org.onosproject.drivers.ciena; |
| 18 | |
| 19 | import org.onlab.util.Tools; |
| 20 | import org.onosproject.net.AnnotationKeys; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.net.Port; |
| 23 | import org.onosproject.net.PortNumber; |
| 24 | import org.onosproject.net.behaviour.PortAdmin; |
| 25 | import org.onosproject.net.device.DeviceService; |
| 26 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 27 | import org.onosproject.protocol.rest.RestSBController; |
| 28 | import org.slf4j.Logger; |
| 29 | |
| 30 | import javax.ws.rs.core.MediaType; |
| 31 | import java.util.concurrent.CompletableFuture; |
| 32 | import java.io.ByteArrayInputStream; |
| 33 | import java.io.InputStream; |
| 34 | import java.nio.charset.StandardCharsets; |
| 35 | import javax.ws.rs.core.Response.Status; |
| 36 | |
| 37 | import static com.google.common.base.Preconditions.checkNotNull; |
| 38 | import static org.slf4j.LoggerFactory.getLogger; |
| 39 | |
| 40 | |
| 41 | public class CienaWaveserverPortAdmin extends AbstractHandlerBehaviour |
| 42 | implements PortAdmin { |
| 43 | private final Logger log = getLogger(getClass()); |
| 44 | private static final String APP_JSON = "application/json"; |
| 45 | private static final String ENABLE = "enabled"; |
| 46 | private static final String DISABLE = "disabled"; |
| 47 | |
| 48 | private final String generateUri(long number) { |
| 49 | return String.format("ws-ptps/ptps/%d/state", number); |
| 50 | } |
| 51 | |
| 52 | private final String generateRequest(String state) { |
| 53 | String request = "{\n" + |
| 54 | "\"state\": {\n" + |
| 55 | "\"admin-state\": \"" + state + "\"\n}\n}"; |
| 56 | log.debug("generated request: \n{}", request); |
| 57 | return request; |
| 58 | } |
| 59 | |
| 60 | private final boolean put(long number, String state) { |
| 61 | String uri = generateUri(number); |
| 62 | String request = generateRequest(state); |
| 63 | DeviceId deviceId = handler().data().deviceId(); |
| 64 | RestSBController controller = |
| 65 | checkNotNull(handler().get(RestSBController.class)); |
| 66 | InputStream payload = |
| 67 | new ByteArrayInputStream(request.getBytes(StandardCharsets.UTF_8)); |
| 68 | int response = controller.put(deviceId, uri, payload, |
| 69 | MediaType.valueOf(APP_JSON)); |
| 70 | log.debug("response: {}", response); |
| 71 | // expecting 204/NO_CONTENT_RESPONSE as successful response |
| 72 | return response == Status.NO_CONTENT.getStatusCode(); |
| 73 | } |
| 74 | |
| 75 | // returns null if specified port number was not a line side port |
| 76 | private Long getLineSidePort(PortNumber number) { |
| 77 | DeviceService deviceService = checkNotNull(handler().get(DeviceService.class)); |
| 78 | DeviceId deviceId = handler().data().deviceId(); |
| 79 | Port port = deviceService.getPort(deviceId, number); |
| 80 | if (port != null) { |
| 81 | String channelId = port.annotations().value(AnnotationKeys.CHANNEL_ID); |
| 82 | // any port that has channel is lineSidePort and will have TX and RX |
| 83 | if (channelId != null) { |
| 84 | String portName = port.annotations().value(AnnotationKeys.PORT_NAME); |
| 85 | // last three characters of portName will always be " TX" or " RX" |
| 86 | portName = portName.substring(0, portName.length() - 3); |
| 87 | log.debug("port number {} is mapped to {} lineside port", |
| 88 | number, portName); |
| 89 | return new Long(portName); |
| 90 | } |
| 91 | } |
| 92 | // not a line-side port |
| 93 | return null; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public CompletableFuture<Boolean> disable(PortNumber number) { |
| 98 | log.debug("disabling port {}", number); |
| 99 | Long lineSidePort = getLineSidePort(number); |
| 100 | long devicePortNum; |
| 101 | if (lineSidePort != null) { |
| 102 | devicePortNum = lineSidePort.longValue(); |
| 103 | } else { |
| 104 | devicePortNum = number.toLong(); |
| 105 | } |
| 106 | CompletableFuture<Boolean> result = |
| 107 | CompletableFuture.completedFuture(put(devicePortNum, DISABLE)); |
| 108 | return result; |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public CompletableFuture<Boolean> enable(PortNumber number) { |
| 113 | log.debug("enabling port {}", number); |
| 114 | Long lineSidePort = getLineSidePort(number); |
| 115 | long devicePortNum; |
| 116 | if (lineSidePort != null) { |
| 117 | devicePortNum = lineSidePort.longValue(); |
| 118 | } else { |
| 119 | devicePortNum = number.toLong(); |
| 120 | } |
| 121 | CompletableFuture<Boolean> result = |
| 122 | CompletableFuture.completedFuture(put(devicePortNum, ENABLE)); |
| 123 | return result; |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public CompletableFuture<Boolean> isEnabled(PortNumber number) { |
| 128 | return Tools.exceptionalFuture( |
| 129 | new UnsupportedOperationException("isEnabled is not supported")); |
| 130 | |
| 131 | } |
| 132 | } |