[ONOS-7905] Add default implementation for kubernetes data model
Change-Id: I08e9266dec2050e8299e4189745608a6e03fd06e
diff --git a/apps/k8s-networking/api/src/main/java/org/onosproject/k8snetworking/api/DefaultK8sNetwork.java b/apps/k8s-networking/api/src/main/java/org/onosproject/k8snetworking/api/DefaultK8sNetwork.java
new file mode 100644
index 0000000..3ac1230
--- /dev/null
+++ b/apps/k8s-networking/api/src/main/java/org/onosproject/k8snetworking/api/DefaultK8sNetwork.java
@@ -0,0 +1,187 @@
+/*
+ * Copyright 2019-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.k8snetworking.api;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.base.Objects;
+import org.onlab.packet.IpAddress;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+/**
+ * Default implementation of kubernetes network.
+ */
+public final class DefaultK8sNetwork implements K8sNetwork {
+
+ private static final int DEFAULT_MTU = 1500;
+
+ private final String networkId;
+ private final Type type;
+ private final Integer mtu;
+ private final String segmentId;
+ private final IpAddress gatewayIp;
+ private final String cidr;
+
+ private static final String NOT_NULL_MSG = "Network % cannot be null";
+
+ // private constructor not intended for external invocation
+ private DefaultK8sNetwork(String networkId, Type type, Integer mtu,
+ String segmentId, IpAddress gatewayIp, String cidr) {
+ this.networkId = networkId;
+ this.type = type;
+ this.mtu = mtu;
+ this.segmentId = segmentId;
+ this.gatewayIp = gatewayIp;
+ this.cidr = cidr;
+ }
+
+ @Override
+ public String networkId() {
+ return networkId;
+ }
+
+ @Override
+ public Type type() {
+ return type;
+ }
+
+ @Override
+ public Integer mtu() {
+ return mtu;
+ }
+
+ @Override
+ public String segmentId() {
+ return segmentId;
+ }
+
+ @Override
+ public IpAddress gatewayIp() {
+ return gatewayIp;
+ }
+
+ @Override
+ public String cidr() {
+ return cidr;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ DefaultK8sNetwork that = (DefaultK8sNetwork) o;
+ return Objects.equal(networkId, that.networkId) &&
+ type == that.type &&
+ Objects.equal(mtu, that.mtu) &&
+ Objects.equal(segmentId, that.segmentId) &&
+ Objects.equal(gatewayIp, that.gatewayIp) &&
+ Objects.equal(cidr, that.cidr);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hashCode(networkId, type, mtu, segmentId, gatewayIp, cidr);
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(this)
+ .add("networkId", networkId)
+ .add("type", type)
+ .add("mtu", mtu)
+ .add("segmentId", segmentId)
+ .add("gatewayIp", gatewayIp)
+ .add("cidr", cidr)
+ .toString();
+ }
+
+ /**
+ * Returns new builder instance.
+ *
+ * @return kubernetes network builder
+ */
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ /**
+ * Default implementation of kubernetes network builder.
+ */
+ public static final class Builder implements K8sNetwork.Builder {
+
+ private String networkId;
+ private Type type;
+ private Integer mtu;
+ private String segmentId;
+ private IpAddress gatewayIp;
+ private String cidr;
+
+ @Override
+ public K8sNetwork build() {
+ checkArgument(networkId != null, NOT_NULL_MSG, "networkId");
+ checkArgument(type != null, NOT_NULL_MSG, "type");
+ checkArgument(segmentId != null, NOT_NULL_MSG, "segmentId");
+ checkArgument(gatewayIp != null, NOT_NULL_MSG, "gatewayIp");
+ checkArgument(cidr != null, NOT_NULL_MSG, "cidr");
+
+ if (mtu == null) {
+ mtu = DEFAULT_MTU;
+ }
+
+ return new DefaultK8sNetwork(networkId, type, mtu, segmentId, gatewayIp, cidr);
+ }
+
+ @Override
+ public Builder networkId(String networkId) {
+ this.networkId = networkId;
+ return this;
+ }
+
+ @Override
+ public Builder type(Type type) {
+ this.type = type;
+ return this;
+ }
+
+ @Override
+ public Builder mtu(Integer mtu) {
+ this.mtu = mtu;
+ return this;
+ }
+
+ @Override
+ public Builder segmentId(String segmentId) {
+ this.segmentId = segmentId;
+ return this;
+ }
+
+ @Override
+ public Builder gatewayIp(IpAddress ipAddress) {
+ this.gatewayIp = ipAddress;
+ return this;
+ }
+
+ @Override
+ public Builder cidr(String cidr) {
+ this.cidr = cidr;
+ return this;
+ }
+ }
+}