aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/config/SegmentRouterConfig.java
blob: c8d4a54aadfefe06b783d96c8b9257449a81dd4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
 * Copyright 2015 Open Networking Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onosproject.segmentrouting.config;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.onosproject.net.DeviceId;
import org.onosproject.segmentrouting.config.NetworkConfig.SwitchConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Manages additional configuration for switches configured as Segment Routers.
 */
public class SegmentRouterConfig extends SwitchConfig {
    protected static final Logger log = LoggerFactory
            .getLogger(SegmentRouterConfig.class);
    private String routerIp;
    private String routerMac;
    private int nodeSid;
    private boolean isEdgeRouter;
    private List<AdjacencySid> adjacencySids;
    private List<Subnet> subnets;

    public static final String ROUTER_IP = "routerIp";
    public static final String ROUTER_MAC = "routerMac";
    public static final String NODE_SID = "nodeSid";
    public static final String ADJACENCY_SIDS = "adjacencySids";
    public static final String SUBNETS = "subnets";
    public static final String ISEDGE = "isEdgeRouter";
    private static final int SRGB_MAX = 1000;

    /**
     * Parses and validates the additional configuration parameters applicable
     * to segment routers.
     *
     * @param swc switch configuration
     */
    public SegmentRouterConfig(SwitchConfig swc) {
        this.setName(swc.getName());
        this.setDpid(swc.getDpid());
        this.setType(swc.getType());
        this.setLatitude(swc.getLatitude());
        this.setLongitude(swc.getLongitude());
        this.setParams(swc.getParams());
        this.setAllowed(swc.isAllowed());
        publishAttributes = new ConcurrentHashMap<>();
        adjacencySids = new ArrayList<>();
        subnets = new ArrayList<>();
        parseParams();
        validateParams();
        setPublishAttributes();
    }

    /**
     * Returns the configured segment router IP address.
     *
     * @return ip address in string format
     */
    public String getRouterIp() {
        return routerIp;
    }

    public void setRouterIp(String routerIp) {
        this.routerIp = routerIp;
    }

    /**
     * Returns the configured segment router mac address.
     *
     * @return mac address in string format
     */
    public String getRouterMac() {
        return routerMac;
    }

    public void setRouterMac(String routerMac) {
        this.routerMac = routerMac;
    }

    /**
     * Returns the configured sID for a segment router.
     *
     * @return segment identifier
     */
    public int getNodeSid() {
        return nodeSid;
    }

    public void setNodeSid(int nodeSid) {
        this.nodeSid = nodeSid;
    }

    /**
     * Returns the flag that indicates the configured segment router
     * is edge or backbone router.
     *
     * @return boolean
     */
    public boolean isEdgeRouter() {
        return isEdgeRouter;
    }

    public void setIsEdgeRouter(boolean isEdge) {
        this.isEdgeRouter = isEdge;
    }

    /**
     * Class representing segment router adjacency identifier.
     */
    public static class AdjacencySid {
        private int adjSid;
        private List<Integer> ports;

        public AdjacencySid(int adjSid, List<Integer> ports) {
            this.ports = ports;
            this.adjSid = adjSid;
        }

        /**
         * Returns the list of ports part of a segment
         * router adjacency identifier.
         *
         * @return list of integers
         */
        public List<Integer> getPorts() {
            return ports;
        }

        public void setPorts(List<Integer> ports) {
            this.ports = ports;
        }

        /**
         * Returns the configured adjacency id of a segment router.
         *
         * @return integer
         */
        public int getAdjSid() {
            return adjSid;
        }

        public void setAdjSid(int adjSid) {
            this.adjSid = adjSid;
        }
    }

    /**
     * Returns the configured adjacent segment IDs for a segment router.
     *
     * @return list of adjacency identifier
     */
    public List<AdjacencySid> getAdjacencySids() {
        return adjacencySids;
    }

    public void setAdjacencySids(List<AdjacencySid> adjacencySids) {
        this.adjacencySids = adjacencySids;
    }

