blob: ca7636e78a35830fd440399a80c82a869790a93f [file] [log] [blame]
weibit38c42ed2014-10-09 19:03:54 -07001package org.onlab.onos.optical.cfg;
2
3import static org.onlab.onos.net.DeviceId.deviceId;
4
5import java.io.File;
6import java.io.IOException;
7import java.util.ArrayList;
8import java.util.Iterator;
9import java.util.List;
10import java.util.Map;
11import java.util.Set;
12
13import org.apache.felix.scr.annotations.Activate;
14import org.apache.felix.scr.annotations.Component;
15import org.apache.felix.scr.annotations.Deactivate;
16import org.apache.felix.scr.annotations.Reference;
17import org.apache.felix.scr.annotations.ReferenceCardinality;
18import org.codehaus.jackson.JsonNode;
19import org.codehaus.jackson.JsonParseException;
20import org.codehaus.jackson.annotate.JsonIgnoreProperties;
21import org.codehaus.jackson.map.JsonMappingException;
22import org.codehaus.jackson.map.ObjectMapper;
23import org.onlab.onos.net.ConnectPoint;
24import org.onlab.onos.net.DefaultAnnotations;
25import org.onlab.onos.net.Device;
26import org.onlab.onos.net.DeviceId;
27import org.onlab.onos.net.Link;
28import org.onlab.onos.net.MastershipRole;
29import org.onlab.onos.net.PortNumber;
30import org.onlab.onos.net.device.DefaultDeviceDescription;
31import org.onlab.onos.net.device.DeviceDescription;
32import org.onlab.onos.net.device.DeviceProvider;
33import org.onlab.onos.net.device.DeviceProviderRegistry;
34import org.onlab.onos.net.device.DeviceProviderService;
35import org.onlab.onos.net.link.DefaultLinkDescription;
36import org.onlab.onos.net.link.LinkProvider;
37import org.onlab.onos.net.link.LinkProviderRegistry;
38import org.onlab.onos.net.link.LinkProviderService;
39import org.onlab.onos.net.provider.AbstractProvider;
40import org.onlab.onos.net.provider.ProviderId;
41import org.onlab.packet.ChassisId;
42import org.slf4j.Logger;
43import org.slf4j.LoggerFactory;
44
45/**
46 * OpticalConfigProvider emulates the SB network provider for optical switches,
47 * optical links and any other state that needs to be configured for correct network
48 * operations.
49 *
50 */
51
52@JsonIgnoreProperties(ignoreUnknown = true)
53@Component(immediate = true)
54public class OpticalConfigProvider extends AbstractProvider implements DeviceProvider, LinkProvider {
55
56 protected static final Logger log = LoggerFactory
57 .getLogger(OpticalConfigProvider.class);
58
59 // TODO: fix hard coded file path later.
60 private static final String DEFAULT_CONFIG_FILE =
weibit50eb95b2014-10-25 21:47:54 -070061 "/opt/onos/config/demo-3-roadm-2-ps.json";
weibit38c42ed2014-10-09 19:03:54 -070062 private String configFileName = DEFAULT_CONFIG_FILE;
63
64 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
65 protected LinkProviderRegistry linkProviderRegistry;
66
67 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
68 protected DeviceProviderRegistry deviceProviderRegistry;
69
70 private static final String OPTICAL_ANNOTATION = "optical.";
71
72 private LinkProviderService linkProviderService;
73 private DeviceProviderService deviceProviderService;
74
75 private static final List<Roadm> RAW_ROADMS = new ArrayList<>();
76 private static final List<WdmLink> RAW_WDMLINKS = new ArrayList<>();
77 private static final List<PktOptLink> RAW_PKTOPTLINKS = new ArrayList<>();
78
79 private static final String ROADM = "Roadm";
80 private static final String WDM_LINK = "wdmLink";
81 private static final String PKT_OPT_LINK = "pktOptLink";
82
83 protected OpticalNetworkConfig opticalNetworkConfig;
84
85 public OpticalConfigProvider() {
Praseed Balakrishnan69d95be2014-10-22 13:55:05 -070086 super(new ProviderId("optical", "org.onlab.onos.provider" +
87 ".opticalConfig"));
weibit38c42ed2014-10-09 19:03:54 -070088 }
89
90 @Activate
91 protected void activate() {
92 linkProviderService = linkProviderRegistry.register(this);
93 deviceProviderService = deviceProviderRegistry.register(this);
94 log.info("Starting optical network configuration process...");
95 log.info("Optical config file set to {}", configFileName);
96
97 loadOpticalConfig();
98 parseOpticalConfig();
99 publishOpticalConfig();
100 }
101
102 @Deactivate
103 protected void deactivate() {
104 linkProviderRegistry.unregister(this);
105 linkProviderService = null;
106 deviceProviderRegistry.unregister(this);
107 deviceProviderService = null;
108 RAW_ROADMS.clear();
109 RAW_WDMLINKS.clear();
110 RAW_PKTOPTLINKS.clear();
111 log.info("Stopped");
112 }
113
114 private void loadOpticalConfig() {
115 ObjectMapper mapper = new ObjectMapper();
116 opticalNetworkConfig = new OpticalNetworkConfig();
117 try {
118 opticalNetworkConfig = mapper.readValue(new File(configFileName), OpticalNetworkConfig.class);
119 } catch (JsonParseException e) {
120 String err = String.format("JsonParseException while loading network "
121 + "config from file: %s: %s", configFileName, e.getMessage());
122 log.error(err, e);
123 } catch (JsonMappingException e) {
124 String err = String.format(
125 "JsonMappingException while loading network config "
126 + "from file: %s: %s", configFileName, e.getMessage());
127 log.error(err, e);
128 } catch (IOException e) {
129 String err = String.format("IOException while loading network config "
130 + "from file: %s %s", configFileName, e.getMessage());
131 log.error(err, e);
132 }
133 }
134
135 private void parseOpticalConfig() {
136 List<OpticalSwitchDescription> swList = opticalNetworkConfig.getOpticalSwitches();
137 List<OpticalLinkDescription> lkList = opticalNetworkConfig.getOpticalLinks();
138
139 for (OpticalSwitchDescription sw : swList) {
140 String swtype = sw.getType();
141 boolean allow = sw.isAllowed();
142 if (swtype.equals(ROADM) && allow) {
143 int regNum = 0;
144 Set<Map.Entry<String, JsonNode>> m = sw.params.entrySet();
145 for (Map.Entry<String, JsonNode> e : m) {
146 String key = e.getKey();
147 JsonNode j = e.getValue();
148 if (key.equals("numRegen")) {
149 regNum = j.asInt();
150 }
151 }
152
153 Roadm newRoadm = new Roadm();
154 newRoadm.setName(sw.name);
155 newRoadm.setNodeId(sw.nodeDpid);
156 newRoadm.setLongtitude(sw.longitude);
157 newRoadm.setLatitude(sw.latitude);
158 newRoadm.setRegenNum(regNum);
159
160 RAW_ROADMS.add(newRoadm);
161 log.info(newRoadm.toString());
162 }
163 }
164
165 for (OpticalLinkDescription lk : lkList) {
166 String lktype = lk.getType();
167 switch (lktype) {
168 case WDM_LINK:
169 WdmLink newWdmLink = new WdmLink();
170 newWdmLink.setSrcNodeId(lk.getNodeDpid1());
171 newWdmLink.setSnkNodeId(lk.getNodeDpid2());
172 newWdmLink.setAdminWeight(1000); // default weight for each WDM link.
173 Set<Map.Entry<String, JsonNode>> m = lk.params.entrySet();
174 for (Map.Entry<String, JsonNode> e : m) {
175 String key = e.getKey();
176 JsonNode j = e.getValue();
177 if (key.equals("nodeName1")) {
178 newWdmLink.setSrcNodeName(j.asText());
179 } else if (key.equals("nodeName2")) {
180 newWdmLink.setSnkNodeName(j.asText());
181 } else if (key.equals("port1")) {
182 newWdmLink.setSrcPort(j.asInt());
183 } else if (key.equals("port2")) {
184 newWdmLink.setSnkPort(j.asInt());
185 } else if (key.equals("distKms")) {
186 newWdmLink.setDistance(j.asDouble());
187 } else if (key.equals("numWaves")) {
188 newWdmLink.setWavelengthNumber(j.asInt());
189 } else {
190 log.error("error found");
191 // TODO add exception processing;
192 }
193 }
194 RAW_WDMLINKS.add(newWdmLink);
195 log.info(newWdmLink.toString());
196
197 break;
198
199 case PKT_OPT_LINK:
200 PktOptLink newPktOptLink = new PktOptLink();
201 newPktOptLink.setSrcNodeId(lk.getNodeDpid1());
202 newPktOptLink.setSnkNodeId(lk.getNodeDpid2());
203 newPktOptLink.setAdminWeight(10); // default weight for each packet-optical link.
204 Set<Map.Entry<String, JsonNode>> ptm = lk.params.entrySet();
205 for (Map.Entry<String, JsonNode> e : ptm) {
206 String key = e.getKey();
207 JsonNode j = e.getValue();
208 if (key.equals("nodeName1")) {
209 newPktOptLink.setSrcNodeName(j.asText());
210 } else if (key.equals("nodeName2")) {
211 newPktOptLink.setSnkNodeName(j.asText());
212 } else if (key.equals("port1")) {
213 newPktOptLink.setSrcPort(j.asInt());
214 } else if (key.equals("port2")) {
215 newPktOptLink.setSnkPort(j.asInt());
216 } else if (key.equals("bandWidth")) {
217 newPktOptLink.setBandwdith(j.asDouble());
218 } else {
219 log.error("error found");
220 // TODO add exception processing;
221 }
222 }
223
224 RAW_PKTOPTLINKS.add(newPktOptLink);
225 log.info(newPktOptLink.toString());
226 break;
227 default:
228 }
229 }
230 }
231
232 private void publishOpticalConfig() {
233 if (deviceProviderService == null || linkProviderService == null) {
234 return;
235 }
236
237 // Discover the optical ROADM objects
238 Iterator<Roadm> iterWdmNode = RAW_ROADMS.iterator();
239 while (iterWdmNode.hasNext()) {
240 Roadm value = iterWdmNode.next();
241 DeviceId did = deviceId("of:" + value.getNodeId().replace(":", ""));
Praseed Balakrishnan69d95be2014-10-22 13:55:05 -0700242 ChassisId cid = new ChassisId();
weibit38c42ed2014-10-09 19:03:54 -0700243 DefaultAnnotations extendedAttributes = DefaultAnnotations.builder()
244 .set(OPTICAL_ANNOTATION + "switchType", "ROADM")
245 .set(OPTICAL_ANNOTATION + "switchName", value.getName())
246 .set(OPTICAL_ANNOTATION + "latitude", Double.toString(value.getLatitude()))
247 .set(OPTICAL_ANNOTATION + "longtitude", Double.toString(value.getLongtitude()))
248 .set(OPTICAL_ANNOTATION + "regNum", Integer.toString(value.getRegenNum()))
249 .build();
250
251 DeviceDescription description =
252 new DefaultDeviceDescription(did.uri(),
253 Device.Type.SWITCH,
254 "",
255 "",
256 "",
257 "",
258 cid,
259 extendedAttributes);
260 deviceProviderService.deviceConnected(did, description);
261 }
262
263 // Discover the optical WDM link objects
264 Iterator<WdmLink> iterWdmlink = RAW_WDMLINKS.iterator();
265 while (iterWdmlink.hasNext()) {
266 WdmLink value = iterWdmlink.next();
267
268 DeviceId srcNodeId = deviceId("of:" + value.getSrcNodeId().replace(":", ""));
269 DeviceId snkNodeId = deviceId("of:" + value.getSnkNodeId().replace(":", ""));
270
271 PortNumber srcPort = PortNumber.portNumber(value.getSrcPort());
272 PortNumber snkPort = PortNumber.portNumber(value.getSnkPort());
273
274 ConnectPoint srcPoint = new ConnectPoint(srcNodeId, srcPort);
275 ConnectPoint snkPoint = new ConnectPoint(snkNodeId, snkPort);
276
277 DefaultAnnotations extendedAttributes = DefaultAnnotations.builder()
278 .set(OPTICAL_ANNOTATION + "linkType", "WDM")
279 .set(OPTICAL_ANNOTATION + "distance", Double.toString(value.getDistance()))
280 .set(OPTICAL_ANNOTATION + "cost", Double.toString(value.getDistance()))
281 .set(OPTICAL_ANNOTATION + "adminWeight", Double.toString(value.getAdminWeight()))
282 .set(OPTICAL_ANNOTATION + "wavelengthNum", Integer.toString(value.getWavelengthNumber()))
283 .build();
284
285 DefaultLinkDescription linkDescription =
286 new DefaultLinkDescription(srcPoint,
287 snkPoint,
Thomas Vachuska0e752bd2014-10-22 22:33:41 -0700288 Link.Type.OPTICAL,
weibit38c42ed2014-10-09 19:03:54 -0700289 extendedAttributes);
290
291 linkProviderService.linkDetected(linkDescription);
292 log.info(String.format("WDM link: %s : %s",
293 linkDescription.src().toString(), linkDescription.dst().toString()));
weibitaca14602014-10-24 10:26:26 -0700294
295
296 DefaultLinkDescription linkDescriptionReverse =
297 new DefaultLinkDescription(snkPoint,
298 srcPoint,
299 Link.Type.OPTICAL,
300 extendedAttributes);
301
302 linkProviderService.linkDetected(linkDescriptionReverse);
303 log.info(String.format("WDM link: %s : %s",
304 linkDescriptionReverse.src().toString(), linkDescriptionReverse.dst().toString()));
weibit38c42ed2014-10-09 19:03:54 -0700305 }
306
307 // Discover the packet optical link objects
308 Iterator<PktOptLink> iterPktOptlink = RAW_PKTOPTLINKS.iterator();
309 while (iterPktOptlink.hasNext()) {
310 PktOptLink value = iterPktOptlink.next();
311 DeviceId srcNodeId = deviceId("of:" + value.getSrcNodeId().replace(":", ""));
312 DeviceId snkNodeId = deviceId("of:" + value.getSnkNodeId().replace(":", ""));
313
314 PortNumber srcPort = PortNumber.portNumber(value.getSrcPort());
315 PortNumber snkPort = PortNumber.portNumber(value.getSnkPort());
316
317 ConnectPoint srcPoint = new ConnectPoint(srcNodeId, srcPort);
318 ConnectPoint snkPoint = new ConnectPoint(snkNodeId, snkPort);
319
320 DefaultAnnotations extendedAttributes = DefaultAnnotations.builder()
321 .set(OPTICAL_ANNOTATION + "linkType", "PktOptLink")
322 .set(OPTICAL_ANNOTATION + "bandwidth", Double.toString(value.getBandwidth()))
323 .set(OPTICAL_ANNOTATION + "cost", Double.toString(value.getBandwidth()))
324 .set(OPTICAL_ANNOTATION + "adminWeight", Double.toString(value.getAdminWeight()))
325 .build();
326
327 DefaultLinkDescription linkDescription =
328 new DefaultLinkDescription(srcPoint,
329 snkPoint,
Thomas Vachuska0e752bd2014-10-22 22:33:41 -0700330 Link.Type.OPTICAL,
weibit38c42ed2014-10-09 19:03:54 -0700331 extendedAttributes);
332
333 linkProviderService.linkDetected(linkDescription);
334 log.info(String.format("Packet-optical link: %s : %s",
335 linkDescription.src().toString(), linkDescription.dst().toString()));
weibitaca14602014-10-24 10:26:26 -0700336
337 DefaultLinkDescription linkDescriptionReverse =
338 new DefaultLinkDescription(snkPoint,
339 srcPoint,
340 Link.Type.OPTICAL,
341 extendedAttributes);
342
343 linkProviderService.linkDetected(linkDescriptionReverse);
344 log.info(String.format("Packet-optical link: %s : %s",
345 linkDescriptionReverse.src().toString(), linkDescriptionReverse.dst().toString()));
weibit38c42ed2014-10-09 19:03:54 -0700346 }
347
348 }
349
350 @Override
351 public void triggerProbe(Device device) {
352 // TODO We may want to consider re-reading config files and publishing them based on this event.
353 }
354
355 @Override
356 public void roleChanged(Device device, MastershipRole newRole) {
357 // TODO Auto-generated method stub.
358 }
359
360}