Rename L2 load balancer to port load balancer
CLI commands are also renamed to plbs, plb-add and plb-remove
Change-Id: I4c26b390acc5a961594a1dca92a76bee2148c090
diff --git a/apps/portloadbalancer/BUILD b/apps/portloadbalancer/BUILD
new file mode 100644
index 0000000..3b5301e
--- /dev/null
+++ b/apps/portloadbalancer/BUILD
@@ -0,0 +1,19 @@
+COMPILE_DEPS = CORE_DEPS + KRYO + CLI + [
+ "//core/store/serializers:onos-core-serializers",
+]
+
+TEST_DEPS = TEST_ADAPTERS
+
+osgi_jar_with_tests(
+ karaf_command_packages = ["org.onosproject.portloadbalancer.cli"],
+ test_deps = TEST_DEPS,
+ deps = COMPILE_DEPS,
+)
+
+onos_app(
+ app_name = "org.onosproject.portloadbalancer",
+ category = "Utilities",
+ description = "Port Load Balance Service",
+ title = "Port Load Balance Service",
+ url = "http://onosproject.org",
+)
diff --git a/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/api/PortLoadBalancer.java b/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/api/PortLoadBalancer.java
new file mode 100644
index 0000000..6cd8df1
--- /dev/null
+++ b/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/api/PortLoadBalancer.java
@@ -0,0 +1,110 @@
+/*
+ * Copyright 2018-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.portloadbalancer.api;
+
+import org.onosproject.net.PortNumber;
+
+import java.util.Objects;
+import java.util.Set;
+
+import static com.google.common.base.MoreObjects.toStringHelper;
+
+/**
+ * Represents port load balancer information.
+ */
+public class PortLoadBalancer {
+ private PortLoadBalancerId portLoadBalancerId;
+ private Set<PortNumber> ports;
+ private PortLoadBalancerMode mode;
+
+ /**
+ * Constructs a port load balancer.
+ *
+ * @param portLoadBalancerId port load balancer ID
+ * @param ports Set of member ports
+ * @param mode port load balancer mode
+ */
+ public PortLoadBalancer(PortLoadBalancerId portLoadBalancerId, Set<PortNumber> ports, PortLoadBalancerMode mode) {
+ this.portLoadBalancerId = portLoadBalancerId;
+ this.ports = ports;
+ this.mode = mode;
+ }
+
+ /**
+ * Gets port load balancer ID.
+ *
+ * @return port load balancer ID
+ */
+ public PortLoadBalancerId portLoadBalancerId() {
+ return portLoadBalancerId;
+ }
+
+ /**
+ * Gets set of member ports.
+ *
+ * @return Set of member ports
+ */
+ public Set<PortNumber> ports() {
+ return ports;
+ }
+
+ /**
+ * Gets port load balancer mode.
+ *
+ * @return port load balancer mode.
+ */
+ public PortLoadBalancerMode mode() {
+ return mode;
+ }
+
+ /**
+ * Gets port load balancer data.
+ *
+ * @return port load balancer data
+ */
+ public PortLoadBalancerData data() {
+ return new PortLoadBalancerData(portLoadBalancerId);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(portLoadBalancerId, ports, mode);
+ }
+
+ @Override
+ public boolean equals(final Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (!(obj instanceof PortLoadBalancer)) {
+ return false;
+ }
+ final PortLoadBalancer other = (PortLoadBalancer) obj;
+
+ return Objects.equals(this.portLoadBalancerId, other.portLoadBalancerId) &&
+ Objects.equals(this.ports, other.ports) &&
+ this.mode == other.mode;
+ }
+
+ @Override
+ public String toString() {
+ return toStringHelper(getClass())
+ .add("id", portLoadBalancerId)
+ .add("ports", ports)
+ .add("mode", mode)
+ .toString();
+ }
+}
diff --git a/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/api/PortLoadBalancerAdminService.java b/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/api/PortLoadBalancerAdminService.java
new file mode 100644
index 0000000..2a2ef20
--- /dev/null
+++ b/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/api/PortLoadBalancerAdminService.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2018-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.portloadbalancer.api;
+
+import org.onosproject.net.PortNumber;
+
+import java.util.Set;
+
+/**
+ * Port load balancer admin service.
+ */
+public interface PortLoadBalancerAdminService {
+ /**
+ * Creates or updates a port load balancer.
+ *
+ * @param portLoadBalancerId port load balancer id
+ * @param ports physical ports in the port load balancer
+ * @param mode port load balancer mode
+ * @return port load balancer that is created or updated
+ */
+ PortLoadBalancer createOrUpdate(PortLoadBalancerId portLoadBalancerId, Set<PortNumber> ports,
+ PortLoadBalancerMode mode);
+
+ /**
+ * Removes a port load balancer.
+ *
+ * @param portLoadBalancerId port load balancer id
+ * @return port load balancer that is removed or null if it was not possible
+ */
+ PortLoadBalancer remove(PortLoadBalancerId portLoadBalancerId);
+
+}
diff --git a/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/api/PortLoadBalancerData.java b/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/api/PortLoadBalancerData.java
new file mode 100644
index 0000000..7d1e93d
--- /dev/null
+++ b/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/api/PortLoadBalancerData.java
@@ -0,0 +1,106 @@
+/*
+ * Copyright 2018-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.portloadbalancer.api;
+
+
+import java.util.Objects;
+
+import static com.google.common.base.MoreObjects.toStringHelper;
+
+/**
+ * Represents port load balancer event data.
+ */
+public class PortLoadBalancerData {
+
+ // We exchange only id and nextid in the events
+ private PortLoadBalancerId portLoadBalancerId;
+ private int nextId;
+
+ /**
+ * Constructs a port load balancer data.
+ *
+ * @param portLoadBalancerId port load balancer ID
+ */
+ public PortLoadBalancerData(PortLoadBalancerId portLoadBalancerId) {
+ this.portLoadBalancerId = portLoadBalancerId;
+ this.nextId = -1;
+ }
+
+ /**
+ * Constructs a port load balancer data.
+ *
+ * @param portLoadBalancerId port load balancer ID
+ * @param nextId port load balancer next id
+ */
+ public PortLoadBalancerData(PortLoadBalancerId portLoadBalancerId, int nextId) {
+ this.portLoadBalancerId = portLoadBalancerId;
+ this.nextId = nextId;
+ }
+
+ /**
+ * Gets port load balancer ID.
+ *
+ * @return port load balancer ID
+ */
+ public PortLoadBalancerId portLoadBalancerId() {
+ return portLoadBalancerId;
+ }
+
+ /**
+ * Gets port load balancer next id.
+ *
+ * @return port load balancer next id
+ */
+ public int nextId() {
+ return nextId;
+ }
+
+ /**
+ * Sets port load balancer next id.
+ *
+ * @param nextId port load balancer next id
+ */
+ public void setNextId(int nextId) {
+ this.nextId = nextId;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(portLoadBalancerId, nextId);
+ }
+
+ @Override
+ public boolean equals(final Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (!(obj instanceof PortLoadBalancerData)) {
+ return false;
+ }
+ final PortLoadBalancerData other = (PortLoadBalancerData) obj;
+
+ return Objects.equals(this.portLoadBalancerId, other.portLoadBalancerId) &&
+ this.nextId == other.nextId;
+ }
+
+ @Override
+ public String toString() {
+ return toStringHelper(getClass())
+ .add("portLoadBalancerId", portLoadBalancerId)
+ .add("nextId", nextId)
+ .toString();
+ }
+}
diff --git a/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/api/PortLoadBalancerEvent.java b/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/api/PortLoadBalancerEvent.java
new file mode 100644
index 0000000..bd9c234
--- /dev/null
+++ b/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/api/PortLoadBalancerEvent.java
@@ -0,0 +1,119 @@
+/*
+ * Copyright 2018-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.portloadbalancer.api;
+
+import org.onlab.util.Tools;
+import org.onosproject.event.AbstractEvent;
+import java.util.Objects;
+
+import static com.google.common.base.MoreObjects.toStringHelper;
+
+/**
+ * Port load balancer event.
+ */
+public class PortLoadBalancerEvent extends AbstractEvent<PortLoadBalancerEvent.Type, PortLoadBalancerData> {
+
+ private PortLoadBalancerData prevSubject;
+
+ /**
+ * Port load balancer event type.
+ */
+ public enum Type {
+ /**
+ * Port load balancer creation is requested.
+ */
+ ADDED,
+
+ /**
+ * Port load balancer deletion is requested.
+ */
+ REMOVED,
+
+ /**
+ * Port load balancer update is requested.
+ * E.g. member change.
+ */
+ UPDATED,
+
+ /**
+ * Port load balancer creation/update is completed successfully.
+ */
+ INSTALLED,
+
+ /**
+ * Port load balancer deletion is completed successfully.
+ */
+ UNINSTALLED,
+
+ /**
+ * Error occurs during creation/update/deletion of a port load balancer.
+ */
+ FAILED
+ }
+
+ /**
+ * Constructs a port load balancer event.
+ *
+ * @param type event type
+ * @param subject current port load balancer information
+ * @param prevSubject previous port load balancer information
+ */
+ public PortLoadBalancerEvent(Type type, PortLoadBalancerData subject, PortLoadBalancerData prevSubject) {
+ super(type, subject);
+ this.prevSubject = prevSubject;
+ }
+
+ /**
+ * Gets previous port load balancer information.
+ *
+ * @return previous subject
+ */
+ public PortLoadBalancerData prevSubject() {
+ return prevSubject;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(subject(), time(), prevSubject);
+ }
+
+ @Override
+ public boolean equals(Object other) {
+ if (this == other) {
+ return true;
+ }
+
+ if (!(other instanceof PortLoadBalancerEvent)) {
+ return false;
+ }
+
+ PortLoadBalancerEvent that = (PortLoadBalancerEvent) other;
+ return Objects.equals(this.subject(), that.subject()) &&
+ Objects.equals(this.type(), that.type()) &&
+ Objects.equals(this.prevSubject, that.prevSubject);
+ }
+
+ @Override
+ public String toString() {
+ return toStringHelper(this)
+ .add("type", type())
+ .add("subject", subject())
+ .add("prevSubject", prevSubject)
+ .add("time", Tools.defaultOffsetDataTime(time()))
+ .toString();
+ }
+}
diff --git a/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/api/PortLoadBalancerId.java b/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/api/PortLoadBalancerId.java
new file mode 100644
index 0000000..a102dd5
--- /dev/null
+++ b/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/api/PortLoadBalancerId.java
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2018-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.portloadbalancer.api;
+
+import org.onosproject.net.DeviceId;
+
+import java.util.Objects;
+
+/**
+ * Port load balancer identifier.
+ * It is used to identify a load balancer across the entire system and therefore has to be unique system-wide.
+ */
+public class PortLoadBalancerId {
+ private final DeviceId deviceId;
+
+ /**
+ * Port load balancer key.
+ * It is used to identify a load balancer on a specific device and therefore has to be unique device-wide.
+ */
+ private final int key;
+
+ /**
+ * Constructs port load balancer ID.
+ *
+ * @param deviceId device ID
+ * @param key port load balancer key
+ */
+ public PortLoadBalancerId(DeviceId deviceId, int key) {
+ this.deviceId = deviceId;
+ this.key = key;
+ }
+
+ /**
+ * Returns port load balancer device ID.
+ *
+ * @return port load balancer device ID
+ */
+ public DeviceId deviceId() {
+ return deviceId;
+ }
+
+ /**
+ * Returns port load balancer key.
+ *
+ * @return port load balancer key
+ */
+ public int key() {
+ return key;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(deviceId, key);
+ }
+
+ @Override
+ public boolean equals(final Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (!(obj instanceof PortLoadBalancerId)) {
+ return false;
+ }
+ final PortLoadBalancerId other = (PortLoadBalancerId) obj;
+
+ return Objects.equals(this.deviceId, other.deviceId) &&
+ Objects.equals(this.key, other.key);
+ }
+
+ @Override
+ public String toString() {
+ return deviceId.toString() + ":" + key;
+ }
+}
diff --git a/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/api/PortLoadBalancerListener.java b/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/api/PortLoadBalancerListener.java
new file mode 100644
index 0000000..de4efa8
--- /dev/null
+++ b/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/api/PortLoadBalancerListener.java
@@ -0,0 +1,25 @@
+/*
+ * Copyright 2018-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.portloadbalancer.api;
+
+import org.onosproject.event.EventListener;
+
+/**
+ * Port load balancer event listener.
+ */
+public interface PortLoadBalancerListener extends EventListener<PortLoadBalancerEvent> {
+}
diff --git a/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/api/PortLoadBalancerMode.java b/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/api/PortLoadBalancerMode.java
new file mode 100644
index 0000000..233c116
--- /dev/null
+++ b/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/api/PortLoadBalancerMode.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2018-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.portloadbalancer.api;
+
+/**
+ * Port load balancer mode.
+ */
+public enum PortLoadBalancerMode {
+ /**
+ * Static port load balancer.
+ *
+ * In STATIC mode, all member ports of this load balancer will be used to transmit data as long as it is enabled.
+ */
+ STATIC,
+
+ /**
+ * Port load balancer based on LACP.
+ *
+ * In LACP mode, we decide whether to use a member port to transmit data or not according to
+ * the LACP negotiation result.
+ */
+ LACP
+}
diff --git a/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/api/PortLoadBalancerService.java b/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/api/PortLoadBalancerService.java
new file mode 100644
index 0000000..5c7f1d3
--- /dev/null
+++ b/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/api/PortLoadBalancerService.java
@@ -0,0 +1,97 @@
+/*
+ * Copyright 2018-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.portloadbalancer.api;
+
+import org.onosproject.core.ApplicationId;
+import org.onosproject.event.ListenerService;
+
+import java.util.Map;
+
+/**
+ * Port load balance service.
+ */
+public interface PortLoadBalancerService extends ListenerService<PortLoadBalancerEvent, PortLoadBalancerListener> {
+ /**
+ * Gets all port load balancers from the store.
+ *
+ * @return port load balancer ID and port load balancer information mapping
+ */
+ Map<PortLoadBalancerId, PortLoadBalancer> getPortLoadBalancers();
+
+ /**
+ * Gets port load balancer that matches given device ID and key, or null if not found.
+ *
+ * @param portLoadBalancerId port load balancer id
+ * @return port load balancer information
+ */
+ PortLoadBalancer getPortLoadBalancer(PortLoadBalancerId portLoadBalancerId);
+
+ /**
+ * Gets the mapping between port load balancer id and
+ * the next objective id that represents the port load balancer.
+ *
+ * @return port load balancer id and next id mapping
+ */
+ Map<PortLoadBalancerId, Integer> getPortLoadBalancerNexts();
+
+ /**
+ * Gets port load balancer next id that matches given device Id and key, or null if not found.
+ *
+ * @param portLoadBalancerId port load balancer id
+ * @return next ID
+ */
+ int getPortLoadBalancerNext(PortLoadBalancerId portLoadBalancerId);
+
+ /**
+ * Reserves a port load balancer. Only one application
+ * at time can reserve a given port load balancer.
+ *
+ * @param portLoadBalancerId the port load balancer id
+ * @param appId the application id
+ * @return true if reservation was successful false otherwise
+ */
+ boolean reserve(PortLoadBalancerId portLoadBalancerId, ApplicationId appId);
+
+ /**
+ * Releases a port load balancer. Once released
+ * by the owner the port load balancer is eligible
+ * for removal.
+ *
+ * @param portLoadBalancerId the port load balancer id
+ * @param appId the application id
+ * @return true if release was successful false otherwise
+ */
+ boolean release(PortLoadBalancerId portLoadBalancerId, ApplicationId appId);
+
+ /**
+ * Gets reservation of a port load balancer. Only one application
+ * at time can reserve a given port load balancer.
+ *
+ * @param portLoadBalancerId the port load balancer id
+ * @return the id of the application using the port load balancer
+ */
+ ApplicationId getReservation(PortLoadBalancerId portLoadBalancerId);
+
+ /**
+ * Gets port load balancer reservations. Only one application
+ * at time can reserve a given port load balancer.
+ *
+ * @return reservations of the port load balancer resources
+ */
+ Map<PortLoadBalancerId, ApplicationId> getReservations();
+
+}
diff --git a/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/api/package-info.java b/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/api/package-info.java
new file mode 100644
index 0000000..63a0bf0
--- /dev/null
+++ b/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/api/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2018-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.
+ */
+
+/**
+ * Port load balancer API.
+ */
+package org.onosproject.portloadbalancer.api;
\ No newline at end of file
diff --git a/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/app/PortLoadBalancerManager.java b/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/app/PortLoadBalancerManager.java
new file mode 100644
index 0000000..ed59699
--- /dev/null
+++ b/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/app/PortLoadBalancerManager.java
@@ -0,0 +1,633 @@
+/*
+ * Copyright 2018-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.portloadbalancer.app;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Sets;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
+import org.onlab.util.KryoNamespace;
+import org.onosproject.cluster.ClusterService;
+import org.onosproject.cluster.LeadershipService;
+import org.onosproject.cluster.NodeId;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.portloadbalancer.api.PortLoadBalancer;
+import org.onosproject.portloadbalancer.api.PortLoadBalancerData;
+import org.onosproject.portloadbalancer.api.PortLoadBalancerEvent;
+import org.onosproject.portloadbalancer.api.PortLoadBalancerAdminService;
+import org.onosproject.portloadbalancer.api.PortLoadBalancerId;
+import org.onosproject.portloadbalancer.api.PortLoadBalancerListener;
+import org.onosproject.portloadbalancer.api.PortLoadBalancerMode;
+import org.onosproject.portloadbalancer.api.PortLoadBalancerService;
+import org.onosproject.mastership.MastershipService;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.device.DeviceEvent;
+import org.onosproject.net.device.DeviceListener;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.flow.DefaultTrafficSelector;
+import org.onosproject.net.flow.DefaultTrafficTreatment;
+import org.onosproject.net.flow.TrafficSelector;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.flow.instructions.Instruction;
+import org.onosproject.net.flow.instructions.Instructions;
+import org.onosproject.net.flowobjective.DefaultNextObjective;
+import org.onosproject.net.flowobjective.DefaultNextTreatment;
+import org.onosproject.net.flowobjective.FlowObjectiveService;
+import org.onosproject.net.flowobjective.NextObjective;
+import org.onosproject.net.flowobjective.Objective;
+import org.onosproject.net.flowobjective.ObjectiveContext;
+import org.onosproject.net.flowobjective.ObjectiveError;
+import org.onosproject.store.serializers.KryoNamespaces;
+import org.onosproject.store.service.ConsistentMap;
+import org.onosproject.store.service.MapEvent;
+import org.onosproject.store.service.MapEventListener;
+import org.onosproject.store.service.Serializer;
+import org.onosproject.store.service.StorageService;
+import org.onosproject.store.service.Versioned;
+import org.slf4j.Logger;
+
+import java.util.Collection;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.stream.Collectors;
+
+import static org.onlab.util.Tools.groupedThreads;
+import static org.slf4j.LoggerFactory.getLogger;
+
+@Component(
+ immediate = true,
+ service = { PortLoadBalancerService.class, PortLoadBalancerAdminService.class }
+)
+public class PortLoadBalancerManager implements PortLoadBalancerService, PortLoadBalancerAdminService {
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ private CoreService coreService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ private StorageService storageService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ private FlowObjectiveService flowObjService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ private MastershipService mastershipService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ private LeadershipService leadershipService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ private ClusterService clusterService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ private DeviceService deviceService;
+
+ private static final Logger log = getLogger(PortLoadBalancerManager.class);
+ private static final String APP_NAME = "org.onosproject.portloadbalancer";
+
+ private ApplicationId appId;
+ private ConsistentMap<PortLoadBalancerId, PortLoadBalancer> portLoadBalancerStore;
+ private ConsistentMap<PortLoadBalancerId, Integer> portLoadBalancerNextStore;
+ // TODO Evaluate if ResourceService is a better option
+ private ConsistentMap<PortLoadBalancerId, ApplicationId> portLoadBalancerResStore;
+ private Set<PortLoadBalancerListener> listeners = Sets.newConcurrentHashSet();
+
+ private ExecutorService portLoadBalancerEventExecutor;
+ private ExecutorService portLoadBalancerProvExecutor;
+ private ExecutorService deviceEventExecutor;
+
+ private MapEventListener<PortLoadBalancerId, PortLoadBalancer> portLoadBalancerStoreListener;
+ // TODO build CLI to view and clear the next store
+ private MapEventListener<PortLoadBalancerId, Integer> portLoadBalancerNextStoreListener;
+ private MapEventListener<PortLoadBalancerId, ApplicationId> portLoadBalancerResStoreListener;
+ private final DeviceListener deviceListener = new InternalDeviceListener();
+
+ @Activate
+ public void activate() {
+ appId = coreService.registerApplication(APP_NAME);
+
+ portLoadBalancerEventExecutor = Executors.newSingleThreadExecutor(
+ groupedThreads("portloadbalancer-event", "%d", log));
+ portLoadBalancerProvExecutor = Executors.newSingleThreadExecutor(
+ groupedThreads("portloadbalancer-prov", "%d", log));
+ deviceEventExecutor = Executors.newSingleThreadScheduledExecutor(
+ groupedThreads("portloadbalancer-dev-event", "%d", log));
+ portLoadBalancerStoreListener = new PortLoadBalancerStoreListener();
+ portLoadBalancerNextStoreListener = new PortLoadBalancerNextStoreListener();
+ portLoadBalancerResStoreListener = new PortLoadBalancerResStoreListener();
+
+ KryoNamespace serializer = KryoNamespace.newBuilder()
+ .register(KryoNamespaces.API)
+ .register(PortLoadBalancer.class)
+ .register(PortLoadBalancerId.class)
+ .register(PortLoadBalancerMode.class)
+ .build();
+ portLoadBalancerStore = storageService.<PortLoadBalancerId, PortLoadBalancer>consistentMapBuilder()
+ .withName("onos-portloadbalancer-store")
+ .withRelaxedReadConsistency()
+ .withSerializer(Serializer.using(serializer))
+ .build();
+ portLoadBalancerStore.addListener(portLoadBalancerStoreListener);
+ portLoadBalancerNextStore = storageService.<PortLoadBalancerId, Integer>consistentMapBuilder()
+ .withName("onos-portloadbalancer-next-store")
+ .withRelaxedReadConsistency()
+ .withSerializer(Serializer.using(serializer))
+ .build();
+ portLoadBalancerNextStore.addListener(portLoadBalancerNextStoreListener);
+ portLoadBalancerResStore = storageService.<PortLoadBalancerId, ApplicationId>consistentMapBuilder()
+ .withName("onos-portloadbalancer-res-store")
+ .withRelaxedReadConsistency()
+ .withSerializer(Serializer.using(serializer))
+ .build();
+ portLoadBalancerResStore.addListener(portLoadBalancerResStoreListener);
+
+ deviceService.addListener(deviceListener);
+
+ log.info("Started");
+ }
+
+ @Deactivate
+ public void deactivate() {
+ portLoadBalancerStore.removeListener(portLoadBalancerStoreListener);
+ portLoadBalancerNextStore.removeListener(portLoadBalancerNextStoreListener);
+
+ portLoadBalancerEventExecutor.shutdown();
+ portLoadBalancerProvExecutor.shutdown();
+ deviceEventExecutor.shutdown();
+
+ log.info("Stopped");
+ }
+
+ @Override
+ public void addListener(PortLoadBalancerListener listener) {
+ listeners.add(listener);
+ }
+
+ @Override
+ public void removeListener(PortLoadBalancerListener listener) {
+ listeners.remove(listener);
+ }
+
+ @Override
+ public PortLoadBalancer createOrUpdate(PortLoadBalancerId portLoadBalancerId, Set<PortNumber> ports,
+ PortLoadBalancerMode mode) {
+ log.debug("Putting {} -> {} {} into port load balancer store", portLoadBalancerId, mode, ports);
+ return Versioned.valueOrNull(portLoadBalancerStore.put(portLoadBalancerId,
+ new PortLoadBalancer(portLoadBalancerId, ports, mode)));
+ }
+
+ @Override
+ public PortLoadBalancer remove(PortLoadBalancerId portLoadBalancerId) {
+ ApplicationId reservation = Versioned.valueOrNull(portLoadBalancerResStore.get(portLoadBalancerId));
+ // Remove only if it is not used - otherwise it is necessary to release first
+ if (reservation == null) {
+ log.debug("Removing {} from port load balancer store", portLoadBalancerId);
+ return Versioned.valueOrNull(portLoadBalancerStore.remove(portLoadBalancerId));
+ }
+ log.warn("Removal {} from port load balancer store was not possible " +
+ "due to a previous reservation", portLoadBalancerId);
+ return null;
+ }
+
+ @Override
+ public Map<PortLoadBalancerId, PortLoadBalancer> getPortLoadBalancers() {
+ return ImmutableMap.copyOf(portLoadBalancerStore.asJavaMap());
+ }
+
+ @Override
+ public PortLoadBalancer getPortLoadBalancer(PortLoadBalancerId portLoadBalancerId) {
+ return Versioned.valueOrNull(portLoadBalancerStore.get(portLoadBalancerId));
+ }
+
+ @Override
+ public Map<PortLoadBalancerId, Integer> getPortLoadBalancerNexts() {
+ return ImmutableMap.copyOf(portLoadBalancerNextStore.asJavaMap());
+ }
+
+ @Override
+ public int getPortLoadBalancerNext(PortLoadBalancerId portLoadBalancerId) {
+ return Versioned.valueOrNull(portLoadBalancerNextStore.get(portLoadBalancerId));
+ }
+
+ @Override
+ public boolean reserve(PortLoadBalancerId portLoadBalancerId, ApplicationId appId) {
+ // Check if the resource is available
+ ApplicationId reservation = Versioned.valueOrNull(portLoadBalancerResStore.get(portLoadBalancerId));
+ PortLoadBalancer portLoadBalancer = Versioned.valueOrNull(portLoadBalancerStore.get(portLoadBalancerId));
+ if (reservation == null && portLoadBalancer != null) {
+ log.debug("Reserving {} -> {} into port load balancer reservation store", portLoadBalancerId, appId);
+ return portLoadBalancerResStore.put(portLoadBalancerId, appId) == null;
+ } else if (reservation != null && reservation.equals(appId)) {
+ // App try to reserve the resource a second time
+ log.debug("Already reserved {} -> {} skip reservation", portLoadBalancerId, appId);
+ return true;
+ }
+ log.warn("Reservation failed {} -> {}", portLoadBalancerId, appId);
+ return false;
+ }
+
+ @Override
+ public boolean release(PortLoadBalancerId portLoadBalancerId, ApplicationId appId) {
+ // Check if the resource is reserved
+ ApplicationId reservation = Versioned.valueOrNull(portLoadBalancerResStore.get(portLoadBalancerId));
+ if (reservation != null && reservation.equals(appId)) {
+ log.debug("Removing {} -> {} from port load balancer reservation store", portLoadBalancerId, appId);
+ return portLoadBalancerResStore.remove(portLoadBalancerId) != null;
+ }
+ log.warn("Release failed {} -> {}", portLoadBalancerId, appId);
+ return false;
+ }
+
+ @Override
+ public ApplicationId getReservation(PortLoadBalancerId portLoadBalancerId) {
+ return Versioned.valueOrNull(portLoadBalancerResStore.get(portLoadBalancerId));
+ }
+
+ @Override
+ public Map<PortLoadBalancerId, ApplicationId> getReservations() {
+ return portLoadBalancerResStore.asJavaMap();
+ }
+
+ private class PortLoadBalancerStoreListener implements MapEventListener<PortLoadBalancerId, PortLoadBalancer> {
+ public void event(MapEvent<PortLoadBalancerId, PortLoadBalancer> event) {
+ switch (event.type()) {
+ case INSERT:
+ log.debug("PortLoadBalancer {} insert new={}, old={}", event.key(), event.newValue(),
+ event.oldValue());
+ post(new PortLoadBalancerEvent(PortLoadBalancerEvent.Type.ADDED, event.newValue().value().data(),
+ null));
+ populatePortLoadBalancer(event.newValue().value());
+ break;
+ case REMOVE:
+ log.debug("PortLoadBalancer {} remove new={}, old={}", event.key(), event.newValue(),
+ event.oldValue());
+ post(new PortLoadBalancerEvent(PortLoadBalancerEvent.Type.REMOVED, null,
+ event.oldValue().value().data()));
+ revokePortLoadBalancer(event.oldValue().value());
+ break;
+ case UPDATE:
+ log.debug("PortLoadBalancer {} update new={}, old={}", event.key(), event.newValue(),
+ event.oldValue());
+ post(new PortLoadBalancerEvent(PortLoadBalancerEvent.Type.UPDATED, event.newValue().value().data(),
+ event.oldValue().value().data()));
+ updatePortLoadBalancer(event.newValue().value(), event.oldValue().value());
+ break;
+ default:
+ break;
+ }
+ }
+ }
+
+ private class PortLoadBalancerNextStoreListener implements MapEventListener<PortLoadBalancerId, Integer> {
+ public void event(MapEvent<PortLoadBalancerId, Integer> event) {
+ switch (event.type()) {
+ case INSERT:
+ log.debug("PortLoadBalancer next {} insert new={}, old={}", event.key(), event.newValue(),
+ event.oldValue());
+ break;
+ case REMOVE:
+ log.debug("PortLoadBalancer next {} remove new={}, old={}", event.key(), event.newValue(),
+ event.oldValue());
+ break;
+ case UPDATE:
+ log.debug("PortLoadBalancer next {} update new={}, old={}", event.key(), event.newValue(),
+ event.oldValue());
+ break;
+ default:
+ break;
+ }
+ }
+ }
+
+ private class PortLoadBalancerResStoreListener implements MapEventListener<PortLoadBalancerId, ApplicationId> {
+ public void event(MapEvent<PortLoadBalancerId, ApplicationId> event) {
+ switch (event.type()) {
+ case INSERT:
+ log.debug("PortLoadBalancer reservation {} insert new={}, old={}", event.key(), event.newValue(),
+ event.oldValue());
+ break;
+ case REMOVE:
+ log.debug("PortLoadBalancer reservation {} remove new={}, old={}", event.key(), event.newValue(),
+ event.oldValue());
+ break;
+ case UPDATE:
+ log.debug("PortLoadBalancer reservation {} update new={}, old={}", event.key(), event.newValue(),
+ event.oldValue());
+ break;
+ default:
+ break;
+ }
+ }
+ }
+
+ private class InternalDeviceListener implements DeviceListener {
+ // We want to manage only a subset of events and if we are the leader
+ @Override
+ public void event(DeviceEvent event) {
+ deviceEventExecutor.execute(() -> {
+ DeviceId deviceId = event.subject().id();
+ if (!isLocalLeader(deviceId)) {
+ log.debug("Not the leader of {}. Skip event {}", deviceId, event);
+ return;
+ }
+ // Populate or revoke according to the device availability
+ if (deviceService.isAvailable(deviceId)) {
+ init(deviceId);
+ } else {
+ cleanup(deviceId);
+ }
+ });
+ }
+ // Some events related to the devices are skipped
+ @Override
+ public boolean isRelevant(DeviceEvent event) {
+ return event.type() == DeviceEvent.Type.DEVICE_ADDED ||
+ event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
+ event.type() == DeviceEvent.Type.DEVICE_UPDATED;
+ }
+ }
+
+ private void post(PortLoadBalancerEvent portLoadBalancerEvent) {
+ portLoadBalancerEventExecutor.execute(() -> {
+ for (PortLoadBalancerListener l : listeners) {
+ if (l.isRelevant(portLoadBalancerEvent)) {
+ l.event(portLoadBalancerEvent);
+ }
+ }
+ });
+ }
+
+ private void init(DeviceId deviceId) {
+ portLoadBalancerStore.entrySet().stream()
+ .filter(portLoadBalancerEntry -> portLoadBalancerEntry.getKey().deviceId().equals(deviceId))
+ .forEach(portLoadBalancerEntry -> populatePortLoadBalancer(portLoadBalancerEntry.getValue().value()));
+ }
+
+ private void cleanup(DeviceId deviceId) {
+ portLoadBalancerStore.entrySet().stream()
+ .filter(entry -> entry.getKey().deviceId().equals(deviceId))
+ .forEach(entry -> portLoadBalancerNextStore.remove(entry.getKey()));
+ log.debug("{} is removed from portLoadBalancerNextStore", deviceId);
+ }
+
+ private void populatePortLoadBalancer(PortLoadBalancer portLoadBalancer) {
+ DeviceId deviceId = portLoadBalancer.portLoadBalancerId().deviceId();
+ if (!isLocalLeader(deviceId)) {
+ log.debug("Not the leader of {}. Skip populatePortLoadBalancer {}", deviceId,
+ portLoadBalancer.portLoadBalancerId());
+ return;
+ }
+
+ portLoadBalancerProvExecutor.execute(() -> {
+ Integer nextid = Versioned.valueOrNull(portLoadBalancerNextStore
+ .get(portLoadBalancer.portLoadBalancerId()));
+ if (nextid == null) {
+ // Build a new context and new next objective
+ PortLoadBalancerObjectiveContext context =
+ new PortLoadBalancerObjectiveContext(portLoadBalancer.portLoadBalancerId());
+ NextObjective nextObj = nextObjBuilder(portLoadBalancer.portLoadBalancerId(),
+ portLoadBalancer.ports(), nextid).add(context);
+ // Finally submit, store, and register the resource
+ flowObjService.next(deviceId, nextObj);
+ portLoadBalancerNextStore.put(portLoadBalancer.portLoadBalancerId(), nextObj.id());
+ } else {
+ log.info("NextObj for {} already exists. Skip populatePortLoadBalancer",
+ portLoadBalancer.portLoadBalancerId());
+ }
+ });
+ }
+
+ private void revokePortLoadBalancer(PortLoadBalancer portLoadBalancer) {
+ DeviceId deviceId = portLoadBalancer.portLoadBalancerId().deviceId();
+ if (!isLocalLeader(deviceId)) {
+ log.debug("Not the leader of {}. Skip revokePortLoadBalancer {}", deviceId,
+ portLoadBalancer.portLoadBalancerId());
+ return;
+ }
+
+ portLoadBalancerProvExecutor.execute(() -> {
+ Integer nextid = Versioned.valueOrNull(portLoadBalancerNextStore.get(portLoadBalancer
+ .portLoadBalancerId()));
+ if (nextid != null) {
+ // Build a new context and remove old next objective
+ PortLoadBalancerObjectiveContext context =
+ new PortLoadBalancerObjectiveContext(portLoadBalancer.portLoadBalancerId());
+ NextObjective nextObj = nextObjBuilder(portLoadBalancer.portLoadBalancerId(), portLoadBalancer.ports(),
+ nextid).remove(context);
+ // Finally submit and invalidate the store
+ flowObjService.next(deviceId, nextObj);
+ portLoadBalancerNextStore.remove(portLoadBalancer.portLoadBalancerId());
+ } else {
+ log.info("NextObj for {} does not exist. Skip revokePortLoadBalancer",
+ portLoadBalancer.portLoadBalancerId());
+ }
+ });
+ }
+
+ private void updatePortLoadBalancer(PortLoadBalancer newPortLoadBalancer, PortLoadBalancer oldPortLoadBalancer) {
+ DeviceId deviceId = newPortLoadBalancer.portLoadBalancerId().deviceId();
+ if (!isLocalLeader(deviceId)) {
+ log.debug("Not the leader of {}. Skip updatePortLoadBalancer {}", deviceId,
+ newPortLoadBalancer.portLoadBalancerId());
+ return;
+ }
+
+ portLoadBalancerProvExecutor.execute(() -> {
+ Integer nextid = Versioned.valueOrNull(portLoadBalancerNextStore
+ .get(newPortLoadBalancer.portLoadBalancerId()));
+ if (nextid != null) {
+ // Compute modifications and context
+ PortLoadBalancerObjectiveContext context =
+ new PortLoadBalancerObjectiveContext(newPortLoadBalancer.portLoadBalancerId());
+ Set<PortNumber> portsToBeAdded =
+ Sets.difference(newPortLoadBalancer.ports(), oldPortLoadBalancer.ports());
+ Set<PortNumber> portsToBeRemoved =
+ Sets.difference(oldPortLoadBalancer.ports(), newPortLoadBalancer.ports());
+ // and send them to the flowobj subsystem
+ if (!portsToBeAdded.isEmpty()) {
+ flowObjService.next(deviceId, nextObjBuilder(newPortLoadBalancer.portLoadBalancerId(),
+ portsToBeAdded, nextid)
+ .addToExisting(context));
+ } else {
+ log.debug("NextObj for {} nothing to add", newPortLoadBalancer.portLoadBalancerId());
+
+ }
+ if (!portsToBeRemoved.isEmpty()) {
+ flowObjService.next(deviceId, nextObjBuilder(newPortLoadBalancer.portLoadBalancerId(),
+ portsToBeRemoved, nextid)
+ .removeFromExisting(context));
+ } else {
+ log.debug("NextObj for {} nothing to remove", newPortLoadBalancer.portLoadBalancerId());
+ }
+ } else {
+ log.info("NextObj for {} does not exist. Skip updatePortLoadBalancer",
+ newPortLoadBalancer.portLoadBalancerId());
+ }
+ });
+ }
+
+ private NextObjective.Builder nextObjBuilder(PortLoadBalancerId portLoadBalancerId, Set<PortNumber> ports,
+ Integer nextId) {
+ if (nextId == null) {
+ nextId = flowObjService.allocateNextId();
+ }
+ // This metadata is used to pass the key to the driver.
+ // Some driver, e.g. OF-DPA, will use that information while creating load balancing group.
+ // TODO This is not an actual LAG port. In the future, we should extend metadata structure to carry
+ // generic information. We should avoid using in_port in the metadata once generic metadata is available.
+ TrafficSelector meta = DefaultTrafficSelector.builder()
+ .matchInPort(PortNumber.portNumber(portLoadBalancerId.key())).build();
+ NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder()
+ .withId(nextId)
+ .withMeta(meta)
+ .withType(NextObjective.Type.HASHED)
+ .fromApp(appId);
+ ports.forEach(port -> {
+ TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(port).build();
+ nextObjBuilder.addTreatment(DefaultNextTreatment.of(treatment));
+ });
+ return nextObjBuilder;
+ }
+
+ // Custom-built function, when the device is not available we need a fallback mechanism
+ private boolean isLocalLeader(DeviceId deviceId) {
+ if (!mastershipService.isLocalMaster(deviceId)) {
+ // When the device is available we just check the mastership
+ if (deviceService.isAvailable(deviceId)) {
+ return false;
+ }
+ // Fallback with Leadership service - device id is used as topic
+ NodeId leader = leadershipService.runForLeadership(
+ deviceId.toString()).leaderNodeId();
+ // Verify if this node is the leader
+ return clusterService.getLocalNode().id().equals(leader);
+ }
+ return true;
+ }
+
+ private final class PortLoadBalancerObjectiveContext implements ObjectiveContext {
+ private final PortLoadBalancerId portLoadBalancerId;
+
+ private PortLoadBalancerObjectiveContext(PortLoadBalancerId portLoadBalancerId) {
+ this.portLoadBalancerId = portLoadBalancerId;
+ }
+
+ @Override
+ public void onSuccess(Objective objective) {
+ NextObjective nextObj = (NextObjective) objective;
+ log.debug("Successfully {} nextobj {} for port load balancer {}. NextObj={}",
+ nextObj.op(), nextObj.id(), portLoadBalancerId, nextObj);
+ portLoadBalancerProvExecutor.execute(() -> onSuccessHandler(nextObj, portLoadBalancerId));
+ }
+
+ @Override
+ public void onError(Objective objective, ObjectiveError error) {
+ NextObjective nextObj = (NextObjective) objective;
+ log.warn("Failed to {} nextobj {} for port load balancer {} due to {}. NextObj={}",
+ nextObj.op(), nextObj.id(), portLoadBalancerId, error, nextObj);
+ portLoadBalancerProvExecutor.execute(() -> onErrorHandler(nextObj, portLoadBalancerId));
+ }
+ }
+
+ private void onSuccessHandler(NextObjective nextObjective, PortLoadBalancerId portLoadBalancerId) {
+ // Operation done
+ PortLoadBalancerData oldPortLoadBalancerData = new PortLoadBalancerData(portLoadBalancerId);
+ PortLoadBalancerData newPortLoadBalancerData = new PortLoadBalancerData(portLoadBalancerId);
+ // Other operations will not lead to a generation of an event
+ switch (nextObjective.op()) {
+ case ADD:
+ newPortLoadBalancerData.setNextId(nextObjective.id());
+ post(new PortLoadBalancerEvent(PortLoadBalancerEvent.Type.INSTALLED, newPortLoadBalancerData,
+ oldPortLoadBalancerData));
+ break;
+ case REMOVE:
+ oldPortLoadBalancerData.setNextId(nextObjective.id());
+ post(new PortLoadBalancerEvent(PortLoadBalancerEvent.Type.UNINSTALLED, newPortLoadBalancerData,
+ oldPortLoadBalancerData));
+ break;
+ default:
+ break;
+ }
+ }
+
+ private void onErrorHandler(NextObjective nextObjective, PortLoadBalancerId portLoadBalancerId) {
+ // There was a failure
+ PortLoadBalancerData portLoadBalancerData = new PortLoadBalancerData(portLoadBalancerId);
+ // send FAILED event;
+ switch (nextObjective.op()) {
+ case ADD:
+ // If ADD is failing apps do not know the next id; let's update the store
+ portLoadBalancerNextStore.remove(portLoadBalancerId);
+ portLoadBalancerResStore.remove(portLoadBalancerId);
+ portLoadBalancerStore.remove(portLoadBalancerId);
+ post(new PortLoadBalancerEvent(PortLoadBalancerEvent.Type.FAILED, portLoadBalancerData,
+ portLoadBalancerData));
+ break;
+ case ADD_TO_EXISTING:
+ // If ADD_TO_EXISTING is failing let's remove the failed ports
+ Collection<PortNumber> addedPorts = nextObjective.next().stream()
+ .flatMap(t -> t.allInstructions().stream())
+ .filter(i -> i.type() == Instruction.Type.OUTPUT)
+ .map(i -> ((Instructions.OutputInstruction) i).port())
+ .collect(Collectors.toList());
+ portLoadBalancerStore.compute(portLoadBalancerId, (key, value) -> {
+ if (value != null && value.ports() != null && !value.ports().isEmpty()) {
+ value.ports().removeAll(addedPorts);
+ }
+ return value;
+ });
+ portLoadBalancerData.setNextId(nextObjective.id());
+ post(new PortLoadBalancerEvent(PortLoadBalancerEvent.Type.FAILED, portLoadBalancerData,
+ portLoadBalancerData));
+ break;
+ case REMOVE_FROM_EXISTING:
+ // If REMOVE_TO_EXISTING is failing let's re-add the failed ports
+ Collection<PortNumber> removedPorts = nextObjective.next().stream()
+ .flatMap(t -> t.allInstructions().stream())
+ .filter(i -> i.type() == Instruction.Type.OUTPUT)
+ .map(i -> ((Instructions.OutputInstruction) i).port())
+ .collect(Collectors.toList());
+ portLoadBalancerStore.compute(portLoadBalancerId, (key, value) -> {
+ if (value != null && value.ports() != null) {
+ value.ports().addAll(removedPorts);
+ }
+ return value;
+ });
+ portLoadBalancerData.setNextId(nextObjective.id());
+ post(new PortLoadBalancerEvent(PortLoadBalancerEvent.Type.FAILED, portLoadBalancerData,
+ portLoadBalancerData));
+ break;
+ case VERIFY:
+ case REMOVE:
+ // If ADD/REMOVE_TO_EXISTING, REMOVE and VERIFY are failing let's send
+ // also the info about the next id
+ portLoadBalancerData.setNextId(nextObjective.id());
+ post(new PortLoadBalancerEvent(PortLoadBalancerEvent.Type.FAILED, portLoadBalancerData,
+ portLoadBalancerData));
+ break;
+ default:
+ break;
+ }
+
+ }
+}
\ No newline at end of file
diff --git a/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/app/package-info.java b/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/app/package-info.java
new file mode 100644
index 0000000..a92eee5
--- /dev/null
+++ b/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/app/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2018-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.
+ */
+
+/**
+ * Port load balancer app.
+ */
+package org.onosproject.portloadbalancer.app;
\ No newline at end of file
diff --git a/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/cli/PortLoadBalancerAddCommand.java b/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/cli/PortLoadBalancerAddCommand.java
new file mode 100644
index 0000000..7f0c124
--- /dev/null
+++ b/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/cli/PortLoadBalancerAddCommand.java
@@ -0,0 +1,79 @@
+/*
+ * Copyright 2018-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.portloadbalancer.cli;
+
+import com.google.common.collect.Sets;
+import org.apache.karaf.shell.api.action.Argument;
+import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.portloadbalancer.api.PortLoadBalancer;
+import org.onosproject.portloadbalancer.api.PortLoadBalancerAdminService;
+import org.onosproject.portloadbalancer.api.PortLoadBalancerId;
+import org.onosproject.portloadbalancer.api.PortLoadBalancerMode;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
+
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Command to add a port load balancer.
+ */
+@Service
+@Command(scope = "onos", name = "plb-add", description = "Create or update port load balancer ")
+public class PortLoadBalancerAddCommand extends AbstractShellCommand {
+ @Argument(index = 0, name = "deviceId",
+ description = "Device ID",
+ required = true, multiValued = false)
+ private String deviceIdStr;
+
+ @Argument(index = 1, name = "key",
+ description = "port load balancer key",
+ required = true, multiValued = false)
+ private String keyStr;
+
+ @Argument(index = 2, name = "mode",
+ description = "port load balancer mode. STATIC or LACP",
+ required = true, multiValued = false)
+ private String modeStr;
+
+ @Argument(index = 3, name = "ports",
+ description = "port load balancer physical ports",
+ required = true, multiValued = true)
+ private String[] portsStr;
+
+ // Operation constants
+ private static final String CREATE = "Create";
+ private static final String UPDATE = "Update";
+
+ @Override
+ protected void doExecute() {
+ DeviceId deviceId = DeviceId.deviceId(deviceIdStr);
+ int portLoadBalancerKey = Integer.parseInt(keyStr);
+
+ PortLoadBalancerMode mode = PortLoadBalancerMode.valueOf(modeStr.toUpperCase());
+ Set<PortNumber> ports = Sets.newHashSet(portsStr).stream()
+ .map(PortNumber::fromString).collect(Collectors.toSet());
+
+ PortLoadBalancerAdminService portLoadBalancerAdminService = get(PortLoadBalancerAdminService.class);
+ PortLoadBalancerId portLoadBalancerId = new PortLoadBalancerId(deviceId, portLoadBalancerKey);
+ PortLoadBalancer portLoadBalancer = portLoadBalancerAdminService
+ .createOrUpdate(portLoadBalancerId, ports, mode);
+ print("%s of %s executed", portLoadBalancer == null ? CREATE : UPDATE, portLoadBalancerId);
+
+ }
+}
diff --git a/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/cli/PortLoadBalancerListCommand.java b/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/cli/PortLoadBalancerListCommand.java
new file mode 100644
index 0000000..6850b0f
--- /dev/null
+++ b/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/cli/PortLoadBalancerListCommand.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2018-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.portloadbalancer.cli;
+
+import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.portloadbalancer.api.PortLoadBalancer;
+import org.onosproject.portloadbalancer.api.PortLoadBalancerId;
+import org.onosproject.portloadbalancer.api.PortLoadBalancerService;
+
+import java.util.Map;
+
+/**
+ * Command to show all port load balancers.
+ */
+@Service
+@Command(scope = "onos", name = "plbs", description = "Lists port load balancers")
+public class PortLoadBalancerListCommand extends AbstractShellCommand {
+
+ // Operation constant
+ private static final String AVAILABLE = "Available";
+
+ @Override
+ public void doExecute() {
+ PortLoadBalancerService service = get(PortLoadBalancerService.class);
+ // Get port load balancers and reservations
+ Map<PortLoadBalancerId, PortLoadBalancer> portLoadBalancerStore = service.getPortLoadBalancers();
+ Map<PortLoadBalancerId, ApplicationId> portLoadBalancerResStore = service.getReservations();
+ // Print id -> ports, mode, reservation
+ portLoadBalancerStore.forEach((portLoadBalancerId, portLoadBalancer) ->
+ print("%s -> %s, %s, %s", portLoadBalancerId, portLoadBalancer.ports(), portLoadBalancer.mode(),
+ portLoadBalancerResStore.get(portLoadBalancerId) == null ? AVAILABLE :
+ portLoadBalancerResStore.get(portLoadBalancerId).name()));
+ }
+}
\ No newline at end of file
diff --git a/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/cli/PortLoadBalancerRemoveCommand.java b/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/cli/PortLoadBalancerRemoveCommand.java
new file mode 100644
index 0000000..8d19a27
--- /dev/null
+++ b/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/cli/PortLoadBalancerRemoveCommand.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2018-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.portloadbalancer.cli;
+
+import org.apache.karaf.shell.api.action.Argument;
+import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.portloadbalancer.api.PortLoadBalancer;
+import org.onosproject.portloadbalancer.api.PortLoadBalancerAdminService;
+import org.onosproject.portloadbalancer.api.PortLoadBalancerId;
+import org.onosproject.net.DeviceId;
+
+/**
+ * Command to remove a port load balancer.
+ */
+@Service
+@Command(scope = "onos", name = "plb-remove", description = "Remove port load balancers ")
+public class PortLoadBalancerRemoveCommand extends AbstractShellCommand {
+ @Argument(index = 0, name = "deviceId",
+ description = "Device ID",
+ required = true, multiValued = false)
+ private String deviceIdStr;
+
+ @Argument(index = 1, name = "key",
+ description = "port load balancer key",
+ required = true, multiValued = false)
+ private String keyStr;
+
+ // Operation constants
+ private static final String EXECUTED = "Executed";
+ private static final String FAILED = "Failed";
+
+ @Override
+ protected void doExecute() {
+ DeviceId deviceId = DeviceId.deviceId(deviceIdStr);
+ int portLoadBalancerKey = Integer.parseInt(keyStr);
+
+ PortLoadBalancerAdminService portLoadBalancerAdminService = get(PortLoadBalancerAdminService.class);
+ PortLoadBalancerId portLoadBalancerId = new PortLoadBalancerId(deviceId, portLoadBalancerKey);
+ PortLoadBalancer portLoadBalancer = portLoadBalancerAdminService.remove(portLoadBalancerId);
+ print("Removal of %s %s", portLoadBalancerId, portLoadBalancer != null ? EXECUTED : FAILED);
+ }
+}
diff --git a/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/cli/package-info.java b/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/cli/package-info.java
new file mode 100644
index 0000000..7e448ce
--- /dev/null
+++ b/apps/portloadbalancer/src/main/java/org/onosproject/portloadbalancer/cli/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2018-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.
+ */
+
+/**
+ * Port load balancer CLI.
+ */
+package org.onosproject.portloadbalancer.cli;
\ No newline at end of file