Ayaka Koshibe | cc260d2 | 2015-08-04 17:13:38 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Ayaka Koshibe | cc260d2 | 2015-08-04 17:13:38 -0700 | [diff] [blame] | 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.net.link.impl; |
| 17 | |
| 18 | import static org.slf4j.LoggerFactory.getLogger; |
Ayaka Koshibe | 2c59acf | 2015-09-08 15:37:47 -0700 | [diff] [blame] | 19 | import static com.google.common.base.Preconditions.checkNotNull; |
Ayaka Koshibe | cc260d2 | 2015-08-04 17:13:38 -0700 | [diff] [blame] | 20 | |
| 21 | import java.time.Duration; |
| 22 | |
| 23 | import org.onosproject.net.AnnotationKeys; |
Ray Milkey | a412236 | 2015-08-18 15:19:08 -0700 | [diff] [blame] | 24 | import org.onosproject.net.config.ConfigOperator; |
Thomas Vachuska | 4998caa | 2015-08-26 13:28:38 -0700 | [diff] [blame] | 25 | import org.onosproject.net.config.basics.BasicLinkConfig; |
Ayaka Koshibe | 2c59acf | 2015-09-08 15:37:47 -0700 | [diff] [blame] | 26 | import org.onosproject.net.ConnectPoint; |
Ayaka Koshibe | cc260d2 | 2015-08-04 17:13:38 -0700 | [diff] [blame] | 27 | import org.onosproject.net.DefaultAnnotations; |
| 28 | import org.onosproject.net.Link; |
| 29 | import org.onosproject.net.SparseAnnotations; |
| 30 | import org.onosproject.net.link.DefaultLinkDescription; |
| 31 | import org.onosproject.net.link.LinkDescription; |
| 32 | import org.slf4j.Logger; |
| 33 | |
| 34 | /** |
| 35 | * Implementations of merge policies for various sources of link configuration |
| 36 | * information. This includes applications, provides, and network configurations. |
| 37 | */ |
Ayaka Koshibe | 5373e76 | 2015-08-06 12:31:44 -0700 | [diff] [blame] | 38 | public final class BasicLinkOperator implements ConfigOperator { |
Ayaka Koshibe | cc260d2 | 2015-08-04 17:13:38 -0700 | [diff] [blame] | 39 | |
Ayaka Koshibe | 5373e76 | 2015-08-06 12:31:44 -0700 | [diff] [blame] | 40 | private static final long DEF_BANDWIDTH = -1L; |
Aaron Dunlap | 239ea5c | 2020-02-26 12:10:31 -0600 | [diff] [blame] | 41 | private static final double DEF_JITTER = -1.0; |
| 42 | private static final double DEF_DELAY = -1.0; |
| 43 | private static final double DEF_LOSS = -1.0; |
| 44 | private static final double DEF_AVAILABILITY = -1.0; |
| 45 | private static final double DEF_FLAPPING = -1.0; |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 46 | private static final double DEF_METRIC = -1; |
David Glantz | de82ada | 2020-03-19 14:43:32 -0500 | [diff] [blame^] | 47 | private static final long DEF_TIER = -1; |
| 48 | private static final double DEF_METERED_USAGE = -1; |
Ayaka Koshibe | 5373e76 | 2015-08-06 12:31:44 -0700 | [diff] [blame] | 49 | private static final Duration DEF_DURATION = Duration.ofNanos(-1L); |
Ayaka Koshibe | cc260d2 | 2015-08-04 17:13:38 -0700 | [diff] [blame] | 50 | private static final Logger log = getLogger(BasicLinkOperator.class); |
| 51 | |
| 52 | private BasicLinkOperator() { |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Generates a LinkDescription containing fields from a LinkDescription and |
| 57 | * a LinkConfig. |
| 58 | * |
| 59 | * @param cfg the link config entity from network config |
| 60 | * @param descr a LinkDescription |
| 61 | * @return LinkDescription based on both sources |
| 62 | */ |
| 63 | public static LinkDescription combine(BasicLinkConfig cfg, LinkDescription descr) { |
| 64 | if (cfg == null) { |
| 65 | return descr; |
| 66 | } |
| 67 | |
Ayaka Koshibe | cc260d2 | 2015-08-04 17:13:38 -0700 | [diff] [blame] | 68 | Link.Type type = descr.type(); |
Yuta HIGUCHI | 3142f64 | 2017-06-07 11:12:45 -0700 | [diff] [blame] | 69 | if (cfg.isTypeConfigured()) { |
| 70 | if (cfg.type() != type) { |
| 71 | type = cfg.type(); |
| 72 | } |
Ayaka Koshibe | cc260d2 | 2015-08-04 17:13:38 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | SparseAnnotations sa = combine(cfg, descr.annotations()); |
| 76 | return new DefaultLinkDescription(descr.src(), descr.dst(), type, sa); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Generates an annotation from an existing annotation and LinkConfig. |
| 81 | * |
| 82 | * @param cfg the link config entity from network config |
| 83 | * @param an the annotation |
| 84 | * @return annotation combining both sources |
| 85 | */ |
| 86 | public static SparseAnnotations combine(BasicLinkConfig cfg, SparseAnnotations an) { |
| 87 | DefaultAnnotations.Builder b = DefaultAnnotations.builder(); |
Yuta HIGUCHI | 9eed0b1 | 2017-06-07 11:59:18 -0700 | [diff] [blame] | 88 | b.putAll(an); |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 89 | if (cfg.metric() != DEF_METRIC) { |
| 90 | b.set(AnnotationKeys.METRIC, String.valueOf(cfg.metric())); |
| 91 | } |
Yuta HIGUCHI | f92598b | 2017-02-17 11:26:17 -0800 | [diff] [blame] | 92 | if (!cfg.latency().equals(DEF_DURATION)) { |
dingdamu | d3a0b21 | 2017-05-16 11:18:27 +0200 | [diff] [blame] | 93 | //Convert the latency from Duration to long, |
| 94 | //so that it's computable in the latencyConstraint. |
| 95 | b.set(AnnotationKeys.LATENCY, String.valueOf(cfg.latency().toNanos())); |
Ayaka Koshibe | cc260d2 | 2015-08-04 17:13:38 -0700 | [diff] [blame] | 96 | } |
Ayaka Koshibe | 5373e76 | 2015-08-06 12:31:44 -0700 | [diff] [blame] | 97 | if (cfg.bandwidth() != DEF_BANDWIDTH) { |
Ayaka Koshibe | cc260d2 | 2015-08-04 17:13:38 -0700 | [diff] [blame] | 98 | b.set(AnnotationKeys.BANDWIDTH, String.valueOf(cfg.bandwidth())); |
| 99 | } |
Ayaka Koshibe | 2c59acf | 2015-09-08 15:37:47 -0700 | [diff] [blame] | 100 | if (cfg.isDurable() != null) { |
| 101 | b.set(AnnotationKeys.DURABLE, String.valueOf(cfg.isDurable())); |
| 102 | } |
Aaron Dunlap | 239ea5c | 2020-02-26 12:10:31 -0600 | [diff] [blame] | 103 | if (cfg.jitter() != DEF_JITTER) { |
| 104 | b.set(AnnotationKeys.JITTER, String.valueOf(cfg.jitter())); |
| 105 | } |
| 106 | if (cfg.delay() != DEF_DELAY) { |
| 107 | b.set(AnnotationKeys.DELAY, String.valueOf(cfg.delay())); |
| 108 | } |
| 109 | if (cfg.loss() != DEF_LOSS) { |
| 110 | b.set(AnnotationKeys.LOSS, String.valueOf(cfg.loss())); |
| 111 | } |
| 112 | if (cfg.availability() != DEF_AVAILABILITY) { |
| 113 | b.set(AnnotationKeys.AVAILABILITY, String.valueOf(cfg.availability())); |
| 114 | } |
| 115 | if (cfg.flapping() != DEF_FLAPPING) { |
| 116 | b.set(AnnotationKeys.FLAPPING, String.valueOf(cfg.flapping())); |
| 117 | } |
David Glantz | 0843f5d | 2020-03-05 21:23:10 -0600 | [diff] [blame] | 118 | if (cfg.isMetered() != null) { |
| 119 | b.set(AnnotationKeys.METERED, String.valueOf(cfg.isMetered())); |
| 120 | } |
David Glantz | de82ada | 2020-03-19 14:43:32 -0500 | [diff] [blame^] | 121 | if (cfg.tier() != DEF_TIER) { |
| 122 | b.set(AnnotationKeys.TIER, String.valueOf(cfg.tier())); |
| 123 | } |
| 124 | if (cfg.meteredUsage() != DEF_METERED_USAGE) { |
| 125 | b.set(AnnotationKeys.METERED_USAGE, String.valueOf(cfg.meteredUsage())); |
| 126 | } |
Yuta HIGUCHI | 9eed0b1 | 2017-06-07 11:59:18 -0700 | [diff] [blame] | 127 | return b.build(); |
Ayaka Koshibe | cc260d2 | 2015-08-04 17:13:38 -0700 | [diff] [blame] | 128 | } |
Ayaka Koshibe | 2c59acf | 2015-09-08 15:37:47 -0700 | [diff] [blame] | 129 | |
| 130 | /** |
| 131 | * Generates a link description from a link description entity. The endpoints |
| 132 | * must be specified to indicate directionality. |
| 133 | * |
| 134 | * @param src the source ConnectPoint |
| 135 | * @param dst the destination ConnectPoint |
| 136 | * @param link the link config entity |
| 137 | * @return a linkDescription based on the config |
| 138 | */ |
| 139 | public static LinkDescription descriptionOf( |
| 140 | ConnectPoint src, ConnectPoint dst, Link link) { |
| 141 | checkNotNull(src, "Must supply a source endpoint"); |
| 142 | checkNotNull(dst, "Must supply a destination endpoint"); |
| 143 | checkNotNull(link, "Must supply a link"); |
| 144 | return new DefaultLinkDescription( |
Yuta HIGUCHI | 0f4d63a | 2017-03-06 14:21:03 -0800 | [diff] [blame] | 145 | src, dst, link.type(), |
| 146 | link.isExpected(), |
| 147 | (SparseAnnotations) link.annotations()); |
Ayaka Koshibe | 2c59acf | 2015-09-08 15:37:47 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Generates a link description from a link config entity. This is for |
| 152 | * links that cannot be discovered and has to be injected. The endpoints |
| 153 | * must be specified to indicate directionality. |
| 154 | * |
| 155 | * @param src the source ConnectPoint |
| 156 | * @param dst the destination ConnectPoint |
| 157 | * @param link the link config entity |
| 158 | * @return a linkDescription based on the config |
| 159 | */ |
| 160 | public static LinkDescription descriptionOf( |
| 161 | ConnectPoint src, ConnectPoint dst, BasicLinkConfig link) { |
| 162 | checkNotNull(src, "Must supply a source endpoint"); |
| 163 | checkNotNull(dst, "Must supply a destination endpoint"); |
| 164 | checkNotNull(link, "Must supply a link config"); |
Yuta HIGUCHI | 0f4d63a | 2017-03-06 14:21:03 -0800 | [diff] [blame] | 165 | // Only allowed link is expected link |
| 166 | boolean expected = link.isAllowed(); |
Ayaka Koshibe | 2c59acf | 2015-09-08 15:37:47 -0700 | [diff] [blame] | 167 | return new DefaultLinkDescription( |
Yuta HIGUCHI | 0f4d63a | 2017-03-06 14:21:03 -0800 | [diff] [blame] | 168 | src, dst, link.type(), |
| 169 | expected, |
| 170 | combine(link, DefaultAnnotations.EMPTY)); |
Ayaka Koshibe | 2c59acf | 2015-09-08 15:37:47 -0700 | [diff] [blame] | 171 | } |
Ayaka Koshibe | cc260d2 | 2015-08-04 17:13:38 -0700 | [diff] [blame] | 172 | } |