ONOS-5595 netcfg for protection
- ProtectionConfig
- TransportEndpointDescriptionCodec
Change-Id: I79e304a20e9d1f95a4b432542738c64102550650
diff --git a/core/api/src/main/java/org/onosproject/net/behaviour/protection/ProtectionConfig.java b/core/api/src/main/java/org/onosproject/net/behaviour/protection/ProtectionConfig.java
new file mode 100644
index 0000000..94ed965
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/behaviour/protection/ProtectionConfig.java
@@ -0,0 +1,136 @@
+/*
+ * 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.net.behaviour.protection;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.util.List;
+
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.config.BaseConfig;
+
+// FIXME Move this to Protection handling Intent related package?
+/**
+ * Config object for protection end-point.
+ * <p>
+ * Contains equivalent of {@link ProtectedTransportEndpointDescription}.
+ */
+public class ProtectionConfig
+ extends BaseConfig<DeviceId> {
+
+ /**
+ * {@value #CONFIG_KEY} : a netcfg ConfigKey for {@link ProtectionConfig}.
+ */
+ public static final String CONFIG_KEY = "protection";
+
+ /**
+ * JSON key for paths.
+ * <p>
+ * Value is list of {@link TransportEndpointDescription} in JSON.
+ */
+ private static final String PATHS = "paths";
+ /**
+ * JSON key for Peer {@link DeviceId}.
+ */
+ private static final String PEER = "peer";
+ private static final String FINGERPRINT = "fingerprint";
+
+
+ @Override
+ public boolean isValid() {
+ return isString(PEER, FieldPresence.MANDATORY) &&
+ isString(FINGERPRINT, FieldPresence.MANDATORY) &&
+ hasField(PATHS);
+ }
+
+
+ /**
+ * Returns List of underlying transport entity endpoints in priority order.
+ *
+ * @return the transport entity endpoint descriptions
+ */
+ public List<TransportEndpointDescription> paths() {
+ return getList(PATHS,
+ jsonStr -> decode(jsonStr, TransportEndpointDescription.class));
+ }
+
+ /**
+ * Sets the List of underlying transport entity endpoints in priority order.
+ *
+ * @param paths the transport entity endpoint descriptions
+ * @return self
+ */
+ public ProtectionConfig paths(List<TransportEndpointDescription> paths) {
+ setList(PATHS,
+ elm -> encode(elm, TransportEndpointDescription.class).toString(),
+ paths);
+ return this;
+ }
+
+ /**
+ * Returns DeviceId of remote peer of this endpoint.
+ *
+ * @return the peer
+ */
+ public DeviceId peer() {
+ return DeviceId.deviceId(get(PEER, ""));
+ }
+
+ /**
+ * Sets the DeviceId of remote peer of this endpoint.
+ *
+ * @param peer DeviceId
+ * @return self
+ */
+ public ProtectionConfig peer(DeviceId peer) {
+ setOrClear(PEER, peer.toString());
+ return this;
+ }
+
+ /**
+ * Returns fingerprint to identify this protected transport entity.
+ *
+ * @return the fingerprint
+ */
+ public String fingerprint() {
+ return get(FINGERPRINT, "");
+ }
+
+ /**
+ * Sets the fingerprint to identify this protected transport entity.
+ *
+ * @param fingerprint the fingerprint
+ * @return self
+ */
+ public ProtectionConfig fingerprint(String fingerprint) {
+ setOrClear(FINGERPRINT, checkNotNull(fingerprint));
+ return this;
+ }
+
+ /**
+ * Returns equivalent of this Config as {@link ProtectedTransportEndpointDescription}.
+ *
+ * @return {@link ProtectedTransportEndpointDescription}
+ */
+ public ProtectedTransportEndpointDescription asDescription() {
+ return ProtectedTransportEndpointDescription.of(paths(), peer(), fingerprint());
+ }
+
+ @Override
+ public String toString() {
+ return object.toString();
+ }
+}
diff --git a/core/api/src/main/java/org/onosproject/net/config/BaseConfig.java b/core/api/src/main/java/org/onosproject/net/config/BaseConfig.java
index 33d485f..5f87ed1 100644
--- a/core/api/src/main/java/org/onosproject/net/config/BaseConfig.java
+++ b/core/api/src/main/java/org/onosproject/net/config/BaseConfig.java
@@ -37,8 +37,7 @@
extends Config<S>
implements CodecContext {
- // might need to make it non-final for unit testing
- private static final ServiceDirectory SERVICES = new DefaultServiceDirectory();
+ private static ServiceDirectory services = new DefaultServiceDirectory();
private static final Logger log = getLogger(BaseConfig.class);
@Override
@@ -48,7 +47,7 @@
@Override
public <T> T getService(Class<T> serviceClass) {
- return SERVICES.get(serviceClass);
+ return services.get(serviceClass);
}
@Override
diff --git a/core/api/src/main/java/org/onosproject/net/config/Config.java b/core/api/src/main/java/org/onosproject/net/config/Config.java
index a3824c8..9cea1b9 100644
--- a/core/api/src/main/java/org/onosproject/net/config/Config.java
+++ b/core/api/src/main/java/org/onosproject/net/config/Config.java
@@ -33,6 +33,7 @@
import java.util.List;
import java.util.Set;
import java.util.function.Function;
+import java.util.stream.Collectors;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
@@ -358,11 +359,28 @@
protected <T> List<T> getList(String name, Function<String, T> function) {
List<T> list = Lists.newArrayList();
ArrayNode arrayNode = (ArrayNode) object.path(name);
- arrayNode.forEach(i -> list.add(function.apply(i.asText())));
+ arrayNode.forEach(i -> list.add(function.apply(asString(i))));
return list;
}
/**
+ * Converts JSON node to a String.
+ * <p>
+ * If the {@code node} was a text node, text is returned as-is,
+ * all other node type will be converted to String by toString().
+ *
+ * @param node JSON node to convert
+ * @return String representation
+ */
+ private static String asString(JsonNode node) {
+ if (node.isTextual()) {
+ return node.asText();
+ } else {
+ return node.toString();
+ }
+ }
+
+ /**
* Gets the specified array property as a list of items.
*
* @param name property name
@@ -378,11 +396,30 @@
return defaultValue;
}
ArrayNode arrayNode = (ArrayNode) jsonNode;
- arrayNode.forEach(i -> list.add(function.apply(i.asText())));
+ arrayNode.forEach(i -> list.add(function.apply(asString(i))));
return list;
}
/**
+ * Sets the specified property as an array of items in a given collection
+ * transformed into a String with supplied {@code function}.
+ *
+ * @param name propertyName
+ * @param function to transform item to a String
+ * @param value list of items
+ * @param <T> type of items
+ * @return self
+ */
+ protected <T> Config<S> setList(String name,
+ Function<? super T, String> function,
+ List<T> value) {
+ Collection<String> mapped = value.stream()
+ .map(function)
+ .collect(Collectors.toList());
+ return setOrClear(name, mapped);
+ }
+
+ /**
* Sets the specified property as an array of items in a given collection or
* clears it if null is given.
*
diff --git a/core/api/src/main/java/org/onosproject/net/intent/ProtectionEndpointIntent.java b/core/api/src/main/java/org/onosproject/net/intent/ProtectionEndpointIntent.java
new file mode 100644
index 0000000..518db0b
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/intent/ProtectionEndpointIntent.java
@@ -0,0 +1,181 @@
+/*
+ * 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.net.intent;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import java.util.Collection;
+
+import javax.annotation.concurrent.Immutable;
+
+import org.onosproject.core.ApplicationId;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.NetworkResource;
+import org.onosproject.net.behaviour.protection.ProtectedTransportEndpointDescription;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.MoreObjects;
+import com.google.common.collect.ImmutableList;
+
+/**
+ * Installable Intent for the ProtectionEndpoint (head/tail).
+ */
+@Immutable
+@Beta
+public class ProtectionEndpointIntent extends Intent {
+
+ private final DeviceId deviceId;
+ private final ProtectedTransportEndpointDescription description;
+
+
+ protected ProtectionEndpointIntent(ApplicationId appId, Key key,
+ Collection<NetworkResource> resources,
+ int priority,
+ DeviceId deviceId,
+ ProtectedTransportEndpointDescription description) {
+ super(appId, key, resources, priority);
+
+ this.deviceId = checkNotNull(deviceId);
+ this.description = checkNotNull(description);
+ }
+
+ /**
+ * Returns the identifier of the device to be configured.
+ *
+ * @return the deviceId
+ */
+ public DeviceId deviceId() {
+ return deviceId;
+ }
+
+ /**
+ * Returns the description of this protection endpoint.
+ *
+ * @return the description
+ */
+ public ProtectedTransportEndpointDescription description() {
+ return description;
+ }
+
+ @Override
+ public boolean isInstallable() {
+ return true;
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(getClass())
+ .add("id", id())
+ .add("key", key())
+ .add("appId", appId())
+ .add("priority", priority())
+ .add("resources", resources())
+ .add("deviceId", deviceId)
+ .add("description", description)
+ .toString();
+ }
+
+ /**
+ * Returns a new {@link ProtectionEndpointIntent} builder.
+ *
+ * @return the builder
+ */
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ /**
+ * Builder for {@link ProtectionEndpointIntent}.
+ */
+ public static class Builder extends Intent.Builder {
+
+ private DeviceId deviceId;
+ private ProtectedTransportEndpointDescription description;
+
+ /**
+ * Creates a new empty builder.
+ */
+ protected Builder() {
+ resources = ImmutableList.of();
+ }
+
+ /**
+ * Creates a new builder pre-populated with the information in the given
+ * intent.
+ *
+ * @param intent initial intent
+ */
+ protected Builder(ProtectionEndpointIntent intent) {
+ super(intent);
+ }
+
+ // TODO remove these overrides
+ @Override
+ public Builder key(Key key) {
+ super.key(key);
+ return this;
+ }
+
+ @Override
+ public Builder appId(ApplicationId appId) {
+ super.appId(appId);
+ return this;
+ }
+
+ @Override
+ public Builder resources(Collection<NetworkResource> resources) {
+ super.resources(resources);
+ return this;
+ }
+
+ @Override
+ public Builder priority(int priority) {
+ super.priority(priority);
+ return this;
+ }
+
+ public Builder deviceId(DeviceId deviceId) {
+ this.deviceId = deviceId;
+ return this;
+ }
+
+ public Builder description(ProtectedTransportEndpointDescription description) {
+ this.description = description;
+ return this;
+ }
+
+ public ProtectionEndpointIntent build() {
+ checkNotNull(key, "Key inherited from origin Intent expected.");
+ return new ProtectionEndpointIntent(appId,
+ key,
+ resources,
+ priority,
+ deviceId,
+ description);
+ }
+
+ }
+
+
+
+ /*
+ * For serialization.
+ */
+ @SuppressWarnings("unused")
+ private ProtectionEndpointIntent() {
+ this.deviceId = null;
+ this.description = null;
+ }
+}