    /**
     * Class representing a subnet attached to a segment router.
     */
    public static class Subnet {
        private int portNo;
        private String subnetIp;

        public Subnet(int portNo, String subnetIp) {
            this.portNo = portNo;
            this.subnetIp = subnetIp;
        }

        /**
         * Returns the port number of segment router on
         * which subnet is attached.
         *
         * @return integer
         */
        public int getPortNo() {
            return portNo;
        }

        public void setPortNo(int portNo) {
            this.portNo = portNo;
        }

        /**
         * Returns the configured subnet address.
         *
         * @return subnet ip address in string format
         */
        public String getSubnetIp() {
            return subnetIp;
        }

        public void setSubnetIp(String subnetIp) {
            this.subnetIp = subnetIp;
        }
    }

    /**
     * Returns the configured subnets for a segment router.
     *
     * @return list of subnets
     */
    public List<Subnet> getSubnets() {
        return subnets;
    }

    public void setSubnets(List<Subnet> subnets) {
        this.subnets = subnets;
    }

    // ********************
    // Helper methods
    // ********************

    private void parseParams() {
        if (params == null) {
            throw new NetworkConfigException.ParamsNotSpecified(name);
        }

        Set<Entry<String, JsonNode>> m = params.entrySet();
        for (Entry<String, JsonNode> e : m) {
            String key = e.getKey();
            JsonNode j = e.getValue();
            if (key.equals("routerIp")) {
                setRouterIp(j.asText());
            } else if (key.equals("routerMac")) {
                setRouterMac(j.asText());
            } else if (key.equals("nodeSid")) {
                setNodeSid(j.asInt());
            } else if (key.equals("isEdgeRouter")) {
                setIsEdgeRouter(j.asBoolean());
            } else if (key.equals("adjacencySids") || key.equals("subnets")) {
                getInnerParams(j, key);
            } else {
                throw new UnknownSegmentRouterConfig(key, dpid);
            }
        }
    }

    private void getInnerParams(JsonNode j, String innerParam) {
        Iterator<JsonNode> innerList = j.elements();
        while (innerList.hasNext()) {
            Iterator<Entry<String, JsonNode>> f = innerList.next().fields();
            int portNo = -1;
            int adjSid = -1;
            String subnetIp = null;
            List<Integer> ports = null;
            while (f.hasNext()) {
                Entry<String, JsonNode> fe = f.next();
                if (fe.getKey().equals("portNo")) {
                    portNo = fe.getValue().asInt();
                } else if (fe.getKey().equals("adjSid")) {
                    adjSid = fe.getValue().asInt();
                } else if (fe.getKey().equals("subnetIp")) {
                    subnetIp = fe.getValue().asText();
                } else if (fe.getKey().equals("ports")) {
                    if (fe.getValue().isArray()) {
                        Iterator<JsonNode> i = fe.getValue().elements();
                        ports = new ArrayList<>();
                        while (i.hasNext()) {
                            ports.add(i.next().asInt());
                        }
                    }
                } else {
                    throw new UnknownSegmentRouterConfig(fe.getKey(), dpid);
                }
            }
            if (innerParam.equals("adjacencySids")) {
                AdjacencySid ads = new AdjacencySid(adjSid, ports);
                adjacencySids.add(ads);
            } else {
                Subnet sip = new Subnet(portNo, subnetIp);
                subnets.add(sip);
            }
        }
    }

    private void validateParams() {
        if (routerIp == null) {
            throw new IpNotSpecified(dpid);
        }
        if (routerMac == null) {
            throw new MacNotSpecified(dpid);
        }
        if (isEdgeRouter && subnets.isEmpty()) {
            throw new SubnetNotSpecifiedInEdgeRouter(dpid);
        }
        if (!isEdgeRouter && !subnets.isEmpty()) {
            throw new SubnetSpecifiedInBackboneRouter(dpid);
        }
        if (nodeSid > SRGB_MAX) {
            throw new NodeLabelNotInSRGB(nodeSid, dpid);
        }
        for (AdjacencySid as : adjacencySids) {
            int label = as.getAdjSid();
            List<Integer> plist = as.getPorts();
            if (label <= SRGB_MAX) {
                throw new AdjacencyLabelInSRGB(label, dpid);
            }
            if (plist.size() <= 1) {
                throw new AdjacencyLabelNotEnoughPorts(label, dpid);
            }
        }


        // TODO more validations
    }

