blob: fbefb5a9f118e3a2b56cf43b4b8f30154816bbed [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";
46 private final Set<String> severityLevels = ImmutableSet.of(
47 "none", "info", "minor", "major", "critical");
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));
64 return reply;
65 }
66
67 try {
68 StringBuilder request = new StringBuilder();
69 request.append(VOLT_NE_OPEN).append(VOLT_NE_NAMESPACE);
70 request.append(ANGLE_RIGHT).append(NEW_LINE);
71 request.append(buildStartTag(VOLT_ALERTS));
72 request.append(buildEmptyTag(ALERT_FILTER));
73 request.append(buildEndTag(VOLT_ALERTS));
74 request.append(VOLT_NE_CLOSE);
75
76 reply = controller.
77 getDevicesMap().get(ncDeviceId).getSession().
78 get(request.toString(), REPORT_ALL);
79 } catch (IOException e) {
80 log.error("Cannot communicate to device {} exception ", ncDeviceId, e);
81 }
82 return reply;
83 }
84
85 @Override
86 public void setAlertFilter(String severity) {
87 DriverHandler handler = handler();
88 NetconfController controller = handler.get(NetconfController.class);
89 MastershipService mastershipService = handler.get(MastershipService.class);
90 DeviceId ncDeviceId = handler.data().deviceId();
91 checkNotNull(controller, "Netconf controller is null");
92
93 if (!mastershipService.isLocalMaster(ncDeviceId)) {
94 log.warn("Not master for {} Use {} to execute command",
95 ncDeviceId,
96 mastershipService.getMasterFor(ncDeviceId));
97 return;
98 }
99
100 if (!severityLevels.contains(severity)) {
101 log.error("Invalid severity level: " + severity);
102 return;
103 }
104
105 try {
106 StringBuilder request = new StringBuilder();
107 request.append(VOLT_NE_OPEN).append(VOLT_NE_NAMESPACE);
108 request.append(ANGLE_RIGHT).append(NEW_LINE);
109 request.append(buildStartTag(VOLT_ALERTS));
110 request.append(buildStartTag(ALERT_FILTER, false));
111 request.append(severity);
112 request.append(buildEndTag(ALERT_FILTER));
113 request.append(buildEndTag(VOLT_ALERTS));
114 request.append(VOLT_NE_CLOSE);
115
116 controller.getDevicesMap().get(ncDeviceId).getSession().
117 editConfig(RUNNING, null, request.toString());
118 } catch (IOException e) {
119 log.error("Cannot communicate to device {} exception ", ncDeviceId, e);
120 }
121 }
122
123 @Override
124 public void subscribe(String mode) {
125 DriverHandler handler = handler();
126 NetconfController controller = handler.get(NetconfController.class);
127 MastershipService mastershipService = handler.get(MastershipService.class);
128 DeviceId ncDeviceId = handler.data().deviceId();
129 checkNotNull(controller, "Netconf controller is null");
130
131 if (!mastershipService.isLocalMaster(ncDeviceId)) {
132 log.warn("Not master for {} Use {} to execute command",
133 ncDeviceId,
134 mastershipService.getMasterFor(ncDeviceId));
135 return;
136 }
137
138 if (mode != null) {
139 if (!DISABLE.equals(mode)) {
140 log.error("Invalid mode: " + mode);
141 return;
142 }
143 }
144
145 try {
146 if (mode != null) {
147 controller.getDevicesMap().get(ncDeviceId).getSession().
148 endSubscription();
149 } else {
150 StringBuilder request = new StringBuilder();
151 request.append(ANGLE_LEFT).append(NOTIFY_ALERT).append(SPACE);
152 request.append(VOLT_NE_NAMESPACE).append(SLASH).append(ANGLE_RIGHT);
153
154 controller.getDevicesMap().get(ncDeviceId).getSession().
155 startSubscription(request.toString());
156 }
157 } catch (IOException e) {
158 log.error("Cannot communicate to device {} exception ", ncDeviceId, e);
159 }
160 }
161
162}