blob: 675378babb831f9f0214f5e9d62ca178b172505b [file] [log] [blame]
Akihiro Yamanouchid4912842016-07-01 10:38:46 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Akihiro Yamanouchid4912842016-07-01 10:38:46 +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.mastership.MastershipService;
20import org.onosproject.net.DeviceId;
Akihiro Yamanouchi8d3a9d32016-07-12 11:41:44 +090021import org.onosproject.drivers.fujitsu.behaviour.VoltPonLinkConfig;
Akihiro Yamanouchid4912842016-07-01 10:38:46 +090022import org.onosproject.net.driver.AbstractHandlerBehaviour;
23import org.onosproject.net.driver.DriverHandler;
24import org.onosproject.netconf.NetconfController;
Yuta HIGUCHI234eaf32017-09-06 13:45:05 -070025import org.onosproject.netconf.NetconfException;
Akihiro Yamanouchid4912842016-07-01 10:38:46 +090026import org.slf4j.Logger;
27
xueliang714dd2b2016-09-13 16:43:32 +090028import java.util.Arrays;
29import java.util.List;
30import java.util.HashMap;
31import java.util.Map;
Akihiro Yamanouchid4912842016-07-01 10:38:46 +090032import java.util.Set;
33
34import com.google.common.collect.ImmutableSet;
35import static com.google.common.base.Preconditions.checkNotNull;
36import static org.onosproject.drivers.fujitsu.FujitsuVoltXmlUtility.*;
37import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHIdd7c3f82017-09-03 14:18:01 -070038import static org.onosproject.netconf.DatastoreId.RUNNING;
Akihiro Yamanouchid4912842016-07-01 10:38:46 +090039
40/**
41 * Implementation to get and set parameters available in vOLT
42 * through the Netconf protocol.
43 */
44public class FujitsuVoltPonLinkConfig extends AbstractHandlerBehaviour
45 implements VoltPonLinkConfig {
46
47 private final Logger log = getLogger(FujitsuVoltPonLinkConfig.class);
xueliang714dd2b2016-09-13 16:43:32 +090048 private static final Map<String, List<Integer>> PON_LINK_PARAMS = new HashMap<String, List<Integer>>() {
49 {
50 put("onu-discovery-interval", Arrays.asList(ONU_DISCOVERY_INTERVAL_MIN, ONU_DISCOVERY_INTERVAL_MAX));
51 put("dba-cycle-time", Arrays.asList(DBA_CYCLE_TIME_MIN, DBA_CYCLE_TIME_MAX));
52 put("mac-age-time", Arrays.asList(MAC_AGE_TIME_MIN, MAC_AGE_TIME_MAX));
53 put("lof-threshold", Arrays.asList(LOF_THRESHOLD_MIN, LOF_THRESHOLD_MAX));
54 put("los-threshold", Arrays.asList(LOS_THRESHOLD_MIN, LOS_THRESHOLD_MAX));
55 put(ONU_DISCOVERY_MODE, null);
56 put(PM_ENABLE, null);
57 put(ADMIN_STATE, null);
xueliang0e946fc2016-12-08 15:00:49 +090058 put(LOOPBACK_ENABLE, null);
xueliang714dd2b2016-09-13 16:43:32 +090059 }
60 };
Akihiro Yamanouchid4912842016-07-01 10:38:46 +090061 private static final String VOLT_PORTS = "volt-ports";
62 private static final String GPON_PONLINK_PORTS = "gpon-ponlink-ports";
63 private static final String GPON_PONLINK_PORT = "gpon-ponlink-port";
xueliang714dd2b2016-09-13 16:43:32 +090064 private static final String ADMIN_STATE = "admin-state";
65 private static final String ONU_DISCOVERY_MODE = "onu-discovery-mode";
66 private static final String PM_ENABLE = "pm-enable";
xueliang0e946fc2016-12-08 15:00:49 +090067 private static final String LOOPBACK_ENABLE = "loopback-enable";
xueliang714dd2b2016-09-13 16:43:32 +090068 private static final Set<String> ADMINSTATES =
69 ImmutableSet.of("enable", "disable");
70 private static final Set<String> ONUDISCOVERYMODES =
71 ImmutableSet.of("auto", "manual", "disabled");
72 private static final Set<String> PMENABLES =
73 ImmutableSet.of("true", "false");
xueliang0e946fc2016-12-08 15:00:49 +090074 private static final Set<String> LOOPBACKENABLES =
75 ImmutableSet.of("true", "false");
xueliang714dd2b2016-09-13 16:43:32 +090076
77 private static final int ONU_DISCOVERY_INTERVAL_MIN = 1;
78 private static final int ONU_DISCOVERY_INTERVAL_MAX = 3600;
79 private static final int DBA_CYCLE_TIME_MIN = 2;
80 private static final int DBA_CYCLE_TIME_MAX = 40;
81 private static final int MAC_AGE_TIME_MIN = 1000;
82 private static final int MAC_AGE_TIME_MAX = 3600000;
83 private static final int LOF_THRESHOLD_MIN = 1;
84 private static final int LOF_THRESHOLD_MAX = 10;
85 private static final int LOS_THRESHOLD_MIN = 1;
86 private static final int LOS_THRESHOLD_MAX = 10;
xueliang714dd2b2016-09-13 16:43:32 +090087 private static final int RANGE_MIN = 0;
88 private static final int RANGE_MAX = 1;
Akihiro Yamanouchid4912842016-07-01 10:38:46 +090089
90 @Override
91 public String getPonLinks(String target) {
92 DriverHandler handler = handler();
93 NetconfController controller = handler.get(NetconfController.class);
94 MastershipService mastershipService = handler.get(MastershipService.class);
95 DeviceId ncDeviceId = handler.data().deviceId();
96 checkNotNull(controller, "Netconf controller is null");
97 String reply = null;
98
99 if (!mastershipService.isLocalMaster(ncDeviceId)) {
100 log.warn("Not master for {} Use {} to execute command",
101 ncDeviceId,
102 mastershipService.getMasterFor(ncDeviceId));
xueliang5b3d34442016-09-23 10:37:33 +0900103 return null;
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900104 }
105
106 try {
107 StringBuilder request = new StringBuilder();
xueliangc6e47e22016-10-20 16:37:24 +0900108 request.append(VOLT_NE_OPEN + VOLT_NE_NAMESPACE);
109 request.append(ANGLE_RIGHT + NEW_LINE);
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900110 request.append(buildStartTag(VOLT_PORTS));
111 if (target != null) {
xueliang714dd2b2016-09-13 16:43:32 +0900112 int pon;
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900113 try {
114 pon = Integer.parseInt(target);
xueliang714dd2b2016-09-13 16:43:32 +0900115 if (pon <= ZERO) {
116 log.error("Invalid integer for ponlink-id:{}", target);
xueliang5b3d34442016-09-23 10:37:33 +0900117 return null;
xueliang714dd2b2016-09-13 16:43:32 +0900118 }
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900119 } catch (NumberFormatException e) {
xueliang714dd2b2016-09-13 16:43:32 +0900120 log.error("Non-number input for ponlink-id:{}", target);
xueliang5b3d34442016-09-23 10:37:33 +0900121 return null;
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900122 }
xueliangc6e47e22016-10-20 16:37:24 +0900123 request.append(buildStartTag(GPON_PONLINK_PORTS))
124 .append(buildStartTag(GPON_PONLINK_PORT))
125 .append(buildStartTag(PONLINK_ID, false))
126 .append(target)
127 .append(buildEndTag(PONLINK_ID))
128 .append(buildEndTag(GPON_PONLINK_PORT))
129 .append(buildEndTag(GPON_PONLINK_PORTS));
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900130 } else {
131 request.append(buildEmptyTag(GPON_PONLINK_PORTS));
132 }
133 request.append(buildEndTag(VOLT_PORTS));
134 request.append(VOLT_NE_CLOSE);
135
xueliang714dd2b2016-09-13 16:43:32 +0900136 reply = controller
137 .getDevicesMap()
138 .get(ncDeviceId)
139 .getSession()
140 .get(request.toString(), REPORT_ALL);
Yuta HIGUCHI234eaf32017-09-06 13:45:05 -0700141 } catch (NetconfException e) {
xueliang714dd2b2016-09-13 16:43:32 +0900142 log.error("Cannot communicate to device {} exception {}", ncDeviceId, e);
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900143 }
144 return reply;
145 }
146
147 @Override
xueliang714dd2b2016-09-13 16:43:32 +0900148 public boolean setPonLink(String target) {
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900149 DriverHandler handler = handler();
150 NetconfController controller = handler.get(NetconfController.class);
151 MastershipService mastershipService = handler.get(MastershipService.class);
152 DeviceId ncDeviceId = handler.data().deviceId();
153 checkNotNull(controller, "Netconf controller is null");
154
155 if (!mastershipService.isLocalMaster(ncDeviceId)) {
156 log.warn("Not master for {} Use {} to execute command",
157 ncDeviceId,
158 mastershipService.getMasterFor(ncDeviceId));
xueliang714dd2b2016-09-13 16:43:32 +0900159 return false;
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900160 }
161
xueliang714dd2b2016-09-13 16:43:32 +0900162 String[] data = checkSetInput(target);
163 if (data == null) {
164 log.error("Failed to check input: {}", target);
165 return false;
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900166 }
167
xueliang714dd2b2016-09-13 16:43:32 +0900168 boolean result = false;
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900169 try {
170 StringBuilder request = new StringBuilder();
xueliangc6e47e22016-10-20 16:37:24 +0900171 request.append(VOLT_NE_OPEN + VOLT_NE_NAMESPACE);
172 request.append(ANGLE_RIGHT + NEW_LINE);
173 request.append(buildStartTag(VOLT_PORTS))
174 .append(buildStartTag(GPON_PONLINK_PORTS))
175 .append(buildStartTag(GPON_PONLINK_PORT))
176 .append(buildStartTag(PONLINK_ID, false))
177 .append(data[FIRST_PART])
178 .append(buildEndTag(PONLINK_ID))
179 .append(buildStartTag(data[SECOND_PART], false))
180 .append(data[THIRD_PART])
181 .append(buildEndTag(data[SECOND_PART]))
182 .append(buildEndTag(GPON_PONLINK_PORT))
183 .append(buildEndTag(GPON_PONLINK_PORTS))
184 .append(buildEndTag(VOLT_PORTS))
185 .append(VOLT_NE_CLOSE);
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900186
xueliang714dd2b2016-09-13 16:43:32 +0900187 result = controller.getDevicesMap().get(ncDeviceId).getSession().
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900188 editConfig(RUNNING, null, request.toString());
Yuta HIGUCHI234eaf32017-09-06 13:45:05 -0700189 } catch (NetconfException e) {
xueliang714dd2b2016-09-13 16:43:32 +0900190 log.error("Cannot communicate to device {} exception {}", ncDeviceId, e);
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900191 }
xueliang714dd2b2016-09-13 16:43:32 +0900192 return result;
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900193 }
194
xueliang714dd2b2016-09-13 16:43:32 +0900195 /**
196 * Verifies input string for valid options.
197 *
198 * @param target input data in string
xueliangc6e47e22016-10-20 16:37:24 +0900199 * @return String array containing IDs; may be null if an error is detected
xueliang714dd2b2016-09-13 16:43:32 +0900200 */
201 private String[] checkSetInput(String target) {
202 String[] data = target.split(COLON);
203 String paramName = data[SECOND_PART];
204 String paramValue = data[THIRD_PART];
205 int pon;
206
207 if (data.length != THREE) {
208 log.error("Invalid number of arguments {}", data.length);
209 return null;
210 }
211
212 try {
213 pon = Integer.parseInt(data[FIRST_PART]);
214 if (pon <= ZERO) {
215 log.error("Invalid integer for ponlink-id: {}",
216 data[FIRST_PART]);
217 return null;
218 }
219 } catch (NumberFormatException e) {
220 log.error("Non-number input for ponlink-id: {}",
221 data[FIRST_PART]);
222 return null;
223 }
224
225 if (!PON_LINK_PARAMS.containsKey(paramName)) {
226 log.error("Unsupported parameter: {}", paramName);
227 return null;
228 }
229
230 List<Integer> range = PON_LINK_PARAMS.get(paramName);
231 if (range == null) {
232 switch (paramName) {
233 case ADMIN_STATE:
234 if (!validState(ADMINSTATES, paramName, paramValue)) {
235 return null;
236 }
237 break;
238 case ONU_DISCOVERY_MODE:
239 if (!validState(ONUDISCOVERYMODES, paramName, paramValue)) {
240 return null;
241 }
242 break;
xueliang0e946fc2016-12-08 15:00:49 +0900243 case PM_ENABLE:
xueliang714dd2b2016-09-13 16:43:32 +0900244 if (!validState(PMENABLES, paramName, paramValue)) {
245 return null;
246 }
247 break;
xueliang0e946fc2016-12-08 15:00:49 +0900248 default:
249 if (!validState(LOOPBACKENABLES, paramName, paramValue)) {
250 return null;
251 }
252 break;
xueliang714dd2b2016-09-13 16:43:32 +0900253 }
254 } else {
255 int value;
256
257 try {
258 value = Integer.parseInt(paramValue);
259 } catch (NumberFormatException e) {
260 log.error("Non-number input for Name {} : Value {}.",
261 paramName, paramValue);
262 return null;
263 }
264
265 if ((value < range.get(RANGE_MIN)) ||
266 (value > range.get(RANGE_MAX))) {
267 log.error("Out of value range for Name {} : Value {}.",
268 paramName, paramValue);
269 return null;
270 }
271 }
272
273 return data;
274 }
275
276 /**
277 * Verifies input string for valid options.
278 *
279 * @param states input data in string for parameter state
280 * @param name input data in string for parameter name
281 * @param value input data in string for parameter value
xueliangc6e47e22016-10-20 16:37:24 +0900282 * @return true/false if the param is valid/invalid
xueliang714dd2b2016-09-13 16:43:32 +0900283 */
284 private boolean validState(Set<String> states, String name, String value) {
285 if (!states.contains(value)) {
286 log.error("Invalid value for Name {} : Value {}.", name, value);
287 return false;
288 }
289 return true;
290 }
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900291}