blob: 1d6e14505ea2ae336d3a7d206b153d040d252544 [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);
57 }
58 };
Akihiro Yamanouchid4912842016-07-01 10:38:46 +090059 private static final String VOLT_PORTS = "volt-ports";
60 private static final String GPON_PONLINK_PORTS = "gpon-ponlink-ports";
61 private static final String GPON_PONLINK_PORT = "gpon-ponlink-port";
xueliang714dd2b2016-09-13 16:43:32 +090062 private static final String ADMIN_STATE = "admin-state";
63 private static final String ONU_DISCOVERY_MODE = "onu-discovery-mode";
64 private static final String PM_ENABLE = "pm-enable";
65 private static final Set<String> ADMINSTATES =
66 ImmutableSet.of("enable", "disable");
67 private static final Set<String> ONUDISCOVERYMODES =
68 ImmutableSet.of("auto", "manual", "disabled");
69 private static final Set<String> PMENABLES =
70 ImmutableSet.of("true", "false");
71
72 private static final int ONU_DISCOVERY_INTERVAL_MIN = 1;
73 private static final int ONU_DISCOVERY_INTERVAL_MAX = 3600;
74 private static final int DBA_CYCLE_TIME_MIN = 2;
75 private static final int DBA_CYCLE_TIME_MAX = 40;
76 private static final int MAC_AGE_TIME_MIN = 1000;
77 private static final int MAC_AGE_TIME_MAX = 3600000;
78 private static final int LOF_THRESHOLD_MIN = 1;
79 private static final int LOF_THRESHOLD_MAX = 10;
80 private static final int LOS_THRESHOLD_MIN = 1;
81 private static final int LOS_THRESHOLD_MAX = 10;
xueliang714dd2b2016-09-13 16:43:32 +090082 private static final int RANGE_MIN = 0;
83 private static final int RANGE_MAX = 1;
Akihiro Yamanouchid4912842016-07-01 10:38:46 +090084
85 @Override
86 public String getPonLinks(String target) {
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 String reply = null;
93
94 if (!mastershipService.isLocalMaster(ncDeviceId)) {
95 log.warn("Not master for {} Use {} to execute command",
96 ncDeviceId,
97 mastershipService.getMasterFor(ncDeviceId));
xueliang5b3d34442016-09-23 10:37:33 +090098 return null;
Akihiro Yamanouchid4912842016-07-01 10:38:46 +090099 }
100
101 try {
102 StringBuilder request = new StringBuilder();
xueliangc6e47e22016-10-20 16:37:24 +0900103 request.append(VOLT_NE_OPEN + VOLT_NE_NAMESPACE);
104 request.append(ANGLE_RIGHT + NEW_LINE);
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900105 request.append(buildStartTag(VOLT_PORTS));
106 if (target != null) {
xueliang714dd2b2016-09-13 16:43:32 +0900107 int pon;
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900108 try {
109 pon = Integer.parseInt(target);
xueliang714dd2b2016-09-13 16:43:32 +0900110 if (pon <= ZERO) {
111 log.error("Invalid integer for ponlink-id:{}", target);
xueliang5b3d34442016-09-23 10:37:33 +0900112 return null;
xueliang714dd2b2016-09-13 16:43:32 +0900113 }
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900114 } catch (NumberFormatException e) {
xueliang714dd2b2016-09-13 16:43:32 +0900115 log.error("Non-number input for ponlink-id:{}", target);
xueliang5b3d34442016-09-23 10:37:33 +0900116 return null;
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900117 }
xueliangc6e47e22016-10-20 16:37:24 +0900118 request.append(buildStartTag(GPON_PONLINK_PORTS))
119 .append(buildStartTag(GPON_PONLINK_PORT))
120 .append(buildStartTag(PONLINK_ID, false))
121 .append(target)
122 .append(buildEndTag(PONLINK_ID))
123 .append(buildEndTag(GPON_PONLINK_PORT))
124 .append(buildEndTag(GPON_PONLINK_PORTS));
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900125 } else {
126 request.append(buildEmptyTag(GPON_PONLINK_PORTS));
127 }
128 request.append(buildEndTag(VOLT_PORTS));
129 request.append(VOLT_NE_CLOSE);
130
xueliang714dd2b2016-09-13 16:43:32 +0900131 reply = controller
132 .getDevicesMap()
133 .get(ncDeviceId)
134 .getSession()
135 .get(request.toString(), REPORT_ALL);
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900136 } catch (IOException e) {
xueliang714dd2b2016-09-13 16:43:32 +0900137 log.error("Cannot communicate to device {} exception {}", ncDeviceId, e);
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900138 }
139 return reply;
140 }
141
142 @Override
xueliang714dd2b2016-09-13 16:43:32 +0900143 public boolean setPonLink(String target) {
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900144 DriverHandler handler = handler();
145 NetconfController controller = handler.get(NetconfController.class);
146 MastershipService mastershipService = handler.get(MastershipService.class);
147 DeviceId ncDeviceId = handler.data().deviceId();
148 checkNotNull(controller, "Netconf controller is null");
149
150 if (!mastershipService.isLocalMaster(ncDeviceId)) {
151 log.warn("Not master for {} Use {} to execute command",
152 ncDeviceId,
153 mastershipService.getMasterFor(ncDeviceId));
xueliang714dd2b2016-09-13 16:43:32 +0900154 return false;
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900155 }
156
xueliang714dd2b2016-09-13 16:43:32 +0900157 String[] data = checkSetInput(target);
158 if (data == null) {
159 log.error("Failed to check input: {}", target);
160 return false;
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900161 }
162
xueliang714dd2b2016-09-13 16:43:32 +0900163 boolean result = false;
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900164 try {
165 StringBuilder request = new StringBuilder();
xueliangc6e47e22016-10-20 16:37:24 +0900166 request.append(VOLT_NE_OPEN + VOLT_NE_NAMESPACE);
167 request.append(ANGLE_RIGHT + NEW_LINE);
168 request.append(buildStartTag(VOLT_PORTS))
169 .append(buildStartTag(GPON_PONLINK_PORTS))
170 .append(buildStartTag(GPON_PONLINK_PORT))
171 .append(buildStartTag(PONLINK_ID, false))
172 .append(data[FIRST_PART])
173 .append(buildEndTag(PONLINK_ID))
174 .append(buildStartTag(data[SECOND_PART], false))
175 .append(data[THIRD_PART])
176 .append(buildEndTag(data[SECOND_PART]))
177 .append(buildEndTag(GPON_PONLINK_PORT))
178 .append(buildEndTag(GPON_PONLINK_PORTS))
179 .append(buildEndTag(VOLT_PORTS))
180 .append(VOLT_NE_CLOSE);
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900181
xueliang714dd2b2016-09-13 16:43:32 +0900182 result = controller.getDevicesMap().get(ncDeviceId).getSession().
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900183 editConfig(RUNNING, null, request.toString());
184 } catch (IOException e) {
xueliang714dd2b2016-09-13 16:43:32 +0900185 log.error("Cannot communicate to device {} exception {}", ncDeviceId, e);
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900186 }
xueliang714dd2b2016-09-13 16:43:32 +0900187 return result;
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900188 }
189
xueliang714dd2b2016-09-13 16:43:32 +0900190 /**
191 * Verifies input string for valid options.
192 *
193 * @param target input data in string
xueliangc6e47e22016-10-20 16:37:24 +0900194 * @return String array containing IDs; may be null if an error is detected
xueliang714dd2b2016-09-13 16:43:32 +0900195 */
196 private String[] checkSetInput(String target) {
197 String[] data = target.split(COLON);
198 String paramName = data[SECOND_PART];
199 String paramValue = data[THIRD_PART];
200 int pon;
201
202 if (data.length != THREE) {
203 log.error("Invalid number of arguments {}", data.length);
204 return null;
205 }
206
207 try {
208 pon = Integer.parseInt(data[FIRST_PART]);
209 if (pon <= ZERO) {
210 log.error("Invalid integer for ponlink-id: {}",
211 data[FIRST_PART]);
212 return null;
213 }
214 } catch (NumberFormatException e) {
215 log.error("Non-number input for ponlink-id: {}",
216 data[FIRST_PART]);
217 return null;
218 }
219
220 if (!PON_LINK_PARAMS.containsKey(paramName)) {
221 log.error("Unsupported parameter: {}", paramName);
222 return null;
223 }
224
225 List<Integer> range = PON_LINK_PARAMS.get(paramName);
226 if (range == null) {
227 switch (paramName) {
228 case ADMIN_STATE:
229 if (!validState(ADMINSTATES, paramName, paramValue)) {
230 return null;
231 }
232 break;
233 case ONU_DISCOVERY_MODE:
234 if (!validState(ONUDISCOVERYMODES, paramName, paramValue)) {
235 return null;
236 }
237 break;
238 default:
239 if (!validState(PMENABLES, paramName, paramValue)) {
240 return null;
241 }
242 break;
243 }
244 } else {
245 int value;
246
247 try {
248 value = Integer.parseInt(paramValue);
249 } catch (NumberFormatException e) {
250 log.error("Non-number input for Name {} : Value {}.",
251 paramName, paramValue);
252 return null;
253 }
254
255 if ((value < range.get(RANGE_MIN)) ||
256 (value > range.get(RANGE_MAX))) {
257 log.error("Out of value range for Name {} : Value {}.",
258 paramName, paramValue);
259 return null;
260 }
261 }
262
263 return data;
264 }
265
266 /**
267 * Verifies input string for valid options.
268 *
269 * @param states input data in string for parameter state
270 * @param name input data in string for parameter name
271 * @param value input data in string for parameter value
xueliangc6e47e22016-10-20 16:37:24 +0900272 * @return true/false if the param is valid/invalid
xueliang714dd2b2016-09-13 16:43:32 +0900273 */
274 private boolean validState(Set<String> states, String name, String value) {
275 if (!states.contains(value)) {
276 log.error("Invalid value for Name {} : Value {}.", name, value);
277 return false;
278 }
279 return true;
280 }
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900281}