blob: d7d5e533d60ed0a41a4842ea80defae84360d91d [file] [log] [blame]
Andrea Campanella75ef9f52017-07-27 20:14:32 +02001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Andrea Campanella75ef9f52017-07-27 20:14:32 +02003 *
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.incubator.net.config.basics;
18
19import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import com.google.common.annotations.Beta;
22import com.google.common.collect.ImmutableList;
23import org.onosproject.net.DefaultAnnotations;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.Port;
26import org.onosproject.net.PortNumber;
27import org.onosproject.net.config.Config;
28import org.onosproject.net.device.DefaultPortDescription;
29import org.onosproject.net.device.PortDescription;
30import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
33import java.util.Iterator;
34import java.util.List;
35import java.util.Map;
36
37/**
38 * Configuration for Ports. Creates a list of PortDescription based on the given Json.
39 */
40@Beta
41public class PortDescriptionsConfig extends Config<DeviceId> {
42 private static Logger log = LoggerFactory.getLogger(PortDescriptionsConfig.class);
43
44 private static final String NUMBER = "number";
45 private static final String NAME = "name";
46 private static final String ENABLED = "enabled";
47 private static final String REMOVED = "removed";
48 private static final String TYPE = "type";
49 private static final String SPEED = "speed";
50 private static final String ANNOTATIONS = "annotations";
51
52 private static final String CONFIG_VALUE_ERROR = "Error parsing config value";
53
54 @Override
55 public boolean isValid() {
56 for (Iterator<Map.Entry<String, JsonNode>> it = node.fields(); it.hasNext();) {
57 JsonNode nodePort = it.next().getValue();
58 if (!hasOnlyFields((ObjectNode) nodePort, NUMBER, NAME, ENABLED, REMOVED, TYPE,
59 SPEED, ANNOTATIONS)) {
60 return false;
61 }
62 ObjectNode obj = (ObjectNode) nodePort;
63
64 if (!(isNumber(obj, NUMBER, FieldPresence.MANDATORY) &&
65 isString(obj, NAME, FieldPresence.OPTIONAL) &&
66 isBoolean(obj, ENABLED, FieldPresence.OPTIONAL) &&
67 isBoolean(obj, REMOVED, FieldPresence.OPTIONAL) &&
68 isString(obj, TYPE, FieldPresence.OPTIONAL) &&
69 isIntegralNumber(obj, SPEED, FieldPresence.OPTIONAL))) {
70 return false;
71 }
72
73 if (node.has(ANNOTATIONS) && !node.get(ANNOTATIONS).isObject()) {
74 log.error("Annotations must be an inner json node");
75 return false;
76 }
77
78 }
79 return true;
80 }
81
82 /**
83 * Retrieves all port descriptions.
84 *
85 * @return set of port descriptions
86 */
87 public List<PortDescription> portDescriptions() {
88
89 try {
90 ImmutableList.Builder<PortDescription> portDescriptions = ImmutableList.builder();
91 for (Iterator<Map.Entry<String, JsonNode>> it = node.fields(); it.hasNext();) {
92 JsonNode portNode = it.next().getValue();
93 long number = portNode.path(NUMBER).asLong();
94
95 String name = portNode.path(NAME).asText(null);
96
97 PortNumber portNumber = createPortNumber(number, name);
98
99 DefaultPortDescription.Builder builder = DefaultPortDescription.builder()
Yuta HIGUCHI53e47962018-03-01 23:50:48 -0800100 .withPortNumber(portNumber);
Andrea Campanella75ef9f52017-07-27 20:14:32 +0200101 if (portNode.has(ENABLED)) {
102 builder.isEnabled(portNode.path(ENABLED).asBoolean());
103 }
104
105 if (portNode.has(REMOVED)) {
106 builder.isRemoved(portNode.path(REMOVED).asBoolean());
107 }
108
109 if (portNode.has(TYPE)) {
110 builder.type(Port.Type.valueOf(portNode.path(TYPE).asText().toUpperCase()));
111 }
112
113 if (portNode.has(SPEED)) {
114 builder.portSpeed(portNode.path(SPEED).asLong());
115 }
116
117 if (portNode.has(ANNOTATIONS)) {
118 DefaultAnnotations.Builder annotationsBuilder = DefaultAnnotations.builder();
119 Iterator<Map.Entry<String, JsonNode>> annotationsIt = portNode.get(ANNOTATIONS).fields();
Carmelo Cascone693c38c2018-01-31 14:46:29 -0800120 while (annotationsIt.hasNext()) {
Andrea Campanella75ef9f52017-07-27 20:14:32 +0200121 Map.Entry<String, JsonNode> entry = annotationsIt.next();
122 annotationsBuilder.set(entry.getKey(), entry.getValue().asText());
123 }
124 builder.annotations(annotationsBuilder.build());
125 }
126
127 portDescriptions.add(builder.build());
128 }
129
130 return portDescriptions.build();
131
132 } catch (IllegalArgumentException e) {
133 log.error(CONFIG_VALUE_ERROR, e);
134 return ImmutableList.of();
135 }
136 }
137
138 private PortNumber createPortNumber(long number, String name) {
139 if (name == null) {
140 return PortNumber.portNumber(number);
141 }
142 return PortNumber.portNumber(number, name);
143 }
144
145
146}