6dc3f0dbbcff2a76829b6bc0d6a0ad901cdc6aa2
[onosfw.git] /
1 /*
2  * Copyright 2014-2015 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
17 package org.onosproject.segmentrouting.config;
18
19 import com.fasterxml.jackson.databind.node.ArrayNode;
20 import org.onlab.packet.Ip4Address;
21 import org.onlab.packet.MacAddress;
22 import org.onosproject.net.DeviceId;
23 import org.onosproject.net.config.Config;
24 import org.onosproject.net.config.basics.BasicElementConfig;
25
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Optional;
29
30 /**
31  * Configuration object for Segment Routing Application.
32  */
33 public class SegmentRoutingConfig extends Config<DeviceId> {
34     private static final String NAME = "name";
35     private static final String IP = "routerIp";
36     private static final String MAC = "routerMac";
37     private static final String SID = "nodeSid";
38     private static final String EDGE = "isEdgeRouter";
39     private static final String ADJSID = "adjacencySids";
40
41     public Optional<String> getName() {
42         String name = get(NAME, null);
43         return name != null ? Optional.of(name) : Optional.empty();
44     }
45
46     public BasicElementConfig setName(String name) {
47         return (BasicElementConfig) setOrClear(NAME, name);
48     }
49
50     public Ip4Address getIp() {
51         String ip = get(IP, null);
52         return ip != null ? Ip4Address.valueOf(ip) : null;
53     }
54
55     public BasicElementConfig setIp(String ip) {
56         return (BasicElementConfig) setOrClear(IP, ip);
57     }
58
59     public MacAddress getMac() {
60         String mac = get(MAC, null);
61         return mac != null ? MacAddress.valueOf(mac) : null;
62     }
63
64     public BasicElementConfig setMac(String mac) {
65         return (BasicElementConfig) setOrClear(MAC, mac);
66     }
67
68     public int getSid() {
69         return get(SID, -1);
70     }
71
72     public BasicElementConfig setSid(int sid) {
73         return (BasicElementConfig) setOrClear(SID, sid);
74     }
75
76     public boolean isEdgeRouter() {
77         return get(EDGE, false);
78     }
79
80     public BasicElementConfig setEdgeRouter(boolean isEdgeRouter) {
81         return (BasicElementConfig) setOrClear(EDGE, isEdgeRouter);
82     }
83
84     public List<AdjacencySid> getAdjacencySids() {
85         ArrayList<AdjacencySid> adjacencySids = new ArrayList<>();
86
87         if (!object.has(ADJSID)) {
88             return adjacencySids;
89         }
90
91         ArrayNode adjacencySidNodes = (ArrayNode) object.path(ADJSID);
92         adjacencySidNodes.forEach(adjacencySidNode -> {
93             int asid = adjacencySidNode.path(AdjacencySid.ASID).asInt();
94
95             ArrayList<Integer> ports = new ArrayList<Integer>();
96             ArrayNode portsNodes = (ArrayNode) adjacencySidNode.path(AdjacencySid.PORTS);
97             portsNodes.forEach(portNode -> {
98                 ports.add(portNode.asInt());
99             });
100
101             AdjacencySid adjacencySid = new AdjacencySid(asid, ports);
102             adjacencySids.add(adjacencySid);
103         });
104
105         return adjacencySids;
106     }
107
108     public class AdjacencySid {
109         private static final String ASID = "adjSid";
110         private static final String PORTS = "ports";
111
112         int asid;
113         List<Integer> ports;
114
115         public AdjacencySid(int asid, List<Integer> ports) {
116             this.asid = asid;
117             this.ports = ports;
118         }
119
120         public int getAsid() {
121             return asid;
122         }
123
124         public List<Integer> getPorts() {
125             return ports;
126         }
127     }
128 }