blob: a237e81265d42296a14d414b3e56890ffdfd75bb [file] [log] [blame]
Ivan Eroshkin45ff4862019-10-08 14:59:38 +02001/*
Sudeep Desaif0911a72019-12-09 18:31:37 +05302 * Copyright 2019-present Open Networking Foundation
Ivan Eroshkin45ff4862019-10-08 14:59:38 +02003 *
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 * This work was partially supported by EC H2020 project METRO-HAUL (761727).
17 */
18
19package org.onosproject.drivers.odtn;
20
21import com.google.common.collect.Range;
Sudeep Desaif0911a72019-12-09 18:31:37 +053022import org.onosproject.drivers.odtn.openconfig.TerminalDevicePowerConfig;
Ivan Eroshkin45ff4862019-10-08 14:59:38 +020023import org.onosproject.net.DeviceId;
24import org.onosproject.net.PortNumber;
Sudeep Desaif0911a72019-12-09 18:31:37 +053025import org.onosproject.netconf.DatastoreId;
Ivan Eroshkin45ff4862019-10-08 14:59:38 +020026import org.onosproject.netconf.NetconfController;
27import org.onosproject.netconf.NetconfDevice;
28import org.onosproject.netconf.NetconfException;
29import org.onosproject.netconf.NetconfSession;
30import org.slf4j.Logger;
31
Sudeep Desaif0911a72019-12-09 18:31:37 +053032import java.util.HashMap;
33import java.util.Map;
Ivan Eroshkin45ff4862019-10-08 14:59:38 +020034import java.util.Optional;
35
Ivan Eroshkin45ff4862019-10-08 14:59:38 +020036import static org.slf4j.LoggerFactory.getLogger;
37
38/**
39 * Driver Implementation of the PowerConfig for OpenConfig terminal devices.
40 * Currently works only with PSI-2T.
41 * If you want to make it work with ROADM, you need to implement this interface again.
42 *
43 */
44public class NokiaTerminalDevicePowerConfig<T>
Sudeep Desaif0911a72019-12-09 18:31:37 +053045 extends TerminalDevicePowerConfig<T> {
Ivan Eroshkin45ff4862019-10-08 14:59:38 +020046
47 private static final String OPTICAL_CHANNEL = "OCH";
48
49 private static final Logger log = getLogger(NokiaTerminalDevicePowerConfig.class);
50
Ivan Eroshkin45ff4862019-10-08 14:59:38 +020051 //The username and password are different from the username and password in the netconf-cfg file
52 private static final String USER_NAME = "admin";
53 private static final String PASSWORD = "admin";
54
Ivan Eroshkin45ff4862019-10-08 14:59:38 +020055 /**
56 * Login to the device by providing the correct user and password in order to configure the device
57 * Returns the NetconfSession with the device for which the method was called.
58 *
59 * @param deviceId device identifier
60 * @param userName username to access the device
61 * @param passwd password to access the device
62 * @return The netconf session or null
63 */
Sudeep Desaif0911a72019-12-09 18:31:37 +053064 @Override
65 public NetconfSession getNetconfSession(DeviceId deviceId, String userName, String passwd) {
66 userName = USER_NAME;
67 passwd = PASSWORD;
Ivan Eroshkin45ff4862019-10-08 14:59:38 +020068 NetconfController nc = handler().get(NetconfController.class);
69 NetconfDevice ndev = nc.getDevicesMap().get(deviceId);
70 if (ndev == null) {
71 log.debug("NetConf device " + deviceId + " is not found, returning null session");
72 return null;
73 }
74 NetconfSession ns = ndev.getSession();
75 if (ns == null) {
76 log.error("discoverPorts called with null session for \n {}", deviceId);
77 return null;
78 }
79
80 try {
81 String reply = ns.requestSync(buildLoginRpc(userName, passwd));
82 if (reply.contains("<ok/>")) {
83 return ns;
84 } else {
85 log.debug("Reply contains this: \n {}", reply);
86 return null;
87 }
88 } catch (NetconfException e) {
89 log.error("Can NOT login to the device", e);
90 }
91 return ns;
92 }
93
94 /**
95 * Construct a rpc login message.
96 *
97 * @param userName username to access the device
98 * @param passwd password to access the device
99 * @return RPC message
100 */
101 private String buildLoginRpc(String userName, String passwd) {
102 StringBuilder rpc = new StringBuilder(RPC_TAG_NETCONF_BASE);
103 rpc.append("<login xmlns=\"http://nokia.com/yang/nokia-security\">");
104 rpc.append("<username>");
105 rpc.append(userName);
106 rpc.append("</username>");
107 rpc.append("<password>");
108 rpc.append(passwd);
109 rpc.append("</password>");
110 rpc.append("</login>");
111 rpc.append(RPC_CLOSE_TAG);
112 return rpc.toString();
113 }
114
115 //crude way of removing rpc-reply envelope (copy from netconf session)
116 private String getDataOfRpcReply(String rpcReply) {
117 String data = null;
118 int begin = rpcReply.indexOf("<data>");
119 int end = rpcReply.lastIndexOf("</data>");
120 if (begin != -1 && end != -1) {
121 data = (String) rpcReply.subSequence(begin, end + "</data>".length());
122 } else {
123 data = rpcReply;
124 }
125 return data;
126 }
127
128 /**
Ivan Eroshkin45ff4862019-10-08 14:59:38 +0200129 *
Sudeep Desaif0911a72019-12-09 18:31:37 +0530130 * @param param the config parameter.
131 * @return array of string
Ivan Eroshkin45ff4862019-10-08 14:59:38 +0200132 */
Sudeep Desaif0911a72019-12-09 18:31:37 +0530133 @Override
134 public Map<String, String> buildRpcString(String param) {
135 Map<String, String> rpcMap = new HashMap<String, String>();
136 switch (param) {
137 case TARGET_POWER:
138 rpcMap.put("TARGET_OUTPUT_PATH", "components/component/oc-opt-term:optical-channel/oc-opt-term:config");
139 rpcMap.put("TARGET_OUTPUT_LEAF", "oc-opt-term:target-output-power");
140 case CURRENT_POWER:
141 rpcMap.put("CURRENT_POWER_PATH", "components.component." +
142 "oc-opt-term:optical-channel." +
143 "oc-opt-term:state." +
144 "oc-opt-term:output-power");
145 rpcMap.put("CURRENT_POWER_ROUTE", "<oc-opt-term:output-power>" +
146 "<oc-opt-term:instant/>" +
147 "</oc-opt-term:output-power>");
148 rpcMap.put("CURRENT_POWER_LEAF", "oc-opt-term:instant");
149 default:
150 rpcMap.put("CURRENT_INPUT_POWER_PATH", "components.component." +
151 "oc-opt-term:optical-channel." +
152 "oc-opt-term:state." +
153 "oc-opt-term:input-power");
154 rpcMap.put("CURRENT_INPUT_POWER_ROUTE", "<oc-opt-term:input-power>" +
155 "<oc-opt-term:instant/>" +
156 "</oc-opt-term:input-power>");
157 rpcMap.put("CURRENT_INPUT_POWER_LEAF", "oc-opt-term:instant");
158 }
159 return rpcMap;
Ivan Eroshkin45ff4862019-10-08 14:59:38 +0200160 }
161
162 /**
Sudeep Desaif0911a72019-12-09 18:31:37 +0530163 * Construct a rpc target power message.
164 *
165 * @param filter to build rpc
166 * @return RPC payload
Ivan Eroshkin45ff4862019-10-08 14:59:38 +0200167 */
Sudeep Desaif0911a72019-12-09 18:31:37 +0530168 @Override
169 public StringBuilder getTargetPowerRequestRpc(String filter) {
170 StringBuilder rpc = new StringBuilder();
171 rpc.append("<get>")
172 .append("<filter type='subtree'>")
173 .append(filter)
174 .append("</filter>")
175 .append("</get>");
176 return rpc;
177 }
Ivan Eroshkin45ff4862019-10-08 14:59:38 +0200178
Sudeep Desaif0911a72019-12-09 18:31:37 +0530179 /**
180 * Construct a rpc target power message.
181 *
182 * @return RPC payload
183 */
184 @Override
185 public DatastoreId getDataStoreId() {
186 return DatastoreId.RUNNING;
187 }
188
189 /**
190 * Construct a rpc target power message.
191 *
192 * @param name
193 * @param underState to build rpc for setting configuration
194 * @return RPC payload
195 */
196 @Override
197 public StringBuilder getOpticalChannelStateRequestRpc(String name, String underState) {
198 StringBuilder rpc = new StringBuilder();
199 rpc.append("<name>").append(name).append("</name>")
200 .append("<oc-opt-term:optical-channel " +
201 "xmlns:oc-opt-term=\"http://openconfig.net/yang/terminal-device\">")
202 .append("<oc-opt-term:state>")
203 .append(underState)
204 .append("</oc-opt-term:state>")
205 .append("</oc-opt-term:optical-channel>")
206 .append("</component></components></filter></get>");
207 return rpc;
208 }
209
210 /**
211 * Getting target value of output power.
212 * @param port port
213 * @param component the component
214 * @return target output power range
215 */
216 @Override
217 public Optional<Range<Double>> getTargetPowerRange(PortNumber port, Object component) {
218 double targetMin = -20;
219 double targetMax = 5;
220 return Optional.of(Range.open(targetMin, targetMax));
221 }
222
223 @Override
224 public Optional<Range<Double>> getInputPowerRange(PortNumber port, Object component) {
225 double targetMin = -20;
226 double targetMax = 5;
227 return Optional.of(Range.open(targetMin, targetMax));
228 }
229
230 /**
231 * Construct a rpc target power message.
232 *
233 * @param name for optical channel name
234 * @param power to build rpc for setting configuration
235 * @return RPC payload
236 */
237 @Override
238 public StringBuilder parsePortRequestRpc(Double power, String name) {
239 if (name != null) {
240 StringBuilder rpc = new StringBuilder();
241 rpc.append("<component>").append("<name>").append(name).append("</name>");
242 if (power != null) {
243 // This is an edit-config operation.
244 rpc.append("<oc-opt-term:optical-channel " +
245 "xmlns:oc-opt-term=\"http://openconfig.net/yang/terminal-device\">")
246 .append("<oc-opt-term:config>")
247 .append("<oc-opt-term:target-output-power>")
248 .append(power)
249 .append("</oc-opt-term:target-output-power>")
250 .append("</oc-opt-term:config>")
251 .append("</oc-opt-term:optical-channel>");
252 }
253 return rpc;
Ivan Eroshkin45ff4862019-10-08 14:59:38 +0200254 }
255 return null;
256 }
257
Ivan Eroshkin45ff4862019-10-08 14:59:38 +0200258}