/** * Generates a link description from a link description entity. The endpoints * must be specified to indicate directionality. * * @param src the source ConnectPoint * @param dst the destination ConnectPoint * @param link the link config entity * @return a linkDescription based on the config */ public static LinkDescription descriptionOf( ConnectPoint src, ConnectPoint dst, Link link) { checkNotNull(src, "Must supply a source endpoint"); checkNotNull(dst, "Must supply a destination endpoint"); checkNotNull(link, "Must supply a link"); return new DefaultLinkDescription( src, dst, link.type(), link.isExpected(), (SparseAnnotations) link.annotations()); }
private boolean isEdgePort(ConnectPoint point) { // Logical ports are not counted as edge ports nor are infrastructure // ports. Ports that have only edge links are considered edge ports. return !point.port().isLogical() && deviceService.getPort(point) != null && linkService.getLinks(point).stream() .allMatch(link -> link.type() == Type.EDGE); }
/** * Create a list of devices ordered from the ingress to the egress of a path. * @param resources the resources of the intent * @return a list of devices */ private List<DeviceId> createIngressToEgressDeviceList(Collection<NetworkResource> resources) { List<DeviceId> deviceIds = Lists.newArrayList(); List<Link> links = Lists.newArrayList(); for (NetworkResource resource : resources) { if (resource instanceof Link) { Link linkToAdd = (Link) resource; if (linkToAdd.type() != Link.Type.EDGE) { links.add(linkToAdd); } } } Collections.sort(links, LINK_COMPARATOR); int i = 0; for (Link orderedLink : links) { deviceIds.add(orderedLink.src().deviceId()); if (i == resources.size() - 1) { deviceIds.add(orderedLink.dst().deviceId()); } i++; } return deviceIds; }
@Override public double weight(TopologyEdge edge) { return (edge.link().state() == INACTIVE) || (edge.link().type() == INDIRECT) ? -1 : 1; } }
private ImmutableSet<ConnectPoint> findInfrastructurePoints() { ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder(); for (TopologyEdge edge : graph.getEdges()) { if (edge.link().type() == Type.EDGE) { // exclude EDGE link from infrastructure link // - Device <-> Host // - Device <-> remote domain Device continue; } builder.add(edge.link().src()); builder.add(edge.link().dst()); } return builder.build(); }
@Override public double weight(TopologyEdge edge) { // To force preference to use direct paths first, make indirect // links as expensive as the linear vertex traversal. return edge.link().state() == ACTIVE ? (edge.link().type() == INDIRECT ? indirectLinkCost : 1) : -1; } }
private LinkEvent updateLink(LinkKey key, Link oldLink, Link newLink) { if (oldLink.state() != newLink.state() || (oldLink.type() == INDIRECT && newLink.type() == DIRECT) || !AnnotationsUtil.isEqual(oldLink.annotations(), newLink.annotations())) { links.put(key, newLink); // strictly speaking following can be ommitted srcLinks.put(oldLink.src().deviceId(), key); dstLinks.put(oldLink.dst().deviceId(), key); return new LinkEvent(LINK_UPDATED, newLink); } return null; }
@Override public ObjectNode encode(Link link, CodecContext context) { checkNotNull(link, "Link cannot be null"); JsonCodec<ConnectPoint> codec = context.codec(ConnectPoint.class); ObjectNode result = context.mapper().createObjectNode(); result.set(SRC, codec.encode(link.src(), context)); result.set(DST, codec.encode(link.dst(), context)); result.put(TYPE, link.type().toString()); if (link.state() != null) { result.put(STATE, link.state().toString()); } return annotate(result, link, context); }
@Override public Weight weight(TopologyEdge edge) { return (edge.link().state() == INACTIVE) || (edge.link().type() == INDIRECT) ? getNonViableWeight() : new ScalarWeight(HOP_WEIGHT_VALUE); } }
private LinkDescription description(Link link, String key, String value) { checkNotNull(key, "Key cannot be null"); DefaultAnnotations.Builder builder = DefaultAnnotations.builder(); if (value != null) { builder.set(key, value); } else { builder.remove(key); } return new DefaultLinkDescription(link.src(), link.dst(), link.type(), link.isExpected(), builder.build()); }
/** * Returns a formatted string representing the given link. * * @param link infrastructure link * @return formatted link string */ public static String linkString(Link link) { return String.format(FMT, link.src().deviceId(), link.src().port(), link.dst().deviceId(), link.dst().port(), link.type(), link.state(), annotations(link.annotations()), link.isExpected()); }
.filter(link -> !link.type().equals(EDGE)) .collect(Collectors.toSet());
private Link reverseLink(Link link) { return DefaultLink.builder().providerId(link.providerId()) .src(link.dst()) .dst(link.src()) .type(link.type()) .state(link.state()) .isExpected(link.isExpected()) .build(); }
@Override public LinkEvent removeOrDownLink(ConnectPoint src, ConnectPoint dst) { Link link = getLink(src, dst); if (link == null) { return null; } if (link.isDurable()) { return link.state() == INACTIVE ? null : updateLink(linkKey(link.src(), link.dst()), link, new DefaultLink(link.providerId(), link.src(), link.dst(), link.type(), INACTIVE, link.isDurable(), link.annotations())); } return removeLink(src, dst); }
private void processLinkEvent(LinkEvent event) { // negative Link event can result in increase of edge ports boolean addEdgePort = event.type() == LinkEvent.Type.LINK_REMOVED; // but if the Link is an Edge type, it will be the opposite if (event.subject().type() == Type.EDGE) { addEdgePort = !addEdgePort; } if (addEdgePort) { addEdgePort(event.subject().src()); addEdgePort(event.subject().dst()); } else { removeEdgePort(event.subject().src()); removeEdgePort(event.subject().dst()); } }
/** * Returns a link in reverse direction. * * @param link to revese * @return reversed link */ Link reverse(Link link) { return DefaultLink.builder() .providerId(link.providerId()) .src(link.dst()) .dst(link.src()) .type(link.type()) .state(link.state()) .isExpected(link.isExpected()) .annotations(link.annotations()) .build(); }
return ScalarWeight.toWeight(-1); if (edge.link().type() != Link.Type.OPTICAL) { return ScalarWeight.toWeight(-1);