blob: db127e7f544faa53f42d8460aaf7a27e08dd7bf7 [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;
25import org.onosproject.net.Path;
26import org.onosproject.net.intent.IntentId;
27
28import java.time.Duration;
29import java.util.HashSet;
30import java.util.List;
31import java.util.Set;
32
33import static com.google.common.base.Preconditions.checkNotNull;
34
35/**
36 * Entity to store optical connectivity request and related information.
37 */
38@Beta
39public class OpticalConnectivity {
40
41 private final OpticalConnectivityId id;
42 private final List<Link> links;
43 private final Bandwidth requestBandwidth;
44 private final Duration requestLatency;
45
46 // Bandwidth capacity of optical layer
47 private Bandwidth opticalCapacity;
48
49 private final Set<PacketLinkRealizedByOptical> realizingLinks = new HashSet<>();
50
51 // TODO: This IntentId is used only to reserve bandwidth resource.
52 // After ResourceManager is made to accept app-defined ResourceConsumer,
53 // this Intent should be replaced with OpticalConnectivityId.
54 private IntentId intentId;
55
56 private State state = State.CREATED;
57
58 public enum State {
59 CREATED,
60 INSTALLING,
61 INSTALLED,
62 WITHDRAWING,
63 WITHDRAWN,
64 FAILED
65 }
66
67 public OpticalConnectivity(OpticalConnectivityId id, Path path, Bandwidth requestBandwidth,
68 Duration requestLatency) {
69 this.id = id;
70 this.links = ImmutableList.copyOf(path.links());
71 this.requestBandwidth = requestBandwidth;
72 this.requestLatency = requestLatency;
73 }
74
75 public void setLinkEstablished(ConnectPoint src, ConnectPoint dst) {
76 realizingLinks.stream().filter(l -> l.isBetween(src, dst))
77 .findAny()
78 .ifPresent(l -> l.setEstablished(true));
79 }
80
81 public void setLinkRemoved(ConnectPoint src, ConnectPoint dst) {
82 realizingLinks.stream().filter(l -> l.isBetween(src, dst))
83 .findAny()
84 .ifPresent(l -> l.setEstablished(false));
85 }
86
87 public boolean isAllRealizingLinkEstablished() {
88 return realizingLinks.stream().allMatch(PacketLinkRealizedByOptical::isEstablished);
89 }
90
91 public boolean isAllRealizingLinkNotEstablished() {
92 return !realizingLinks.stream().anyMatch(PacketLinkRealizedByOptical::isEstablished);
93 }
94
95 public OpticalConnectivityId id() {
96 return id;
97 }
98
99 public List<Link> links() {
100 return links;
101 }
102
103 public Bandwidth bandwidth() {
104 return requestBandwidth;
105 }
106
107 public Duration latency() {
108 return requestLatency;
109 }
110
111 public State state() {
112 return state;
113 }
114
115 public boolean state(State state) {
116 boolean valid = true;
117 // reject invalid state transition
118 switch (this.state) {
119 case CREATED:
120 valid = (state == State.INSTALLING || state == State.FAILED);
121 break;
122 case INSTALLING:
123 valid = (state == State.INSTALLED || state == State.FAILED);
124 break;
125 case INSTALLED:
126 valid = (state == State.WITHDRAWING || state == State.FAILED);
127 break;
128 case WITHDRAWING:
129 valid = (state == State.WITHDRAWN || state == State.FAILED);
130 break;
131 case FAILED:
132 valid = (state == State.INSTALLING || state == State.WITHDRAWING || state == State.FAILED);
133 break;
134 default:
135 break;
136 }
137
138 if (valid) {
139 this.state = state;
140 }
141
142 return valid;
143 }
144
145 public Bandwidth getOpticalCapacity() {
146 return opticalCapacity;
147 }
148
149 public void setOpticalCapacity(Bandwidth opticalCapacity) {
150 this.opticalCapacity = opticalCapacity;
151 }
152
153 public void addRealizingLink(PacketLinkRealizedByOptical link) {
154 checkNotNull(link);
155 realizingLinks.add(link);
156 }
157
158 public void removeRealizingLink(PacketLinkRealizedByOptical link) {
159 checkNotNull(link);
160 realizingLinks.remove(link);
161 }
162
163 public Set<PacketLinkRealizedByOptical> getRealizingLinks() {
164 return ImmutableSet.copyOf(realizingLinks);
165 }
166
167 public IntentId getIntentId() {
168 return intentId;
169 }
170
171 public void setIntentId(IntentId intentId) {
172 this.intentId = intentId;
173 }
174}