Akihiro Yamanouchi | d491284 | 2016-07-01 10:38:46 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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.net.behaviour.VoltPonLinkConfig; |
| 22 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 23 | import org.onosproject.net.driver.DriverHandler; |
| 24 | import org.onosproject.netconf.NetconfController; |
| 25 | import org.slf4j.Logger; |
| 26 | |
| 27 | import java.io.IOException; |
| 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; |
| 34 | |
| 35 | /** |
| 36 | * Implementation to get and set parameters available in vOLT |
| 37 | * through the Netconf protocol. |
| 38 | */ |
| 39 | public class FujitsuVoltPonLinkConfig extends AbstractHandlerBehaviour |
| 40 | implements VoltPonLinkConfig { |
| 41 | |
| 42 | private final Logger log = getLogger(FujitsuVoltPonLinkConfig.class); |
| 43 | private final Set<String> ponLinkParams = ImmutableSet.of( |
| 44 | "admin-state", "onu-discovery-mode", "onu-discovery-interval", |
| 45 | "dba-cycle-time", "mac-age-time", "lof-threshold", |
| 46 | "los-threshold", "pm-enable"); |
| 47 | private static final String VOLT_PORTS = "volt-ports"; |
| 48 | private static final String GPON_PONLINK_PORTS = "gpon-ponlink-ports"; |
| 49 | private static final String GPON_PONLINK_PORT = "gpon-ponlink-port"; |
| 50 | private int pon; |
| 51 | |
| 52 | @Override |
| 53 | public String getPonLinks(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 reply; |
| 66 | } |
| 67 | |
| 68 | try { |
| 69 | StringBuilder request = new StringBuilder(); |
| 70 | request.append(VOLT_NE_OPEN).append(VOLT_NE_NAMESPACE); |
| 71 | request.append(ANGLE_RIGHT).append(NEW_LINE); |
| 72 | request.append(buildStartTag(VOLT_PORTS)); |
| 73 | if (target != null) { |
| 74 | try { |
| 75 | pon = Integer.parseInt(target); |
| 76 | } catch (NumberFormatException e) { |
| 77 | log.error("Non-number input"); |
| 78 | return reply; |
| 79 | } |
| 80 | request.append(buildStartTag(GPON_PONLINK_PORTS)); |
| 81 | request.append(buildStartTag(GPON_PONLINK_PORT)); |
| 82 | request.append(buildStartTag(PONLINK_ID, false)); |
| 83 | request.append(target); |
| 84 | request.append(buildEndTag(PONLINK_ID)); |
| 85 | |
| 86 | request.append(buildEndTag(GPON_PONLINK_PORT)); |
| 87 | request.append(buildEndTag(GPON_PONLINK_PORTS)); |
| 88 | } else { |
| 89 | request.append(buildEmptyTag(GPON_PONLINK_PORTS)); |
| 90 | } |
| 91 | request.append(buildEndTag(VOLT_PORTS)); |
| 92 | request.append(VOLT_NE_CLOSE); |
| 93 | |
| 94 | reply = controller. |
| 95 | getDevicesMap().get(ncDeviceId).getSession(). |
| 96 | get(request.toString(), REPORT_ALL); |
| 97 | } catch (IOException e) { |
| 98 | log.error("Cannot communicate to device {} exception ", ncDeviceId, e); |
| 99 | } |
| 100 | return reply; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void setPonLink(String target) { |
| 105 | DriverHandler handler = handler(); |
| 106 | NetconfController controller = handler.get(NetconfController.class); |
| 107 | MastershipService mastershipService = handler.get(MastershipService.class); |
| 108 | DeviceId ncDeviceId = handler.data().deviceId(); |
| 109 | checkNotNull(controller, "Netconf controller is null"); |
| 110 | |
| 111 | if (!mastershipService.isLocalMaster(ncDeviceId)) { |
| 112 | log.warn("Not master for {} Use {} to execute command", |
| 113 | ncDeviceId, |
| 114 | mastershipService.getMasterFor(ncDeviceId)); |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | String[] data = target.split(COLON); |
| 119 | if (data.length != 3) { |
| 120 | log.error("Invalid number of arguments"); |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | try { |
| 125 | pon = Integer.parseInt(data[0]); |
| 126 | } catch (NumberFormatException e) { |
| 127 | log.error("Non-number input"); |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | if (!ponLinkParams.contains(data[1])) { |
| 132 | log.error("Unsupported parameter: {} ", data[1]); |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | try { |
| 137 | StringBuilder request = new StringBuilder(); |
| 138 | request.append(VOLT_NE_OPEN).append(VOLT_NE_NAMESPACE); |
| 139 | request.append(ANGLE_RIGHT).append(NEW_LINE); |
| 140 | request.append(buildStartTag(VOLT_PORTS)); |
| 141 | request.append(buildStartTag(GPON_PONLINK_PORTS)); |
| 142 | request.append(buildStartTag(GPON_PONLINK_PORT)); |
| 143 | request.append(buildStartTag(PONLINK_ID, false)); |
| 144 | request.append(data[0]); |
| 145 | request.append(buildEndTag(PONLINK_ID)); |
| 146 | |
| 147 | request.append(buildStartTag(data[1], false)); |
| 148 | request.append(data[2]); |
| 149 | request.append(buildEndTag(data[1])); |
| 150 | |
| 151 | request.append(buildEndTag(GPON_PONLINK_PORT)); |
| 152 | request.append(buildEndTag(GPON_PONLINK_PORTS)); |
| 153 | request.append(buildEndTag(VOLT_PORTS)); |
| 154 | request.append(VOLT_NE_CLOSE); |
| 155 | |
| 156 | controller.getDevicesMap().get(ncDeviceId).getSession(). |
| 157 | editConfig(RUNNING, null, request.toString()); |
| 158 | } catch (IOException e) { |
| 159 | log.error("Cannot communicate to device {} exception ", ncDeviceId, e); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | } |