blob: 4ab311c7c7524cf1a1076cee280f09bf93ff2dd0 [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.drivers.server.devices.RestServerSBDevice;
20import org.onosproject.drivers.server.devices.nic.NicDevice;
21
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.PortNumber;
24import org.onosproject.net.behaviour.PortAdmin;
25import org.onosproject.net.driver.DriverHandler;
26
27import org.slf4j.Logger;
28
29import com.fasterxml.jackson.databind.ObjectMapper;
30import com.fasterxml.jackson.databind.node.ObjectNode;
31
32import java.io.ByteArrayInputStream;
33import java.util.concurrent.CompletableFuture;
34
35import static com.google.common.base.Preconditions.checkNotNull;
36import static java.util.concurrent.CompletableFuture.completedFuture;
37import static org.onosproject.drivers.server.Constants.JSON;
38import static org.onosproject.drivers.server.Constants.MSG_HANDLER_NULL;
39import static org.onosproject.drivers.server.Constants.MSG_DEVICE_ID_NULL;
40import static org.onosproject.drivers.server.Constants.PARAM_NIC_PORT;
41import static org.onosproject.drivers.server.Constants.PARAM_NIC_PORT_STATUS;
42import static org.onosproject.drivers.server.Constants.URL_NIC_PORT_ADMIN;
43import static org.slf4j.LoggerFactory.getLogger;
44
45/**
46 * Implementation of port admin behaviour for server devices.
47 */
48public class ServerPortAdmin
49 extends BasicServerDriver
50 implements PortAdmin {
51
52 private final Logger log = getLogger(getClass());
53
54 public ServerPortAdmin() {
55 super();
56 log.debug("Started");
57 }
58
59 @Override
60 public DriverHandler handler() {
61 return super.getHandler();
62 }
63
64 @Override
65 public void setHandler(DriverHandler handler) {
66 checkNotNull(handler, MSG_HANDLER_NULL);
67 this.handler = handler;
68 }
69
70 @Override
71 public CompletableFuture<Boolean> enable(PortNumber number) {
72 return doEnable(number, true);
73 }
74
75 @Override
76 public CompletableFuture<Boolean> disable(PortNumber number) {
77 return doEnable(number, false);
78 }
79
80 @Override
81 public CompletableFuture<Boolean> isEnabled(PortNumber number) {
82 // Retrieve the device ID
83 DeviceId deviceId = super.getDeviceId();
84 checkNotNull(deviceId, MSG_DEVICE_ID_NULL);
85
86 // ...and the device itself
87 RestServerSBDevice device = null;
88 try {
89 device = (RestServerSBDevice) getDevice(deviceId);
90 } catch (ClassCastException ccEx) {
91 log.error("Failed to discover ports for device {}", deviceId);
92 return completedFuture(false);
93 }
94
95 // Iterate server's NICs to find the correct port
96 for (NicDevice nic : device.nics()) {
97 if (nic.portNumber() == number.toLong()) {
98 return completedFuture(nic.status());
99 }
100 }
101
102 return completedFuture(false);
103 }
104
105 /**
106 * Perform a NIC port management command.
107 *
108 * @param portNumber port number to manage
109 * @param enabled management flag (true to enable, false to disable the port)
110 * @return boolean status
111 */
112 private CompletableFuture<Boolean> doEnable(PortNumber portNumber, boolean enabled) {
113 // Retrieve the device ID from the handler
114 DeviceId deviceId = super.getDeviceId();
115 checkNotNull(deviceId, MSG_DEVICE_ID_NULL);
116
117 // Create an object node with the port status
118 ObjectMapper mapper = new ObjectMapper();
119 ObjectNode sendObjNode = mapper.createObjectNode();
120 sendObjNode.put(PARAM_NIC_PORT, portNumber.toLong());
121 sendObjNode.put(PARAM_NIC_PORT_STATUS, enabled ? "enable" : "disable");
122
123 // Post the connect message to the server
124 int response = getController().post(
125 deviceId, URL_NIC_PORT_ADMIN,
126 new ByteArrayInputStream(sendObjNode.toString().getBytes()), JSON);
127
128 // Upon an error, return an empty set of rules
129 if (!checkStatusCode(response)) {
130 log.error("Failed to connect to device {}", deviceId);
131 return completedFuture(false);
132 }
133
134 log.info("Successfully sent port {} command to device {}",
135 enabled ? "enable" : "disable", deviceId);
136
137 return completedFuture(true);
138 }
139
140}