blob: 57e247f76ae49c495809bb049a2a92ce4165d1f9 [file] [log] [blame]
Thomas Vachuska9252bc32014-10-23 02:33:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska9252bc32014-10-23 02:33:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska9252bc32014-10-23 02:33:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska9252bc32014-10-23 02:33:25 -070015 */
16package org.onlab.onos.rest;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import org.onlab.onos.net.ConnectPoint;
20import org.onlab.onos.net.DefaultAnnotations;
21import org.onlab.onos.net.Device;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070022import org.onlab.onos.net.DeviceId;
Thomas Vachuska9252bc32014-10-23 02:33:25 -070023import org.onlab.onos.net.Host;
24import org.onlab.onos.net.HostId;
25import org.onlab.onos.net.HostLocation;
26import org.onlab.onos.net.Link;
27import org.onlab.onos.net.MastershipRole;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070028import org.onlab.onos.net.Port;
Thomas Vachuska9252bc32014-10-23 02:33:25 -070029import org.onlab.onos.net.SparseAnnotations;
30import org.onlab.onos.net.device.DefaultDeviceDescription;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070031import org.onlab.onos.net.device.DefaultPortDescription;
Thomas Vachuska9252bc32014-10-23 02:33:25 -070032import org.onlab.onos.net.device.DeviceDescription;
33import org.onlab.onos.net.device.DeviceProvider;
34import org.onlab.onos.net.device.DeviceProviderRegistry;
35import org.onlab.onos.net.device.DeviceProviderService;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070036import org.onlab.onos.net.device.PortDescription;
Thomas Vachuska9252bc32014-10-23 02:33:25 -070037import org.onlab.onos.net.host.DefaultHostDescription;
38import org.onlab.onos.net.host.HostProvider;
39import org.onlab.onos.net.host.HostProviderRegistry;
40import org.onlab.onos.net.host.HostProviderService;
41import org.onlab.onos.net.link.DefaultLinkDescription;
42import org.onlab.onos.net.link.LinkProvider;
43import org.onlab.onos.net.link.LinkProviderRegistry;
44import org.onlab.onos.net.link.LinkProviderService;
45import org.onlab.onos.net.provider.ProviderId;
46import org.onlab.packet.ChassisId;
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070047import org.onlab.packet.IpAddress;
Thomas Vachuska9252bc32014-10-23 02:33:25 -070048import org.onlab.packet.MacAddress;
49import org.onlab.packet.VlanId;
50
51import java.net.URI;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070052import java.util.ArrayList;
Thomas Vachuska9252bc32014-10-23 02:33:25 -070053import java.util.Iterator;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070054import java.util.List;
Thomas Vachuska9252bc32014-10-23 02:33:25 -070055
56import static com.google.common.base.Preconditions.checkNotNull;
57import static org.onlab.onos.net.DeviceId.deviceId;
58import static org.onlab.onos.net.PortNumber.portNumber;
59
60/**
61 * Provider of devices and links parsed from a JSON configuration structure.
62 */
63class ConfigProvider implements DeviceProvider, LinkProvider, HostProvider {
64
65 private static final ProviderId PID =
66 new ProviderId("cfg", "org.onlab.onos.rest", true);
67
68 private final JsonNode cfg;
69 private final DeviceProviderRegistry deviceProviderRegistry;
70 private final LinkProviderRegistry linkProviderRegistry;
71 private final HostProviderRegistry hostProviderRegistry;
72
73 /**
74 * Creates a new configuration provider.
75 *
76 * @param cfg JSON configuration
77 * @param deviceProviderRegistry device provider registry
78 * @param linkProviderRegistry link provider registry
79 * @param hostProviderRegistry host provider registry
80 */
81 ConfigProvider(JsonNode cfg,
82 DeviceProviderRegistry deviceProviderRegistry,
83 LinkProviderRegistry linkProviderRegistry,
84 HostProviderRegistry hostProviderRegistry) {
85 this.cfg = checkNotNull(cfg, "Configuration cannot be null");
86 this.deviceProviderRegistry = checkNotNull(deviceProviderRegistry, "Device provider registry cannot be null");
87 this.linkProviderRegistry = checkNotNull(linkProviderRegistry, "Link provider registry cannot be null");
88 this.hostProviderRegistry = checkNotNull(hostProviderRegistry, "Host provider registry cannot be null");
89 }
90
91 /**
92 * Parses the given JSON and provides links as configured.
93 */
94 void parse() {
95 parseDevices();
96 parseLinks();
97 parseHosts();
98 }
99
100 // Parses the given JSON and provides devices.
101 private void parseDevices() {
102 try {
103 DeviceProviderService dps = deviceProviderRegistry.register(this);
104 JsonNode nodes = cfg.get("devices");
105 if (nodes != null) {
106 for (JsonNode node : nodes) {
107 parseDevice(dps, node);
108 }
109 }
110 } finally {
111 deviceProviderRegistry.unregister(this);
112 }
113 }
114
115 // Parses the given node with device data and supplies the device.
116 private void parseDevice(DeviceProviderService dps, JsonNode node) {
117 URI uri = URI.create(get(node, "uri"));
118 Device.Type type = Device.Type.valueOf(get(node, "type"));
119 String mfr = get(node, "mfr");
120 String hw = get(node, "hw");
121 String sw = get(node, "sw");
122 String serial = get(node, "serial");
123 ChassisId cid = new ChassisId(get(node, "mac"));
124 SparseAnnotations annotations = annotations(node.get("annotations"));
125
126 DeviceDescription desc =
127 new DefaultDeviceDescription(uri, type, mfr, hw, sw, serial,
128 cid, annotations);
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700129 DeviceId deviceId = deviceId(uri);
130 dps.deviceConnected(deviceId, desc);
131
132 JsonNode ports = node.get("ports");
133 if (ports != null) {
134 parsePorts(dps, deviceId, ports);
135 }
136 }
137
138 // Parses the given node with list of device ports.
139 private void parsePorts(DeviceProviderService dps, DeviceId deviceId, JsonNode nodes) {
140 List<PortDescription> ports = new ArrayList<>();
141 for (JsonNode node : nodes) {
142 ports.add(parsePort(node));
143 }
144 dps.updatePorts(deviceId, ports);
145 }
146
147 // Parses the given node with port information.
148 private PortDescription parsePort(JsonNode node) {
149 Port.Type type = Port.Type.valueOf(node.path("type").asText("COPPER"));
150 return new DefaultPortDescription(portNumber(node.path("port").asLong(0)),
151 node.path("enabled").asBoolean(true),
152 type, node.path("speed").asLong(1_000));
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700153 }
154
155 // Parses the given JSON and provides links as configured.
156 private void parseLinks() {
157 try {
158 LinkProviderService lps = linkProviderRegistry.register(this);
159 JsonNode nodes = cfg.get("links");
160 if (nodes != null) {
161 for (JsonNode node : nodes) {
162 parseLink(lps, node, false);
163 if (!node.has("halfplex")) {
164 parseLink(lps, node, true);
165 }
166 }
167 }
168 } finally {
169 linkProviderRegistry.unregister(this);
170 }
171 }
172
173 // Parses the given node with link data and supplies the link.
174 private void parseLink(LinkProviderService lps, JsonNode node, boolean reverse) {
175 ConnectPoint src = connectPoint(get(node, "src"));
176 ConnectPoint dst = connectPoint(get(node, "dst"));
177 Link.Type type = Link.Type.valueOf(get(node, "type"));
178 SparseAnnotations annotations = annotations(node.get("annotations"));
179
180 DefaultLinkDescription desc = reverse ?
181 new DefaultLinkDescription(dst, src, type, annotations) :
182 new DefaultLinkDescription(src, dst, type, annotations);
183 lps.linkDetected(desc);
184 }
185
186 // Parses the given JSON and provides hosts as configured.
187 private void parseHosts() {
188 try {
189 HostProviderService hps = hostProviderRegistry.register(this);
190 JsonNode nodes = cfg.get("hosts");
191 if (nodes != null) {
192 for (JsonNode node : nodes) {
193 parseHost(hps, node);
194 }
195 }
196 } finally {
197 hostProviderRegistry.unregister(this);
198 }
199 }
200
201 // Parses the given node with host data and supplies the host.
202 private void parseHost(HostProviderService hps, JsonNode node) {
203 MacAddress mac = MacAddress.valueOf(get(node, "mac"));
204 VlanId vlanId = VlanId.vlanId(node.get("vlan").shortValue());
205 HostId hostId = HostId.hostId(mac, vlanId);
206 SparseAnnotations annotations = annotations(node.get("annotations"));
207 HostLocation location = new HostLocation(connectPoint(get(node, "location")), 0);
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700208 IpAddress ip = IpAddress.valueOf(get(node, "ip"));
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700209
210 DefaultHostDescription desc =
211 new DefaultHostDescription(mac, vlanId, location, ip, annotations);
212 hps.hostDetected(hostId, desc);
213 }
214
215 // Produces set of annotations from the given JSON node.
216 private SparseAnnotations annotations(JsonNode node) {
217 if (node == null) {
218 return null;
219 }
220
221 DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
222 Iterator<String> it = node.fieldNames();
223 while (it.hasNext()) {
224 String k = it.next();
225 builder.set(k, node.get(k).asText());
226 }
227 return builder.build();
228 }
229
230 // Produces a connection point from the specified uri/port text.
231 private ConnectPoint connectPoint(String text) {
232 int i = text.lastIndexOf("/");
233 return new ConnectPoint(deviceId(text.substring(0, i)),
234 portNumber(text.substring(i + 1)));
235 }
236
237 // Returns string form of the named property in the given JSON object.
238 private String get(JsonNode node, String name) {
239 return node.path(name).asText();
240 }
241
242 @Override
243 public void triggerProbe(Device device) {
244 }
245
246 @Override
247 public void roleChanged(Device device, MastershipRole newRole) {
248 }
249
250 @Override
251 public void triggerProbe(Host host) {
252 }
253
254 @Override
255 public ProviderId id() {
256 return PID;
257 }
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700258
259 @Override
260 public boolean isReachable(Device device) {
261 return false;
262 }
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700263}