blob: 124de16c48df706b12426627f9ce70339d123d3d [file] [log] [blame]
Georgios Katsikas13ccba62020-03-18 12:05:03 +01001/*
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
17package org.onosproject.drivers.server;
18
19import org.onosproject.net.DeviceId;
20import org.onosproject.net.behaviour.BasicSystemOperations;
21import org.onosproject.net.driver.DriverHandler;
22import org.onosproject.protocol.rest.RestSBDevice;
23
24import org.slf4j.Logger;
25
26import com.fasterxml.jackson.databind.JsonNode;
27import com.fasterxml.jackson.databind.ObjectMapper;
28
29import java.io.InputStream;
30import java.io.IOException;
31import java.util.Map;
32import java.util.concurrent.CompletableFuture;
33import javax.ws.rs.ProcessingException;
34
35import static com.google.common.base.Preconditions.checkArgument;
36import static com.google.common.base.Preconditions.checkNotNull;
37import static java.util.concurrent.CompletableFuture.completedFuture;
38import static org.onosproject.drivers.server.Constants.JSON;
39import static org.onosproject.drivers.server.Constants.MSG_HANDLER_NULL;
Georgios Katsikas13ccba62020-03-18 12:05:03 +010040import static org.onosproject.drivers.server.Constants.MSG_DEVICE_ID_NULL;
Georgios Katsikas1a64c3a2020-06-03 17:30:43 +020041import static org.onosproject.drivers.server.Constants.MSG_DEVICE_NULL;
Georgios Katsikas13ccba62020-03-18 12:05:03 +010042import static org.onosproject.drivers.server.Constants.PARAM_TIME;
43import static org.onosproject.drivers.server.Constants.URL_SRV_TIME_DISCOVERY;
44import static org.slf4j.LoggerFactory.getLogger;
45
46/**
47 * Implementation of basic system operations' behaviour for server devices.
48 */
49public 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}