blob: 05943e51253608eb3f633014db35b7155fabc423 [file] [log] [blame]
Ayaka Koshibecc260d22015-08-04 17:13:38 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Ayaka Koshibecc260d22015-08-04 17:13:38 -07003 *
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.net.link.impl;
17
18import static org.slf4j.LoggerFactory.getLogger;
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -070019import static com.google.common.base.Preconditions.checkNotNull;
Ayaka Koshibecc260d22015-08-04 17:13:38 -070020
21import java.time.Duration;
22
23import org.onosproject.net.AnnotationKeys;
Ray Milkeya4122362015-08-18 15:19:08 -070024import org.onosproject.net.config.ConfigOperator;
Thomas Vachuska4998caa2015-08-26 13:28:38 -070025import org.onosproject.net.config.basics.BasicLinkConfig;
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -070026import org.onosproject.net.ConnectPoint;
Ayaka Koshibecc260d22015-08-04 17:13:38 -070027import org.onosproject.net.DefaultAnnotations;
28import org.onosproject.net.Link;
29import org.onosproject.net.SparseAnnotations;
30import org.onosproject.net.link.DefaultLinkDescription;
31import org.onosproject.net.link.LinkDescription;
32import 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 Koshibe5373e762015-08-06 12:31:44 -070038public final class BasicLinkOperator implements ConfigOperator {
Ayaka Koshibecc260d22015-08-04 17:13:38 -070039
Ayaka Koshibe5373e762015-08-06 12:31:44 -070040 private static final long DEF_BANDWIDTH = -1L;
Aaron Dunlap239ea5c2020-02-26 12:10:31 -060041 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 Vachuska41fe1ec2015-12-03 23:17:02 -080046 private static final double DEF_METRIC = -1;
Ayaka Koshibe5373e762015-08-06 12:31:44 -070047 private static final Duration DEF_DURATION = Duration.ofNanos(-1L);
Ayaka Koshibecc260d22015-08-04 17:13:38 -070048 private static final Logger log = getLogger(BasicLinkOperator.class);
49
50 private BasicLinkOperator() {
51 }
52
53 /**
54 * Generates a LinkDescription containing fields from a LinkDescription and
55 * a LinkConfig.
56 *
57 * @param cfg the link config entity from network config
58 * @param descr a LinkDescription
59 * @return LinkDescription based on both sources
60 */
61 public static LinkDescription combine(BasicLinkConfig cfg, LinkDescription descr) {
62 if (cfg == null) {
63 return descr;
64 }
65
Ayaka Koshibecc260d22015-08-04 17:13:38 -070066 Link.Type type = descr.type();
Yuta HIGUCHI3142f642017-06-07 11:12:45 -070067 if (cfg.isTypeConfigured()) {
68 if (cfg.type() != type) {
69 type = cfg.type();
70 }
Ayaka Koshibecc260d22015-08-04 17:13:38 -070071 }
72
73 SparseAnnotations sa = combine(cfg, descr.annotations());
74 return new DefaultLinkDescription(descr.src(), descr.dst(), type, sa);
75 }
76
77 /**
78 * Generates an annotation from an existing annotation and LinkConfig.
79 *
80 * @param cfg the link config entity from network config
81 * @param an the annotation
82 * @return annotation combining both sources
83 */
84 public static SparseAnnotations combine(BasicLinkConfig cfg, SparseAnnotations an) {
85 DefaultAnnotations.Builder b = DefaultAnnotations.builder();
Yuta HIGUCHI9eed0b12017-06-07 11:59:18 -070086 b.putAll(an);
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080087 if (cfg.metric() != DEF_METRIC) {
88 b.set(AnnotationKeys.METRIC, String.valueOf(cfg.metric()));
89 }
Yuta HIGUCHIf92598b2017-02-17 11:26:17 -080090 if (!cfg.latency().equals(DEF_DURATION)) {
dingdamud3a0b212017-05-16 11:18:27 +020091 //Convert the latency from Duration to long,
92 //so that it's computable in the latencyConstraint.
93 b.set(AnnotationKeys.LATENCY, String.valueOf(cfg.latency().toNanos()));
Ayaka Koshibecc260d22015-08-04 17:13:38 -070094 }
Ayaka Koshibe5373e762015-08-06 12:31:44 -070095 if (cfg.bandwidth() != DEF_BANDWIDTH) {
Ayaka Koshibecc260d22015-08-04 17:13:38 -070096 b.set(AnnotationKeys.BANDWIDTH, String.valueOf(cfg.bandwidth()));
97 }
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -070098 if (cfg.isDurable() != null) {
99 b.set(AnnotationKeys.DURABLE, String.valueOf(cfg.isDurable()));
100 }
Aaron Dunlap239ea5c2020-02-26 12:10:31 -0600101 if (cfg.jitter() != DEF_JITTER) {
102 b.set(AnnotationKeys.JITTER, String.valueOf(cfg.jitter()));
103 }
104 if (cfg.delay() != DEF_DELAY) {
105 b.set(AnnotationKeys.DELAY, String.valueOf(cfg.delay()));
106 }
107 if (cfg.loss() != DEF_LOSS) {
108 b.set(AnnotationKeys.LOSS, String.valueOf(cfg.loss()));
109 }
110 if (cfg.availability() != DEF_AVAILABILITY) {
111 b.set(AnnotationKeys.AVAILABILITY, String.valueOf(cfg.availability()));
112 }
113 if (cfg.flapping() != DEF_FLAPPING) {
114 b.set(AnnotationKeys.FLAPPING, String.valueOf(cfg.flapping()));
115 }
David Glantz0843f5d2020-03-05 21:23:10 -0600116 if (cfg.isMetered() != null) {
117 b.set(AnnotationKeys.METERED, String.valueOf(cfg.isMetered()));
118 }
Yuta HIGUCHI9eed0b12017-06-07 11:59:18 -0700119 return b.build();
Ayaka Koshibecc260d22015-08-04 17:13:38 -0700120 }
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -0700121
122 /**
123 * Generates a link description from a link description entity. The endpoints
124 * must be specified to indicate directionality.
125 *
126 * @param src the source ConnectPoint
127 * @param dst the destination ConnectPoint
128 * @param link the link config entity
129 * @return a linkDescription based on the config
130 */
131 public static LinkDescription descriptionOf(
132 ConnectPoint src, ConnectPoint dst, Link link) {
133 checkNotNull(src, "Must supply a source endpoint");
134 checkNotNull(dst, "Must supply a destination endpoint");
135 checkNotNull(link, "Must supply a link");
136 return new DefaultLinkDescription(
Yuta HIGUCHI0f4d63a2017-03-06 14:21:03 -0800137 src, dst, link.type(),
138 link.isExpected(),
139 (SparseAnnotations) link.annotations());
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -0700140 }
141
142 /**
143 * Generates a link description from a link config entity. This is for
144 * links that cannot be discovered and has to be injected. The endpoints
145 * must be specified to indicate directionality.
146 *
147 * @param src the source ConnectPoint
148 * @param dst the destination ConnectPoint
149 * @param link the link config entity
150 * @return a linkDescription based on the config
151 */
152 public static LinkDescription descriptionOf(
153 ConnectPoint src, ConnectPoint dst, BasicLinkConfig link) {
154 checkNotNull(src, "Must supply a source endpoint");
155 checkNotNull(dst, "Must supply a destination endpoint");
156 checkNotNull(link, "Must supply a link config");
Yuta HIGUCHI0f4d63a2017-03-06 14:21:03 -0800157 // Only allowed link is expected link
158 boolean expected = link.isAllowed();
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -0700159 return new DefaultLinkDescription(
Yuta HIGUCHI0f4d63a2017-03-06 14:21:03 -0800160 src, dst, link.type(),
161 expected,
162 combine(link, DefaultAnnotations.EMPTY));
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -0700163 }
Ayaka Koshibecc260d22015-08-04 17:13:38 -0700164}