blob: 4e050010383d5da9c1d2668c521e58813ba4e8fe [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;
82 private static final int FIRST_PART = 0;
83 private static final int SECOND_PART = 1;
84 private static final int THIRD_PART = 2;
85 private static final int RANGE_MIN = 0;
86 private static final int RANGE_MAX = 1;
87 private static final int ZERO = 0;
88 private static final int THREE = 3;
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));
103 return reply;
104 }
105
106 try {
107 StringBuilder request = new StringBuilder();
108 request.append(VOLT_NE_OPEN).append(VOLT_NE_NAMESPACE);
109 request.append(ANGLE_RIGHT).append(NEW_LINE);
110 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);
117 return reply;
118 }
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);
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900121 return reply;
122 }
123 request.append(buildStartTag(GPON_PONLINK_PORTS));
124 request.append(buildStartTag(GPON_PONLINK_PORT));
125 request.append(buildStartTag(PONLINK_ID, false));
126 request.append(target);
127 request.append(buildEndTag(PONLINK_ID));
128
129 request.append(buildEndTag(GPON_PONLINK_PORT));
130 request.append(buildEndTag(GPON_PONLINK_PORTS));
131 } else {
132 request.append(buildEmptyTag(GPON_PONLINK_PORTS));
133 }
134 request.append(buildEndTag(VOLT_PORTS));
135 request.append(VOLT_NE_CLOSE);
136
xueliang714dd2b2016-09-13 16:43:32 +0900137 reply = controller
138 .getDevicesMap()
139 .get(ncDeviceId)
140 .getSession()
141 .get(request.toString(), REPORT_ALL);
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900142 } catch (IOException e) {
xueliang714dd2b2016-09-13 16:43:32 +0900143 log.error("Cannot communicate to device {} exception {}", ncDeviceId, e);
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900144 }
145 return reply;
146 }
147
148 @Override
xueliang714dd2b2016-09-13 16:43:32 +0900149 public boolean setPonLink(String target) {
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900150 DriverHandler handler = handler();
151 NetconfController controller = handler.get(NetconfController.class);
152 MastershipService mastershipService = handler.get(MastershipService.class);
153 DeviceId ncDeviceId = handler.data().deviceId();
154 checkNotNull(controller, "Netconf controller is null");
155
156 if (!mastershipService.isLocalMaster(ncDeviceId)) {
157 log.warn("Not master for {} Use {} to execute command",
158 ncDeviceId,
159 mastershipService.getMasterFor(ncDeviceId));
xueliang714dd2b2016-09-13 16:43:32 +0900160 return false;
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900161 }
162
xueliang714dd2b2016-09-13 16:43:32 +0900163 String[] data = checkSetInput(target);
164 if (data == null) {
165 log.error("Failed to check input: {}", target);
166 return false;
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900167 }
168
xueliang714dd2b2016-09-13 16:43:32 +0900169 boolean result = false;
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900170 try {
171 StringBuilder request = new StringBuilder();
172 request.append(VOLT_NE_OPEN).append(VOLT_NE_NAMESPACE);
173 request.append(ANGLE_RIGHT).append(NEW_LINE);
174 request.append(buildStartTag(VOLT_PORTS));
175 request.append(buildStartTag(GPON_PONLINK_PORTS));
176 request.append(buildStartTag(GPON_PONLINK_PORT));
177 request.append(buildStartTag(PONLINK_ID, false));
xueliang714dd2b2016-09-13 16:43:32 +0900178 request.append(data[FIRST_PART]);
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900179 request.append(buildEndTag(PONLINK_ID));
180
xueliang714dd2b2016-09-13 16:43:32 +0900181 request.append(buildStartTag(data[SECOND_PART], false));
182 request.append(data[THIRD_PART]);
183 request.append(buildEndTag(data[SECOND_PART]));
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900184
185 request.append(buildEndTag(GPON_PONLINK_PORT));
186 request.append(buildEndTag(GPON_PONLINK_PORTS));
187 request.append(buildEndTag(VOLT_PORTS));
188 request.append(VOLT_NE_CLOSE);
189
xueliang714dd2b2016-09-13 16:43:32 +0900190 result = controller.getDevicesMap().get(ncDeviceId).getSession().
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900191 editConfig(RUNNING, null, request.toString());
192 } catch (IOException e) {
xueliang714dd2b2016-09-13 16:43:32 +0900193 log.error("Cannot communicate to device {} exception {}", ncDeviceId, e);
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900194 }
xueliang714dd2b2016-09-13 16:43:32 +0900195 return result;
Akihiro Yamanouchid4912842016-07-01 10:38:46 +0900196 }
197
xueliang714dd2b2016-09-13 16:43:32 +0900198 /**
199 * Verifies input string for valid options.
200 *
201 * @param target input data in string
202 * @return String array
203 * @return null if an error condition is detected
204 */
205 private String[] checkSetInput(String target) {
206 String[] data = target.split(COLON);
207 String paramName = data[SECOND_PART];
208 String paramValue = data[THIRD_PART];
209 int pon;
210
211 if (data.length != THREE) {
212 log.error("Invalid number of arguments {}", data.length);
213 return null;
214 }
215
216 try {
217 pon = Integer.parseInt(data[FIRST_PART]);
218 if (pon <= ZERO) {
219 log.error("Invalid integer for ponlink-id: {}",
220 data[FIRST_PART]);
221 return null;
222 }
223 } catch (NumberFormatException e) {
224 log.error("Non-number input for ponlink-id: {}",
225 data[FIRST_PART]);
226 return null;
227 }
228
229 if (!PON_LINK_PARAMS.containsKey(paramName)) {
230 log.error("Unsupported parameter: {}", paramName);
231 return null;
232 }
233
234 List<Integer> range = PON_LINK_PARAMS.get(paramName);
235 if (range == null) {
236 switch (paramName) {
237 case ADMIN_STATE:
238 if (!validState(ADMINSTATES, paramName, paramValue)) {
239 return null;
240 }
241 break;
242 case ONU_DISCOVERY_MODE:
243 if (!validState(ONUDISCOVERYMODES, paramName, paramValue)) {
244 return null;
245 }
246 break;
247 default:
248 if (!validState(PMENABLES, paramName, paramValue)) {
249 return null;
250 }
251 break;
252 }
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
281 * @return true if the param is valid
282 * @return false if the param is invalid
283 */
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}