blob: 9f89c41bc9b6ed4399f7930a9b72212cc24dc377 [file] [log] [blame]
Kyuhwi Choidc2973b2016-05-13 14:54:31 +09001/*
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 */
16package org.onosproject.scalablegateway.api;
17
18
19import com.fasterxml.jackson.databind.JsonNode;
20import com.google.common.collect.Lists;
21import com.google.common.collect.Sets;
22import org.onlab.packet.Ip4Address;
23import org.onosproject.core.ApplicationId;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.config.Config;
26import org.slf4j.Logger;
27
28import java.util.Collections;
29import java.util.List;
30import java.util.Set;
31
32import static org.slf4j.LoggerFactory.getLogger;
33import static org.onosproject.net.config.Config.FieldPresence.MANDATORY;
34
35/**
36 * Configuration object for OpensatckNode service.
37 */
38public class GatewayNodeConfig extends Config<ApplicationId> {
39
40 protected final Logger log = getLogger(getClass());
41
42 public static final String NODES = "nodes";
43 public static final String BRIDGE_ID = "bridgeId";
44 public static final String DATAPLANE_IP = "dataPlaneIp";
45 public static final String EXTERNAL_INTERFACE_NAME = "gatewayExternalInterfaceName";
46
47 /**
48 * Returns the set of nodes read from network config.
49 *
50 * @return set of OpensatckNodeConfig or null
51 */
52 public Set<GatewayNode> gatewayNodes() {
53
54 Set<GatewayNode> nodes = Sets.newHashSet();
55
56 JsonNode jsonNodes = object.get(NODES);
57 if (jsonNodes == null) {
58 return null;
59 }
60
61 jsonNodes.forEach(jsonNode -> {
62 try {
63 nodes.add(new GatewayNode.Builder()
64 .gatewayDeviceId(DeviceId.deviceId(jsonNode.path(BRIDGE_ID).asText()))
65 .gatewayExternalInterfaceNames(
66 getExternalInterfaceName(jsonNode.path(EXTERNAL_INTERFACE_NAME).asText()))
67 .dataIpAddress(Ip4Address.valueOf(jsonNode.path(DATAPLANE_IP).asText())).build());
68 } catch (IllegalArgumentException | NullPointerException e) {
69 log.error("Failed to read {}", e.toString());
70 }
71 });
72 return nodes;
73 }
74
75 private List<String> getExternalInterfaceName(String s) {
76 List<String> list = Lists.newArrayList();
77 return Collections.addAll(list, s.split(",")) ? list : null;
78 }
79
80 @Override
81 public boolean isValid() {
82 return hasOnlyFields(NODES, BRIDGE_ID, DATAPLANE_IP, EXTERNAL_INTERFACE_NAME) &&
83 isIpAddress(DATAPLANE_IP, MANDATORY) && isString(BRIDGE_ID, MANDATORY) &&
84 isString(EXTERNAL_INTERFACE_NAME, MANDATORY);
85 }
86
87}