Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 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; |
| 23 | import org.onosproject.net.ConnectPoint; |
| 24 | import org.onosproject.net.Link; |
| 25 | import org.onosproject.net.Path; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 26 | |
| 27 | import java.time.Duration; |
| 28 | import java.util.HashSet; |
| 29 | import java.util.List; |
| 30 | import java.util.Set; |
| 31 | |
| 32 | import static com.google.common.base.Preconditions.checkNotNull; |
| 33 | |
| 34 | /** |
| 35 | * Entity to store optical connectivity request and related information. |
| 36 | */ |
| 37 | @Beta |
| 38 | public 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 Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 50 | 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 Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 160 | } |