blob: aeaffe0b1c4bd1ee048cbd15be51251d41d9fc14 [file] [log] [blame]
Naoki Shiota5a056062016-05-05 18:43:59 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Naoki Shiota5a056062016-05-05 18:43:59 -07003 *
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 */
16package org.onosproject.newoptical;
17
18import com.google.common.annotations.Beta;
19import com.google.common.collect.ImmutableList;
20import com.google.common.collect.ImmutableSet;
21import org.onlab.util.Bandwidth;
22import org.onosproject.newoptical.api.OpticalConnectivityId;
Yuta HIGUCHIe2689ee2017-05-04 16:53:18 -070023import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
Naoki Shiota5a056062016-05-05 18:43:59 -070025import org.onosproject.net.ConnectPoint;
26import org.onosproject.net.Link;
Naoki Shiota5a056062016-05-05 18:43:59 -070027
28import java.time.Duration;
Naoki Shiota5a056062016-05-05 18:43:59 -070029import java.util.List;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070030import java.util.Optional;
Naoki Shiota5a056062016-05-05 18:43:59 -070031import java.util.Set;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070032import java.util.stream.Collectors;
Naoki Shiota5a056062016-05-05 18:43:59 -070033
Naoki Shiota5a056062016-05-05 18:43:59 -070034/**
35 * Entity to store optical connectivity request and related information.
36 */
37@Beta
38public class OpticalConnectivity {
39
Yuta HIGUCHIe2689ee2017-05-04 16:53:18 -070040 private static final Logger log = LoggerFactory.getLogger(OpticalConnectivity.class);
41
Naoki Shiota5a056062016-05-05 18:43:59 -070042 private final OpticalConnectivityId id;
43 private final List<Link> links;
44 private final Bandwidth requestBandwidth;
45 private final Duration requestLatency;
46
Naoki Shiota7c3111b2016-06-09 16:12:11 -070047 /**
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 Shiota5a056062016-05-05 18:43:59 -070052
Naoki Shiota7c3111b2016-06-09 16:12:11 -070053 /**
54 * Set of packet link that is already established.
55 */
56 private final ImmutableSet<PacketLinkRealizedByOptical> establishedLinks;
Naoki Shiota5a056062016-05-05 18:43:59 -070057
Naoki Shiota7c3111b2016-06-09 16:12:11 -070058 public OpticalConnectivity(OpticalConnectivityId id,
59 List<Link> links,
60 Bandwidth requestBandwidth,
61 Duration requestLatency,
62 Set<PacketLinkRealizedByOptical> unestablishedLinks,
63 Set<PacketLinkRealizedByOptical> establishedLinks) {
Naoki Shiota5a056062016-05-05 18:43:59 -070064 this.id = id;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070065 this.links = ImmutableList.copyOf(links);
Naoki Shiota5a056062016-05-05 18:43:59 -070066 this.requestBandwidth = requestBandwidth;
67 this.requestLatency = requestLatency;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070068 this.unestablishedLinks = ImmutableSet.copyOf(unestablishedLinks);
69 this.establishedLinks = ImmutableSet.copyOf(establishedLinks);
Naoki Shiota5a056062016-05-05 18:43:59 -070070 }
71
Naoki Shiota7c3111b2016-06-09 16:12:11 -070072 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 Shiota5a056062016-05-05 18:43:59 -070079 }
80
81 public boolean isAllRealizingLinkEstablished() {
Naoki Shiota7c3111b2016-06-09 16:12:11 -070082 // Check if all links are established
83 return unestablishedLinks.isEmpty();
Naoki Shiota5a056062016-05-05 18:43:59 -070084 }
85
86 public boolean isAllRealizingLinkNotEstablished() {
Naoki Shiota7c3111b2016-06-09 16:12:11 -070087 // Check if any link is not established
88 return establishedLinks.isEmpty();
Naoki Shiota5a056062016-05-05 18:43:59 -070089 }
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 Shiota7c3111b2016-06-09 16:12:11 -0700107 public Set<PacketLinkRealizedByOptical> getEstablishedLinks() {
108 return establishedLinks;
Naoki Shiota5a056062016-05-05 18:43:59 -0700109 }
110
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700111 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 Shiota7c3111b2016-06-09 16:12:11 -0700125
Yuta HIGUCHIe2689ee2017-05-04 16:53:18 -0700126 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 Shiota7c3111b2016-06-09 16:12:11 -0700149 } 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 Shiota7c3111b2016-06-09 16:12:11 -0700153
Yuta HIGUCHIe2689ee2017-05-04 16:53:18 -0700154 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 Shiota5a056062016-05-05 18:43:59 -0700176 }
177
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700178 return new OpticalConnectivity(this.id,
179 this.links,
180 this.requestBandwidth,
181 this.requestLatency,
182 newUnestablishedLinks,
183 newEstablishedLinks);
Naoki Shiota5a056062016-05-05 18:43:59 -0700184 }
185
186 public Set<PacketLinkRealizedByOptical> getRealizingLinks() {
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700187 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 Shiota5a056062016-05-05 18:43:59 -0700195 }
Naoki Shiota5a056062016-05-05 18:43:59 -0700196}