Committed by
Gerrit Code Review
device config operator
Combination operator for device-related configuration information, including tests. Change-Id: I0320f7b568979063947a37cf2749daae7a1a05f1
Showing
3 changed files
with
187 additions
and
2 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.device.impl; | ||
| 17 | + | ||
| 18 | +import static org.slf4j.LoggerFactory.getLogger; | ||
| 19 | + | ||
| 20 | +import org.onosproject.incubator.net.config.basics.BasicDeviceConfig; | ||
| 21 | +import org.onosproject.net.AnnotationKeys; | ||
| 22 | +import org.onosproject.net.DefaultAnnotations; | ||
| 23 | +import org.onosproject.net.Device; | ||
| 24 | +import org.onosproject.net.SparseAnnotations; | ||
| 25 | +import org.onosproject.net.device.DefaultDeviceDescription; | ||
| 26 | +import org.onosproject.net.device.DeviceDescription; | ||
| 27 | +import org.slf4j.Logger; | ||
| 28 | + | ||
| 29 | +/** | ||
| 30 | + * Implementations of merge policies for various sources of device configuration | ||
| 31 | + * information. This includes applications, provides, and network configurations. | ||
| 32 | + */ | ||
| 33 | +public final class BasicDeviceOperator extends BasicDeviceConfig { | ||
| 34 | + | ||
| 35 | + private static final Logger log = getLogger(BasicDeviceOperator.class); | ||
| 36 | + | ||
| 37 | + private BasicDeviceOperator() { | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + /** | ||
| 41 | + * Generates a DeviceDescription containing fields from a DeviceDescription and | ||
| 42 | + * a DeviceConfig. | ||
| 43 | + * | ||
| 44 | + * @param bdc the device config entity from network config | ||
| 45 | + * @param descr a DeviceDescription | ||
| 46 | + * @return DeviceDescription based on both sources | ||
| 47 | + */ | ||
| 48 | + public static DeviceDescription combine(BasicDeviceConfig bdc, DeviceDescription descr) { | ||
| 49 | + if (bdc == null) { | ||
| 50 | + return descr; | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + Device.Type type = descr.type(); | ||
| 54 | + if (bdc.type() != null && bdc.type() != type) { | ||
| 55 | + type = bdc.type(); | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | + SparseAnnotations sa = combine(bdc, descr.annotations()); | ||
| 59 | + return new DefaultDeviceDescription(descr.deviceURI(), type, descr.manufacturer(), | ||
| 60 | + descr.hwVersion(), descr.swVersion(), | ||
| 61 | + descr.serialNumber(), descr.chassisId(), sa); | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + /** | ||
| 65 | + * Generates an annotation from an existing annotation and DeviceConfig. | ||
| 66 | + * | ||
| 67 | + * @param bdc the device config entity from network config | ||
| 68 | + * @param an the annotation | ||
| 69 | + * @return annotation combining both sources | ||
| 70 | + */ | ||
| 71 | + public static SparseAnnotations combine(BasicDeviceConfig bdc, SparseAnnotations an) { | ||
| 72 | + DefaultAnnotations.Builder newBuilder = DefaultAnnotations.builder(); | ||
| 73 | + if (bdc.driver() != an.value(DRIVER)) { | ||
| 74 | + newBuilder.set(AnnotationKeys.DRIVER, bdc.driver()); | ||
| 75 | + } | ||
| 76 | + if (bdc.name() != null) { | ||
| 77 | + newBuilder.set(AnnotationKeys.NAME, bdc.name()); | ||
| 78 | + } | ||
| 79 | + if (bdc.latitude() != DEFAULT_COORD) { | ||
| 80 | + newBuilder.set(AnnotationKeys.LATITUDE, Double.toString(bdc.latitude())); | ||
| 81 | + } | ||
| 82 | + if (bdc.longitude() != DEFAULT_COORD) { | ||
| 83 | + newBuilder.set(AnnotationKeys.LONGITUDE, Double.toString(bdc.longitude())); | ||
| 84 | + } | ||
| 85 | + if (bdc.rackAddress() != null) { | ||
| 86 | + newBuilder.set(RACK_ADDRESS, bdc.rackAddress()); | ||
| 87 | + } | ||
| 88 | + if (bdc.owner() != null) { | ||
| 89 | + newBuilder.set(OWNER, bdc.owner()); | ||
| 90 | + } | ||
| 91 | + DefaultAnnotations newAnnotations = newBuilder.build(); | ||
| 92 | + return DefaultAnnotations.union(an, newAnnotations); | ||
| 93 | + } | ||
| 94 | +} |
| 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.device.impl; | ||
| 17 | + | ||
| 18 | +import static org.onosproject.net.Device.Type.SWITCH; | ||
| 19 | +import static org.onosproject.net.Device.Type.ROADM; | ||
| 20 | +import static org.junit.Assert.assertEquals; | ||
| 21 | + | ||
| 22 | +import java.net.URI; | ||
| 23 | + | ||
| 24 | +import org.junit.Before; | ||
| 25 | +import org.junit.Test; | ||
| 26 | +import org.onlab.packet.ChassisId; | ||
| 27 | +import org.onosproject.incubator.net.config.Config; | ||
| 28 | +import org.onosproject.incubator.net.config.ConfigApplyDelegate; | ||
| 29 | +import org.onosproject.incubator.net.config.basics.BasicDeviceConfig; | ||
| 30 | +import org.onosproject.net.AnnotationKeys; | ||
| 31 | +import org.onosproject.net.DefaultAnnotations; | ||
| 32 | +import org.onosproject.net.DeviceId; | ||
| 33 | +import org.onosproject.net.SparseAnnotations; | ||
| 34 | +import org.onosproject.net.device.DefaultDeviceDescription; | ||
| 35 | +import org.onosproject.net.device.DeviceDescription; | ||
| 36 | + | ||
| 37 | +import com.fasterxml.jackson.databind.ObjectMapper; | ||
| 38 | +import com.fasterxml.jackson.databind.node.JsonNodeFactory; | ||
| 39 | + | ||
| 40 | +public class BasicDeviceOperatorTest { | ||
| 41 | + | ||
| 42 | + private static final String NAME1 = "of:foo"; | ||
| 43 | + private static final String NAME2 = "of:bar"; | ||
| 44 | + private static final String OWNER = "somebody"; | ||
| 45 | + private static final URI DURI = URI.create(NAME1); | ||
| 46 | + private static final String MFR = "whitebox"; | ||
| 47 | + private static final String HW = "1.1.x"; | ||
| 48 | + private static final String SW = "3.9.1"; | ||
| 49 | + private static final String SN = "43311-12345"; | ||
| 50 | + private static final ChassisId CID = new ChassisId(); | ||
| 51 | + | ||
| 52 | + private static final SparseAnnotations SA = DefaultAnnotations.builder() | ||
| 53 | + .set(AnnotationKeys.DRIVER, NAME2).build(); | ||
| 54 | + | ||
| 55 | + private static final DeviceDescription DEV1 = new DefaultDeviceDescription( | ||
| 56 | + DURI, SWITCH, MFR, HW, SW, SN, CID, SA); | ||
| 57 | + | ||
| 58 | + private final ConfigApplyDelegate delegate = new ConfigApplyDelegate() { | ||
| 59 | + @Override | ||
| 60 | + public void onApply(Config config) { | ||
| 61 | + } | ||
| 62 | + }; | ||
| 63 | + private final ObjectMapper mapper = new ObjectMapper(); | ||
| 64 | + | ||
| 65 | + private static final BasicDeviceConfig SW_BDC = new BasicDeviceConfig(); | ||
| 66 | + private static final BasicDeviceConfig RD_BDC = new BasicDeviceConfig(); | ||
| 67 | + | ||
| 68 | + @Before | ||
| 69 | + public void setUp() { | ||
| 70 | + SW_BDC.init(DeviceId.deviceId(NAME1), NAME1, JsonNodeFactory.instance.objectNode(), mapper, delegate); | ||
| 71 | + SW_BDC.type(SWITCH).driver(NAME1).owner(OWNER); | ||
| 72 | + RD_BDC.init(DeviceId.deviceId(NAME2), NAME2, JsonNodeFactory.instance.objectNode(), mapper, delegate); | ||
| 73 | + RD_BDC.type(ROADM); | ||
| 74 | + } | ||
| 75 | + | ||
| 76 | + @Test | ||
| 77 | + public void testDescOps() { | ||
| 78 | + DeviceDescription desc = BasicDeviceOperator.combine(null, DEV1); | ||
| 79 | + assertEquals(desc, DEV1); | ||
| 80 | + | ||
| 81 | + // override driver name | ||
| 82 | + desc = BasicDeviceOperator.combine(SW_BDC, DEV1); | ||
| 83 | + assertEquals(NAME1, desc.annotations().value(AnnotationKeys.DRIVER)); | ||
| 84 | + | ||
| 85 | + // override Device Type | ||
| 86 | + desc = BasicDeviceOperator.combine(RD_BDC, DEV1); | ||
| 87 | + assertEquals(ROADM, desc.type()); | ||
| 88 | + } | ||
| 89 | +} |
| ... | @@ -30,6 +30,8 @@ public abstract class BasicElementConfig<S> extends AllowedEntityConfig<S> { | ... | @@ -30,6 +30,8 @@ public abstract class BasicElementConfig<S> extends AllowedEntityConfig<S> { |
| 30 | public static final String RACK_ADDRESS = "rackAddress"; | 30 | public static final String RACK_ADDRESS = "rackAddress"; |
| 31 | public static final String OWNER = "owner"; | 31 | public static final String OWNER = "owner"; |
| 32 | 32 | ||
| 33 | + protected static final double DEFAULT_COORD = -1.0; | ||
| 34 | + | ||
| 33 | /** | 35 | /** |
| 34 | * Returns friendly label for the element. | 36 | * Returns friendly label for the element. |
| 35 | * | 37 | * |
| ... | @@ -55,7 +57,7 @@ public abstract class BasicElementConfig<S> extends AllowedEntityConfig<S> { | ... | @@ -55,7 +57,7 @@ public abstract class BasicElementConfig<S> extends AllowedEntityConfig<S> { |
| 55 | * @return element latitude; -1 if not set | 57 | * @return element latitude; -1 if not set |
| 56 | */ | 58 | */ |
| 57 | public double latitude() { | 59 | public double latitude() { |
| 58 | - return get(LATITUDE, -1.0); | 60 | + return get(LATITUDE, DEFAULT_COORD); |
| 59 | } | 61 | } |
| 60 | 62 | ||
| 61 | /** | 63 | /** |
| ... | @@ -74,7 +76,7 @@ public abstract class BasicElementConfig<S> extends AllowedEntityConfig<S> { | ... | @@ -74,7 +76,7 @@ public abstract class BasicElementConfig<S> extends AllowedEntityConfig<S> { |
| 74 | * @return element latitude; -1 if not set | 76 | * @return element latitude; -1 if not set |
| 75 | */ | 77 | */ |
| 76 | public double longitude() { | 78 | public double longitude() { |
| 77 | - return get(LONGITUDE, -1.0); | 79 | + return get(LONGITUDE, DEFAULT_COORD); |
| 78 | } | 80 | } |
| 79 | 81 | ||
| 80 | /** | 82 | /** | ... | ... |
-
Please register or login to post a comment