    /**
     * Setting publishAttributes implies that this is the configuration that
     * will be added to Topology.Switch object before it is published on the
     * channel to other controller instances.
     */
    private void setPublishAttributes() {
        publishAttributes.put(ROUTER_IP, routerIp);
        publishAttributes.put(ROUTER_MAC, routerMac);
        publishAttributes.put(NODE_SID, String.valueOf(nodeSid));
        publishAttributes.put(ISEDGE, String.valueOf(isEdgeRouter));
        ObjectMapper mapper = new ObjectMapper();
        try {
            publishAttributes.put(ADJACENCY_SIDS,
                    mapper.writeValueAsString(adjacencySids));
            publishAttributes.put(SUBNETS,
                    mapper.writeValueAsString(subnets));
        } catch (JsonProcessingException e) {
            log.error("Error while writing SR config: {}", e.getCause());
        } catch (IOException e) {
            log.error("Error while writing SR config: {}", e.getCause());
        }
    }

    // ********************
    // Exceptions
    // ********************

    public static class IpNotSpecified extends RuntimeException {
        private static final long serialVersionUID = -3001502553646331686L;

        public IpNotSpecified(DeviceId dpid) {
            super();
            log.error("Router IP address not specified for SR config dpid:{}",
                    dpid);
        }
    }

    public static class MacNotSpecified extends RuntimeException {
        private static final long serialVersionUID = -5850132094884129179L;

        public MacNotSpecified(DeviceId dpid) {
            super();
            log.error("Router Mac address not specified for SR config dpid:{}",
                    dpid);
        }
    }

    public static class UnknownSegmentRouterConfig extends RuntimeException {
        private static final long serialVersionUID = -5750132094884129179L;

        public UnknownSegmentRouterConfig(String key, DeviceId dpid) {
            super();
            log.error("Unknown Segment Router config {} in dpid: {}", key,
                    dpid);
        }
    }

    public static class SubnetNotSpecifiedInEdgeRouter extends RuntimeException {
        private static final long serialVersionUID = -5855458472668581268L;

        public SubnetNotSpecifiedInEdgeRouter(DeviceId dpid) {
            super();
            log.error("Subnet was not specified for edge router in dpid: {}",
                    dpid);
        }
    }

    public static class SubnetSpecifiedInBackboneRouter extends RuntimeException {
        private static final long serialVersionUID = 1L;

        public SubnetSpecifiedInBackboneRouter(DeviceId dpid) {
            super();
            log.error("Subnet was specified in backbone router in dpid: {}",
                    dpid);
        }
    }

    public static class NodeLabelNotInSRGB extends RuntimeException {
        private static final long serialVersionUID = -8482670903748519526L;

        public NodeLabelNotInSRGB(int label, DeviceId dpid) {
            super();
            log.error("Node sif {} specified in not in global label-base "
                    + "in dpid: {}", label,
                    dpid);
        }
    }

    public static class AdjacencyLabelInSRGB extends RuntimeException {
        private static final long serialVersionUID = -8482670903748519526L;

        public AdjacencyLabelInSRGB(int label, DeviceId dpid) {
            super();
            log.error("Adjaceny label {} specified from global label-base "
                    + "in dpid: {}", label,
                    dpid);
        }
    }

    public static class AdjacencyLabelNotEnoughPorts extends RuntimeException {
        private static final long serialVersionUID = -8482670903748519526L;

        public AdjacencyLabelNotEnoughPorts(int label, DeviceId dpid) {
            super();
            log.error("Adjaceny label {} must be specified for at least 2 ports. "
                    + "Adjacency labels for single ports are auto-generated "
                    + "in dpid: {}", label,
                    dpid);
        }
    }
}