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 | |
sdn | 9952dab | 2018-06-12 14:52:51 +0900 | [diff] [blame] | 19 | import java.util.ArrayList; |
| 20 | import java.util.Iterator; |
| 21 | import java.util.List; |
| 22 | import java.util.Optional; |
| 23 | |
DongRyeol Cha | 06041fa | 2018-06-07 10:23:32 +0900 | [diff] [blame] | 24 | import com.google.common.collect.Lists; |
sdn | 9952dab | 2018-06-12 14:52:51 +0900 | [diff] [blame] | 25 | import com.google.common.collect.ImmutableList; |
| 26 | import org.onlab.packet.IpAddress; |
Palash Kala | c99b15a | 2017-06-14 09:36:56 +0900 | [diff] [blame] | 27 | import org.onosproject.net.behaviour.ControllerConfig; |
| 28 | import org.onosproject.net.behaviour.ControllerInfo; |
| 29 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
Palash Kala | c99b15a | 2017-06-14 09:36:56 +0900 | [diff] [blame] | 30 | import org.slf4j.Logger; |
sdn | 9952dab | 2018-06-12 14:52:51 +0900 | [diff] [blame] | 31 | import com.fasterxml.jackson.databind.JsonNode; |
Palash Kala | c99b15a | 2017-06-14 09:36:56 +0900 | [diff] [blame] | 32 | |
Palash Kala | c99b15a | 2017-06-14 09:36:56 +0900 | [diff] [blame] | 33 | import static org.slf4j.LoggerFactory.getLogger; |
| 34 | |
| 35 | /** |
| 36 | * Sets, gets and removes the openflow controller configuration from a Arista Rest device. |
| 37 | */ |
| 38 | public class ControllerConfigAristaImpl extends AbstractHandlerBehaviour implements ControllerConfig { |
| 39 | |
sdn | 9952dab | 2018-06-12 14:52:51 +0900 | [diff] [blame] | 40 | private static final String SHOW_CONTROLLER_CMD = "show openflow"; |
Palash Kala | c99b15a | 2017-06-14 09:36:56 +0900 | [diff] [blame] | 41 | private static final String CONFIGURE_TERMINAL = "configure"; |
| 42 | private static final String OPENFLOW_CMD = "openflow"; |
sdn | 9952dab | 2018-06-12 14:52:51 +0900 | [diff] [blame] | 43 | private static final String SET_CONTROLLER_CMD = "controller tcp:%s:%d"; |
| 44 | private static final String NO_SHUTDOWN_CMD = "no shutdown"; |
| 45 | private static final String SHUTDOWN_CMD = "shutdown"; |
Palash Kala | c99b15a | 2017-06-14 09:36:56 +0900 | [diff] [blame] | 46 | private static final String REMOVE_CONTROLLER_CMD = "no controller tcp:%s:%d"; |
sdn | 9952dab | 2018-06-12 14:52:51 +0900 | [diff] [blame] | 47 | private static final String COPY_RUNNING_CONFIG = "copy running-config startup-config"; |
HelloONOS | 4baf78a | 2019-02-14 18:47:19 +0900 | [diff] [blame^] | 48 | private static final String CONTROLLER_INFO = "controllersInfo"; |
sdn | 9952dab | 2018-06-12 14:52:51 +0900 | [diff] [blame] | 49 | private static final String CONTROLLER_ADDR = "controllerAddr"; |
| 50 | private static final String CONTROLLER_IP = "ip"; |
| 51 | private static final String CONTROLLER_PORT = "port"; |
| 52 | private static final String PROTOCOL_TCP = "tcp"; |
| 53 | |
| 54 | |
Palash Kala | c99b15a | 2017-06-14 09:36:56 +0900 | [diff] [blame] | 55 | private static final int MAX_CONTROLLERS = 8; |
Palash Kala | c99b15a | 2017-06-14 09:36:56 +0900 | [diff] [blame] | 56 | |
| 57 | private final Logger log = getLogger(getClass()); |
| 58 | |
| 59 | @Override |
| 60 | public List<ControllerInfo> getControllers() { |
sdn | 9952dab | 2018-06-12 14:52:51 +0900 | [diff] [blame] | 61 | log.debug("Arista get Controllers"); |
| 62 | |
| 63 | List<ControllerInfo> controllers = new ArrayList<>(); |
| 64 | Optional<JsonNode> res = AristaUtils.retrieveCommandResult(handler(), SHOW_CONTROLLER_CMD); |
| 65 | if (res == null) { |
HelloONOS | 4baf78a | 2019-02-14 18:47:19 +0900 | [diff] [blame^] | 66 | log.warn("There is no connected controller."); |
sdn | 9952dab | 2018-06-12 14:52:51 +0900 | [diff] [blame] | 67 | return controllers; |
| 68 | } |
| 69 | |
HelloONOS | 4baf78a | 2019-02-14 18:47:19 +0900 | [diff] [blame^] | 70 | JsonNode controllerInfo = res.get().findValue(CONTROLLER_INFO); |
sdn | 9952dab | 2018-06-12 14:52:51 +0900 | [diff] [blame] | 71 | Iterator<JsonNode> controlleriter = controllerInfo.iterator(); |
| 72 | while (controlleriter.hasNext()) { |
| 73 | JsonNode temp1 = controlleriter.next(); |
| 74 | if (temp1.has(CONTROLLER_ADDR)) { |
| 75 | JsonNode controllerAddr = temp1.get(CONTROLLER_ADDR); |
| 76 | if (controllerAddr.has(CONTROLLER_IP) && controllerAddr.has(CONTROLLER_PORT)) { |
| 77 | String ip = controllerAddr.get(CONTROLLER_IP).asText(); |
| 78 | int port = controllerAddr.get(CONTROLLER_PORT).asInt(); |
| 79 | ControllerInfo info = new ControllerInfo(IpAddress.valueOf(ip), port, PROTOCOL_TCP); |
| 80 | controllers.add(info); |
| 81 | log.debug("Controller Information {}", info.target()); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return ImmutableList.copyOf(controllers); |
Palash Kala | c99b15a | 2017-06-14 09:36:56 +0900 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public void setControllers(List<ControllerInfo> controllers) { |
sdn | 9952dab | 2018-06-12 14:52:51 +0900 | [diff] [blame] | 91 | log.debug("Arista set Controllers"); |
| 92 | |
| 93 | List<String> cmds = new ArrayList<>(); |
| 94 | cmds.add(CONFIGURE_TERMINAL); |
| 95 | cmds.add(OPENFLOW_CMD); |
| 96 | //The Arista switch supports up to 8 multi-controllers. |
| 97 | controllers.stream().limit(MAX_CONTROLLERS).forEach(c -> cmds |
| 98 | .add(String.format(SET_CONTROLLER_CMD, c.ip().toString(), c.port()))); |
HelloONOS | 4baf78a | 2019-02-14 18:47:19 +0900 | [diff] [blame^] | 99 | if (controllers.size() > MAX_CONTROLLERS) { |
sdn | 9952dab | 2018-06-12 14:52:51 +0900 | [diff] [blame] | 100 | log.warn(" {} Arista Switch maximun 8 controllers, not adding {} excessive ones", |
HelloONOS | 4baf78a | 2019-02-14 18:47:19 +0900 | [diff] [blame^] | 101 | handler().data().deviceId(), controllers.size() - MAX_CONTROLLERS); |
sdn | 9952dab | 2018-06-12 14:52:51 +0900 | [diff] [blame] | 102 | } |
| 103 | cmds.add(NO_SHUTDOWN_CMD); |
| 104 | cmds.add(COPY_RUNNING_CONFIG); |
| 105 | |
| 106 | AristaUtils.retrieveCommandResult(handler(), cmds); |
Palash Kala | c99b15a | 2017-06-14 09:36:56 +0900 | [diff] [blame] | 107 | } |
| 108 | |
sdn | 9952dab | 2018-06-12 14:52:51 +0900 | [diff] [blame] | 109 | |
Palash Kala | c99b15a | 2017-06-14 09:36:56 +0900 | [diff] [blame] | 110 | @Override |
| 111 | public void removeControllers(List<ControllerInfo> controllers) { |
sdn | 9952dab | 2018-06-12 14:52:51 +0900 | [diff] [blame] | 112 | log.debug("Arista remove Controllers"); |
Palash Kala | c99b15a | 2017-06-14 09:36:56 +0900 | [diff] [blame] | 113 | |
sdn | 9952dab | 2018-06-12 14:52:51 +0900 | [diff] [blame] | 114 | List<String> cmds = Lists.newArrayList(); |
Palash Kala | c99b15a | 2017-06-14 09:36:56 +0900 | [diff] [blame] | 115 | cmds.add(CONFIGURE_TERMINAL); |
| 116 | cmds.add(OPENFLOW_CMD); |
| 117 | controllers.stream().limit(MAX_CONTROLLERS).forEach(c -> cmds |
| 118 | .add(String.format(REMOVE_CONTROLLER_CMD, c.ip().toString(), c.port()))); |
| 119 | |
DongRyeol Cha | 06041fa | 2018-06-07 10:23:32 +0900 | [diff] [blame] | 120 | AristaUtils.retrieveCommandResult(handler(), cmds); |
Palash Kala | c99b15a | 2017-06-14 09:36:56 +0900 | [diff] [blame] | 121 | } |
| 122 | } |