xueliang | 0e946fc | 2016-12-08 15:00:49 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
xueliang | 0e946fc | 2016-12-08 15:00:49 +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.fujitsu; |
| 18 | |
| 19 | import org.onosproject.mastership.MastershipService; |
| 20 | import org.onosproject.net.DeviceId; |
| 21 | import org.onosproject.drivers.fujitsu.behaviour.VoltNniLinkConfig; |
| 22 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 23 | import org.onosproject.net.driver.DriverHandler; |
| 24 | import org.onosproject.netconf.NetconfController; |
Yuta HIGUCHI | 234eaf3 | 2017-09-06 13:45:05 -0700 | [diff] [blame^] | 25 | import org.onosproject.netconf.NetconfException; |
xueliang | 0e946fc | 2016-12-08 15:00:49 +0900 | [diff] [blame] | 26 | import org.slf4j.Logger; |
| 27 | |
xueliang | 0e946fc | 2016-12-08 15:00:49 +0900 | [diff] [blame] | 28 | import java.util.Set; |
| 29 | |
| 30 | import com.google.common.collect.ImmutableSet; |
| 31 | import static com.google.common.base.Preconditions.checkNotNull; |
| 32 | import static org.onosproject.drivers.fujitsu.FujitsuVoltXmlUtility.*; |
| 33 | import static org.slf4j.LoggerFactory.getLogger; |
Yuta HIGUCHI | dd7c3f8 | 2017-09-03 14:18:01 -0700 | [diff] [blame] | 34 | import static org.onosproject.netconf.DatastoreId.RUNNING; |
xueliang | 0e946fc | 2016-12-08 15:00:49 +0900 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Implementation to get and set parameters available in vOLT |
| 38 | * through the Netconf protocol. |
| 39 | */ |
| 40 | public class FujitsuVoltNniLinkConfig extends AbstractHandlerBehaviour |
| 41 | implements VoltNniLinkConfig { |
| 42 | |
| 43 | private final Logger log = getLogger(FujitsuVoltNniLinkConfig.class); |
| 44 | private static final Set<String> NNILINKPARAMS = |
| 45 | ImmutableSet.of("loopback-enable"); |
| 46 | private static final Set<String> ENABLES = ImmutableSet.of("true", "false"); |
| 47 | private static final String VOLT_PORTS = "volt-ports"; |
| 48 | private static final String ETH_NNILINK_PORTS = "eth-nnilink-ports"; |
| 49 | private static final String ETH_NNILINK_PORT = "eth-nnilink-port"; |
| 50 | private static final String NNILINK_ID = "nnilink-id"; |
| 51 | |
| 52 | @Override |
| 53 | public String getNniLinks(String target) { |
| 54 | DriverHandler handler = handler(); |
| 55 | NetconfController controller = handler.get(NetconfController.class); |
| 56 | MastershipService mastershipService = handler.get(MastershipService.class); |
| 57 | DeviceId ncDeviceId = handler.data().deviceId(); |
| 58 | checkNotNull(controller, "Netconf controller is null"); |
| 59 | String reply = null; |
| 60 | |
| 61 | if (!mastershipService.isLocalMaster(ncDeviceId)) { |
| 62 | log.warn("Not master for {} Use {} to execute command", |
| 63 | ncDeviceId, |
| 64 | mastershipService.getMasterFor(ncDeviceId)); |
| 65 | return null; |
| 66 | } |
| 67 | |
| 68 | try { |
| 69 | StringBuilder request = new StringBuilder(); |
| 70 | request.append(VOLT_NE_OPEN + VOLT_NE_NAMESPACE) |
| 71 | .append(ANGLE_RIGHT + NEW_LINE) |
| 72 | .append(buildStartTag(VOLT_PORTS)); |
| 73 | if (target != null) { |
| 74 | int nni; |
| 75 | try { |
| 76 | nni = Integer.parseInt(target); |
| 77 | if (nni <= ZERO) { |
| 78 | log.error("Invalid integer for nnilink-id:{}", target); |
| 79 | return null; |
| 80 | } |
| 81 | } catch (NumberFormatException e) { |
| 82 | log.error("Non-number input for nnilink-id:{}", target); |
| 83 | return null; |
| 84 | } |
| 85 | request.append(buildStartTag(ETH_NNILINK_PORTS)) |
| 86 | .append(buildStartTag(ETH_NNILINK_PORT)) |
| 87 | .append(buildStartTag(NNILINK_ID, false)) |
| 88 | .append(target) |
| 89 | .append(buildEndTag(NNILINK_ID)) |
| 90 | .append(buildEndTag(ETH_NNILINK_PORT)) |
| 91 | .append(buildEndTag(ETH_NNILINK_PORTS)); |
| 92 | } else { |
| 93 | request.append(buildEmptyTag(ETH_NNILINK_PORTS)); |
| 94 | } |
| 95 | request.append(buildEndTag(VOLT_PORTS)) |
| 96 | .append(VOLT_NE_CLOSE); |
| 97 | |
| 98 | reply = controller.getDevicesMap() |
| 99 | .get(ncDeviceId) |
| 100 | .getSession() |
| 101 | .get(request.toString(), REPORT_ALL); |
Yuta HIGUCHI | 234eaf3 | 2017-09-06 13:45:05 -0700 | [diff] [blame^] | 102 | } catch (NetconfException e) { |
xueliang | 0e946fc | 2016-12-08 15:00:49 +0900 | [diff] [blame] | 103 | log.error("Cannot communicate to device {} exception {}", ncDeviceId, e); |
| 104 | } |
| 105 | return reply; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public boolean setNniLink(String target) { |
| 110 | DriverHandler handler = handler(); |
| 111 | NetconfController controller = handler.get(NetconfController.class); |
| 112 | MastershipService mastershipService = handler.get(MastershipService.class); |
| 113 | DeviceId ncDeviceId = handler.data().deviceId(); |
| 114 | checkNotNull(controller, "Netconf controller is null"); |
| 115 | |
| 116 | if (!mastershipService.isLocalMaster(ncDeviceId)) { |
| 117 | log.warn("Not master for {} Use {} to execute command", |
| 118 | ncDeviceId, |
| 119 | mastershipService.getMasterFor(ncDeviceId)); |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | String[] data = target.split(COLON); |
| 124 | if (data.length != THREE) { |
| 125 | log.error("Invalid number of arguments {}", target); |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | try { |
| 130 | int nni = Integer.parseInt(data[FIRST_PART]); |
| 131 | if (nni <= ZERO) { |
| 132 | log.error("Invalid integer for nnilink-id:{}", target); |
| 133 | return false; |
| 134 | } |
| 135 | } catch (NumberFormatException e) { |
| 136 | log.error("Non-number input for nnilink-id:{}", target); |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | if (!checkSetParam(data[SECOND_PART], data[THIRD_PART])) { |
| 141 | log.error("Failed to check input {}", target); |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | try { |
| 146 | StringBuilder request = new StringBuilder(); |
| 147 | request.append(VOLT_NE_OPEN + VOLT_NE_NAMESPACE) |
| 148 | .append(ANGLE_RIGHT + NEW_LINE) |
| 149 | .append(buildStartTag(VOLT_PORTS)) |
| 150 | .append(buildStartTag(ETH_NNILINK_PORTS)) |
| 151 | .append(buildStartTag(ETH_NNILINK_PORT)) |
| 152 | .append(buildStartTag(NNILINK_ID, false)) |
| 153 | .append(data[FIRST_PART]) |
| 154 | .append(buildEndTag(NNILINK_ID)) |
| 155 | |
| 156 | .append(buildStartTag(data[SECOND_PART], false)) |
| 157 | .append(data[THIRD_PART]) |
| 158 | .append(buildEndTag(data[SECOND_PART])) |
| 159 | |
| 160 | .append(buildEndTag(ETH_NNILINK_PORT)) |
| 161 | .append(buildEndTag(ETH_NNILINK_PORTS)) |
| 162 | .append(buildEndTag(VOLT_PORTS)) |
| 163 | .append(VOLT_NE_CLOSE); |
| 164 | |
| 165 | controller.getDevicesMap() |
| 166 | .get(ncDeviceId) |
| 167 | .getSession() |
| 168 | .editConfig(RUNNING, null, request.toString()); |
Yuta HIGUCHI | 234eaf3 | 2017-09-06 13:45:05 -0700 | [diff] [blame^] | 169 | } catch (NetconfException e) { |
xueliang | 0e946fc | 2016-12-08 15:00:49 +0900 | [diff] [blame] | 170 | log.error("Cannot communicate to device {} exception {}", ncDeviceId, e); |
| 171 | return false; |
| 172 | } |
| 173 | return true; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Verifies input string for valid options. |
| 178 | * |
| 179 | * @param name input data in string |
| 180 | * @param value input data in string |
| 181 | * @return true/false if the param is valid/invalid |
| 182 | */ |
| 183 | private boolean checkSetParam(String name, String value) { |
| 184 | if (!NNILINKPARAMS.contains(name)) { |
| 185 | log.error("Unsupported parameter: {}", name); |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | switch (name) { |
| 190 | default: |
| 191 | if (!ENABLES.contains(value)) { |
| 192 | log.error("Invalid value for Name {} : Value {}.", name, value); |
| 193 | return false; |
| 194 | } |
| 195 | break; |
| 196 | } |
| 197 | return true; |
| 198 | } |
| 199 | |
| 200 | } |