blob: 18fe8ee34140477cfbe8e0bc0917fdce7619828e [file] [log] [blame]
Akihiro Yamanouchi45122222016-07-15 13:13:11 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Akihiro Yamanouchi45122222016-07-15 13:13:11 +09003 *
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
17package org.onosproject.drivers.fujitsu;
18
19import org.onosproject.net.DeviceId;
20import org.onosproject.drivers.fujitsu.behaviour.VoltAlertConfig;
21import org.onosproject.net.driver.AbstractHandlerBehaviour;
22import org.onosproject.net.driver.DriverHandler;
23import org.onosproject.netconf.NetconfController;
Yuta HIGUCHI234eaf32017-09-06 13:45:05 -070024import org.onosproject.netconf.NetconfException;
Akihiro Yamanouchi45122222016-07-15 13:13:11 +090025import org.onosproject.mastership.MastershipService;
26import org.slf4j.Logger;
27
Akihiro Yamanouchi45122222016-07-15 13:13:11 +090028import java.util.Set;
29
30import com.google.common.collect.ImmutableSet;
31import static com.google.common.base.Preconditions.checkNotNull;
32import static org.onosproject.drivers.fujitsu.FujitsuVoltXmlUtility.*;
33import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHIdd7c3f82017-09-03 14:18:01 -070034import static org.onosproject.netconf.DatastoreId.RUNNING;
Akihiro Yamanouchi45122222016-07-15 13:13:11 +090035
36/**
37 * Implementation to get and set parameters available in vOLT
38 * through the Netconf protocol.
39 */
40public 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";
xueliang54525f52016-09-29 17:28:35 +090047 private static final Set<String> SEVERITYLEVELS =
48 ImmutableSet.of("none", "info", "minor", "major", "critical");
Akihiro Yamanouchi45122222016-07-15 13:13:11 +090049 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));
xueliang54525f52016-09-29 17:28:35 +090065 return null;
Akihiro Yamanouchi45122222016-07-15 13:13:11 +090066 }
67
68 try {
69 StringBuilder request = new StringBuilder();
xueliang54525f52016-09-29 17:28:35 +090070 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 Yamanouchi45122222016-07-15 13:13:11 +090076
xueliang54525f52016-09-29 17:28:35 +090077 reply = controller
78 .getDevicesMap()
79 .get(ncDeviceId)
80 .getSession()
81 .get(request.toString(), REPORT_ALL);
Yuta HIGUCHI234eaf32017-09-06 13:45:05 -070082 } catch (NetconfException e) {
xueliang54525f52016-09-29 17:28:35 +090083 log.error("Cannot communicate to device {} exception {}", ncDeviceId, e);
Akihiro Yamanouchi45122222016-07-15 13:13:11 +090084 }
85 return reply;
86 }
87
88 @Override
xueliang54525f52016-09-29 17:28:35 +090089 public boolean setAlertFilter(String severity) {
Akihiro Yamanouchi45122222016-07-15 13:13:11 +090090 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));
xueliang54525f52016-09-29 17:28:35 +0900100 return false;
Akihiro Yamanouchi45122222016-07-15 13:13:11 +0900101 }
102
xueliang54525f52016-09-29 17:28:35 +0900103 if (!SEVERITYLEVELS.contains(severity)) {
104 log.error("Invalid severity level: {}", severity);
105 return false;
Akihiro Yamanouchi45122222016-07-15 13:13:11 +0900106 }
107
108 try {
109 StringBuilder request = new StringBuilder();
xueliang54525f52016-09-29 17:28:35 +0900110 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 Yamanouchi45122222016-07-15 13:13:11 +0900118
119 controller.getDevicesMap().get(ncDeviceId).getSession().
120 editConfig(RUNNING, null, request.toString());
Yuta HIGUCHI234eaf32017-09-06 13:45:05 -0700121 } catch (NetconfException e) {
xueliang54525f52016-09-29 17:28:35 +0900122 log.error("Cannot communicate to device {} exception {}", ncDeviceId, e);
123 return false;
Akihiro Yamanouchi45122222016-07-15 13:13:11 +0900124 }
xueliang54525f52016-09-29 17:28:35 +0900125 return true;
Akihiro Yamanouchi45122222016-07-15 13:13:11 +0900126 }
127
128 @Override
xueliang54525f52016-09-29 17:28:35 +0900129 public boolean subscribe(String mode) {
Akihiro Yamanouchi45122222016-07-15 13:13:11 +0900130 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));
xueliang54525f52016-09-29 17:28:35 +0900140 return false;
Akihiro Yamanouchi45122222016-07-15 13:13:11 +0900141 }
142
143 if (mode != null) {
144 if (!DISABLE.equals(mode)) {
xueliang54525f52016-09-29 17:28:35 +0900145 log.error("Invalid mode: {}", mode);
146 return false;
Akihiro Yamanouchi45122222016-07-15 13:13:11 +0900147 }
148 }
149
150 try {
151 if (mode != null) {
152 controller.getDevicesMap().get(ncDeviceId).getSession().
153 endSubscription();
154 } else {
155 StringBuilder request = new StringBuilder();
xueliang54525f52016-09-29 17:28:35 +0900156 request.append(ANGLE_LEFT + NOTIFY_ALERT + SPACE);
157 request.append(VOLT_NE_NAMESPACE + SLASH + ANGLE_RIGHT);
Akihiro Yamanouchi45122222016-07-15 13:13:11 +0900158
159 controller.getDevicesMap().get(ncDeviceId).getSession().
160 startSubscription(request.toString());
161 }
Yuta HIGUCHI234eaf32017-09-06 13:45:05 -0700162 } catch (NetconfException e) {
xueliang54525f52016-09-29 17:28:35 +0900163 log.error("Cannot communicate to device {} exception {}", ncDeviceId, e);
164 return false;
Akihiro Yamanouchi45122222016-07-15 13:13:11 +0900165 }
xueliang54525f52016-09-29 17:28:35 +0900166 return true;
Akihiro Yamanouchi45122222016-07-15 13:13:11 +0900167 }
168
169}