Ayaka Koshibe | cc260d2 | 2015-08-04 17:13:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014-2015 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.net.link.impl; |
| 17 | |
| 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
| 20 | import java.time.Duration; |
| 21 | |
| 22 | import org.onosproject.net.AnnotationKeys; |
Ray Milkey | a412236 | 2015-08-18 15:19:08 -0700 | [diff] [blame] | 23 | import org.onosproject.net.config.ConfigOperator; |
Thomas Vachuska | 4998caa | 2015-08-26 13:28:38 -0700 | [diff] [blame] | 24 | import org.onosproject.net.config.basics.BasicLinkConfig; |
Ayaka Koshibe | cc260d2 | 2015-08-04 17:13:38 -0700 | [diff] [blame] | 25 | import org.onosproject.net.DefaultAnnotations; |
| 26 | import org.onosproject.net.Link; |
| 27 | import org.onosproject.net.SparseAnnotations; |
| 28 | import org.onosproject.net.link.DefaultLinkDescription; |
| 29 | import org.onosproject.net.link.LinkDescription; |
| 30 | import org.slf4j.Logger; |
| 31 | |
| 32 | /** |
| 33 | * Implementations of merge policies for various sources of link configuration |
| 34 | * information. This includes applications, provides, and network configurations. |
| 35 | */ |
Ayaka Koshibe | 5373e76 | 2015-08-06 12:31:44 -0700 | [diff] [blame] | 36 | public final class BasicLinkOperator implements ConfigOperator { |
Ayaka Koshibe | cc260d2 | 2015-08-04 17:13:38 -0700 | [diff] [blame] | 37 | |
Ayaka Koshibe | 5373e76 | 2015-08-06 12:31:44 -0700 | [diff] [blame] | 38 | private static final long DEF_BANDWIDTH = -1L; |
| 39 | private static final Duration DEF_DURATION = Duration.ofNanos(-1L); |
Ayaka Koshibe | cc260d2 | 2015-08-04 17:13:38 -0700 | [diff] [blame] | 40 | private static final Logger log = getLogger(BasicLinkOperator.class); |
| 41 | |
| 42 | private BasicLinkOperator() { |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Generates a LinkDescription containing fields from a LinkDescription and |
| 47 | * a LinkConfig. |
| 48 | * |
| 49 | * @param cfg the link config entity from network config |
| 50 | * @param descr a LinkDescription |
| 51 | * @return LinkDescription based on both sources |
| 52 | */ |
| 53 | public static LinkDescription combine(BasicLinkConfig cfg, LinkDescription descr) { |
| 54 | if (cfg == null) { |
| 55 | return descr; |
| 56 | } |
| 57 | |
| 58 | // cfg.type() defaults to DIRECT, so there is a risk of unwanted override. |
| 59 | // do we want this behavior? |
| 60 | Link.Type type = descr.type(); |
| 61 | if (cfg.type() != type) { |
| 62 | type = cfg.type(); |
| 63 | } |
| 64 | |
| 65 | SparseAnnotations sa = combine(cfg, descr.annotations()); |
| 66 | return new DefaultLinkDescription(descr.src(), descr.dst(), type, sa); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Generates an annotation from an existing annotation and LinkConfig. |
| 71 | * |
| 72 | * @param cfg the link config entity from network config |
| 73 | * @param an the annotation |
| 74 | * @return annotation combining both sources |
| 75 | */ |
| 76 | public static SparseAnnotations combine(BasicLinkConfig cfg, SparseAnnotations an) { |
| 77 | DefaultAnnotations.Builder b = DefaultAnnotations.builder(); |
Ayaka Koshibe | 5373e76 | 2015-08-06 12:31:44 -0700 | [diff] [blame] | 78 | if (cfg.latency() != DEF_DURATION) { |
Ayaka Koshibe | cc260d2 | 2015-08-04 17:13:38 -0700 | [diff] [blame] | 79 | b.set(AnnotationKeys.LATENCY, cfg.latency().toString()); |
| 80 | } |
Ayaka Koshibe | 5373e76 | 2015-08-06 12:31:44 -0700 | [diff] [blame] | 81 | if (cfg.bandwidth() != DEF_BANDWIDTH) { |
Ayaka Koshibe | cc260d2 | 2015-08-04 17:13:38 -0700 | [diff] [blame] | 82 | b.set(AnnotationKeys.BANDWIDTH, String.valueOf(cfg.bandwidth())); |
| 83 | } |
| 84 | return DefaultAnnotations.union(an, b.build()); |
| 85 | } |
| 86 | } |