Device config synchronizer
- initial sketch of Device Config Synchronizer outline (ONOS-6745)
Change-Id: I57c8ab6c3511f12c15e3501aa61498eb18264b27
diff --git a/apps/configsync/src/main/java/org/onosproject/d/config/sync/operation/SetRequest.java b/apps/configsync/src/main/java/org/onosproject/d/config/sync/operation/SetRequest.java
new file mode 100644
index 0000000..d4d3254
--- /dev/null
+++ b/apps/configsync/src/main/java/org/onosproject/d/config/sync/operation/SetRequest.java
@@ -0,0 +1,240 @@
+/*
+ * Copyright 2017-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.d.config.sync.operation;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+import java.util.Optional;
+
+import org.apache.commons.lang3.tuple.Pair;
+import org.onosproject.d.config.sync.operation.SetRequest.Change.Operation;
+import org.onosproject.yang.model.DataNode;
+import org.onosproject.yang.model.ResourceId;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.MoreObjects;
+import com.google.common.collect.ImmutableList;
+
+// One SetRequest is expected to be a transaction, all-or-nothing
+/**
+ * Collection of changes about a single Device,
+ * intended to be applied to the Device transactionally.
+ */
+@Beta
+public final class SetRequest {
+
+ private final Collection<Change> changes;
+
+ SetRequest(Collection<Change> changes) {
+ this.changes = ImmutableList.copyOf(changes);
+ }
+
+ public Collection<Change> changes() {
+ return changes;
+ }
+
+ public Collection<Pair<Operation, ResourceId>> subjects() {
+ return changes.stream()
+ .map(c -> Pair.of(c.op(), c.path()))
+ .collect(ImmutableList.toImmutableList());
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj instanceof SetRequest) {
+ SetRequest that = (SetRequest) obj;
+ return Objects.equals(this.changes, that.changes);
+ }
+ return false;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(changes);
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(getClass())
+ .add("changes", changes)
+ .toString();
+ }
+ public static SetRequest.Builder builder() {
+ return new Builder();
+ }
+
+ public static class Builder {
+ private final List<Change> changes = new ArrayList<>();
+
+ /**
+ * Returns changes contained in this builder.
+ *
+ * @return unmodifiable list view of Changes
+ */
+ public List<Change> changes() {
+ return Collections.unmodifiableList(changes);
+ }
+
+ /**
+ * Adds request to remove specified {@code path}.
+ *
+ * @param path resource path relative to device root node
+ * @return self
+ */
+ public SetRequest.Builder delete(ResourceId path) {
+ changes.add(Change.delete(path));
+ return this;
+ }
+
+ /**
+ * Adds request to replace specified {@code path} with specified {@code val}.
+ *
+ * @param path resource path relative to device root node
+ * @param val resource value
+ * @return self
+ */
+ public SetRequest.Builder replace(ResourceId path, DataNode val) {
+ changes.add(Change.replace(path, val));
+ return this;
+ }
+
+ /**
+ * Adds request to update/merge specified {@code val} to the {@code path}.
+ *
+ * @param path resource path relative to device root node
+ * @param val resource value
+ * @return self
+ */
+ public SetRequest.Builder update(ResourceId path, DataNode val) {
+ changes.add(Change.update(path, val));
+ return this;
+ }
+
+ public SetRequest build() {
+ return new SetRequest(changes);
+ }
+ }
+
+ public static final class Change {
+
+ public enum Operation {
+
+ // Note: equivalent to remove in netconf
+ /**
+ * Request to delete specified {@code path}.
+ * If path does not exist, it is silently ignored.
+ */
+ DELETE,
+ // Note: equivalent to replace in netconf
+ /**
+ * Request to replace specified {@code path} with specified {@code val}.
+ */
+ REPLACE,
+ // Note: equivalent to merge in netconf
+ /**
+ * Request to update/merge specified {@code val} to the {@code path}.
+ */
+ UPDATE
+ }
+
+ private final Operation op;
+ private final ResourceId path;
+ private final Optional<DataNode> val;
+
+ public static Change delete(ResourceId path) {
+ return new Change(Operation.DELETE, path, Optional.empty());
+ }
+
+ public static Change replace(ResourceId path, DataNode val) {
+ return new Change(Operation.REPLACE, path, Optional.of(val));
+ }
+
+ public static Change update(ResourceId path, DataNode val) {
+ return new Change(Operation.UPDATE, path, Optional.of(val));
+ }
+
+ Change(Operation op, ResourceId path, Optional<DataNode> val) {
+ this.op = checkNotNull(op);
+ this.path = checkNotNull(path);
+ this.val = checkNotNull(val);
+ }
+
+ /**
+ * Returns type of change operation.
+ *
+ * @return Operation
+ */
+ public Operation op() {
+ return op;
+ }
+
+ /**
+ * Returns resource path to be changed.
+ *
+ * @return resource path relative to device root node
+ */
+ public ResourceId path() {
+ return path;
+ }
+
+ /**
+ * Returns the {@code val} specified.
+ *
+ * @return {@code val}
+ * @throws NoSuchElementException if this object represent {@code DELETE} op.
+ */
+ public DataNode val() {
+ return val.get();
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj instanceof Change) {
+ Change that = (Change) obj;
+ return Objects.equals(this.op, that.op) &&
+ Objects.equals(this.path, that.path) &&
+ Objects.equals(this.val, that.val);
+ }
+ return false;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(op, path, val);
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(getClass())
+ .add("op", op)
+ .add("path", path)
+ .add("val", val)
+ .toString();
+ }
+ }
+}
\ No newline at end of file
diff --git a/apps/configsync/src/main/java/org/onosproject/d/config/sync/operation/SetResponse.java b/apps/configsync/src/main/java/org/onosproject/d/config/sync/operation/SetResponse.java
new file mode 100644
index 0000000..0bc9502
--- /dev/null
+++ b/apps/configsync/src/main/java/org/onosproject/d/config/sync/operation/SetResponse.java
@@ -0,0 +1,136 @@
+/*
+ * Copyright 2017-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.d.config.sync.operation;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.util.Collection;
+import java.util.Objects;
+
+import org.apache.commons.lang3.tuple.Pair;
+import org.onosproject.d.config.sync.operation.SetRequest.Change.Operation;
+import org.onosproject.yang.model.ResourceId;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.MoreObjects;
+import com.google.common.collect.ImmutableList;
+
+
+@Beta
+public final class SetResponse {
+
+ // partially borrowed from io.grpc.Status.Code,
+ // might want to borrow all of them
+ public enum Code {
+ OK,
+ CANCELLED,
+
+ UNKNOWN,
+
+ INVALID_ARGUMENT,
+
+ NOT_FOUND,
+ ALREADY_EXISTS,
+
+ FAILED_PRECONDITION,
+ ABORTED,
+ UNAVAILABLE,
+ }
+
+ private final Collection<Pair<Operation, ResourceId>> subjects;
+
+ private final SetResponse.Code code;
+
+ // human readable error message for logging purpose
+ private final String message;
+
+ SetResponse(Collection<Pair<Operation, ResourceId>> subjects,
+ SetResponse.Code code,
+ String message) {
+ this.subjects = ImmutableList.copyOf(subjects);
+ this.code = checkNotNull(code);
+ this.message = checkNotNull(message);
+ }
+
+ public Collection<Pair<Operation, ResourceId>> subjects() {
+ return subjects;
+ }
+
+ public Code code() {
+ return code;
+ }
+
+ public String message() {
+ return message;
+ }
+
+
+ /**
+ * Creates SetResponse instance from request.
+ *
+ * @param request original request this response corresponds to
+ * @param code response status code
+ * @param message human readable error message for logging purpose.
+ * can be left empty string on OK response.
+ * @return SetResponse instance
+ */
+ public static SetResponse response(SetRequest request,
+ Code code,
+ String message) {
+ return new SetResponse(request.subjects(), code, checkNotNull(message));
+ }
+
+ /**
+ * Creates successful SetResponce instance from request.
+ *
+ * @param request original request this response corresponds to
+ * @return SetResponse instance
+ */
+ public static SetResponse ok(SetRequest request) {
+ return new SetResponse(request.subjects(), Code.OK, "");
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(subjects, code, message);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj instanceof SetResponse) {
+ SetResponse that = (SetResponse) obj;
+ return Objects.equals(this.subjects, that.subjects) &&
+ Objects.equals(this.code, that.code) &&
+ Objects.equals(this.message, that.message);
+ }
+ return false;
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(getClass())
+ .add("code", code)
+ .add("subjects", subjects)
+ .add("message", message)
+ .toString();
+ }
+
+
+
+}
\ No newline at end of file
diff --git a/apps/configsync/src/main/java/org/onosproject/d/config/sync/operation/package-info.java b/apps/configsync/src/main/java/org/onosproject/d/config/sync/operation/package-info.java
new file mode 100644
index 0000000..d7612e9
--- /dev/null
+++ b/apps/configsync/src/main/java/org/onosproject/d/config/sync/operation/package-info.java
@@ -0,0 +1,19 @@
+/*
+ * Copyright 2017-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.
+ */
+/**
+ * Dynamic config synchronizer API related value objects.
+ */
+package org.onosproject.d.config.sync.operation;