Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
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; |
Yuta HIGUCHI | e2689ee | 2017-05-04 16:53:18 -0700 | [diff] [blame] | 23 | import org.slf4j.Logger; |
| 24 | import org.slf4j.LoggerFactory; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 25 | import org.onosproject.net.ConnectPoint; |
| 26 | import org.onosproject.net.Link; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 27 | |
| 28 | import java.time.Duration; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 29 | import java.util.List; |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 30 | import java.util.Optional; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 31 | import java.util.Set; |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 32 | import java.util.stream.Collectors; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 33 | |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 34 | /** |
| 35 | * Entity to store optical connectivity request and related information. |
| 36 | */ |
| 37 | @Beta |
| 38 | public class OpticalConnectivity { |
| 39 | |
Yuta HIGUCHI | e2689ee | 2017-05-04 16:53:18 -0700 | [diff] [blame] | 40 | private static final Logger log = LoggerFactory.getLogger(OpticalConnectivity.class); |
| 41 | |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 42 | private final OpticalConnectivityId id; |
| 43 | private final List<Link> links; |
| 44 | private final Bandwidth requestBandwidth; |
| 45 | private final Duration requestLatency; |
| 46 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 47 | /** |
| 48 | * Set of packet link that is not yet established. |
| 49 | * Packet links in this set are expected to be discovered after underlying (optical) path has been provisioned. |
| 50 | */ |
| 51 | private final ImmutableSet<PacketLinkRealizedByOptical> unestablishedLinks; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 52 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 53 | /** |
| 54 | * Set of packet link that is already established. |
| 55 | */ |
| 56 | private final ImmutableSet<PacketLinkRealizedByOptical> establishedLinks; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 57 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 58 | public OpticalConnectivity(OpticalConnectivityId id, |
| 59 | List<Link> links, |
| 60 | Bandwidth requestBandwidth, |
| 61 | Duration requestLatency, |
| 62 | Set<PacketLinkRealizedByOptical> unestablishedLinks, |
| 63 | Set<PacketLinkRealizedByOptical> establishedLinks) { |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 64 | this.id = id; |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 65 | this.links = ImmutableList.copyOf(links); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 66 | this.requestBandwidth = requestBandwidth; |
| 67 | this.requestLatency = requestLatency; |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 68 | this.unestablishedLinks = ImmutableSet.copyOf(unestablishedLinks); |
| 69 | this.establishedLinks = ImmutableSet.copyOf(establishedLinks); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 72 | private OpticalConnectivity(OpticalConnectivity connectivity) { |
| 73 | this.id = connectivity.id; |
| 74 | this.links = ImmutableList.copyOf(connectivity.links); |
| 75 | this.requestBandwidth = connectivity.requestBandwidth; |
| 76 | this.requestLatency = connectivity.requestLatency; |
| 77 | this.unestablishedLinks = ImmutableSet.copyOf(connectivity.unestablishedLinks); |
| 78 | this.establishedLinks = ImmutableSet.copyOf(connectivity.establishedLinks); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | public boolean isAllRealizingLinkEstablished() { |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 82 | // Check if all links are established |
| 83 | return unestablishedLinks.isEmpty(); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | public boolean isAllRealizingLinkNotEstablished() { |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 87 | // Check if any link is not established |
| 88 | return establishedLinks.isEmpty(); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | public OpticalConnectivityId id() { |
| 92 | return id; |
| 93 | } |
| 94 | |
| 95 | public List<Link> links() { |
| 96 | return links; |
| 97 | } |
| 98 | |
| 99 | public Bandwidth bandwidth() { |
| 100 | return requestBandwidth; |
| 101 | } |
| 102 | |
| 103 | public Duration latency() { |
| 104 | return requestLatency; |
| 105 | } |
| 106 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 107 | public Set<PacketLinkRealizedByOptical> getEstablishedLinks() { |
| 108 | return establishedLinks; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 111 | public Set<PacketLinkRealizedByOptical> getUnestablishedLinks() { |
| 112 | return unestablishedLinks; |
| 113 | } |
| 114 | |
| 115 | public OpticalConnectivity setLinkEstablished(ConnectPoint src, |
| 116 | ConnectPoint dst, |
| 117 | boolean established) { |
| 118 | Set<PacketLinkRealizedByOptical> newEstablishedLinks; |
| 119 | Set<PacketLinkRealizedByOptical> newUnestablishedLinks; |
| 120 | |
| 121 | if (established) { |
| 122 | // move PacketLink from unestablished set to established set |
| 123 | Optional<PacketLinkRealizedByOptical> link = this.unestablishedLinks.stream() |
| 124 | .filter(l -> l.isBetween(src, dst)).findAny(); |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 125 | |
Yuta HIGUCHI | e2689ee | 2017-05-04 16:53:18 -0700 | [diff] [blame] | 126 | if (link.isPresent()) { |
| 127 | |
| 128 | newUnestablishedLinks = this.unestablishedLinks.stream() |
| 129 | .filter(l -> !l.isBetween(src, dst)) |
| 130 | .collect(Collectors.toSet()); |
| 131 | newEstablishedLinks = ImmutableSet.<PacketLinkRealizedByOptical>builder() |
| 132 | .addAll(this.establishedLinks) |
| 133 | .add(link.get()) |
| 134 | .build(); |
| 135 | } else { |
| 136 | // no-op: |
| 137 | newEstablishedLinks = ImmutableSet.copyOf(establishedLinks); |
| 138 | newUnestablishedLinks = ImmutableSet.copyOf(unestablishedLinks); |
| 139 | |
| 140 | // sanity check |
| 141 | boolean alreadyThere = establishedLinks.stream() |
| 142 | .filter(l -> l.isBetween(src, dst)) |
| 143 | .findAny().isPresent(); |
| 144 | if (!alreadyThere) { |
| 145 | log.warn("Attempted to change {}-{} to established, " |
| 146 | + "which is not part of {}", src, dst, this); |
| 147 | } |
| 148 | } |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 149 | } else { |
| 150 | // move PacketLink from established set to unestablished set |
| 151 | Optional<PacketLinkRealizedByOptical> link = this.establishedLinks.stream() |
| 152 | .filter(l -> l.isBetween(src, dst)).findAny(); |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 153 | |
Yuta HIGUCHI | e2689ee | 2017-05-04 16:53:18 -0700 | [diff] [blame] | 154 | if (link.isPresent()) { |
| 155 | newEstablishedLinks = this.establishedLinks.stream() |
| 156 | .filter(l -> !l.isBetween(src, dst)) |
| 157 | .collect(Collectors.toSet()); |
| 158 | newUnestablishedLinks = ImmutableSet.<PacketLinkRealizedByOptical>builder() |
| 159 | .addAll(this.unestablishedLinks) |
| 160 | .add(link.get()) |
| 161 | .build(); |
| 162 | } else { |
| 163 | // no-op: |
| 164 | newEstablishedLinks = ImmutableSet.copyOf(establishedLinks); |
| 165 | newUnestablishedLinks = ImmutableSet.copyOf(unestablishedLinks); |
| 166 | |
| 167 | // sanity check |
| 168 | boolean alreadyThere = unestablishedLinks.stream() |
| 169 | .filter(l -> l.isBetween(src, dst)) |
| 170 | .findAny().isPresent(); |
| 171 | if (!alreadyThere) { |
| 172 | log.warn("Attempted to change {}-{} to unestablished, " |
| 173 | + "which is not part of {}", src, dst, this); |
| 174 | } |
| 175 | } |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 178 | return new OpticalConnectivity(this.id, |
| 179 | this.links, |
| 180 | this.requestBandwidth, |
| 181 | this.requestLatency, |
| 182 | newUnestablishedLinks, |
| 183 | newEstablishedLinks); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | public Set<PacketLinkRealizedByOptical> getRealizingLinks() { |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 187 | return ImmutableSet.<PacketLinkRealizedByOptical>builder() |
| 188 | .addAll(unestablishedLinks) |
| 189 | .addAll(establishedLinks) |
| 190 | .build(); |
| 191 | } |
| 192 | |
| 193 | public static OpticalConnectivity copyOf(OpticalConnectivity connectivity) { |
| 194 | return new OpticalConnectivity(connectivity); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 195 | } |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 196 | } |