blob: 79d0b067468ed94771afc1c6904d118949c39293 [file] [log] [blame]
Akihiro Yamanouchi45122222016-07-15 13:13:11 +09001/*
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
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;
24import org.onosproject.mastership.MastershipService;
25import org.slf4j.Logger;
26
27import java.io.IOException;
28import 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;
34
35/**
36 * Implementation to get and set parameters available in vOLT
37 * through the Netconf protocol.
38 */
39public 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";
xueliang54525f52016-09-29 17:28:35 +090046 private static final Set<String> SEVERITYLEVELS =
47 ImmutableSet.of("none", "info", "minor", "major", "critical");
Akihiro Yamanouchi45122222016-07-15 13:13:11 +090048 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));
xueliang54525f52016-09-29 17:28:35 +090064 return null;
Akihiro Yamanouchi45122222016-07-15 13:13:11 +090065 }
66
67 try {
68 StringBuilder request = new StringBuilder();
xueliang54525f52016-09-29 17:28:35 +090069 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 Yamanouchi45122222016-07-15 13:13:11 +090075
xueliang54525f52016-09-29 17:28:35 +090076 reply = controller
77 .getDevicesMap()
78 .get(ncDeviceId)
79 .getSession()
80 .get(request.toString(), REPORT_ALL);
Akihiro Yamanouchi45122222016-07-15 13:13:11 +090081 } catch (IOException e) {
xueliang54525f52016-09-29 17:28:35 +090082 log.error("Cannot communicate to device {} exception {}", ncDeviceId, e);
Akihiro Yamanouchi45122222016-07-15 13:13:11 +090083 }
84 return reply;
85 }
86
87 @Override
xueliang54525f52016-09-29 17:28:35 +090088 public boolean setAlertFilter(String severity) {
Akihiro Yamanouchi45122222016-07-15 13:13:11 +090089 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));
xueliang54525f52016-09-29 17:28:35 +090099 return false;
Akihiro Yamanouchi45122222016-07-15 13:13:11 +0900100 }
101
xueliang54525f52016-09-29 17:28:35 +0900102 if (!SEVERITYLEVELS.contains(severity)) {
103 log.error("Invalid severity level: {}", severity);
104 return false;
Akihiro Yamanouchi45122222016-07-15 13:13:11 +0900105 }
106
107 try {
108 StringBuilder request = new StringBuilder();
xueliang54525f52016-09-29 17:28:35 +0900109 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 Yamanouchi45122222016-07-15 13:13:11 +0900117
118 controller.getDevicesMap().get(ncDeviceId).getSession().
119 editConfig(RUNNING, null, request.toString());
120 } catch (IOException e) {
xueliang54525f52016-09-29 17:28:35 +0900121 log.error("Cannot communicate to device {} exception {}", ncDeviceId, e);
122 return false;
Akihiro Yamanouchi45122222016-07-15 13:13:11 +0900123 }
xueliang54525f52016-09-29 17:28:35 +0900124 return true;
Akihiro Yamanouchi45122222016-07-15 13:13:11 +0900125 }
126
127 @Override
xueliang54525f52016-09-29 17:28:35 +0900128 public boolean subscribe(String mode) {
Akihiro Yamanouchi45122222016-07-15 13:13:11 +0900129 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));
xueliang54525f52016-09-29 17:28:35 +0900139 return false;
Akihiro Yamanouchi45122222016-07-15 13:13:11 +0900140 }
141
142 if (mode != null) {
143 if (!DISABLE.equals(mode)) {
xueliang54525f52016-09-29 17:28:35 +0900144 log.error("Invalid mode: {}", mode);
145 return false;
Akihiro Yamanouchi45122222016-07-15 13:13:11 +0900146 }
147 }
148
149 try {
150 if (mode != null) {
151 controller.getDevicesMap().get(ncDeviceId).getSession().
152 endSubscription();
153 } else {
154 StringBuilder request = new StringBuilder();
xueliang54525f52016-09-29 17:28:35 +0900155 request.append(ANGLE_LEFT + NOTIFY_ALERT + SPACE);
156 request.append(VOLT_NE_NAMESPACE + SLASH + ANGLE_RIGHT);
Akihiro Yamanouchi45122222016-07-15 13:13:11 +0900157
158 controller.getDevicesMap().get(ncDeviceId).getSession().
159 startSubscription(request.toString());
160 }
161 } catch (IOException e) {
xueliang54525f52016-09-29 17:28:35 +0900162 log.error("Cannot communicate to device {} exception {}", ncDeviceId, e);
163 return false;
Akihiro Yamanouchi45122222016-07-15 13:13:11 +0900164 }
xueliang54525f52016-09-29 17:28:35 +0900165 return true;
Akihiro Yamanouchi45122222016-07-15 13:13:11 +0900166 }
167
168}