Georgios Katsikas | 13ccba6 | 2020-03-18 12:05:03 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-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.server; |
| 18 | |
| 19 | import org.onosproject.net.DeviceId; |
| 20 | import org.onosproject.net.MastershipRole; |
| 21 | import org.onosproject.net.device.DeviceHandshaker; |
| 22 | import org.onosproject.net.driver.DriverHandler; |
| 23 | import org.onosproject.net.flow.FlowRuleService; |
| 24 | import org.onosproject.net.flow.TableStatisticsEntry; |
| 25 | import org.onosproject.mastership.MastershipService; |
| 26 | import org.onosproject.protocol.rest.RestSBDevice; |
| 27 | |
| 28 | import org.slf4j.Logger; |
| 29 | |
| 30 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 31 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 32 | import com.google.common.collect.Lists; |
| 33 | |
| 34 | import java.io.ByteArrayInputStream; |
| 35 | import java.util.List; |
| 36 | import java.util.concurrent.CompletableFuture; |
| 37 | |
| 38 | import static com.google.common.base.Preconditions.checkNotNull; |
| 39 | import static java.util.concurrent.CompletableFuture.completedFuture; |
| 40 | import static org.onosproject.drivers.server.Constants.JSON; |
| 41 | import static org.onosproject.drivers.server.Constants.PARAM_CONNECTION_STATUS; |
| 42 | import static org.onosproject.drivers.server.Constants.MSG_HANDLER_NULL; |
| 43 | import static org.onosproject.drivers.server.Constants.MSG_DEVICE_NULL; |
| 44 | import static org.onosproject.drivers.server.Constants.MSG_DEVICE_ID_NULL; |
| 45 | import static org.onosproject.drivers.server.Constants.URL_SRV_PROBE_CONNECT; |
| 46 | import static org.onosproject.drivers.server.Constants.URL_SRV_PROBE_DISCONNECT; |
| 47 | import static org.slf4j.LoggerFactory.getLogger; |
| 48 | |
| 49 | /** |
| 50 | * Implementation of device handshaker behaviour for server devices. |
| 51 | */ |
| 52 | public class ServerHandshaker |
| 53 | extends BasicServerDriver |
| 54 | implements DeviceHandshaker { |
| 55 | |
| 56 | private final Logger log = getLogger(getClass()); |
| 57 | |
| 58 | public ServerHandshaker() { |
| 59 | super(); |
| 60 | log.debug("Started"); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public DriverHandler handler() { |
| 65 | return super.getHandler(); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public void setHandler(DriverHandler handler) { |
| 70 | checkNotNull(handler, MSG_HANDLER_NULL); |
| 71 | this.handler = handler; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public boolean connect() throws IllegalStateException { |
| 76 | // Retrieve the device ID from the handler |
| 77 | DeviceId deviceId = super.getDeviceId(); |
| 78 | checkNotNull(deviceId, MSG_DEVICE_ID_NULL); |
| 79 | |
| 80 | // Create an object node |
| 81 | ObjectMapper mapper = new ObjectMapper(); |
| 82 | ObjectNode sendObjNode = mapper.createObjectNode(); |
| 83 | sendObjNode.put(PARAM_CONNECTION_STATUS, "connect"); |
| 84 | |
| 85 | // Post the connect message to the server |
| 86 | int response = getController().post( |
| 87 | deviceId, URL_SRV_PROBE_CONNECT, |
| 88 | new ByteArrayInputStream(sendObjNode.toString().getBytes()), JSON); |
| 89 | |
| 90 | // Upon an error, return an empty set of rules |
| 91 | if (!checkStatusCode(response)) { |
| 92 | log.error("Failed to connect to device {}", deviceId); |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | log.info("Successfully connected to device {}", deviceId); |
| 97 | |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public boolean hasConnection() { |
| 103 | return isReachable(); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public void disconnect() { |
| 108 | // Retrieve the device ID from the handler |
| 109 | DeviceId deviceId = super.getDeviceId(); |
| 110 | checkNotNull(deviceId, MSG_DEVICE_ID_NULL); |
| 111 | |
| 112 | // Create an object node |
| 113 | ObjectMapper mapper = new ObjectMapper(); |
| 114 | ObjectNode sendObjNode = mapper.createObjectNode(); |
| 115 | sendObjNode.put(PARAM_CONNECTION_STATUS, "disconnect"); |
| 116 | |
| 117 | // Post the disconnect message to the server |
| 118 | int response = getController().post( |
| 119 | deviceId, URL_SRV_PROBE_DISCONNECT, |
| 120 | new ByteArrayInputStream(sendObjNode.toString().getBytes()), JSON); |
| 121 | |
| 122 | if (!checkStatusCode(response)) { |
| 123 | log.error("Failed to disconnect from device {}", deviceId); |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | log.info("Successfully disconnected from device {}", deviceId); |
| 128 | |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public boolean isReachable() { |
| 134 | // Retrieve the device ID from the handler |
| 135 | DeviceId deviceId = super.getDeviceId(); |
| 136 | checkNotNull(deviceId, MSG_DEVICE_ID_NULL); |
| 137 | |
| 138 | // Get the device |
| 139 | RestSBDevice device = super.getDevice(deviceId); |
| 140 | checkNotNull(device, MSG_DEVICE_NULL); |
| 141 | |
| 142 | return deviceIsActive(device); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public boolean isAvailable() { |
| 147 | return isReachable(); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public CompletableFuture<Boolean> probeReachability() { |
| 152 | // Retrieve the device ID from the handler |
| 153 | DeviceId deviceId = super.getDeviceId(); |
| 154 | checkNotNull(deviceId, MSG_DEVICE_ID_NULL); |
| 155 | |
| 156 | // Probe the driver to ask for flow rule service |
| 157 | FlowRuleService flowService = getHandler().get(FlowRuleService.class); |
| 158 | List<TableStatisticsEntry> tableStats = Lists.newArrayList( |
| 159 | flowService.getFlowTableStatistics(deviceId)); |
| 160 | |
| 161 | // If no statistics fetched, the server is not reachable |
| 162 | return completedFuture(tableStats.isEmpty() ? false : true); |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public CompletableFuture<Boolean> probeAvailability() { |
| 167 | return probeReachability(); |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public void roleChanged(MastershipRole newRole) { |
| 172 | throw new UnsupportedOperationException("Mastership operation not supported"); |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public MastershipRole getRole() { |
| 177 | // Retrieve the device ID from the handler |
| 178 | DeviceId deviceId = super.getDeviceId(); |
| 179 | checkNotNull(deviceId, MSG_DEVICE_ID_NULL); |
| 180 | |
| 181 | // Probe the driver to ask for mastership service |
| 182 | MastershipService mastershipService = getHandler().get(MastershipService.class); |
| 183 | return mastershipService.getLocalRole(deviceId); |
| 184 | } |
| 185 | |
| 186 | } |