blob: b67564cafe6aa1d6cd387693591e5458261cad39 [file] [log] [blame]
Naoki Shiota5a056062016-05-05 18:43:59 -07001/*
2 * Copyright 2016 Open Networking Laboratory
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 */
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;
23import org.onosproject.net.ConnectPoint;
24import org.onosproject.net.Link;
Naoki Shiota5a056062016-05-05 18:43:59 -070025
26import java.time.Duration;
Naoki Shiota5a056062016-05-05 18:43:59 -070027import java.util.List;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070028import java.util.Optional;
Naoki Shiota5a056062016-05-05 18:43:59 -070029import java.util.Set;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070030import java.util.stream.Collectors;
Naoki Shiota5a056062016-05-05 18:43:59 -070031
Naoki Shiota7c3111b2016-06-09 16:12:11 -070032import static com.google.common.base.Preconditions.checkState;
Naoki Shiota5a056062016-05-05 18:43:59 -070033
34/**
35 * Entity to store optical connectivity request and related information.
36 */
37@Beta
38public 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 Shiota7c3111b2016-06-09 16:12:11 -070045 /**
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 Shiota5a056062016-05-05 18:43:59 -070050
Naoki Shiota7c3111b2016-06-09 16:12:11 -070051 /**
52 * Set of packet link that is already established.
53 */
54 private final ImmutableSet<PacketLinkRealizedByOptical> establishedLinks;
Naoki Shiota5a056062016-05-05 18:43:59 -070055
Naoki Shiota7c3111b2016-06-09 16:12:11 -070056 public OpticalConnectivity(OpticalConnectivityId id,
57 List<Link> links,
58 Bandwidth requestBandwidth,
59 Duration requestLatency,
60 Set<PacketLinkRealizedByOptical> unestablishedLinks,
61 Set<PacketLinkRealizedByOptical> establishedLinks) {
Naoki Shiota5a056062016-05-05 18:43:59 -070062 this.id = id;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070063 this.links = ImmutableList.copyOf(links);
Naoki Shiota5a056062016-05-05 18:43:59 -070064 this.requestBandwidth = requestBandwidth;
65 this.requestLatency = requestLatency;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070066 this.unestablishedLinks = ImmutableSet.copyOf(unestablishedLinks);
67 this.establishedLinks = ImmutableSet.copyOf(establishedLinks);
Naoki Shiota5a056062016-05-05 18:43:59 -070068 }
69
Naoki Shiota7c3111b2016-06-09 16:12:11 -070070 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 Shiota5a056062016-05-05 18:43:59 -070077 }
78
79 public boolean isAllRealizingLinkEstablished() {
Naoki Shiota7c3111b2016-06-09 16:12:11 -070080 // Check if all links are established
81 return unestablishedLinks.isEmpty();
Naoki Shiota5a056062016-05-05 18:43:59 -070082 }
83
84 public boolean isAllRealizingLinkNotEstablished() {
Naoki Shiota7c3111b2016-06-09 16:12:11 -070085 // Check if any link is not established
86 return establishedLinks.isEmpty();
Naoki Shiota5a056062016-05-05 18:43:59 -070087 }
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 Shiota7c3111b2016-06-09 16:12:11 -0700105 public Set<PacketLinkRealizedByOptical> getEstablishedLinks() {
106 return establishedLinks;
Naoki Shiota5a056062016-05-05 18:43:59 -0700107 }
108
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700109 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 Shiota5a056062016-05-05 18:43:59 -0700145 }
146
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700147 return new OpticalConnectivity(this.id,
148 this.links,
149 this.requestBandwidth,
150 this.requestLatency,
151 newUnestablishedLinks,
152 newEstablishedLinks);
Naoki Shiota5a056062016-05-05 18:43:59 -0700153 }
154
155 public Set<PacketLinkRealizedByOptical> getRealizingLinks() {
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700156 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 Shiota5a056062016-05-05 18:43:59 -0700164 }
Naoki Shiota5a056062016-05-05 18:43:59 -0700165}