Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +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.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; |
| 24 | import org.onosproject.mastership.MastershipService; |
| 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 FujitsuVoltAlertConfig extends AbstractHandlerBehaviour |
| 40 | implements VoltAlertConfig { |
| 41 | |
| 42 | private final Logger log = getLogger(FujitsuVoltAlertConfig.class); |
| 43 | private static final String VOLT_ALERTS = "volt-alerts"; |
| 44 | private static final String ALERT_FILTER = "alert-filter"; |
| 45 | private static final String NOTIFY_ALERT = "notify-alert"; |
xueliang | 54525f5 | 2016-09-29 17:28:35 +0900 | [diff] [blame] | 46 | private static final Set<String> SEVERITYLEVELS = |
| 47 | ImmutableSet.of("none", "info", "minor", "major", "critical"); |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +0900 | [diff] [blame] | 48 | private static final String DISABLE = "disable"; |
| 49 | |
| 50 | |
| 51 | @Override |
| 52 | public String getAlertFilter() { |
| 53 | DriverHandler handler = handler(); |
| 54 | NetconfController controller = handler.get(NetconfController.class); |
| 55 | MastershipService mastershipService = handler.get(MastershipService.class); |
| 56 | DeviceId ncDeviceId = handler.data().deviceId(); |
| 57 | checkNotNull(controller, "Netconf controller is null"); |
| 58 | String reply = null; |
| 59 | |
| 60 | if (!mastershipService.isLocalMaster(ncDeviceId)) { |
| 61 | log.warn("Not master for {} Use {} to execute command", |
| 62 | ncDeviceId, |
| 63 | mastershipService.getMasterFor(ncDeviceId)); |
xueliang | 54525f5 | 2016-09-29 17:28:35 +0900 | [diff] [blame] | 64 | return null; |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +0900 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | try { |
| 68 | StringBuilder request = new StringBuilder(); |
xueliang | 54525f5 | 2016-09-29 17:28:35 +0900 | [diff] [blame] | 69 | request.append(VOLT_NE_OPEN + VOLT_NE_NAMESPACE); |
| 70 | request.append(ANGLE_RIGHT + NEW_LINE); |
| 71 | request.append(buildStartTag(VOLT_ALERTS)) |
| 72 | .append(buildEmptyTag(ALERT_FILTER)) |
| 73 | .append(buildEndTag(VOLT_ALERTS)) |
| 74 | .append(VOLT_NE_CLOSE); |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +0900 | [diff] [blame] | 75 | |
xueliang | 54525f5 | 2016-09-29 17:28:35 +0900 | [diff] [blame] | 76 | reply = controller |
| 77 | .getDevicesMap() |
| 78 | .get(ncDeviceId) |
| 79 | .getSession() |
| 80 | .get(request.toString(), REPORT_ALL); |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +0900 | [diff] [blame] | 81 | } catch (IOException e) { |
xueliang | 54525f5 | 2016-09-29 17:28:35 +0900 | [diff] [blame] | 82 | log.error("Cannot communicate to device {} exception {}", ncDeviceId, e); |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +0900 | [diff] [blame] | 83 | } |
| 84 | return reply; |
| 85 | } |
| 86 | |
| 87 | @Override |
xueliang | 54525f5 | 2016-09-29 17:28:35 +0900 | [diff] [blame] | 88 | public boolean setAlertFilter(String severity) { |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +0900 | [diff] [blame] | 89 | DriverHandler handler = handler(); |
| 90 | NetconfController controller = handler.get(NetconfController.class); |
| 91 | MastershipService mastershipService = handler.get(MastershipService.class); |
| 92 | DeviceId ncDeviceId = handler.data().deviceId(); |
| 93 | checkNotNull(controller, "Netconf controller is null"); |
| 94 | |
| 95 | if (!mastershipService.isLocalMaster(ncDeviceId)) { |
| 96 | log.warn("Not master for {} Use {} to execute command", |
| 97 | ncDeviceId, |
| 98 | mastershipService.getMasterFor(ncDeviceId)); |
xueliang | 54525f5 | 2016-09-29 17:28:35 +0900 | [diff] [blame] | 99 | return false; |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +0900 | [diff] [blame] | 100 | } |
| 101 | |
xueliang | 54525f5 | 2016-09-29 17:28:35 +0900 | [diff] [blame] | 102 | if (!SEVERITYLEVELS.contains(severity)) { |
| 103 | log.error("Invalid severity level: {}", severity); |
| 104 | return false; |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +0900 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | try { |
| 108 | StringBuilder request = new StringBuilder(); |
xueliang | 54525f5 | 2016-09-29 17:28:35 +0900 | [diff] [blame] | 109 | request.append(VOLT_NE_OPEN + VOLT_NE_NAMESPACE); |
| 110 | request.append(ANGLE_RIGHT + NEW_LINE); |
| 111 | request.append(buildStartTag(VOLT_ALERTS)) |
| 112 | .append(buildStartTag(ALERT_FILTER, false)) |
| 113 | .append(severity) |
| 114 | .append(buildEndTag(ALERT_FILTER)) |
| 115 | .append(buildEndTag(VOLT_ALERTS)) |
| 116 | .append(VOLT_NE_CLOSE); |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +0900 | [diff] [blame] | 117 | |
| 118 | controller.getDevicesMap().get(ncDeviceId).getSession(). |
| 119 | editConfig(RUNNING, null, request.toString()); |
| 120 | } catch (IOException e) { |
xueliang | 54525f5 | 2016-09-29 17:28:35 +0900 | [diff] [blame] | 121 | log.error("Cannot communicate to device {} exception {}", ncDeviceId, e); |
| 122 | return false; |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +0900 | [diff] [blame] | 123 | } |
xueliang | 54525f5 | 2016-09-29 17:28:35 +0900 | [diff] [blame] | 124 | return true; |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +0900 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | @Override |
xueliang | 54525f5 | 2016-09-29 17:28:35 +0900 | [diff] [blame] | 128 | public boolean subscribe(String mode) { |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +0900 | [diff] [blame] | 129 | DriverHandler handler = handler(); |
| 130 | NetconfController controller = handler.get(NetconfController.class); |
| 131 | MastershipService mastershipService = handler.get(MastershipService.class); |
| 132 | DeviceId ncDeviceId = handler.data().deviceId(); |
| 133 | checkNotNull(controller, "Netconf controller is null"); |
| 134 | |
| 135 | if (!mastershipService.isLocalMaster(ncDeviceId)) { |
| 136 | log.warn("Not master for {} Use {} to execute command", |
| 137 | ncDeviceId, |
| 138 | mastershipService.getMasterFor(ncDeviceId)); |
xueliang | 54525f5 | 2016-09-29 17:28:35 +0900 | [diff] [blame] | 139 | return false; |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +0900 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | if (mode != null) { |
| 143 | if (!DISABLE.equals(mode)) { |
xueliang | 54525f5 | 2016-09-29 17:28:35 +0900 | [diff] [blame] | 144 | log.error("Invalid mode: {}", mode); |
| 145 | return false; |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +0900 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | try { |
| 150 | if (mode != null) { |
| 151 | controller.getDevicesMap().get(ncDeviceId).getSession(). |
| 152 | endSubscription(); |
| 153 | } else { |
| 154 | StringBuilder request = new StringBuilder(); |
xueliang | 54525f5 | 2016-09-29 17:28:35 +0900 | [diff] [blame] | 155 | request.append(ANGLE_LEFT + NOTIFY_ALERT + SPACE); |
| 156 | request.append(VOLT_NE_NAMESPACE + SLASH + ANGLE_RIGHT); |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +0900 | [diff] [blame] | 157 | |
| 158 | controller.getDevicesMap().get(ncDeviceId).getSession(). |
| 159 | startSubscription(request.toString()); |
| 160 | } |
| 161 | } catch (IOException e) { |
xueliang | 54525f5 | 2016-09-29 17:28:35 +0900 | [diff] [blame] | 162 | log.error("Cannot communicate to device {} exception {}", ncDeviceId, e); |
| 163 | return false; |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +0900 | [diff] [blame] | 164 | } |
xueliang | 54525f5 | 2016-09-29 17:28:35 +0900 | [diff] [blame] | 165 | return true; |
Akihiro Yamanouchi | 4512222 | 2016-07-15 13:13:11 +0900 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | } |