Committed by
Gerrit Code Review
host config operator
Combination operator for host-related configuration information, including tests. Change-Id: I3631db111b7a1140badf1b9f323a3a5811ac7297
Showing
2 changed files
with
160 additions
and
0 deletions
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.host.impl; | ||
17 | + | ||
18 | +import static org.slf4j.LoggerFactory.getLogger; | ||
19 | + | ||
20 | +import org.slf4j.Logger; | ||
21 | +import org.onosproject.incubator.net.config.ConfigOperator; | ||
22 | +import org.onosproject.incubator.net.config.basics.BasicHostConfig; | ||
23 | +import org.onosproject.net.AnnotationKeys; | ||
24 | +import org.onosproject.net.DefaultAnnotations; | ||
25 | +import org.onosproject.net.SparseAnnotations; | ||
26 | +import org.onosproject.net.host.DefaultHostDescription; | ||
27 | +import org.onosproject.net.host.HostDescription; | ||
28 | + | ||
29 | +/** | ||
30 | + * Implementations of merge policies for various sources of host configuration | ||
31 | + * information. This includes applications, provides, and network configurations. | ||
32 | + */ | ||
33 | +public final class BasicHostOperator implements ConfigOperator { | ||
34 | + | ||
35 | + protected static final double DEFAULT_COORD = -1.0; | ||
36 | + private static final Logger log = getLogger(BasicHostOperator.class); | ||
37 | + | ||
38 | + private BasicHostOperator() { | ||
39 | + } | ||
40 | + | ||
41 | + /** | ||
42 | + * Generates a HostDescription containing fields from a HostDescription and | ||
43 | + * a HostConfig. | ||
44 | + * | ||
45 | + * @param cfg the host config entity from network config | ||
46 | + * @param descr a HostDescription | ||
47 | + * @return HostDescription based on both sources | ||
48 | + */ | ||
49 | + public static HostDescription combine(BasicHostConfig cfg, HostDescription descr) { | ||
50 | + if (cfg == null) { | ||
51 | + return descr; | ||
52 | + } | ||
53 | + SparseAnnotations sa = combine(cfg, descr.annotations()); | ||
54 | + return new DefaultHostDescription(descr.hwAddress(), descr.vlan(), descr.location(), | ||
55 | + descr.ipAddress(), sa); | ||
56 | + } | ||
57 | + | ||
58 | + /** | ||
59 | + * Generates an annotation from an existing annotation and HostConfig. | ||
60 | + * | ||
61 | + * @param cfg the device config entity from network config | ||
62 | + * @param an the annotation | ||
63 | + * @return annotation combining both sources | ||
64 | + */ | ||
65 | + public static SparseAnnotations combine(BasicHostConfig cfg, SparseAnnotations an) { | ||
66 | + DefaultAnnotations.Builder newBuilder = DefaultAnnotations.builder(); | ||
67 | + if (cfg.name() != null) { | ||
68 | + newBuilder.set(AnnotationKeys.NAME, cfg.name()); | ||
69 | + } | ||
70 | + if (cfg.latitude() != DEFAULT_COORD) { | ||
71 | + newBuilder.set(AnnotationKeys.LATITUDE, Double.toString(cfg.latitude())); | ||
72 | + } | ||
73 | + if (cfg.longitude() != DEFAULT_COORD) { | ||
74 | + newBuilder.set(AnnotationKeys.LONGITUDE, Double.toString(cfg.longitude())); | ||
75 | + } | ||
76 | + if (cfg.rackAddress() != null) { | ||
77 | + newBuilder.set(AnnotationKeys.RACK_ADDRESS, cfg.rackAddress()); | ||
78 | + } | ||
79 | + if (cfg.owner() != null) { | ||
80 | + newBuilder.set(AnnotationKeys.OWNER, cfg.owner()); | ||
81 | + } | ||
82 | + return DefaultAnnotations.union(an, newBuilder.build()); | ||
83 | + } | ||
84 | +} |
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.host.impl; | ||
17 | + | ||
18 | +import static org.junit.Assert.assertEquals; | ||
19 | + | ||
20 | +import org.junit.Before; | ||
21 | +import org.junit.Test; | ||
22 | +import org.onlab.packet.IpAddress; | ||
23 | +import org.onlab.packet.MacAddress; | ||
24 | +import org.onlab.packet.VlanId; | ||
25 | +import org.onosproject.incubator.net.config.Config; | ||
26 | +import org.onosproject.incubator.net.config.ConfigApplyDelegate; | ||
27 | +import org.onosproject.incubator.net.config.basics.BasicHostConfig; | ||
28 | +import org.onosproject.net.AnnotationKeys; | ||
29 | +import org.onosproject.net.DeviceId; | ||
30 | +import org.onosproject.net.HostId; | ||
31 | +import org.onosproject.net.HostLocation; | ||
32 | +import org.onosproject.net.PortNumber; | ||
33 | +import org.onosproject.net.host.DefaultHostDescription; | ||
34 | +import org.onosproject.net.host.HostDescription; | ||
35 | + | ||
36 | +import com.fasterxml.jackson.databind.ObjectMapper; | ||
37 | +import com.fasterxml.jackson.databind.node.JsonNodeFactory; | ||
38 | + | ||
39 | +public class BasicHostOperatorTest { | ||
40 | + private static final MacAddress MAC = MacAddress.valueOf("00:00:11:00:00:01"); | ||
41 | + private static final VlanId VLAN = VlanId.vlanId((short) 10); | ||
42 | + private static final IpAddress IP = IpAddress.valueOf("10.0.0.1"); | ||
43 | + | ||
44 | + private static final HostId ID = HostId.hostId(MAC); | ||
45 | + private static final HostLocation LOC = new HostLocation( | ||
46 | + DeviceId.deviceId("of:foo"), | ||
47 | + PortNumber.portNumber(100), | ||
48 | + 123L | ||
49 | + ); | ||
50 | + private static final HostDescription HOST = new DefaultHostDescription(MAC, VLAN, LOC, IP); | ||
51 | + | ||
52 | + private final ConfigApplyDelegate delegate = new ConfigApplyDelegate() { | ||
53 | + @Override | ||
54 | + public void onApply(Config config) { | ||
55 | + } | ||
56 | + }; | ||
57 | + private final ObjectMapper mapper = new ObjectMapper(); | ||
58 | + | ||
59 | + private static final BasicHostConfig BHC = new BasicHostConfig(); | ||
60 | + private static final String NAME = "testhost"; | ||
61 | + private static final double LAT = 40.96; | ||
62 | + | ||
63 | + @Before | ||
64 | + public void setUp() { | ||
65 | + BHC.init(ID, "test", JsonNodeFactory.instance.objectNode(), mapper, delegate); | ||
66 | + BHC.name(NAME).latitude(40.96); | ||
67 | + } | ||
68 | + | ||
69 | + @Test | ||
70 | + public void testDescOps() { | ||
71 | + HostDescription desc = BasicHostOperator.combine(BHC, HOST); | ||
72 | + assertEquals(NAME, desc.annotations().value(AnnotationKeys.NAME)); | ||
73 | + assertEquals(null, desc.annotations().value(AnnotationKeys.LONGITUDE)); | ||
74 | + assertEquals(String.valueOf(LAT), desc.annotations().value(AnnotationKeys.LATITUDE)); | ||
75 | + } | ||
76 | +} |
-
Please register or login to post a comment