Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0a4e674 | 2016-09-15 23:03:10 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 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 | package org.onosproject.newoptical; |
| 17 | |
| 18 | import com.google.common.annotations.Beta; |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import org.onlab.util.Bandwidth; |
| 22 | import org.onosproject.newoptical.api.OpticalConnectivityId; |
| 23 | import org.onosproject.net.ConnectPoint; |
| 24 | import org.onosproject.net.Link; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 25 | |
| 26 | import java.time.Duration; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 27 | import java.util.List; |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 28 | import java.util.Optional; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 29 | import java.util.Set; |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 30 | import java.util.stream.Collectors; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 31 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 32 | import static com.google.common.base.Preconditions.checkState; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Entity to store optical connectivity request and related information. |
| 36 | */ |
| 37 | @Beta |
| 38 | public class OpticalConnectivity { |
| 39 | |
| 40 | private final OpticalConnectivityId id; |
| 41 | private final List<Link> links; |
| 42 | private final Bandwidth requestBandwidth; |
| 43 | private final Duration requestLatency; |
| 44 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 45 | /** |
| 46 | * Set of packet link that is not yet established. |
| 47 | * Packet links in this set are expected to be discovered after underlying (optical) path has been provisioned. |
| 48 | */ |
| 49 | private final ImmutableSet<PacketLinkRealizedByOptical> unestablishedLinks; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 50 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 51 | /** |
| 52 | * Set of packet link that is already established. |
| 53 | */ |
| 54 | private final ImmutableSet<PacketLinkRealizedByOptical> establishedLinks; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 55 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 56 | public OpticalConnectivity(OpticalConnectivityId id, |
| 57 | List<Link> links, |
| 58 | Bandwidth requestBandwidth, |
| 59 | Duration requestLatency, |
| 60 | Set<PacketLinkRealizedByOptical> unestablishedLinks, |
| 61 | Set<PacketLinkRealizedByOptical> establishedLinks) { |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 62 | this.id = id; |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 63 | this.links = ImmutableList.copyOf(links); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 64 | this.requestBandwidth = requestBandwidth; |
| 65 | this.requestLatency = requestLatency; |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 66 | this.unestablishedLinks = ImmutableSet.copyOf(unestablishedLinks); |
| 67 | this.establishedLinks = ImmutableSet.copyOf(establishedLinks); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 70 | private OpticalConnectivity(OpticalConnectivity connectivity) { |
| 71 | this.id = connectivity.id; |
| 72 | this.links = ImmutableList.copyOf(connectivity.links); |
| 73 | this.requestBandwidth = connectivity.requestBandwidth; |
| 74 | this.requestLatency = connectivity.requestLatency; |
| 75 | this.unestablishedLinks = ImmutableSet.copyOf(connectivity.unestablishedLinks); |
| 76 | this.establishedLinks = ImmutableSet.copyOf(connectivity.establishedLinks); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | public boolean isAllRealizingLinkEstablished() { |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 80 | // Check if all links are established |
| 81 | return unestablishedLinks.isEmpty(); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | public boolean isAllRealizingLinkNotEstablished() { |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 85 | // Check if any link is not established |
| 86 | return establishedLinks.isEmpty(); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | public OpticalConnectivityId id() { |
| 90 | return id; |
| 91 | } |
| 92 | |
| 93 | public List<Link> links() { |
| 94 | return links; |
| 95 | } |
| 96 | |
| 97 | public Bandwidth bandwidth() { |
| 98 | return requestBandwidth; |
| 99 | } |
| 100 | |
| 101 | public Duration latency() { |
| 102 | return requestLatency; |
| 103 | } |
| 104 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 105 | public Set<PacketLinkRealizedByOptical> getEstablishedLinks() { |
| 106 | return establishedLinks; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 109 | public Set<PacketLinkRealizedByOptical> getUnestablishedLinks() { |
| 110 | return unestablishedLinks; |
| 111 | } |
| 112 | |
| 113 | public OpticalConnectivity setLinkEstablished(ConnectPoint src, |
| 114 | ConnectPoint dst, |
| 115 | boolean established) { |
| 116 | Set<PacketLinkRealizedByOptical> newEstablishedLinks; |
| 117 | Set<PacketLinkRealizedByOptical> newUnestablishedLinks; |
| 118 | |
| 119 | if (established) { |
| 120 | // move PacketLink from unestablished set to established set |
| 121 | Optional<PacketLinkRealizedByOptical> link = this.unestablishedLinks.stream() |
| 122 | .filter(l -> l.isBetween(src, dst)).findAny(); |
| 123 | checkState(link.isPresent()); |
| 124 | |
| 125 | newUnestablishedLinks = this.unestablishedLinks.stream() |
| 126 | .filter(l -> !l.isBetween(src, dst)) |
| 127 | .collect(Collectors.toSet()); |
| 128 | newEstablishedLinks = ImmutableSet.<PacketLinkRealizedByOptical>builder() |
| 129 | .addAll(this.establishedLinks) |
| 130 | .add(link.get()) |
| 131 | .build(); |
| 132 | } else { |
| 133 | // move PacketLink from established set to unestablished set |
| 134 | Optional<PacketLinkRealizedByOptical> link = this.establishedLinks.stream() |
| 135 | .filter(l -> l.isBetween(src, dst)).findAny(); |
| 136 | checkState(link.isPresent()); |
| 137 | |
| 138 | newEstablishedLinks = this.establishedLinks.stream() |
| 139 | .filter(l -> !l.isBetween(src, dst)) |
| 140 | .collect(Collectors.toSet()); |
| 141 | newUnestablishedLinks = ImmutableSet.<PacketLinkRealizedByOptical>builder() |
| 142 | .addAll(this.unestablishedLinks) |
| 143 | .add(link.get()) |
| 144 | .build(); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 147 | return new OpticalConnectivity(this.id, |
| 148 | this.links, |
| 149 | this.requestBandwidth, |
| 150 | this.requestLatency, |
| 151 | newUnestablishedLinks, |
| 152 | newEstablishedLinks); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | public Set<PacketLinkRealizedByOptical> getRealizingLinks() { |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 156 | return ImmutableSet.<PacketLinkRealizedByOptical>builder() |
| 157 | .addAll(unestablishedLinks) |
| 158 | .addAll(establishedLinks) |
| 159 | .build(); |
| 160 | } |
| 161 | |
| 162 | public static OpticalConnectivity copyOf(OpticalConnectivity connectivity) { |
| 163 | return new OpticalConnectivity(connectivity); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 164 | } |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 165 | } |