blob: d2460ac0bc1c0264422b3a31aab8e8e68f51dae4 [file] [log] [blame]
Akihiro Yamanouchi8d3a9d32016-07-12 11:41:44 +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
xueliangffe73e42016-09-27 10:38:56 +090019import com.google.common.collect.ImmutableSet;
Akihiro Yamanouchi8d3a9d32016-07-12 11:41:44 +090020import org.onosproject.mastership.MastershipService;
21import org.onosproject.net.DeviceId;
22import org.onosproject.drivers.fujitsu.behaviour.VoltOnuOperConfig;
23import org.onosproject.net.driver.AbstractHandlerBehaviour;
24import org.onosproject.net.driver.DriverHandler;
25import org.onosproject.netconf.NetconfController;
26import org.slf4j.Logger;
27
28import java.io.IOException;
xueliangffe73e42016-09-27 10:38:56 +090029import java.util.Set;
Akihiro Yamanouchi8d3a9d32016-07-12 11:41:44 +090030
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 take actions on ONU available in vOLT
37 * through the Netconf protocol.
38 */
39public class FujitsuVoltOnuOperConfig extends AbstractHandlerBehaviour
40 implements VoltOnuOperConfig {
41
42 private final Logger log = getLogger(FujitsuVoltOnuOperConfig.class);
43 private static final String ONU_REBOOT = "onu-reboot";
44 private static final String ONU_ETHPORT_LOOPBACK = "onu-ethport-loopback";
45 private static final String ETHPORT_ID = "ethport-id";
xueliangffe73e42016-09-27 10:38:56 +090046 private static final String LOOPBACK_MODE = "mode";
47 private static final Set<String> LOOPBACKMODES =
48 ImmutableSet.of("operate", "release");
Akihiro Yamanouchi8d3a9d32016-07-12 11:41:44 +090049
50 @Override
51 public String rebootOnu(String target) {
52 DriverHandler handler = handler();
53 NetconfController controller = handler.get(NetconfController.class);
54 MastershipService mastershipService = handler.get(MastershipService.class);
55 DeviceId ncDeviceId = handler.data().deviceId();
56 checkNotNull(controller, "Netconf controller is null");
57 String reply = null;
58 String[] onuId = null;
59
60 if (!mastershipService.isLocalMaster(ncDeviceId)) {
61 log.warn("Not master for {} Use {} to execute command",
62 ncDeviceId,
63 mastershipService.getMasterFor(ncDeviceId));
xueliangffe73e42016-09-27 10:38:56 +090064 return null;
Akihiro Yamanouchi8d3a9d32016-07-12 11:41:44 +090065 }
66
xueliangffe73e42016-09-27 10:38:56 +090067 onuId = checkIdString(target, TWO);
68 if (onuId == null) {
69 log.error("Invalid ONU identifier {}", target);
70 return null;
Akihiro Yamanouchi8d3a9d32016-07-12 11:41:44 +090071 }
72
73 try {
74 StringBuilder request = new StringBuilder();
xueliangffe73e42016-09-27 10:38:56 +090075 request.append(ANGLE_LEFT + ONU_REBOOT + SPACE);
76 request.append(VOLT_NE_NAMESPACE + ANGLE_RIGHT + NEW_LINE);
Akihiro Yamanouchi8d3a9d32016-07-12 11:41:44 +090077
xueliangffe73e42016-09-27 10:38:56 +090078 request.append(buildStartTag(PONLINK_ID, false))
79 .append(onuId[FIRST_PART])
80 .append(buildEndTag(PONLINK_ID))
81 .append(buildStartTag(ONU_ID, false))
82 .append(onuId[SECOND_PART])
83 .append(buildEndTag(ONU_ID))
84 .append(buildEndTag(ONU_REBOOT));
85
86 reply = controller
87 .getDevicesMap()
88 .get(ncDeviceId)
89 .getSession()
90 .doWrappedRpc(request.toString());
Akihiro Yamanouchi8d3a9d32016-07-12 11:41:44 +090091 } catch (IOException e) {
xueliangffe73e42016-09-27 10:38:56 +090092 log.error("Cannot communicate to device {} exception {}", ncDeviceId, e);
Akihiro Yamanouchi8d3a9d32016-07-12 11:41:44 +090093 }
94 return reply;
95 }
96
97 @Override
98 public String loopbackEthOnu(String target) {
99 DriverHandler handler = handler();
100 NetconfController controller = handler.get(NetconfController.class);
101 MastershipService mastershipService = handler.get(MastershipService.class);
102 DeviceId ncDeviceId = handler.data().deviceId();
103 checkNotNull(controller, "Netconf controller is null");
104 String reply = null;
xueliangffe73e42016-09-27 10:38:56 +0900105 String[] data = null;
Akihiro Yamanouchi8d3a9d32016-07-12 11:41:44 +0900106 String[] ethId = null;
107
108 if (!mastershipService.isLocalMaster(ncDeviceId)) {
109 log.warn("Not master for {} Use {} to execute command",
110 ncDeviceId,
111 mastershipService.getMasterFor(ncDeviceId));
xueliangffe73e42016-09-27 10:38:56 +0900112 return null;
Akihiro Yamanouchi8d3a9d32016-07-12 11:41:44 +0900113 }
114
xueliangffe73e42016-09-27 10:38:56 +0900115 data = target.split(COLON);
116 if (data.length > TWO) {
117 log.error("Invalid number of parameters {}", target);
118 return null;
Akihiro Yamanouchi8d3a9d32016-07-12 11:41:44 +0900119 }
xueliangffe73e42016-09-27 10:38:56 +0900120
121 ethId = checkIdString(data[FIRST_PART], THREE);
122 if (ethId == null) {
123 log.error("Invalid ETH port identifier {}", data[FIRST_PART]);
124 return null;
125 }
126
127 if (data.length > ONE) {
128 if (!LOOPBACKMODES.contains(data[SECOND_PART])) {
129 log.error("Unsupported parameter: {}", data[SECOND_PART]);
130 return null;
131 }
Akihiro Yamanouchi8d3a9d32016-07-12 11:41:44 +0900132 }
133
134 try {
135 StringBuilder request = new StringBuilder();
xueliangffe73e42016-09-27 10:38:56 +0900136 request.append(ANGLE_LEFT + ONU_ETHPORT_LOOPBACK + SPACE);
137 request.append(VOLT_NE_NAMESPACE + ANGLE_RIGHT + NEW_LINE);
138
139 request.append(buildStartTag(PONLINK_ID, false))
140 .append(ethId[FIRST_PART])
141 .append(buildEndTag(PONLINK_ID))
142 .append(buildStartTag(ONU_ID, false))
143 .append(ethId[SECOND_PART])
144 .append(buildEndTag(ONU_ID))
145 .append(buildStartTag(ETHPORT_ID, false))
146 .append(ethId[THIRD_PART])
147 .append(buildEndTag(ETHPORT_ID));
148 if (data.length > ONE) {
149 request.append(buildStartTag(LOOPBACK_MODE, false))
150 .append(data[SECOND_PART])
151 .append(buildEndTag(LOOPBACK_MODE));
152 }
Akihiro Yamanouchi8d3a9d32016-07-12 11:41:44 +0900153 request.append(buildEndTag(ONU_ETHPORT_LOOPBACK));
154
xueliangffe73e42016-09-27 10:38:56 +0900155 reply = controller
156 .getDevicesMap()
157 .get(ncDeviceId)
158 .getSession()
159 .doWrappedRpc(request.toString());
Akihiro Yamanouchi8d3a9d32016-07-12 11:41:44 +0900160 } catch (IOException e) {
xueliangffe73e42016-09-27 10:38:56 +0900161 log.error("Cannot communicate to device {} exception {}", ncDeviceId, e);
Akihiro Yamanouchi8d3a9d32016-07-12 11:41:44 +0900162 }
163 return reply;
164 }
165
xueliangffe73e42016-09-27 10:38:56 +0900166 /**
167 * Verifies input string for ponlink-id{-onu-id}{-ethport-id}.
168 *
169 * @param target input data in string
170 * @param expected number of IDs expected
171 * @return String array containing IDs; may be null if an error is detected
172 */
173 private String[] checkIdString(String target, int expected) {
174 String[] id = target.split(HYPHEN);
175 int pon;
176 int onu;
177
178 if (id.length < TWO) {
179 log.error("Invalid number of arguments for id: {}", id.length);
180 return null;
181 }
182 if (id.length != expected) {
183 log.error("Invalid number of arguments for id: {}", id.length);
184 return null;
185 }
186 try {
187 pon = Integer.parseInt(id[FIRST_PART]);
188 if (pon <= ZERO) {
189 log.error("Invalid integer for ponlink-id: {}", id[FIRST_PART]);
190 return null;
191 }
192 onu = Integer.parseInt(id[SECOND_PART]);
193 if (onu <= ZERO) {
194 log.error("Invalid integer for onu-id: {}", id[SECOND_PART]);
195 return null;
196 }
197 if (expected > TWO) {
198 int port = Integer.parseInt(id[THIRD_PART]);
199 if (port <= ZERO) {
200 log.error("Invalid integer for port-id: {}", id[THIRD_PART]);
201 return null;
202 }
203 }
204 } catch (NumberFormatException e) {
205 log.error("Non-number input for id: {}", target);
206 return null;
207 }
208 return id;
209 }
210
Akihiro Yamanouchi8d3a9d32016-07-12 11:41:44 +0900211}