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"; |
| 48 | private static final String CONTROLLER_ADDR = "controllerAddr"; |
| 49 | private static final String CONTROLLER_IP = "ip"; |
| 50 | private static final String CONTROLLER_PORT = "port"; |
| 51 | private static final String PROTOCOL_TCP = "tcp"; |
| 52 | |
| 53 | |
Palash Kala | c99b15a | 2017-06-14 09:36:56 +0900 | [diff] [blame] | 54 | private static final int MAX_CONTROLLERS = 8; |
Palash Kala | c99b15a | 2017-06-14 09:36:56 +0900 | [diff] [blame] | 55 | |
| 56 | private final Logger log = getLogger(getClass()); |
| 57 | |
| 58 | @Override |
| 59 | public List<ControllerInfo> getControllers() { |
sdn | 9952dab | 2018-06-12 14:52:51 +0900 | [diff] [blame] | 60 | log.debug("Arista get Controllers"); |
| 61 | |
| 62 | List<ControllerInfo> controllers = new ArrayList<>(); |
| 63 | Optional<JsonNode> res = AristaUtils.retrieveCommandResult(handler(), SHOW_CONTROLLER_CMD); |
| 64 | if (res == null) { |
| 65 | return controllers; |
| 66 | } |
| 67 | |
| 68 | JsonNode controllerInfo = res.get().findValue("controllersInfo"); |
| 69 | Iterator<JsonNode> controlleriter = controllerInfo.iterator(); |
| 70 | while (controlleriter.hasNext()) { |
| 71 | JsonNode temp1 = controlleriter.next(); |
| 72 | if (temp1.has(CONTROLLER_ADDR)) { |
| 73 | JsonNode controllerAddr = temp1.get(CONTROLLER_ADDR); |
| 74 | if (controllerAddr.has(CONTROLLER_IP) && controllerAddr.has(CONTROLLER_PORT)) { |
| 75 | String ip = controllerAddr.get(CONTROLLER_IP).asText(); |
| 76 | int port = controllerAddr.get(CONTROLLER_PORT).asInt(); |
| 77 | ControllerInfo info = new ControllerInfo(IpAddress.valueOf(ip), port, PROTOCOL_TCP); |
| 78 | controllers.add(info); |
| 79 | log.debug("Controller Information {}", info.target()); |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | return ImmutableList.copyOf(controllers); |
Palash Kala | c99b15a | 2017-06-14 09:36:56 +0900 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public void setControllers(List<ControllerInfo> controllers) { |
sdn | 9952dab | 2018-06-12 14:52:51 +0900 | [diff] [blame] | 89 | log.debug("Arista set Controllers"); |
| 90 | |
| 91 | List<String> cmds = new ArrayList<>(); |
| 92 | cmds.add(CONFIGURE_TERMINAL); |
| 93 | cmds.add(OPENFLOW_CMD); |
| 94 | //The Arista switch supports up to 8 multi-controllers. |
| 95 | controllers.stream().limit(MAX_CONTROLLERS).forEach(c -> cmds |
| 96 | .add(String.format(SET_CONTROLLER_CMD, c.ip().toString(), c.port()))); |
| 97 | if (controllers.size() > 8) { |
| 98 | log.warn(" {} Arista Switch maximun 8 controllers, not adding {} excessive ones", |
| 99 | handler().data().deviceId(), controllers.size() - 8); |
| 100 | } |
| 101 | cmds.add(NO_SHUTDOWN_CMD); |
| 102 | cmds.add(COPY_RUNNING_CONFIG); |
| 103 | |
| 104 | AristaUtils.retrieveCommandResult(handler(), cmds); |
Palash Kala | c99b15a | 2017-06-14 09:36:56 +0900 | [diff] [blame] | 105 | } |
| 106 | |
sdn | 9952dab | 2018-06-12 14:52:51 +0900 | [diff] [blame] | 107 | |
Palash Kala | c99b15a | 2017-06-14 09:36:56 +0900 | [diff] [blame] | 108 | @Override |
| 109 | public void removeControllers(List<ControllerInfo> controllers) { |
sdn | 9952dab | 2018-06-12 14:52:51 +0900 | [diff] [blame] | 110 | log.debug("Arista remove Controllers"); |
Palash Kala | c99b15a | 2017-06-14 09:36:56 +0900 | [diff] [blame] | 111 | |
sdn | 9952dab | 2018-06-12 14:52:51 +0900 | [diff] [blame] | 112 | List<String> cmds = Lists.newArrayList(); |
Palash Kala | c99b15a | 2017-06-14 09:36:56 +0900 | [diff] [blame] | 113 | cmds.add(CONFIGURE_TERMINAL); |
| 114 | cmds.add(OPENFLOW_CMD); |
| 115 | controllers.stream().limit(MAX_CONTROLLERS).forEach(c -> cmds |
| 116 | .add(String.format(REMOVE_CONTROLLER_CMD, c.ip().toString(), c.port()))); |
| 117 | |
DongRyeol Cha | 06041fa | 2018-06-07 10:23:32 +0900 | [diff] [blame] | 118 | AristaUtils.retrieveCommandResult(handler(), cmds); |
Palash Kala | c99b15a | 2017-06-14 09:36:56 +0900 | [diff] [blame] | 119 | } |
| 120 | } |