Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +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.net.DeviceId; |
| 20 | import org.onosproject.drivers.fujitsu.behaviour.VoltAlertConfig; |
| 21 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 22 | import org.onosproject.net.driver.DriverHandler; |
| 23 | import org.onosproject.netconf.NetconfController; |
Yuta HIGUCHI | 234eaf3 | 2017-09-06 13:45:05 -0700 | [diff] [blame^] | 24 | import org.onosproject.netconf.NetconfException; |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +0900 | [diff] [blame] | 25 | import org.onosproject.mastership.MastershipService; |
| 26 | import org.slf4j.Logger; |
| 27 | |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +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; |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +0900 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Implementation to get and set parameters available in vOLT |
| 38 | * through the Netconf protocol. |
| 39 | */ |
| 40 | public class FujitsuVoltAlertConfig extends AbstractHandlerBehaviour |
| 41 | implements VoltAlertConfig { |
| 42 | |
| 43 | private final Logger log = getLogger(FujitsuVoltAlertConfig.class); |
| 44 | private static final String VOLT_ALERTS = "volt-alerts"; |
| 45 | private static final String ALERT_FILTER = "alert-filter"; |
| 46 | private static final String NOTIFY_ALERT = "notify-alert"; |
xueliang | 54525f5 | 2016-09-29 17:28:35 +0900 | [diff] [blame] | 47 | private static final Set<String> SEVERITYLEVELS = |
| 48 | ImmutableSet.of("none", "info", "minor", "major", "critical"); |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +0900 | [diff] [blame] | 49 | private static final String DISABLE = "disable"; |
| 50 | |
| 51 | |
| 52 | @Override |
| 53 | public String getAlertFilter() { |
| 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)); |
xueliang | 54525f5 | 2016-09-29 17:28:35 +0900 | [diff] [blame] | 65 | return null; |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +0900 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | try { |
| 69 | StringBuilder request = new StringBuilder(); |
xueliang | 54525f5 | 2016-09-29 17:28:35 +0900 | [diff] [blame] | 70 | request.append(VOLT_NE_OPEN + VOLT_NE_NAMESPACE); |
| 71 | request.append(ANGLE_RIGHT + NEW_LINE); |
| 72 | request.append(buildStartTag(VOLT_ALERTS)) |
| 73 | .append(buildEmptyTag(ALERT_FILTER)) |
| 74 | .append(buildEndTag(VOLT_ALERTS)) |
| 75 | .append(VOLT_NE_CLOSE); |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +0900 | [diff] [blame] | 76 | |
xueliang | 54525f5 | 2016-09-29 17:28:35 +0900 | [diff] [blame] | 77 | reply = controller |
| 78 | .getDevicesMap() |
| 79 | .get(ncDeviceId) |
| 80 | .getSession() |
| 81 | .get(request.toString(), REPORT_ALL); |
Yuta HIGUCHI | 234eaf3 | 2017-09-06 13:45:05 -0700 | [diff] [blame^] | 82 | } catch (NetconfException e) { |
xueliang | 54525f5 | 2016-09-29 17:28:35 +0900 | [diff] [blame] | 83 | log.error("Cannot communicate to device {} exception {}", ncDeviceId, e); |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +0900 | [diff] [blame] | 84 | } |
| 85 | return reply; |
| 86 | } |
| 87 | |
| 88 | @Override |
xueliang | 54525f5 | 2016-09-29 17:28:35 +0900 | [diff] [blame] | 89 | public boolean setAlertFilter(String severity) { |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +0900 | [diff] [blame] | 90 | DriverHandler handler = handler(); |
| 91 | NetconfController controller = handler.get(NetconfController.class); |
| 92 | MastershipService mastershipService = handler.get(MastershipService.class); |
| 93 | DeviceId ncDeviceId = handler.data().deviceId(); |
| 94 | checkNotNull(controller, "Netconf controller is null"); |
| 95 | |
| 96 | if (!mastershipService.isLocalMaster(ncDeviceId)) { |
| 97 | log.warn("Not master for {} Use {} to execute command", |
| 98 | ncDeviceId, |
| 99 | mastershipService.getMasterFor(ncDeviceId)); |
xueliang | 54525f5 | 2016-09-29 17:28:35 +0900 | [diff] [blame] | 100 | return false; |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +0900 | [diff] [blame] | 101 | } |
| 102 | |
xueliang | 54525f5 | 2016-09-29 17:28:35 +0900 | [diff] [blame] | 103 | if (!SEVERITYLEVELS.contains(severity)) { |
| 104 | log.error("Invalid severity level: {}", severity); |
| 105 | return false; |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +0900 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | try { |
| 109 | StringBuilder request = new StringBuilder(); |
xueliang | 54525f5 | 2016-09-29 17:28:35 +0900 | [diff] [blame] | 110 | request.append(VOLT_NE_OPEN + VOLT_NE_NAMESPACE); |
| 111 | request.append(ANGLE_RIGHT + NEW_LINE); |
| 112 | request.append(buildStartTag(VOLT_ALERTS)) |
| 113 | .append(buildStartTag(ALERT_FILTER, false)) |
| 114 | .append(severity) |
| 115 | .append(buildEndTag(ALERT_FILTER)) |
| 116 | .append(buildEndTag(VOLT_ALERTS)) |
| 117 | .append(VOLT_NE_CLOSE); |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +0900 | [diff] [blame] | 118 | |
| 119 | controller.getDevicesMap().get(ncDeviceId).getSession(). |
| 120 | editConfig(RUNNING, null, request.toString()); |
Yuta HIGUCHI | 234eaf3 | 2017-09-06 13:45:05 -0700 | [diff] [blame^] | 121 | } catch (NetconfException e) { |
xueliang | 54525f5 | 2016-09-29 17:28:35 +0900 | [diff] [blame] | 122 | log.error("Cannot communicate to device {} exception {}", ncDeviceId, e); |
| 123 | return false; |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +0900 | [diff] [blame] | 124 | } |
xueliang | 54525f5 | 2016-09-29 17:28:35 +0900 | [diff] [blame] | 125 | return true; |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +0900 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Override |
xueliang | 54525f5 | 2016-09-29 17:28:35 +0900 | [diff] [blame] | 129 | public boolean subscribe(String mode) { |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +0900 | [diff] [blame] | 130 | DriverHandler handler = handler(); |
| 131 | NetconfController controller = handler.get(NetconfController.class); |
| 132 | MastershipService mastershipService = handler.get(MastershipService.class); |
| 133 | DeviceId ncDeviceId = handler.data().deviceId(); |
| 134 | checkNotNull(controller, "Netconf controller is null"); |
| 135 | |
| 136 | if (!mastershipService.isLocalMaster(ncDeviceId)) { |
| 137 | log.warn("Not master for {} Use {} to execute command", |
| 138 | ncDeviceId, |
| 139 | mastershipService.getMasterFor(ncDeviceId)); |
xueliang | 54525f5 | 2016-09-29 17:28:35 +0900 | [diff] [blame] | 140 | return false; |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +0900 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | if (mode != null) { |
| 144 | if (!DISABLE.equals(mode)) { |
xueliang | 54525f5 | 2016-09-29 17:28:35 +0900 | [diff] [blame] | 145 | log.error("Invalid mode: {}", mode); |
| 146 | return false; |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +0900 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | try { |
| 151 | if (mode != null) { |
| 152 | controller.getDevicesMap().get(ncDeviceId).getSession(). |
| 153 | endSubscription(); |
| 154 | } else { |
| 155 | StringBuilder request = new StringBuilder(); |
xueliang | 54525f5 | 2016-09-29 17:28:35 +0900 | [diff] [blame] | 156 | request.append(ANGLE_LEFT + NOTIFY_ALERT + SPACE); |
| 157 | request.append(VOLT_NE_NAMESPACE + SLASH + ANGLE_RIGHT); |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +0900 | [diff] [blame] | 158 | |
| 159 | controller.getDevicesMap().get(ncDeviceId).getSession(). |
| 160 | startSubscription(request.toString()); |
| 161 | } |
Yuta HIGUCHI | 234eaf3 | 2017-09-06 13:45:05 -0700 | [diff] [blame^] | 162 | } catch (NetconfException e) { |
xueliang | 54525f5 | 2016-09-29 17:28:35 +0900 | [diff] [blame] | 163 | log.error("Cannot communicate to device {} exception {}", ncDeviceId, e); |
| 164 | return false; |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +0900 | [diff] [blame] | 165 | } |
xueliang | 54525f5 | 2016-09-29 17:28:35 +0900 | [diff] [blame] | 166 | return true; |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +0900 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | } |