Palash Kala | c99b15a | 2017-06-14 09:36:56 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Palash Kala | c99b15a | 2017-06-14 09:36:56 +0900 | [diff] [blame] | 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.arista; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 22 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.behaviour.ControllerConfig; |
| 25 | import org.onosproject.net.behaviour.ControllerInfo; |
| 26 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 27 | import org.onosproject.net.driver.DriverHandler; |
| 28 | import org.onosproject.protocol.rest.RestSBController; |
| 29 | import org.slf4j.Logger; |
| 30 | |
| 31 | import javax.ws.rs.core.MediaType; |
| 32 | import java.io.ByteArrayInputStream; |
| 33 | import java.io.IOException; |
| 34 | import java.io.InputStream; |
| 35 | import java.nio.charset.StandardCharsets; |
| 36 | import java.util.ArrayList; |
| 37 | import java.util.List; |
| 38 | |
| 39 | import static com.google.common.base.Preconditions.checkNotNull; |
| 40 | import static org.slf4j.LoggerFactory.getLogger; |
| 41 | |
| 42 | /** |
| 43 | * Sets, gets and removes the openflow controller configuration from a Arista Rest device. |
| 44 | */ |
| 45 | public class ControllerConfigAristaImpl extends AbstractHandlerBehaviour implements ControllerConfig { |
| 46 | |
| 47 | private static final String CONFIGURE_TERMINAL = "configure"; |
| 48 | private static final String OPENFLOW_CMD = "openflow"; |
| 49 | private static final String REMOVE_CONTROLLER_CMD = "no controller tcp:%s:%d"; |
| 50 | private static final String API_ENDPOINT = "/command-api/"; |
| 51 | private static final String JSON = "json"; |
| 52 | private static final String JSONRPC = "jsonrpc"; |
| 53 | private static final String METHOD = "method"; |
| 54 | private static final String RUN_CMDS = "runCmds"; |
| 55 | private static final String VERSION = "version"; |
| 56 | private static final String ID = "id"; |
| 57 | private static final String PARAMS = "params"; |
| 58 | private static final String FORMAT = "format"; |
| 59 | private static final String TIMESTAMPS = "timestamps"; |
| 60 | private static final String CMDS = "cmds"; |
| 61 | private static final String TWO_POINT_ZERO = "2.0"; |
| 62 | private static final String REMOVE_CONTROLLERS = "removeControllers"; |
| 63 | private static final String ENABLE = "enable"; |
| 64 | private static final int MAX_CONTROLLERS = 8; |
| 65 | private static final Boolean FALSE = false; |
| 66 | private static final int VERSION_1 = 1; |
| 67 | |
| 68 | private final Logger log = getLogger(getClass()); |
| 69 | |
| 70 | @Override |
| 71 | public List<ControllerInfo> getControllers() { |
| 72 | throw new UnsupportedOperationException("get controllers configuration is not supported"); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public void setControllers(List<ControllerInfo> controllers) { |
| 77 | throw new UnsupportedOperationException("set controllers configuration is not supported"); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public void removeControllers(List<ControllerInfo> controllers) { |
| 82 | DriverHandler handler = handler(); |
| 83 | RestSBController controller = checkNotNull(handler.get(RestSBController.class)); |
| 84 | DeviceId deviceId = handler.data().deviceId(); |
| 85 | |
| 86 | List<String> cmds = new ArrayList<>(); |
| 87 | cmds.add(CONFIGURE_TERMINAL); |
| 88 | cmds.add(OPENFLOW_CMD); |
| 89 | controllers.stream().limit(MAX_CONTROLLERS).forEach(c -> cmds |
| 90 | .add(String.format(REMOVE_CONTROLLER_CMD, c.ip().toString(), c.port()))); |
| 91 | |
| 92 | String request = generate(cmds); |
| 93 | log.info("request :{}", request); |
| 94 | |
| 95 | InputStream stream = new ByteArrayInputStream(request.getBytes(StandardCharsets.UTF_8)); |
| 96 | String response = controller.post(deviceId, API_ENDPOINT, stream, |
| 97 | MediaType.APPLICATION_JSON_TYPE, String.class); |
| 98 | log.info("response :{}", response); |
| 99 | |
| 100 | try { |
| 101 | JsonNode json = new ObjectMapper().readTree(response); |
| 102 | } catch (IOException e) { |
| 103 | log.error("Cannot communicate with device {} , exception {}", deviceId, e); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Generates a ObjectNode from a list of commands in String format. |
| 109 | * |
| 110 | * @param commands a list of commands |
| 111 | * @return an ObjectNode generated from a list of commands in String format |
| 112 | */ |
| 113 | private static String generate(List<String> commands) { |
| 114 | ObjectMapper om = new ObjectMapper(); |
| 115 | |
| 116 | ArrayNode cmds = om.createArrayNode(); |
| 117 | cmds.add(ENABLE); |
| 118 | commands.stream().forEach(cmds::add); //commands here |
| 119 | |
| 120 | ObjectNode parm = om.createObjectNode(); |
| 121 | parm.put(FORMAT, JSON); |
| 122 | parm.put(TIMESTAMPS, FALSE); |
| 123 | parm.put(CMDS, cmds); |
| 124 | parm.put(VERSION, VERSION_1); |
| 125 | |
| 126 | ObjectNode node = om.createObjectNode(); |
| 127 | node.put(JSONRPC, TWO_POINT_ZERO); |
| 128 | node.put(METHOD, RUN_CMDS); |
| 129 | |
| 130 | node.put(PARAMS, parm); |
| 131 | node.put(ID, REMOVE_CONTROLLERS); |
| 132 | |
| 133 | return node.toString(); |
| 134 | } |
| 135 | } |