blob: 78768867453e8297c260d845b72e80073c21b9d4 [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
Carmelo Casconec32976e2019-04-08 14:50:52 -070019import gnoi.system.SystemOuterClass.RebootMethod;
oleksandr.yashchuk@plvision.eu3dbcaaf2019-03-13 14:44:46 +020020import gnoi.system.SystemOuterClass.RebootRequest;
21import gnoi.system.SystemOuterClass.RebootResponse;
Carmelo Casconec2be50a2019-04-10 00:15:39 -070022import org.onosproject.gnoi.api.GnoiClient;
23import org.onosproject.gnoi.api.GnoiController;
24import org.onosproject.grpc.utils.AbstractGrpcHandlerBehaviour;
oleksandr.yashchuk@plvision.eu3dbcaaf2019-03-13 14:44:46 +020025import org.onosproject.net.behaviour.BasicSystemOperations;
oleksandr.yashchuk@plvision.eu3dbcaaf2019-03-13 14:44:46 +020026
Carmelo Casconec32976e2019-04-08 14:50:52 -070027import java.util.concurrent.CompletableFuture;
28
oleksandr.yashchuk@plvision.eu3dbcaaf2019-03-13 14:44:46 +020029/**
Carmelo Casconec32976e2019-04-08 14:50:52 -070030 * Implementation of the BasicSystemOperations behavior for gNOI-enabled
31 * devices.
oleksandr.yashchuk@plvision.eu3dbcaaf2019-03-13 14:44:46 +020032 */
33public class GnoiBasicSystemOperationsImpl
Carmelo Casconec2be50a2019-04-10 00:15:39 -070034 extends AbstractGrpcHandlerBehaviour<GnoiClient, GnoiController>
35 implements BasicSystemOperations {
oleksandr.yashchuk@plvision.eu3dbcaaf2019-03-13 14:44:46 +020036
Carmelo Casconec2be50a2019-04-10 00:15:39 -070037 public GnoiBasicSystemOperationsImpl() {
38 super(GnoiController.class);
39 }
oleksandr.yashchuk@plvision.eu3dbcaaf2019-03-13 14:44:46 +020040
41 @Override
42 public CompletableFuture<Boolean> reboot() {
Carmelo Casconec32976e2019-04-08 14:50:52 -070043 if (!setupBehaviour("reboot()")) {
oleksandr.yashchuk@plvision.eu3dbcaaf2019-03-13 14:44:46 +020044 return CompletableFuture.completedFuture(false);
45 }
46
47 final RebootRequest.Builder requestMsg = RebootRequest.newBuilder().setMethod(RebootMethod.COLD);
Carmelo Casconec32976e2019-04-08 14:50:52 -070048
49 return client.reboot(requestMsg.build())
oleksandr.yashchuk@plvision.eu3dbcaaf2019-03-13 14:44:46 +020050 .handle((response, error) -> {
51 if (error == null) {
52 log.debug("gNOI reboot() for device {} returned {}", deviceId, response);
53 return RebootResponse.getDefaultInstance().equals(response);
54 } else {
55 log.error("Exception while performing gNOI reboot() for device " + deviceId, error);
56 return false;
57 }
58 });
oleksandr.yashchuk@plvision.eu3dbcaaf2019-03-13 14:44:46 +020059 }
60
61 @Override
62 public CompletableFuture<Long> time() {
Carmelo Casconec32976e2019-04-08 14:50:52 -070063 if (!setupBehaviour("time()")) {
oleksandr.yashchuk@plvision.eu3dbcaaf2019-03-13 14:44:46 +020064 return CompletableFuture.completedFuture(0L);
65 }
Carmelo Casconec32976e2019-04-08 14:50:52 -070066 return client.time()
oleksandr.yashchuk@plvision.eu3dbcaaf2019-03-13 14:44:46 +020067 .handle((response, error) -> {
68 if (error == null) {
69 log.debug("gNOI time() for device {} returned {}", deviceId.uri(), response.getTime());
70 return response.getTime();
71 } else {
72 log.error("Exception while performing gNOI time() for device " + deviceId, error);
73 return 0L;
74 }
75 });
oleksandr.yashchuk@plvision.eu3dbcaaf2019-03-13 14:44:46 +020076 }
77}