Add netcfg for INT
Use network config subsystem to handle such configs
Change-Id: I8ec14d31c30e3d1d8cd2c703d8db5535bb8b5cd4
diff --git a/apps/inbandtelemetry/impl/BUILD b/apps/inbandtelemetry/impl/BUILD
index b3a1d46..c3c3fba 100644
--- a/apps/inbandtelemetry/impl/BUILD
+++ b/apps/inbandtelemetry/impl/BUILD
@@ -4,7 +4,7 @@
"//apps/inbandtelemetry/api:onos-apps-inbandtelemetry-api",
]
-TEST_DEPS = TEST_ADAPTERS
+TEST_DEPS = TEST_ADAPTERS + JACKSON
osgi_jar_with_tests(
test_deps = TEST_DEPS,
diff --git a/apps/inbandtelemetry/impl/src/main/java/org/onosproject/inbandtelemetry/impl/SimpleIntManager.java b/apps/inbandtelemetry/impl/src/main/java/org/onosproject/inbandtelemetry/impl/SimpleIntManager.java
index 906efd8..48a7a24 100644
--- a/apps/inbandtelemetry/impl/src/main/java/org/onosproject/inbandtelemetry/impl/SimpleIntManager.java
+++ b/apps/inbandtelemetry/impl/src/main/java/org/onosproject/inbandtelemetry/impl/SimpleIntManager.java
@@ -21,6 +21,7 @@
import org.onlab.util.SharedScheduledExecutors;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.CoreService;
+import org.onosproject.net.behaviour.inbandtelemetry.IntReportConfig;
import org.onosproject.net.behaviour.inbandtelemetry.IntMetadataType;
import org.onosproject.net.behaviour.inbandtelemetry.IntDeviceConfig;
import org.onosproject.inbandtelemetry.api.IntIntent;
@@ -34,6 +35,12 @@
import org.onosproject.net.DeviceId;
import org.onosproject.net.MastershipRole;
import org.onosproject.net.PortNumber;
+import org.onosproject.net.config.ConfigFactory;
+import org.onosproject.net.config.NetworkConfigEvent;
+import org.onosproject.net.config.NetworkConfigListener;
+import org.onosproject.net.config.NetworkConfigRegistry;
+import org.onosproject.net.config.NetworkConfigService;
+import org.onosproject.net.config.basics.SubjectFactories;
import org.onosproject.net.device.DeviceEvent;
import org.onosproject.net.device.DeviceListener;
import org.onosproject.net.device.DeviceService;
@@ -108,6 +115,12 @@
@Reference(cardinality = ReferenceCardinality.MANDATORY)
private HostService hostService;
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ private NetworkConfigService netcfgService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ private NetworkConfigRegistry netcfgRegistry;
+
private final Striped<Lock> deviceLocks = Striped.lock(10);
private final ConcurrentMap<DeviceId, ScheduledFuture<?>> scheduledDeviceTasks = Maps.newConcurrentMap();
@@ -127,6 +140,16 @@
private final InternalIntStartedListener intStartedListener = new InternalIntStartedListener();
private final InternalDeviceToConfigureListener devicesToConfigureListener =
new InternalDeviceToConfigureListener();
+ private final NetworkConfigListener appConfigListener = new IntAppConfigListener();
+
+ private final ConfigFactory<ApplicationId, IntReportConfig> intAppConfigFactory =
+ new ConfigFactory<>(SubjectFactories.APP_SUBJECT_FACTORY,
+ IntReportConfig.class, "report") {
+ @Override
+ public IntReportConfig createConfig() {
+ return new IntReportConfig();
+ }
+ };
@Activate
public void activate() {
@@ -185,6 +208,9 @@
hostService.addListener(hostListener);
deviceService.addListener(deviceListener);
+ netcfgRegistry.registerConfigFactory(intAppConfigFactory);
+ netcfgService.addListener(appConfigListener);
+
startInt();
log.info("Started", appId.id());
}
@@ -524,4 +550,35 @@
}
}
}
+
+ private class IntAppConfigListener implements NetworkConfigListener {
+
+ @Override
+ public void event(NetworkConfigEvent event) {
+ switch (event.type()) {
+ case CONFIG_ADDED:
+ case CONFIG_UPDATED:
+ event.config()
+ .map(config -> (IntReportConfig) config)
+ .ifPresent(config -> {
+ IntDeviceConfig intDeviceConfig = IntDeviceConfig.builder()
+ .withMinFlowHopLatencyChangeNs(config.minFlowHopLatencyChangeNs())
+ .withCollectorPort(config.collectorPort())
+ .withCollectorIp(config.collectorIp())
+ .enabled(true)
+ .build();
+ setConfig(intDeviceConfig);
+ });
+ break;
+ // TODO: Support removing INT config.
+ default:
+ break;
+ }
+ }
+
+ @Override
+ public boolean isRelevant(NetworkConfigEvent event) {
+ return event.configClass() == IntReportConfig.class;
+ }
+ }
}
diff --git a/apps/inbandtelemetry/impl/src/test/java/org/onosproject/inbandtelemetry/impl/SimpleIntManagerTest.java b/apps/inbandtelemetry/impl/src/test/java/org/onosproject/inbandtelemetry/impl/SimpleIntManagerTest.java
new file mode 100644
index 0000000..6f7ed96
--- /dev/null
+++ b/apps/inbandtelemetry/impl/src/test/java/org/onosproject/inbandtelemetry/impl/SimpleIntManagerTest.java
@@ -0,0 +1,132 @@
+/*
+ * Copyright 2020-present Open Networking Foundation
+ *
+ * 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.inbandtelemetry.impl;
+
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.JsonNodeFactory;
+import com.google.common.collect.ImmutableList;
+import org.easymock.Capture;
+import org.easymock.EasyMock;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.TpPort;
+import org.onosproject.TestApplicationId;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.mastership.MastershipService;
+import org.onosproject.net.behaviour.inbandtelemetry.IntReportConfig;
+import org.onosproject.net.behaviour.inbandtelemetry.IntDeviceConfig;
+import org.onosproject.net.config.ConfigApplyDelegate;
+import org.onosproject.net.config.NetworkConfigEvent;
+import org.onosproject.net.config.NetworkConfigListener;
+import org.onosproject.net.config.NetworkConfigRegistry;
+import org.onosproject.net.config.NetworkConfigService;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.host.HostService;
+import org.onosproject.store.service.StorageService;
+import org.onosproject.store.service.TestStorageService;
+
+import static org.easymock.EasyMock.anyObject;
+import static org.easymock.EasyMock.createNiceMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.expectLastCall;
+import static org.easymock.EasyMock.newCapture;
+import static org.easymock.EasyMock.replay;
+import static org.junit.Assert.assertEquals;
+import static org.onlab.junit.TestUtils.setField;
+
+public class SimpleIntManagerTest {
+ private static final String APP_NAME = "org.onosproject.inbandtelemetry";
+ private static final ApplicationId APP_ID = new TestApplicationId(APP_NAME);
+ private static final IpAddress COLLECTOR_IP = IpAddress.valueOf("10.0.0.1");
+ private static final TpPort COLLECTOR_PORT = TpPort.tpPort(5500);
+ private static final int MIN_FLOW_HOP_LATENCY_CHANGE_NS = 16;
+
+ private SimpleIntManager manager;
+ private StorageService storageService;
+ private MastershipService mastershipService;
+ private CoreService coreService;
+ private HostService hostService;
+ private DeviceService deviceService;
+ private NetworkConfigRegistry networkConfigRegistry;
+ private NetworkConfigService networkConfigService;
+ private NetworkConfigListener networkConfigListener;
+
+
+ @Before
+ public void setup() {
+ storageService = new TestStorageService();
+ mastershipService = createNiceMock(MastershipService.class);
+ coreService = createNiceMock(CoreService.class);
+ hostService = createNiceMock(HostService.class);
+ deviceService = createNiceMock(DeviceService.class);
+ expect(deviceService.getDevices()).andReturn(ImmutableList.of()).anyTimes();
+ networkConfigRegistry = createNiceMock(NetworkConfigRegistry.class);
+ networkConfigService = createNiceMock(NetworkConfigService.class);
+
+ manager = new SimpleIntManager();
+ setField(manager, "coreService", coreService);
+ setField(manager, "deviceService", deviceService);
+ setField(manager, "storageService", storageService);
+ setField(manager, "mastershipService", mastershipService);
+ setField(manager, "hostService", hostService);
+ setField(manager, "netcfgService", networkConfigService);
+ setField(manager, "netcfgRegistry", networkConfigRegistry);
+
+ expect(coreService.registerApplication(APP_NAME)).andReturn(APP_ID).once();
+ networkConfigRegistry.registerConfigFactory(anyObject());
+ expectLastCall().once();
+
+ Capture<NetworkConfigListener> capture = newCapture();
+ networkConfigService.addListener(EasyMock.capture(capture));
+ expectLastCall().once();
+ replay(mastershipService, deviceService, coreService,
+ hostService, networkConfigRegistry, networkConfigService);
+ manager.activate();
+ networkConfigListener = capture.getValue();
+ }
+
+ @After
+ public void teardown() {
+ manager.deactivate();
+ }
+
+ @Test
+ public void testPushIntAppConfig() {
+ IntReportConfig config = new IntReportConfig();
+ ObjectMapper mapper = new ObjectMapper();
+ ConfigApplyDelegate delegate = configApply -> { };
+ config.init(APP_ID, "report", JsonNodeFactory.instance.objectNode(), mapper, delegate);
+ config.setCollectorIp(COLLECTOR_IP)
+ .setCollectorPort(COLLECTOR_PORT)
+ .setMinFlowHopLatencyChangeNs(MIN_FLOW_HOP_LATENCY_CHANGE_NS);
+
+ NetworkConfigEvent event =
+ new NetworkConfigEvent(NetworkConfigEvent.Type.CONFIG_ADDED, APP_ID,
+ config, null, IntReportConfig.class);
+ networkConfigListener.event(event);
+
+ // We expected that the manager will store the device config which
+ // converted from the app config.
+ IntDeviceConfig deviceConfig = manager.getConfig();
+ assertEquals(COLLECTOR_IP, deviceConfig.collectorIp());
+ assertEquals(COLLECTOR_PORT, deviceConfig.collectorPort());
+ assertEquals(MIN_FLOW_HOP_LATENCY_CHANGE_NS, deviceConfig.minFlowHopLatencyChangeNs());
+ }
+}
diff --git a/apps/inbandtelemetry/impl/src/test/java/org/onosproject/inbandtelemetry/impl/package-info.java b/apps/inbandtelemetry/impl/src/test/java/org/onosproject/inbandtelemetry/impl/package-info.java
new file mode 100644
index 0000000..c918fea
--- /dev/null
+++ b/apps/inbandtelemetry/impl/src/test/java/org/onosproject/inbandtelemetry/impl/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2020-present Open Networking Foundation
+ *
+ * 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.
+ */
+
+/**
+ * Unit tests for SimpleIntManager class.
+ */
+package org.onosproject.inbandtelemetry.impl;