blob: 2f642e3938588c16cd08ce681a3dd60a152700ce [file] [log] [blame]
Akihiro Yamanouchid4912842016-07-01 10:38:46 +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.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;
25import org.slf4j.Logger;
26
27import java.io.IOException;
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;
38
39/**
40 * Implementation to get and set parameters available in vOLT
41 * through the Netconf protocol.
42 */
43public class FujitsuVoltPonLinkConfig extends AbstractHandlerBehaviour
44 implements VoltPonLinkConfig {
45
46 private final Logger log = getLogger(FujitsuVoltPonLinkConfig.class);
xueliang714dd2b2016-09-13 16:43:32 +090047 private static final Map<String, List<Integer>> PON_LINK_PARAMS = new HashMap<String, List<Integer>>() {
48 {
49 put("onu-discovery-interval", Arrays.asList(ONU_DISCOVERY_INTERVAL_MIN, ONU_DISCOVERY_INTERVAL_MAX));
50 put("dba-cycle-time", Arrays.asList(DBA_CYCLE_TIME_MIN, DBA_CYCLE_TIME_MAX));
51 put("mac-age-time", Arrays.asList(MAC_AGE_TIME_MIN, MAC_AGE_TIME_MAX));
52 put("lof-threshold", Arrays.asList(LOF_THRESHOLD_MIN, LOF_THRESHOLD_MAX));
53 put("los-threshold", Arrays.asList(LOS_THRESHOLD_MIN, LOS_THRESHOLD_MAX));
54 put(ONU_DISCOVERY_MODE, null);
55 put(PM_ENABLE, null);
56 put(ADMIN_STATE, null);
xueliang0e946fc2016-12-08 15:00:49 +090057 put(LOOPBACK_ENABLE, null);
xueliang714dd2b2016-09-13 16:43:32 +090058 }
59 };
Akihiro Yamanouchid4912842016-07-01 10:38:46 +090060 private static final String VOLT_PORTS = "volt-ports";
61 private static final String GPON_PONLINK_PORTS = "gpon-ponlink-ports";
62 private static final String GPON_PONLINK_PORT = "gpon-ponlink-port";
xueliang714dd2b2016-09-13 16:43:32 +090063 private static final String ADMIN_STATE = "admin-state";
64 private static final String ONU_DISCOVERY_MODE = "onu-discovery-mode";
65 private static final String PM_ENABLE = "pm-enable";
xueliang0e946fc2016-12-08 15:00:49 +090066 private static final String LOOPBACK_ENABLE = "loopback-enable";
xueliang714dd2b2016-09-13 16:43:32 +090067 private static final Set<String> ADMINSTATES =
68 ImmutableSet.of("enable", "disable");
69 private static final Set<String> ONUDISCOVERYMODES =
70 ImmutableSet.of("auto", "manual", "disabled");
71 private static final Set<String> PMENABLES =
72 ImmutableSet.of("true", "false");
xueliang0e946fc2016-12-08 15:00:49 +090073 private static final Set<String> LOOPBACKENABLES =
74 ImmutableSet.of("true", "false");
xueliang714dd2b2016-09-13 16:43:32 +090075
76 private static final int ONU_DISCOVERY_INTERVAL_MIN = 1;
77 private static final int ONU_DISCOVERY_INTERVAL_MAX = 3600;
78 private static final int DBA_CYCLE_TIME_MIN = 2;
79 private static final int DBA_CYCLE_TIME_MAX = 40;
80 private static final int MAC_AGE_TIME_MIN = 1000;
81 private static final int MAC_AGE_TIME_MAX = 3600000;
82 private static final int LOF_THRESHOLD_MIN = 1;
83 private static final int LOF_THRESHOLD_MAX = 10;
84 private static final int LOS_THRESHOLD_MIN = 1;
85 private static final int LOS_THRESHOLD_MAX = 10;
xueliang714dd2b2016-09-13 16:43:32 +090086 private static final int RANGE_MIN = 0;
87 private static final int RANGE_MAX = 1;
Akihiro Yamanouchid4912842016-07-01 10:38:46 +090088
89 @Override
90 public String getPonLinks(String target) {
91 DriverHandler handler = handler();
92 NetconfController controller = handler.get(NetconfController.class);
93 MastershipService mastershipService = handler.get(MastershipService.class);
94 DeviceId ncDeviceId = handler.data().deviceId();
95 checkNotNull(controller, "Netconf controller is null");
96 String reply = null;
97
98 if (!mastershipService.isLocalMaster(ncDeviceId)) {
99 log.warn("Not master for {} Use {} to execute command",
100 ncDeviceId,
101 mastershipService.getMasterFor(ncDeviceId));
xueliang5b3d34442016-09-23 10:37:33 +0900102 return null;
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900103 }
104
105 try {
106 StringBuilder request = new StringBuilder();
xueliangc6e47e22016-10-20 16:37:24 +0900107 request.append(VOLT_NE_OPEN + VOLT_NE_NAMESPACE);
108 request.append(ANGLE_RIGHT + NEW_LINE);
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900109 request.append(buildStartTag(VOLT_PORTS));
110 if (target != null) {
xueliang714dd2b2016-09-13 16:43:32 +0900111 int pon;
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900112 try {
113 pon = Integer.parseInt(target);
xueliang714dd2b2016-09-13 16:43:32 +0900114 if (pon <= ZERO) {
115 log.error("Invalid integer for ponlink-id:{}", target);
xueliang5b3d34442016-09-23 10:37:33 +0900116 return null;
xueliang714dd2b2016-09-13 16:43:32 +0900117 }
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900118 } catch (NumberFormatException e) {
xueliang714dd2b2016-09-13 16:43:32 +0900119 log.error("Non-number input for ponlink-id:{}", target);
xueliang5b3d34442016-09-23 10:37:33 +0900120 return null;
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900121 }
xueliangc6e47e22016-10-20 16:37:24 +0900122 request.append(buildStartTag(GPON_PONLINK_PORTS))
123 .append(buildStartTag(GPON_PONLINK_PORT))
124 .append(buildStartTag(PONLINK_ID, false))
125 .append(target)
126 .append(buildEndTag(PONLINK_ID))
127 .append(buildEndTag(GPON_PONLINK_PORT))
128 .append(buildEndTag(GPON_PONLINK_PORTS));
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900129 } else {
130 request.append(buildEmptyTag(GPON_PONLINK_PORTS));
131 }
132 request.append(buildEndTag(VOLT_PORTS));
133 request.append(VOLT_NE_CLOSE);
134
xueliang714dd2b2016-09-13 16:43:32 +0900135 reply = controller
136 .getDevicesMap()
137 .get(ncDeviceId)
138 .getSession()
139 .get(request.toString(), REPORT_ALL);
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900140 } catch (IOException e) {
xueliang714dd2b2016-09-13 16:43:32 +0900141 log.error("Cannot communicate to device {} exception {}", ncDeviceId, e);
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900142 }
143 return reply;
144 }
145
146 @Override
xueliang714dd2b2016-09-13 16:43:32 +0900147 public boolean setPonLink(String target) {
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900148 DriverHandler handler = handler();
149 NetconfController controller = handler.get(NetconfController.class);
150 MastershipService mastershipService = handler.get(MastershipService.class);
151 DeviceId ncDeviceId = handler.data().deviceId();
152 checkNotNull(controller, "Netconf controller is null");
153
154 if (!mastershipService.isLocalMaster(ncDeviceId)) {
155 log.warn("Not master for {} Use {} to execute command",
156 ncDeviceId,
157 mastershipService.getMasterFor(ncDeviceId));
xueliang714dd2b2016-09-13 16:43:32 +0900158 return false;
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900159 }
160
xueliang714dd2b2016-09-13 16:43:32 +0900161 String[] data = checkSetInput(target);
162 if (data == null) {
163 log.error("Failed to check input: {}", target);
164 return false;
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900165 }
166
xueliang714dd2b2016-09-13 16:43:32 +0900167 boolean result = false;
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900168 try {
169 StringBuilder request = new StringBuilder();
xueliangc6e47e22016-10-20 16:37:24 +0900170 request.append(VOLT_NE_OPEN + VOLT_NE_NAMESPACE);
171 request.append(ANGLE_RIGHT + NEW_LINE);
172 request.append(buildStartTag(VOLT_PORTS))
173 .append(buildStartTag(GPON_PONLINK_PORTS))
174 .append(buildStartTag(GPON_PONLINK_PORT))
175 .append(buildStartTag(PONLINK_ID, false))
176 .append(data[FIRST_PART])
177 .append(buildEndTag(PONLINK_ID))
178 .append(buildStartTag(data[SECOND_PART], false))
179 .append(data[THIRD_PART])
180 .append(buildEndTag(data[SECOND_PART]))
181 .append(buildEndTag(GPON_PONLINK_PORT))
182 .append(buildEndTag(GPON_PONLINK_PORTS))
183 .append(buildEndTag(VOLT_PORTS))
184 .append(VOLT_NE_CLOSE);
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900185
xueliang714dd2b2016-09-13 16:43:32 +0900186 result = controller.getDevicesMap().get(ncDeviceId).getSession().
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900187 editConfig(RUNNING, null, request.toString());
188 } catch (IOException e) {
xueliang714dd2b2016-09-13 16:43:32 +0900189 log.error("Cannot communicate to device {} exception {}", ncDeviceId, e);
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900190 }
xueliang714dd2b2016-09-13 16:43:32 +0900191 return result;
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900192 }
193
xueliang714dd2b2016-09-13 16:43:32 +0900194 /**
195 * Verifies input string for valid options.
196 *
197 * @param target input data in string
xueliangc6e47e22016-10-20 16:37:24 +0900198 * @return String array containing IDs; may be null if an error is detected
xueliang714dd2b2016-09-13 16:43:32 +0900199 */
200 private String[] checkSetInput(String target) {
201 String[] data = target.split(COLON);
202 String paramName = data[SECOND_PART];
203 String paramValue = data[THIRD_PART];
204 int pon;
205
206 if (data.length != THREE) {
207 log.error("Invalid number of arguments {}", data.length);
208 return null;
209 }
210
211 try {
212 pon = Integer.parseInt(data[FIRST_PART]);
213 if (pon <= ZERO) {
214 log.error("Invalid integer for ponlink-id: {}",
215 data[FIRST_PART]);
216 return null;
217 }
218 } catch (NumberFormatException e) {
219 log.error("Non-number input for ponlink-id: {}",
220 data[FIRST_PART]);
221 return null;
222 }
223
224 if (!PON_LINK_PARAMS.containsKey(paramName)) {
225 log.error("Unsupported parameter: {}", paramName);
226 return null;
227 }
228
229 List<Integer> range = PON_LINK_PARAMS.get(paramName);
230 if (range == null) {
231 switch (paramName) {
232 case ADMIN_STATE:
233 if (!validState(ADMINSTATES, paramName, paramValue)) {
234 return null;
235 }
236 break;
237 case ONU_DISCOVERY_MODE:
238 if (!validState(ONUDISCOVERYMODES, paramName, paramValue)) {
239 return null;
240 }
241 break;
xueliang0e946fc2016-12-08 15:00:49 +0900242 case PM_ENABLE:
xueliang714dd2b2016-09-13 16:43:32 +0900243 if (!validState(PMENABLES, paramName, paramValue)) {
244 return null;
245 }
246 break;
xueliang0e946fc2016-12-08 15:00:49 +0900247 default:
248 if (!validState(LOOPBACKENABLES, paramName, paramValue)) {
249 return null;
250 }
251 break;
xueliang714dd2b2016-09-13 16:43:32 +0900252 }
253 } else {
254 int value;
255
256 try {
257 value = Integer.parseInt(paramValue);
258 } catch (NumberFormatException e) {
259 log.error("Non-number input for Name {} : Value {}.",
260 paramName, paramValue);
261 return null;
262 }
263
264 if ((value < range.get(RANGE_MIN)) ||
265 (value > range.get(RANGE_MAX))) {
266 log.error("Out of value range for Name {} : Value {}.",
267 paramName, paramValue);
268 return null;
269 }
270 }
271
272 return data;
273 }
274
275 /**
276 * Verifies input string for valid options.
277 *
278 * @param states input data in string for parameter state
279 * @param name input data in string for parameter name
280 * @param value input data in string for parameter value
xueliangc6e47e22016-10-20 16:37:24 +0900281 * @return true/false if the param is valid/invalid
xueliang714dd2b2016-09-13 16:43:32 +0900282 */
283 private boolean validState(Set<String> states, String name, String value) {
284 if (!states.contains(value)) {
285 log.error("Invalid value for Name {} : Value {}.", name, value);
286 return false;
287 }
288 return true;
289 }
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900290}