Open Kilda Java Documentation
TeTopologyParser.java
Go to the documentation of this file.
1 /* Copyright 2018 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.topo.builders;
17 
18 import static java.lang.String.format;
19 
20 import com.fasterxml.jackson.annotation.JsonIdentityInfo;
21 import com.fasterxml.jackson.annotation.JsonProperty;
22 import com.fasterxml.jackson.annotation.ObjectIdGenerators;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import com.google.common.base.Strings;
25 import lombok.Data;
26 import org.openkilda.topo.Link;
28 import org.openkilda.topo.Switch;
29 import org.openkilda.topo.Topology;
32 
33 import java.io.IOException;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37 
38 
42 public class TeTopologyParser {
43 
44  public static Topology parseTopologyEngineJson(String json) {
45  TopologyDto topologyDto;
46 
47  ObjectMapper mapper = new ObjectMapper();
48  try {
49  topologyDto = mapper.readValue(json, TopologyDto.class);
50  } catch (IOException ex) {
51  throw new TopologyProcessingException(format("Unable to parse the topology '%s'.", json), ex);
52  }
53 
54  Map<String, Switch> switches = new HashMap<>();
55  // Assemble the switches from the provided nodes.
56  topologyDto.getNodes().forEach(node -> {
57  String name = node.getName();
58  if (Strings.isNullOrEmpty(name)) {
59  throw new TopologyProcessingException("The node must have a name.");
60  }
61 
62  String switchId = name.toUpperCase();
63  switches.put(switchId, new Switch(switchId));
64  });
65 
66  Map<String, Link> links = new HashMap<>();
67  // Assemble the links from the provided outgoing_relationships.
68  topologyDto.getNodes().forEach(node -> {
69  String srcId = node.getName().toUpperCase();
70 
71  List<NodeDto> relations = node.getOutgoingRelationships();
72  if (relations != null) {
73  relations.forEach(relation -> {
74  String dstId = relation.getName().toUpperCase();
75 
76  Link link = new Link(
77  // TODO: LinkEndpoint is immutable .. so, with no port/queue .. we should
78  // TODO: probably reuse the same endpoint. Why not?
79  new LinkEndpoint(switches.get(srcId), null, null),
80  new LinkEndpoint(switches.get(dstId), null, null)
81  );
82  links.put(link.getShortSlug(), link);
83  });
84  }
85  });
86 
87  return new Topology(switches, links);
88  }
89 
90  @Data
91  static class TopologyDto {
92 
93  List<NodeDto> nodes;
94 
95  @Data
96  @JsonIdentityInfo(property = "name", generator = ObjectIdGenerators.PropertyGenerator.class)
97  static class NodeDto {
98 
99  String name;
100 
101  @JsonProperty("outgoing_relationships")
102  List<NodeDto> outgoingRelationships;
103  }
104  }
105 }
nodes
Definition: nodes.py:41
name
Definition: setup.py:24
static Topology parseTopologyEngineJson(String json)