blob: cfdeb1f431c1ef32d3f81508d29e3b2f16e1066d [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 =
61 "/opt/onos/config/demo-3-roadm-2-ps.json";
62 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() {
alshabib2374fc92014-10-22 11:03:23 -070086 super(new ProviderId("optical", "org.onlab.onos.provider.opticalConfig", true));
weibit38c42ed2014-10-09 19:03:54 -070087 }
88
89 @Activate
90 protected void activate() {
91 linkProviderService = linkProviderRegistry.register(this);
92 deviceProviderService = deviceProviderRegistry.register(this);
93 log.info("Starting optical network configuration process...");
94 log.info("Optical config file set to {}", configFileName);
95
96 loadOpticalConfig();
97 parseOpticalConfig();
98 publishOpticalConfig();
99 }
100
101 @Deactivate
102 protected void deactivate() {
103 linkProviderRegistry.unregister(this);
104 linkProviderService = null;
105 deviceProviderRegistry.unregister(this);
106 deviceProviderService = null;
107 RAW_ROADMS.clear();
108 RAW_WDMLINKS.clear();
109 RAW_PKTOPTLINKS.clear();
110 log.info("Stopped");
111 }
112
113 private void loadOpticalConfig() {
114 ObjectMapper mapper = new ObjectMapper();
115 opticalNetworkConfig = new OpticalNetworkConfig();
116 try {
117 opticalNetworkConfig = mapper.readValue(new File(configFileName), OpticalNetworkConfig.class);
118 } catch (JsonParseException e) {
119 String err = String.format("JsonParseException while loading network "
120 + "config from file: %s: %s", configFileName, e.getMessage());
121 log.error(err, e);
122 } catch (JsonMappingException e) {
123 String err = String.format(
124 "JsonMappingException while loading network config "
125 + "from file: %s: %s", configFileName, e.getMessage());
126 log.error(err, e);
127 } catch (IOException e) {
128 String err = String.format("IOException while loading network config "
129 + "from file: %s %s", configFileName, e.getMessage());
130 log.error(err, e);
131 }
132 }
133
134 private void parseOpticalConfig() {
135 List<OpticalSwitchDescription> swList = opticalNetworkConfig.getOpticalSwitches();
136 List<OpticalLinkDescription> lkList = opticalNetworkConfig.getOpticalLinks();
137
138 for (OpticalSwitchDescription sw : swList) {
139 String swtype = sw.getType();
140 boolean allow = sw.isAllowed();
141 if (swtype.equals(ROADM) && allow) {
142 int regNum = 0;
143 Set<Map.Entry<String, JsonNode>> m = sw.params.entrySet();
144 for (Map.Entry<String, JsonNode> e : m) {
145 String key = e.getKey();
146 JsonNode j = e.getValue();
147 if (key.equals("numRegen")) {
148 regNum = j.asInt();
149 }
150 }
151
152 Roadm newRoadm = new Roadm();
153 newRoadm.setName(sw.name);
154 newRoadm.setNodeId(sw.nodeDpid);
155 newRoadm.setLongtitude(sw.longitude);
156 newRoadm.setLatitude(sw.latitude);
157 newRoadm.setRegenNum(regNum);
158
159 RAW_ROADMS.add(newRoadm);
160 log.info(newRoadm.toString());
161 }
162 }
163
164 for (OpticalLinkDescription lk : lkList) {
165 String lktype = lk.getType();
166 switch (lktype) {
167 case WDM_LINK:
168 WdmLink newWdmLink = new WdmLink();
169 newWdmLink.setSrcNodeId(lk.getNodeDpid1());
170 newWdmLink.setSnkNodeId(lk.getNodeDpid2());
171 newWdmLink.setAdminWeight(1000); // default weight for each WDM link.
172 Set<Map.Entry<String, JsonNode>> m = lk.params.entrySet();
173 for (Map.Entry<String, JsonNode> e : m) {
174 String key = e.getKey();
175 JsonNode j = e.getValue();
176 if (key.equals("nodeName1")) {
177 newWdmLink.setSrcNodeName(j.asText());
178 } else if (key.equals("nodeName2")) {
179 newWdmLink.setSnkNodeName(j.asText());
180 } else if (key.equals("port1")) {
181 newWdmLink.setSrcPort(j.asInt());
182 } else if (key.equals("port2")) {
183 newWdmLink.setSnkPort(j.asInt());
184 } else if (key.equals("distKms")) {
185 newWdmLink.setDistance(j.asDouble());
186 } else if (key.equals("numWaves")) {
187 newWdmLink.setWavelengthNumber(j.asInt());
188 } else {
189 log.error("error found");
190 // TODO add exception processing;
191 }
192 }
193 RAW_WDMLINKS.add(newWdmLink);
194 log.info(newWdmLink.toString());
195
196 break;
197
198 case PKT_OPT_LINK:
199 PktOptLink newPktOptLink = new PktOptLink();
200 newPktOptLink.setSrcNodeId(lk.getNodeDpid1());
201 newPktOptLink.setSnkNodeId(lk.getNodeDpid2());
202 newPktOptLink.setAdminWeight(10); // default weight for each packet-optical link.
203 Set<Map.Entry<String, JsonNode>> ptm = lk.params.entrySet();
204 for (Map.Entry<String, JsonNode> e : ptm) {
205 String key = e.getKey();
206 JsonNode j = e.getValue();
207 if (key.equals("nodeName1")) {
208 newPktOptLink.setSrcNodeName(j.asText());
209 } else if (key.equals("nodeName2")) {
210 newPktOptLink.setSnkNodeName(j.asText());
211 } else if (key.equals("port1")) {
212 newPktOptLink.setSrcPort(j.asInt());
213 } else if (key.equals("port2")) {
214 newPktOptLink.setSnkPort(j.asInt());
215 } else if (key.equals("bandWidth")) {
216 newPktOptLink.setBandwdith(j.asDouble());
217 } else {
218 log.error("error found");
219 // TODO add exception processing;
220 }
221 }
222
223 RAW_PKTOPTLINKS.add(newPktOptLink);
224 log.info(newPktOptLink.toString());
225 break;
226 default:
227 }
228 }
229 }
230
231 private void publishOpticalConfig() {
232 if (deviceProviderService == null || linkProviderService == null) {
233 return;
234 }
235
236 // Discover the optical ROADM objects
237 Iterator<Roadm> iterWdmNode = RAW_ROADMS.iterator();
238 while (iterWdmNode.hasNext()) {
239 Roadm value = iterWdmNode.next();
240 DeviceId did = deviceId("of:" + value.getNodeId().replace(":", ""));
alshabib2374fc92014-10-22 11:03:23 -0700241 ChassisId cid = new ChassisId();
weibit38c42ed2014-10-09 19:03:54 -0700242 DefaultAnnotations extendedAttributes = DefaultAnnotations.builder()
243 .set(OPTICAL_ANNOTATION + "switchType", "ROADM")
244 .set(OPTICAL_ANNOTATION + "switchName", value.getName())
245 .set(OPTICAL_ANNOTATION + "latitude", Double.toString(value.getLatitude()))
246 .set(OPTICAL_ANNOTATION + "longtitude", Double.toString(value.getLongtitude()))
247 .set(OPTICAL_ANNOTATION + "regNum", Integer.toString(value.getRegenNum()))
248 .build();
249
250 DeviceDescription description =
251 new DefaultDeviceDescription(did.uri(),
252 Device.Type.SWITCH,
253 "",
254 "",
255 "",
256 "",
257 cid,
258 extendedAttributes);
259 deviceProviderService.deviceConnected(did, description);
260 }
261
262 // Discover the optical WDM link objects
263 Iterator<WdmLink> iterWdmlink = RAW_WDMLINKS.iterator();
264 while (iterWdmlink.hasNext()) {
265 WdmLink value = iterWdmlink.next();
266
267 DeviceId srcNodeId = deviceId("of:" + value.getSrcNodeId().replace(":", ""));
268 DeviceId snkNodeId = deviceId("of:" + value.getSnkNodeId().replace(":", ""));
269
270 PortNumber srcPort = PortNumber.portNumber(value.getSrcPort());
271 PortNumber snkPort = PortNumber.portNumber(value.getSnkPort());
272
273 ConnectPoint srcPoint = new ConnectPoint(srcNodeId, srcPort);
274 ConnectPoint snkPoint = new ConnectPoint(snkNodeId, snkPort);
275
276 DefaultAnnotations extendedAttributes = DefaultAnnotations.builder()
277 .set(OPTICAL_ANNOTATION + "linkType", "WDM")
278 .set(OPTICAL_ANNOTATION + "distance", Double.toString(value.getDistance()))
279 .set(OPTICAL_ANNOTATION + "cost", Double.toString(value.getDistance()))
280 .set(OPTICAL_ANNOTATION + "adminWeight", Double.toString(value.getAdminWeight()))
281 .set(OPTICAL_ANNOTATION + "wavelengthNum", Integer.toString(value.getWavelengthNumber()))
282 .build();
283
284 DefaultLinkDescription linkDescription =
285 new DefaultLinkDescription(srcPoint,
286 snkPoint,
Thomas Vachuska0e752bd2014-10-22 22:33:41 -0700287 Link.Type.OPTICAL,
weibit38c42ed2014-10-09 19:03:54 -0700288 extendedAttributes);
289
290 linkProviderService.linkDetected(linkDescription);
291 log.info(String.format("WDM link: %s : %s",
292 linkDescription.src().toString(), linkDescription.dst().toString()));
293 }
294
295 // Discover the packet optical link objects
296 Iterator<PktOptLink> iterPktOptlink = RAW_PKTOPTLINKS.iterator();
297 while (iterPktOptlink.hasNext()) {
298 PktOptLink value = iterPktOptlink.next();
299 DeviceId srcNodeId = deviceId("of:" + value.getSrcNodeId().replace(":", ""));
300 DeviceId snkNodeId = deviceId("of:" + value.getSnkNodeId().replace(":", ""));
301
302 PortNumber srcPort = PortNumber.portNumber(value.getSrcPort());
303 PortNumber snkPort = PortNumber.portNumber(value.getSnkPort());
304
305 ConnectPoint srcPoint = new ConnectPoint(srcNodeId, srcPort);
306 ConnectPoint snkPoint = new ConnectPoint(snkNodeId, snkPort);
307
308 DefaultAnnotations extendedAttributes = DefaultAnnotations.builder()
309 .set(OPTICAL_ANNOTATION + "linkType", "PktOptLink")
310 .set(OPTICAL_ANNOTATION + "bandwidth", Double.toString(value.getBandwidth()))
311 .set(OPTICAL_ANNOTATION + "cost", Double.toString(value.getBandwidth()))
312 .set(OPTICAL_ANNOTATION + "adminWeight", Double.toString(value.getAdminWeight()))
313 .build();
314
315 DefaultLinkDescription linkDescription =
316 new DefaultLinkDescription(srcPoint,
317 snkPoint,
Thomas Vachuska0e752bd2014-10-22 22:33:41 -0700318 Link.Type.OPTICAL,
weibit38c42ed2014-10-09 19:03:54 -0700319 extendedAttributes);
320
321 linkProviderService.linkDetected(linkDescription);
322 log.info(String.format("Packet-optical link: %s : %s",
323 linkDescription.src().toString(), linkDescription.dst().toString()));
324 }
325
326 }
327
328 @Override
329 public void triggerProbe(Device device) {
330 // TODO We may want to consider re-reading config files and publishing them based on this event.
331 }
332
333 @Override
334 public void roleChanged(Device device, MastershipRole newRole) {
335 // TODO Auto-generated method stub.
336 }
337
338}