Avantika-Huawei
Committed by Gerrit Code Review

[ONOS-4166] Store device capapbilities using network config

Change-Id: Ib085c516bec0ecf9845e009eca090aad9302fbb0
......@@ -132,5 +132,10 @@
<artifactId>onlab-misc</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onos-app-pcep-api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
......
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.pcep.api;
import org.onosproject.net.DeviceId;
import org.onosproject.net.config.Config;
/**
* Configuration to specify device capabilities.
*/
public class DeviceCapability extends Config<DeviceId> {
public static final String SRCAP = "srCapabaility";
public static final String LABELSTACKCAP = "labelStackCapability";
public static final String LOCALLABELCAP = "localLabelCapability";
@Override
public boolean isValid() {
return true;
}
/**
* Gets the SR capability of the router.
*
* @return SR capability
*/
public boolean srCap() {
String srCap = get(SRCAP, null);
return srCap != null ?
Boolean.valueOf(srCap) :
false;
}
/**
* Gets the label stack capability of the router.
*
* @return label stack capability
*/
public boolean labelStackCap() {
String labelStackCap = get(LABELSTACKCAP, null);
return labelStackCap != null ?
Boolean.valueOf(labelStackCap) :
false;
}
/**
* Gets the local label capability of the router.
*
* @return local label capability
*/
public boolean localLabelCap() {
String localLabelCap = get(LOCALLABELCAP, null);
return localLabelCap != null ?
Boolean.valueOf(localLabelCap) :
false;
}
/**
* Sets the SR capability of the router.
*
* @param srCap SR capability of the router.
* @return the capability configuration of the device.
*/
public DeviceCapability setSrCap(boolean srCap) {
return (DeviceCapability) setOrClear(SRCAP, srCap);
}
/**
* Sets the label stack capability of the router.
*
* @param labelStackCap label stack capability of the router.
* @return the capability configuration of the device.
*/
public DeviceCapability setLabelStackCap(boolean labelStackCap) {
return (DeviceCapability) setOrClear(LABELSTACKCAP, labelStackCap);
}
/**
* Sets the local label capability of the router.
*
* @param localLabelCap local label capability of the router.
* @return the capability configuration of the device.
*/
public DeviceCapability setLocalLabelCap(boolean localLabelCap) {
return (DeviceCapability) setOrClear(LOCALLABELCAP, localLabelCap);
}
}
......@@ -25,12 +25,12 @@ public interface PcepNodeListener {
*
* @param pc PCEP client details
*/
void addNode(PcepClient pc);
void addDevicePcepConfig(PcepClient pc);
/**
* Notifies that the node was removed.
*
* @param pccId PCEP client ID
*/
void deleteNode(PccId pccId);
void deleteDevicePcepConfig(PccId pccId);
}
......
......@@ -261,7 +261,7 @@ class PcepChannelHandler extends IdleStateAwareChannelHandler {
disconnectDuplicate(h);
} else {
h.setState(ESTABLISHED);
//Session is established, add a PCEP device
//Session is established, add a network configuration with LSR id and device capabilities.
h.addNode();
}
}
......@@ -472,14 +472,14 @@ class PcepChannelHandler extends IdleStateAwareChannelHandler {
}
/**
* Adds PCEP device once session is established.
* Adds PCEP device configuration with capabilities once session is established.
*/
private void addNode() {
pc.addNode(pc);
}
/**
* Deletes PCEP device when session is disconnected.
* Deletes PCEP device configuration when session is disconnected.
*/
private void deleteNode() {
pc.deleteNode(pc.getPccId());
......
......@@ -385,14 +385,14 @@ public class PcepClientControllerImpl implements PcepClientController {
@Override
public void addNode(PcepClient pc) {
for (PcepNodeListener l : pcepNodeListener) {
l.addNode(pc);
l.addDevicePcepConfig(pc);
}
}
@Override
public void deleteNode(PccId pccId) {
for (PcepNodeListener l : pcepNodeListener) {
l.deleteNode(pccId);
l.deleteDevicePcepConfig(pccId);
}
}
......
......@@ -29,6 +29,10 @@ import org.onosproject.net.DefaultAnnotations;
import org.onosproject.net.Device;
import org.onosproject.net.DeviceId;
import org.onosproject.net.Link.Type;
import org.onosproject.net.config.ConfigFactory;
import org.onosproject.net.config.NetworkConfigRegistry;
import org.onosproject.net.config.NetworkConfigService;
import org.onosproject.net.config.basics.SubjectFactories;
import org.onosproject.net.MastershipRole;
import org.onosproject.net.OchPort;
import org.onosproject.net.OduCltPort;
......@@ -54,6 +58,7 @@ import org.onosproject.net.link.LinkProviderService;
import org.onosproject.net.link.LinkService;
import org.onosproject.net.provider.AbstractProvider;
import org.onosproject.net.provider.ProviderId;
import org.onosproject.pcep.api.DeviceCapability;
import org.onosproject.pcep.api.PcepController;
import org.onosproject.pcep.api.PcepDpid;
import org.onosproject.pcep.api.PcepLink;
......@@ -122,29 +127,26 @@ public class PcepTopologyProvider extends AbstractProvider
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected PcepClientController pcepClientController;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected NetworkConfigRegistry netConfigRegistry;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected NetworkConfigService netConfigService;
private DeviceProviderService deviceProviderService;
private LinkProviderService linkProviderService;
private HashMap<Long, List<PortDescription>> portMap = new HashMap<>();
private InternalLinkProvider listener = new InternalLinkProvider();
/*
* For the client supporting SR capability.
*/
public static final String SR_CAPABILITY = "srCapability";
/*
* For the client supporting PCECC capability.
*/
public static final String PCECC_CAPABILITY = "pceccCapability";
/*
* For the client supporting label stack capability.
*/
public static final String LABEL_STACK_CAPABILITY = "labelStackCapability";
public static final String LSRID = "lsrId";
private static final String UNKNOWN = "unknown";
private final ConfigFactory<DeviceId, DeviceCapability> configFactory =
new ConfigFactory<DeviceId, DeviceCapability>(SubjectFactories.DEVICE_SUBJECT_FACTORY,
DeviceCapability.class, "deviceCapability", true) {
@Override
public DeviceCapability createConfig() {
return new DeviceCapability();
}
};
@Activate
public void activate() {
......@@ -153,6 +155,7 @@ public class PcepTopologyProvider extends AbstractProvider
controller.addListener(listener);
controller.addLinkListener(listener);
pcepClientController.addNodeListener(listener);
netConfigRegistry.registerConfigFactory(configFactory);
}
@Deactivate
......@@ -162,6 +165,7 @@ public class PcepTopologyProvider extends AbstractProvider
controller.removeListener(listener);
controller.removeLinkListener(listener);
pcepClientController.removeNodeListener(listener);
netConfigRegistry.unregisterConfigFactory(configFactory);
}
private List<PortDescription> buildPortDescriptions(PcepDpid dpid,
......@@ -334,49 +338,27 @@ public class PcepTopologyProvider extends AbstractProvider
}
@Override
public void addNode(PcepClient pc) {
if (deviceProviderService == null) {
public void addDevicePcepConfig(PcepClient pc) {
if (netConfigRegistry == null) {
log.error("Cannot add PCEP device capability as network config service is not available.");
return;
}
//Right now device URI for PCEP devices is their LSRID
DeviceId deviceId = deviceId(uri(new PcepDpid(pc.getPccId().id().getIp4Address().toInt())));
ChassisId cId = new ChassisId();
Device.Type deviceType = Device.Type.ROUTER;
DefaultAnnotations.Builder annotationBuilder = DefaultAnnotations.builder();
//PCC capabilities (SR, PCECC and PCECC-SR)
annotationBuilder.set(SR_CAPABILITY, String.valueOf(pc.capability().srCapability()));
annotationBuilder.set(PCECC_CAPABILITY, String.valueOf(pc.capability().pceccCapability()));
annotationBuilder.set(LABEL_STACK_CAPABILITY, String.valueOf(pc.capability().labelStackCapability()));
//PccId is the lsrId contained in openMsg, if not present it will be the socket address
annotationBuilder.set(LSRID, String.valueOf(pc.getPccId().id()));
DeviceDescription description = new DefaultDeviceDescription(
deviceId.uri(),
deviceType,
UNKNOWN,
UNKNOWN,
UNKNOWN,
UNKNOWN,
cId,
annotationBuilder.build());
deviceProviderService.deviceConnected(deviceId, description);
DeviceId pccDeviceId = DeviceId.deviceId(String.valueOf(pc.getPccId().ipAddress()));
DeviceCapability deviceCap = netConfigService.addConfig(pccDeviceId, DeviceCapability.class);
deviceCap.setLabelStackCap(pc.capability().labelStackCapability())
.setLocalLabelCap(pc.capability().pceccCapability())
.setSrCap(pc.capability().srCapability())
.apply();
}
@Override
public void deleteNode(PccId pccId) {
if (deviceProviderService == null || deviceService == null) {
return;
}
//TODO: In device manager, in deviceDisconnected() method, get the device but null check is not validated
if (deviceService.getDevice(DeviceId.deviceId(uri(new PcepDpid(pccId.id()
.getIp4Address().toInt())))) == null) {
public void deleteDevicePcepConfig(PccId pccId) {
if (netConfigRegistry == null) {
log.error("Cannot remove PCEP device capability as network config service is not available.");
return;
}
deviceProviderService.deviceDisconnected(deviceId(uri(new PcepDpid(pccId.id().getIp4Address().toInt()))));
DeviceId pccDeviceId = DeviceId.deviceId(String.valueOf(pccId.ipAddress()));
netConfigService.removeConfig(pccDeviceId, DeviceCapability.class);
}
}
......
......@@ -271,14 +271,14 @@ public class PcepClientControllerAdapter implements PcepClientController {
@Override
public void addNode(PcepClient pc) {
for (PcepNodeListener l : pcepNodeListener) {
l.addNode(pc);
l.addDevicePcepConfig(pc);
}
}
@Override
public void deleteNode(PccId pccId) {
for (PcepNodeListener l : pcepNodeListener) {
l.deleteNode(pccId);
l.deleteDevicePcepConfig(pccId);
}
}
......
......@@ -12,13 +12,7 @@
*/
package org.onosproject.provider.pcep.topology.impl;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.onosproject.net.Link.State.ACTIVE;
import static org.onosproject.provider.pcep.topology.impl.PcepTopologyProvider.LABEL_STACK_CAPABILITY;
import static org.onosproject.provider.pcep.topology.impl.PcepTopologyProvider.LSRID;
import static org.onosproject.provider.pcep.topology.impl.PcepTopologyProvider.PCECC_CAPABILITY;
import static org.onosproject.provider.pcep.topology.impl.PcepTopologyProvider.SR_CAPABILITY;
import java.util.Collection;
import java.util.HashMap;
......@@ -39,6 +33,10 @@ import org.onosproject.net.Device;
import org.onosproject.net.DeviceId;
import org.onosproject.net.Link;
import org.onosproject.net.MastershipRole;
import org.onosproject.net.config.Config;
import org.onosproject.net.config.ConfigApplyDelegate;
import org.onosproject.net.config.ConfigFactory;
import org.onosproject.net.config.NetworkConfigRegistryAdapter;
import org.onosproject.net.device.DeviceDescription;
import org.onosproject.net.device.DeviceProvider;
import org.onosproject.net.device.DeviceProviderRegistry;
......@@ -51,11 +49,19 @@ import org.onosproject.net.link.LinkProvider;
import org.onosproject.net.link.LinkProviderRegistry;
import org.onosproject.net.link.LinkProviderService;
import org.onosproject.net.provider.ProviderId;
import org.onosproject.pcep.api.DeviceCapability;
import org.onosproject.pcep.controller.ClientCapability;
import org.onosproject.pcep.controller.PccId;
import org.onosproject.pcep.controller.PcepClient;
import org.onosproject.pcep.controller.PcepNodeListener;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNull.nullValue;
/**
* Test for PCEP topology provider.
*/
......@@ -68,6 +74,7 @@ public class PcepTopologyProviderTest {
private final PcepControllerAdapter controller = new PcepControllerAdapter();
private final MockLinkRegistry linkRegistry = new MockLinkRegistry();
private final MockDeviceService deviceService = new MockDeviceService();
private final MockNetConfigRegistryAdapter netConfigRegistry = new MockNetConfigRegistryAdapter();
private Map<DeviceId, Device> deviceMap = new HashMap<>();
@Before
......@@ -77,6 +84,8 @@ public class PcepTopologyProviderTest {
provider.linkProviderRegistry = linkRegistry;
provider.controller = controller;
provider.deviceService = deviceService;
provider.netConfigRegistry = netConfigRegistry;
provider.netConfigService = netConfigRegistry;
provider.activate();
}
......@@ -88,6 +97,8 @@ public class PcepTopologyProviderTest {
provider.linkProviderRegistry = null;
provider.controller = null;
provider.deviceService = null;
provider.netConfigRegistry = null;
provider.netConfigService = null;
}
/* Class implement device test registry */
......@@ -217,24 +228,104 @@ public class PcepTopologyProviderTest {
}
}
/* Mock test for device service */
private class MockNetConfigRegistryAdapter extends NetworkConfigRegistryAdapter {
private ConfigFactory cfgFactory;
private Map<DeviceId, DeviceCapability> classConfig = new HashMap<>();
@Override
public void registerConfigFactory(ConfigFactory configFactory) {
cfgFactory = configFactory;
}
@Override
public void unregisterConfigFactory(ConfigFactory configFactory) {
cfgFactory = null;
}
@Override
public <S, C extends Config<S>> C addConfig(S subject, Class<C> configClass) {
if (configClass == DeviceCapability.class) {
DeviceCapability devCap = new DeviceCapability();
classConfig.put((DeviceId) subject, devCap);
JsonNode node = new ObjectNode(new MockJsonNode());
ObjectMapper mapper = new ObjectMapper();
ConfigApplyDelegate delegate = new InternalApplyDelegate();
devCap.init((DeviceId) subject, null, node, mapper, delegate);
return (C) devCap;
}
return null;
}
@Override
public <S, C extends Config<S>> void removeConfig(S subject, Class<C> configClass) {
classConfig.remove(subject);
}
@Override
public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) {
if (configClass == DeviceCapability.class) {
return (C) classConfig.get(subject);
}
return null;
}
private class MockJsonNode extends JsonNodeFactory {
}
// Auxiliary delegate to receive notifications about changes applied to
// the network configuration - by the apps.
private class InternalApplyDelegate implements ConfigApplyDelegate {
@Override
public void onApply(Config config) {
//configs.put(config.subject(), config.node());
}
}
}
/**
* Adds the PCEP device and removes it.
* Adds the PCEP device with SR, label stack and local label capabilities and deletes the device.
*/
@Test
public void testPcepTopologyProviderTestAddDevice1() {
PcepClient pc = clientController.getClient(PccId.pccId(IpAddress.valueOf("1.1.1.1")));
for (PcepNodeListener l : clientController.pcepNodeListener) {
pc.setCapability(new ClientCapability(true, true, false, true, true));
l.addNode(pc);
assertThat(nodeRegistry.connected.size(), is(1));
assertThat(deviceMap.keySet().iterator().next(), is(DeviceId.deviceId("l3:1.1.1.1")));
assertThat(deviceMap.values().iterator().next().annotations().value(LABEL_STACK_CAPABILITY), is("true"));
assertThat(deviceMap.values().iterator().next().annotations().value(LSRID), is("1.1.1.1"));
assertThat(deviceMap.values().iterator().next().annotations().value(PCECC_CAPABILITY), is("true"));
assertThat(deviceMap.values().iterator().next().annotations().value(SR_CAPABILITY), is("true"));
l.deleteNode(pc.getPccId());
assertThat(nodeRegistry.connected.size(), is(0));
l.addDevicePcepConfig(pc);
DeviceId pccDeviceId = DeviceId.deviceId(String.valueOf(pc.getPccId().ipAddress()));
DeviceCapability deviceCap = netConfigRegistry.getConfig(pccDeviceId, DeviceCapability.class);
assertThat(deviceCap.srCap(), is(true));
assertThat(deviceCap.labelStackCap(), is(true));
assertThat(deviceCap.localLabelCap(), is(true));
l.deleteDevicePcepConfig(pc.getPccId());
deviceCap = netConfigRegistry.getConfig(pccDeviceId, DeviceCapability.class);
assertThat(deviceCap, is(nullValue()));
}
}
/**
* Adds the PCEP device with SR, and local label capabilities and deletes the device.
*/
@Test
public void testPcepTopologyProviderTestAddDevice2() {
PcepClient pc = clientController.getClient(PccId.pccId(IpAddress.valueOf("1.1.1.1")));
for (PcepNodeListener l : clientController.pcepNodeListener) {
pc.setCapability(new ClientCapability(true, true, false, false, true));
l.addDevicePcepConfig(pc);
DeviceId pccDeviceId = DeviceId.deviceId(String.valueOf(pc.getPccId().ipAddress()));
DeviceCapability deviceCap = netConfigRegistry.getConfig(pccDeviceId, DeviceCapability.class);
assertThat(deviceCap.srCap(), is(true));
assertThat(deviceCap.labelStackCap(), is(false));
assertThat(deviceCap.localLabelCap(), is(true));
l.deleteDevicePcepConfig(pc.getPccId());
deviceCap = netConfigRegistry.getConfig(pccDeviceId, DeviceCapability.class);
assertThat(deviceCap, is(nullValue()));
}
}
}
......
......@@ -275,14 +275,14 @@ public class PcepClientControllerAdapter implements PcepClientController {
@Override
public void addNode(PcepClient pc) {
for (PcepNodeListener l : pcepNodeListener) {
l.addNode(pc);
l.addDevicePcepConfig(pc);
}
}
@Override
public void deleteNode(PccId pccId) {
for (PcepNodeListener l : pcepNodeListener) {
l.deleteNode(pccId);
l.deleteDevicePcepConfig(pccId);
}
}
......