blob: 97b87f0c0de9338546df452eebb18bd693bd3974 [file] [log] [blame]
Ayaka Koshibef1cedf42015-08-05 18:03:56 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Ayaka Koshibef1cedf42015-08-05 18:03:56 -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.host.impl;
17
Thomas Vachuska36008462016-01-07 15:38:20 -080018import org.onlab.packet.IpAddress;
Thomas Vachuska36008462016-01-07 15:38:20 -080019import org.onosproject.net.ConnectPoint;
20import org.onosproject.net.DefaultAnnotations;
21import org.onosproject.net.HostLocation;
22import org.onosproject.net.SparseAnnotations;
Thomas Vachuska4998caa2015-08-26 13:28:38 -070023import org.onosproject.net.config.basics.BasicHostConfig;
Simon Huntbc30e682017-02-15 18:39:23 -080024import org.onosproject.net.device.impl.BasicElementOperator;
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070025import org.onosproject.net.host.DefaultHostDescription;
26import org.onosproject.net.host.HostDescription;
27
Thomas Vachuska36008462016-01-07 15:38:20 -080028import java.util.Set;
29
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070030/**
31 * Implementations of merge policies for various sources of host configuration
Thomas Vachuska36008462016-01-07 15:38:20 -080032 * information. This includes applications, providers, and network configurations.
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070033 */
Simon Huntbc30e682017-02-15 18:39:23 -080034public final class BasicHostOperator extends BasicElementOperator {
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070035
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070036 private BasicHostOperator() {
37 }
38
39 /**
40 * Generates a HostDescription containing fields from a HostDescription and
41 * a HostConfig.
42 *
Thomas Vachuska36008462016-01-07 15:38:20 -080043 * @param cfg the host config entity from network config
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070044 * @param descr a HostDescription
45 * @return HostDescription based on both sources
46 */
Simon Hunt1e20dae2016-10-28 11:26:26 -070047 public static HostDescription combine(BasicHostConfig cfg,
48 HostDescription descr) {
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070049 if (cfg == null) {
50 return descr;
51 }
Thomas Vachuska36008462016-01-07 15:38:20 -080052
53 HostLocation location = descr.location();
54 ConnectPoint cfgLocation = cfg.location();
55 if (cfgLocation != null) {
56 location = new HostLocation(cfgLocation, System.currentTimeMillis());
57 }
58
59 Set<IpAddress> ipAddresses = descr.ipAddress();
60 Set<IpAddress> cfgIpAddresses = cfg.ipAddresses();
61 if (cfgIpAddresses != null) {
62 ipAddresses = cfgIpAddresses;
63 }
64
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070065 SparseAnnotations sa = combine(cfg, descr.annotations());
Thomas Vachuska36008462016-01-07 15:38:20 -080066 return new DefaultHostDescription(descr.hwAddress(), descr.vlan(),
Simon Huntbc30e682017-02-15 18:39:23 -080067 location, ipAddresses,
68 descr.configured(), sa);
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070069 }
70
71 /**
72 * Generates an annotation from an existing annotation and HostConfig.
73 *
Simon Huntbc30e682017-02-15 18:39:23 -080074 * @param cfg the host config entity from network config
Thomas Vachuska36008462016-01-07 15:38:20 -080075 * @param an the annotation
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070076 * @return annotation combining both sources
77 */
Simon Huntbc30e682017-02-15 18:39:23 -080078 public static SparseAnnotations combine(BasicHostConfig cfg, SparseAnnotations an) {
79 DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
80
81 combineElementAnnotations(cfg, builder);
82
83 return DefaultAnnotations.union(an, builder.build());
Ayaka Koshibef1cedf42015-08-05 18:03:56 -070084 }
85}