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.behaviour.BasicSystemOperations; |
| 21 | import org.onosproject.net.driver.DriverHandler; |
| 22 | import org.onosproject.protocol.rest.RestSBDevice; |
| 23 | |
| 24 | import org.slf4j.Logger; |
| 25 | |
| 26 | import com.fasterxml.jackson.databind.JsonNode; |
| 27 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 28 | |
| 29 | import java.io.InputStream; |
| 30 | import java.io.IOException; |
| 31 | import java.util.Map; |
| 32 | import java.util.concurrent.CompletableFuture; |
| 33 | import javax.ws.rs.ProcessingException; |
| 34 | |
| 35 | import static com.google.common.base.Preconditions.checkArgument; |
| 36 | import static com.google.common.base.Preconditions.checkNotNull; |
| 37 | import static java.util.concurrent.CompletableFuture.completedFuture; |
| 38 | import static org.onosproject.drivers.server.Constants.JSON; |
| 39 | import static org.onosproject.drivers.server.Constants.MSG_HANDLER_NULL; |
Georgios Katsikas | 13ccba6 | 2020-03-18 12:05:03 +0100 | [diff] [blame] | 40 | import static org.onosproject.drivers.server.Constants.MSG_DEVICE_ID_NULL; |
Georgios Katsikas | 1a64c3a | 2020-06-03 17:30:43 +0200 | [diff] [blame^] | 41 | import static org.onosproject.drivers.server.Constants.MSG_DEVICE_NULL; |
Georgios Katsikas | 13ccba6 | 2020-03-18 12:05:03 +0100 | [diff] [blame] | 42 | import static org.onosproject.drivers.server.Constants.PARAM_TIME; |
| 43 | import static org.onosproject.drivers.server.Constants.URL_SRV_TIME_DISCOVERY; |
| 44 | import static org.slf4j.LoggerFactory.getLogger; |
| 45 | |
| 46 | /** |
| 47 | * Implementation of basic system operations' behaviour for server devices. |
| 48 | */ |
| 49 | public class ServerBasicSystemOperations |
| 50 | extends BasicServerDriver |
| 51 | implements BasicSystemOperations { |
| 52 | |
| 53 | private final Logger log = getLogger(getClass()); |
| 54 | |
| 55 | public ServerBasicSystemOperations() { |
| 56 | super(); |
| 57 | log.debug("Started"); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public DriverHandler handler() { |
| 62 | return super.getHandler(); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public void setHandler(DriverHandler handler) { |
| 67 | checkNotNull(handler, MSG_HANDLER_NULL); |
| 68 | this.handler = handler; |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public CompletableFuture<Boolean> reboot() { |
| 73 | throw new UnsupportedOperationException("Reboot operation not supported"); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public CompletableFuture<Long> time() { |
| 78 | // Retrieve the device ID from the handler |
| 79 | DeviceId deviceId = super.getDeviceId(); |
| 80 | checkNotNull(deviceId, MSG_DEVICE_ID_NULL); |
| 81 | |
| 82 | // Get the device |
| 83 | RestSBDevice device = super.getDevice(deviceId); |
| 84 | checkNotNull(device, MSG_DEVICE_NULL); |
| 85 | |
| 86 | // Hit the path that provides the server's time |
| 87 | InputStream response = null; |
| 88 | try { |
| 89 | response = getController().get(deviceId, URL_SRV_TIME_DISCOVERY, JSON); |
| 90 | } catch (ProcessingException pEx) { |
| 91 | log.error("Failed to get the time of device: {}", deviceId); |
| 92 | return null; |
| 93 | } |
| 94 | |
| 95 | // Load the JSON into object |
| 96 | ObjectMapper mapper = new ObjectMapper(); |
| 97 | Map<String, Object> jsonMap = null; |
| 98 | JsonNode jsonNode = null; |
| 99 | try { |
| 100 | jsonMap = mapper.readValue(response, Map.class); |
| 101 | jsonNode = mapper.convertValue(jsonMap, JsonNode.class); |
| 102 | } catch (IOException ioEx) { |
| 103 | log.error("Failed to discover the device details of: {}", deviceId); |
| 104 | return null; |
| 105 | } |
| 106 | |
| 107 | if (jsonNode == null) { |
| 108 | log.error("Failed to discover the device details of: {}", deviceId); |
| 109 | return null; |
| 110 | } |
| 111 | |
| 112 | long time = jsonNode.path(PARAM_TIME).asLong(); |
| 113 | checkArgument(time > 0, "Invalid time format: {}", time); |
| 114 | |
| 115 | return completedFuture(new Long(time)); |
| 116 | } |
| 117 | |
| 118 | } |