blob: cd5a2cde2b0746031cf21e579581bc5c9139ce2a [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;
Naoki Shiota5a056062016-05-05 18:43:59 -070026
27import java.time.Duration;
28import java.util.HashSet;
29import java.util.List;
30import java.util.Set;
31
32import static com.google.common.base.Preconditions.checkNotNull;
33
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
45 // Bandwidth capacity of optical layer
46 private Bandwidth opticalCapacity;
47
48 private final Set<PacketLinkRealizedByOptical> realizingLinks = new HashSet<>();
49
Naoki Shiota5a056062016-05-05 18:43:59 -070050 private State state = State.CREATED;
51
52 public enum State {
53 CREATED,
54 INSTALLING,
55 INSTALLED,
56 WITHDRAWING,
57 WITHDRAWN,
58 FAILED
59 }
60
61 public OpticalConnectivity(OpticalConnectivityId id, Path path, Bandwidth requestBandwidth,
62 Duration requestLatency) {
63 this.id = id;
64 this.links = ImmutableList.copyOf(path.links());
65 this.requestBandwidth = requestBandwidth;
66 this.requestLatency = requestLatency;
67 }
68
69 public void setLinkEstablished(ConnectPoint src, ConnectPoint dst) {
70 realizingLinks.stream().filter(l -> l.isBetween(src, dst))
71 .findAny()
72 .ifPresent(l -> l.setEstablished(true));
73 }
74
75 public void setLinkRemoved(ConnectPoint src, ConnectPoint dst) {
76 realizingLinks.stream().filter(l -> l.isBetween(src, dst))
77 .findAny()
78 .ifPresent(l -> l.setEstablished(false));
79 }
80
81 public boolean isAllRealizingLinkEstablished() {
82 return realizingLinks.stream().allMatch(PacketLinkRealizedByOptical::isEstablished);
83 }
84
85 public boolean isAllRealizingLinkNotEstablished() {
86 return !realizingLinks.stream().anyMatch(PacketLinkRealizedByOptical::isEstablished);
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
105 public State state() {
106 return state;
107 }
108
109 public boolean state(State state) {
110 boolean valid = true;
111 // reject invalid state transition
112 switch (this.state) {
113 case CREATED:
114 valid = (state == State.INSTALLING || state == State.FAILED);
115 break;
116 case INSTALLING:
117 valid = (state == State.INSTALLED || state == State.FAILED);
118 break;
119 case INSTALLED:
120 valid = (state == State.WITHDRAWING || state == State.FAILED);
121 break;
122 case WITHDRAWING:
123 valid = (state == State.WITHDRAWN || state == State.FAILED);
124 break;
125 case FAILED:
126 valid = (state == State.INSTALLING || state == State.WITHDRAWING || state == State.FAILED);
127 break;
128 default:
129 break;
130 }
131
132 if (valid) {
133 this.state = state;
134 }
135
136 return valid;
137 }
138
139 public Bandwidth getOpticalCapacity() {
140 return opticalCapacity;
141 }
142
143 public void setOpticalCapacity(Bandwidth opticalCapacity) {
144 this.opticalCapacity = opticalCapacity;
145 }
146
147 public void addRealizingLink(PacketLinkRealizedByOptical link) {
148 checkNotNull(link);
149 realizingLinks.add(link);
150 }
151
152 public void removeRealizingLink(PacketLinkRealizedByOptical link) {
153 checkNotNull(link);
154 realizingLinks.remove(link);
155 }
156
157 public Set<PacketLinkRealizedByOptical> getRealizingLinks() {
158 return ImmutableSet.copyOf(realizingLinks);
159 }
Naoki Shiota5a056062016-05-05 18:43:59 -0700160}