blob: 411a8bf0c720352a5d4b533b78fa9b3bc19472e2 [file] [log] [blame]
oleksandr.yashchuk@plvision.eu3dbcaaf2019-03-13 14:44:46 +02001/*
2 * Copyright 2019-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.gnoi;
18
19import gnoi.system.SystemOuterClass.RebootRequest;
20import gnoi.system.SystemOuterClass.RebootResponse;
21import gnoi.system.SystemOuterClass.RebootMethod;
22import org.onosproject.net.behaviour.BasicSystemOperations;
23import java.util.concurrent.CompletableFuture;
24
25import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
28/**
29 * Implementation of the BasicSystemOperations behavior for gNOI-enabled devices.
30 */
31public class GnoiBasicSystemOperationsImpl
32 extends AbstractGnoiHandlerBehaviour implements BasicSystemOperations {
33
34 private static final Logger log = LoggerFactory
35 .getLogger(GnoiBasicSystemOperationsImpl.class);
36
37 @Override
38 public CompletableFuture<Boolean> reboot() {
39 if (!setupBehaviour()) {
40 return CompletableFuture.completedFuture(false);
41 }
42
43 final RebootRequest.Builder requestMsg = RebootRequest.newBuilder().setMethod(RebootMethod.COLD);
44 final CompletableFuture<Boolean> future = client.reboot(requestMsg.build())
45 .handle((response, error) -> {
46 if (error == null) {
47 log.debug("gNOI reboot() for device {} returned {}", deviceId, response);
48 return RebootResponse.getDefaultInstance().equals(response);
49 } else {
50 log.error("Exception while performing gNOI reboot() for device " + deviceId, error);
51 return false;
52 }
53 });
54
55 return future;
56 }
57
58 @Override
59 public CompletableFuture<Long> time() {
60 if (!setupBehaviour()) {
61 return CompletableFuture.completedFuture(0L);
62 }
63 final CompletableFuture<Long> future = client.time()
64 .handle((response, error) -> {
65 if (error == null) {
66 log.debug("gNOI time() for device {} returned {}", deviceId.uri(), response.getTime());
67 return response.getTime();
68 } else {
69 log.error("Exception while performing gNOI time() for device " + deviceId, error);
70 return 0L;
71 }
72 });
73
74 return future;
75 }
76}