blob: 2ed71e290c7a1c61d2ef645010311f736ec26b99 [file] [log] [blame]
Carmelo Cascone0831efb2016-05-31 14:50:19 -07001/*
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.bmv2;
18
19import com.google.common.collect.ImmutableList;
20import com.google.common.collect.Lists;
21import org.onlab.packet.ChassisId;
22import org.onosproject.bmv2.api.runtime.Bmv2DeviceAgent;
23import org.onosproject.bmv2.api.runtime.Bmv2RuntimeException;
24import org.onosproject.bmv2.api.service.Bmv2Controller;
25import org.onosproject.net.AnnotationKeys;
26import org.onosproject.net.DefaultAnnotations;
27import org.onosproject.net.DeviceId;
28import org.onosproject.net.PortNumber;
29import org.onosproject.net.device.DefaultDeviceDescription;
30import org.onosproject.net.device.DefaultPortDescription;
31import org.onosproject.net.device.DeviceDescription;
32import org.onosproject.net.device.DeviceDescriptionDiscovery;
33import org.onosproject.net.device.PortDescription;
34import org.onosproject.net.driver.AbstractHandlerBehaviour;
35import org.slf4j.Logger;
36import org.slf4j.LoggerFactory;
37
38import java.math.BigInteger;
39import java.util.List;
40
41import static org.onosproject.bmv2.api.runtime.Bmv2Device.*;
42import static org.onosproject.net.Device.Type.SWITCH;
43
44/**
45 * Implementation of the device description discovery behaviour for BMv2.
46 */
47public class Bmv2DeviceDescriptionDiscovery extends AbstractHandlerBehaviour implements DeviceDescriptionDiscovery {
48
49 private static final String JSON_CONFIG_MD5 = "bmv2JsonConfigMd5";
50 private static final String PROCESS_INSTANCE_ID = "bmv2ProcessInstanceId";
51
52 private final Logger log = LoggerFactory.getLogger(this.getClass());
53
54 private Bmv2Controller controller;
55
56 private boolean init() {
57 controller = handler().get(Bmv2Controller.class);
58 if (controller == null) {
59 log.warn("Failed to get a BMv2 controller");
60 return false;
61 }
62 return true;
63 }
64
65 @Override
66 public DeviceDescription discoverDeviceDetails() {
67
68 if (!init()) {
69 return null;
70 }
71
72 DeviceId deviceId = handler().data().deviceId();
73
74 Bmv2DeviceAgent deviceAgent;
75 try {
76 deviceAgent = controller.getAgent(deviceId);
77 } catch (Bmv2RuntimeException e) {
78 log.error("Failed to connect to Bmv2 device", e);
79 return null;
80 }
81
82 DefaultAnnotations.Builder annotationsBuilder = DefaultAnnotations.builder();
83
84 try {
85 String md5 = deviceAgent.getJsonConfigMd5();
86 BigInteger i = new BigInteger(1, md5.getBytes());
87 annotationsBuilder.set(JSON_CONFIG_MD5, String.format("%1$032X", i).toLowerCase());
88 } catch (Bmv2RuntimeException e) {
89 log.warn("Unable to dump JSON configuration from {}: {}", deviceId, e.explain());
90 }
91 try {
92 int instanceId = deviceAgent.getProcessInstanceId();
93 annotationsBuilder.set(PROCESS_INSTANCE_ID, String.valueOf(instanceId));
94 } catch (Bmv2RuntimeException e) {
95 log.warn("Unable to get process instance ID from {}: {}", deviceId, e.explain());
96 }
97
98 annotationsBuilder.set(AnnotationKeys.PROTOCOL, PROTOCOL);
99
100 return new DefaultDeviceDescription(deviceId.uri(),
101 SWITCH,
102 MANUFACTURER,
103 HW_VERSION,
104 SW_VERSION,
105 SERIAL_NUMBER,
106 new ChassisId(),
107 annotationsBuilder.build());
108 }
109
110 @Override
111 public List<PortDescription> discoverPortDetails() {
112
113 if (!init()) {
114 return null;
115 }
116
117 DeviceId deviceId = handler().data().deviceId();
118
119 Bmv2DeviceAgent deviceAgent;
120 try {
121 deviceAgent = controller.getAgent(deviceId);
122 } catch (Bmv2RuntimeException e) {
123 log.error("Failed to connect to Bmv2 device", e);
124 return null;
125 }
126
127 List<PortDescription> portDescriptions = Lists.newArrayList();
128
129 try {
130 deviceAgent.getPortsInfo().forEach(p -> {
131 PortNumber portNumber = PortNumber.portNumber((long) p.number(), p.ifaceName());
132 portDescriptions.add(new DefaultPortDescription(portNumber, p.isUp(), DefaultAnnotations.EMPTY));
133 });
134 } catch (Bmv2RuntimeException e) {
135 log.error("Unable to get port descriptions of {}: {}", deviceId, e);
136 }
137
138 return ImmutableList.copyOf(portDescriptions);
139 }
140}