blob: 7bae152f6fa52ac9da8c9779eb5b623ed16723ca [file] [log] [blame]
Lee Yongjae7c27bb42017-11-17 12:00:45 +09001/*
2 * Copyright 2017-present Open Networking Foundation
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.simplefabric;
18
19import com.fasterxml.jackson.databind.JsonNode;
20import com.google.common.collect.Sets;
21import org.onlab.packet.IpAddress;
22import org.onlab.packet.IpPrefix;
23import org.onlab.packet.MacAddress;
24import org.onosproject.core.ApplicationId;
25import org.onosproject.net.config.Config;
26import org.onosproject.net.EncapsulationType;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30import java.util.Set;
31
32/**
33 * Configuration object for prefix config.
34 */
35public class SimpleFabricConfig extends Config<ApplicationId> {
36 public static final String KEY = "simpleFabric";
37
38 private static final String L2NETWORKS = "l2Networks";
39 private static final String NAME = "name";
40 private static final String INTERFACES = "interfaces";
41 private static final String ENCAPSULATION = "encapsulation";
42 private static final String L2FORWARD = "l2Forward";
Lee Yongjae6dc7e4f2017-12-06 16:17:51 +090043 private static final String L2BROADCAST = "l2Broadcast";
Lee Yongjae7c27bb42017-11-17 12:00:45 +090044 private static final String IPSUBNETS = "ipSubnets";
45 private static final String BORDERROUTES = "borderRoutes";
46 private static final String IPPREFIX = "ipPrefix";
47 private static final String GATEWAYIP = "gatewayIp";
48 private static final String GATEWAYMAC = "gatewayMac";
49 private static final String L2NETWORKNAME = "l2NetworkName";
50 private static final String NEXTHOP = "nextHop";
51
52 private final Logger log = LoggerFactory.getLogger(getClass());
53
54 /**
55 * Returns all l2Networks in this configuration.
56 *
57 * @return A set of L2Network.
58 */
59 public Set<L2Network> getL2Networks() {
60 Set<L2Network> l2Networks = Sets.newHashSet();
61 JsonNode l2NetworkNode = object.get(L2NETWORKS);
62 if (l2NetworkNode == null) {
63 return l2Networks;
64 }
65
66 l2NetworkNode.forEach(jsonNode -> {
67 Set<String> ifaces = Sets.newHashSet();
68 JsonNode l2NetworkIfaces = jsonNode.path(INTERFACES);
69 if (l2NetworkIfaces == null) {
70 log.warn("simple fabric network config cannot find {}; skip: jsonNode={}", INTERFACES, jsonNode);
71 } else if (!l2NetworkIfaces.toString().isEmpty()) {
72 l2NetworkIfaces.forEach(ifacesNode -> ifaces.add(new String(ifacesNode.asText())));
73 }
74 String encapsulation = "NONE"; // NONE or VLAN
75 if (jsonNode.hasNonNull(ENCAPSULATION)) {
76 encapsulation = jsonNode.get(ENCAPSULATION).asText();
77 }
78 boolean l2Forward = true;
79 if (jsonNode.hasNonNull(L2FORWARD)) {
80 l2Forward = jsonNode.get(L2FORWARD).asBoolean();
81 }
Lee Yongjae6dc7e4f2017-12-06 16:17:51 +090082 boolean l2Broadcast = true;
83 if (jsonNode.hasNonNull(L2BROADCAST)) {
84 l2Broadcast = jsonNode.get(L2BROADCAST).asBoolean();
85 }
Lee Yongjae7c27bb42017-11-17 12:00:45 +090086 try {
87 l2Networks.add(new L2Network(
Lee Yongjae6dc7e4f2017-12-06 16:17:51 +090088 jsonNode.get(NAME).asText(), ifaces, EncapsulationType.enumFromString(encapsulation),
89 l2Forward, l2Broadcast));
Lee Yongjae7c27bb42017-11-17 12:00:45 +090090 } catch (Exception e) {
91 log.warn("simple fabric network config l2Network parse failed; skip: error={} jsonNode={}", jsonNode);
92 }
93 });
94 return l2Networks;
95 }
96
97 /**
98 * Gets the set of configured local IP subnets.
99 *
100 * @return IP Subnets
101 */
102 public Set<IpSubnet> ipSubnets() {
103 Set<IpSubnet> subnets = Sets.newHashSet();
104 JsonNode subnetsNode = object.get(IPSUBNETS);
105 if (subnetsNode == null) {
106 log.warn("simple fabric network config ipSubnets is null!");
107 return subnets;
108 }
109
110 subnetsNode.forEach(jsonNode -> {
111 String encapsulation = "NONE"; // NONE or VLAN
112 if (jsonNode.hasNonNull(ENCAPSULATION)) {
113 encapsulation = jsonNode.get(ENCAPSULATION).asText();
114 }
115 try {
116 subnets.add(new IpSubnet(
117 IpPrefix.valueOf(jsonNode.get(IPPREFIX).asText()),
118 IpAddress.valueOf(jsonNode.get(GATEWAYIP).asText()),
119 MacAddress.valueOf(jsonNode.get(GATEWAYMAC).asText()),
120 EncapsulationType.enumFromString(encapsulation),
121 jsonNode.get(L2NETWORKNAME).asText()));
122 } catch (Exception e) {
123 log.warn("simple fabric network config ipSubnet parse failed; skip: error={} jsonNode={}", jsonNode);
124 }
125 });
126
127 return subnets;
128 }
129
130 /**
131 * Returns all routes in this configuration.
132 *
133 * @return A set of route.
134 */
135 public Set<Route> borderRoutes() {
136 Set<Route> routes = Sets.newHashSet();
137
138 JsonNode routesNode = object.get(BORDERROUTES);
139 if (routesNode == null) {
140 //log.warn("simple fabric network config borderRoutes is null!");
141 return routes;
142 }
143
144 routesNode.forEach(jsonNode -> {
145 try {
146 routes.add(new Route(
147 Route.Source.STATIC,
148 IpPrefix.valueOf(jsonNode.path(IPPREFIX).asText()),
149 IpAddress.valueOf(jsonNode.path(NEXTHOP).asText())));
150 } catch (IllegalArgumentException e) {
151 log.warn("simple fabric network config parse error; skip: {}", jsonNode);
152 }
153 });
154
155 return routes;
156 }
157
158}