[ONOS-6316]create gRPC northbound Host Service and add unit tests
Change-Id: I385f52576000affd2523579c6a6f4c6d57e69938
diff --git a/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/ConnectPointProtoTranslator.java b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/ConnectPointProtoTranslator.java
index c0177fe..6620096 100644
--- a/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/ConnectPointProtoTranslator.java
+++ b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/ConnectPointProtoTranslator.java
@@ -16,7 +16,7 @@
package org.onosproject.incubator.protobuf.models.net;
import org.onlab.packet.IpAddress;
-import org.onosproject.grpc.net.models.ConnectPointProtoOuterClass;
+import org.onosproject.grpc.net.models.ConnectPointProtoOuterClass.ConnectPointProto;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.DeviceId;
import org.onosproject.net.HostId;
@@ -40,7 +40,7 @@
* @param connectPoint gRPC message
* @return Optional of equivalent {@link org.onosproject.net.ConnectPoint} or empty if ElementId is not recognized
*/
- public static Optional<ConnectPoint> translate(ConnectPointProtoOuterClass.ConnectPointProto connectPoint) {
+ public static Optional<ConnectPoint> translate(ConnectPointProto connectPoint) {
switch (connectPoint.getElementIdCase()) {
case DEVICE_ID:
return Optional.of(new ConnectPoint(DeviceId.deviceId(connectPoint.getDeviceId()),
@@ -65,20 +65,18 @@
* @param connectPoint {@link org.onosproject.net.ConnectPoint}
* @return gRPC ConnectPoint message
*/
- public static ConnectPointProtoOuterClass.ConnectPointProto translate(ConnectPoint connectPoint) {
+ public static ConnectPointProto translate(ConnectPoint connectPoint) {
+
if (connectPoint.elementId() instanceof DeviceId) {
- return ConnectPointProtoOuterClass.ConnectPointProto.newBuilder()
- .setDeviceId(connectPoint.deviceId().toString())
+ return ConnectPointProto.newBuilder().setDeviceId(connectPoint.deviceId().toString())
.setPortNumber(connectPoint.port().toString())
.build();
} else if (connectPoint.elementId() instanceof HostId) {
- return ConnectPointProtoOuterClass.ConnectPointProto.newBuilder()
- .setHostId(connectPoint.hostId().toString())
+ return ConnectPointProto.newBuilder().setHostId(connectPoint.hostId().toString())
.setPortNumber(connectPoint.port().toString())
.build();
} else if (connectPoint.ipElementId() instanceof IpElementId) {
- return ConnectPointProtoOuterClass.ConnectPointProto.newBuilder()
- .setIpElementId(connectPoint.ipElementId().toString())
+ return ConnectPointProto.newBuilder().setIpElementId(connectPoint.ipElementId().toString())
.setPortNumber(connectPoint.port().toString())
.build();
} else {
@@ -87,8 +85,6 @@
}
}
-
// Utility class not intended for instantiation.
private ConnectPointProtoTranslator() {}
-}
-
+}
\ No newline at end of file
diff --git a/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/HostIdProtoTranslator.java b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/HostIdProtoTranslator.java
new file mode 100644
index 0000000..859cca4
--- /dev/null
+++ b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/HostIdProtoTranslator.java
@@ -0,0 +1,64 @@
+/*
+ * 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.incubator.protobuf.models.net;
+
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.VlanId;
+import org.onosproject.grpc.net.models.HostIdProtoOuterClass.HostIdProto;
+import org.onosproject.net.HostId;
+
+/**
+ * gRPC HostIdProto message to equivalent ONOS HostId conversion related utilities.
+ */
+public final class HostIdProtoTranslator {
+
+ /**
+ * Translates gRPC HostId to {@link HostId}.
+ *
+ * @param hostId gRPC message
+ * @return {@link HostId}
+ */
+ public static HostId translate(HostIdProto hostId) {
+
+ if (hostId.equals(HostIdProto.getDefaultInstance())) {
+ return null;
+ }
+
+ return HostId.hostId(MacAddress.valueOf(hostId.getMac()), VlanId.vlanId((short) hostId.getVlanId()));
+ }
+
+ /**
+ * Translates {@link HostId} to gRPC HostId message.
+ *
+ * @param hostId {@link HostId}
+ * @return gRPC HostId message
+ */
+ public static HostIdProto translate(HostId hostId) {
+
+ if (hostId != null) {
+ return HostIdProto.newBuilder()
+ .setMac(hostId.mac().toString())
+ .setVlanId(hostId.vlanId().toShort())
+ .build();
+ }
+
+ return HostIdProto.getDefaultInstance();
+ }
+
+ // Utility class not intended for instantiation.
+ private HostIdProtoTranslator() {}
+}
diff --git a/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/HostLocationProtoTranslator.java b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/HostLocationProtoTranslator.java
new file mode 100644
index 0000000..098a276
--- /dev/null
+++ b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/HostLocationProtoTranslator.java
@@ -0,0 +1,68 @@
+/*
+ * 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.incubator.protobuf.models.net;
+
+import org.onosproject.grpc.net.models.ConnectPointProtoOuterClass.ConnectPointProto;
+import org.onosproject.grpc.net.models.HostLocationProtoOuterClass.HostLocationProto;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.HostLocation;
+import org.onosproject.net.PortNumber;
+
+/**
+ * gRPC HostLocationProto message to equivalent ONOS Host location conversion related utilities.
+ */
+public final class HostLocationProtoTranslator {
+
+ /**
+ * Translates gRPC HostLocation to {@link HostLocation}.
+ *
+ * @param hostLocation gRPC message
+ * @return {@link HostLocation}
+ */
+ public static HostLocation translate(HostLocationProto hostLocation) {
+
+ if (hostLocation.equals(HostLocationProto.getDefaultInstance())) {
+ return null;
+ }
+
+ return new HostLocation(DeviceId.deviceId(hostLocation.getConnectPoint().getDeviceId()),
+ PortNumber.portNumber(hostLocation.getConnectPoint().getPortNumber()), 0L);
+ }
+
+ /**
+ * Translates {@link HostLocation} to gRPC HostLocation message.
+ *
+ * @param hostLocation {@link HostLocation}
+ * @return gRPC HostLocation message
+ */
+ public static HostLocationProto translate(HostLocation hostLocation) {
+
+ if (hostLocation != null) {
+ return HostLocationProto.newBuilder()
+ .setConnectPoint(ConnectPointProto.newBuilder()
+ .setDeviceId(hostLocation.deviceId().toString())
+ .setPortNumber(hostLocation.port().toString()))
+ .setTime(hostLocation.time())
+ .build();
+ }
+
+ return HostLocationProto.getDefaultInstance();
+ }
+
+ // Utility class not intended for instantiation.
+ private HostLocationProtoTranslator() {}
+}
diff --git a/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/HostProtoTranslator.java b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/HostProtoTranslator.java
new file mode 100644
index 0000000..c12fd7f
--- /dev/null
+++ b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/HostProtoTranslator.java
@@ -0,0 +1,81 @@
+/*
+ * 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.incubator.protobuf.models.net;
+
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.VlanId;
+import org.onosproject.grpc.net.models.HostProtoOuterClass.HostProto;
+import org.onosproject.net.DefaultAnnotations;
+import org.onosproject.net.DefaultHost;
+import org.onosproject.net.Host;
+
+import java.util.stream.Collectors;
+
+/**
+ * gRPC HostProto message to equivalent ONOS Host conversion related utilities.
+ */
+public final class HostProtoTranslator {
+
+
+ /**
+ * Translates {@link Host} to gRPC Host message.
+ *
+ * @param host {@link Host}
+ * @return gRPC HostProto message
+ */
+ public static HostProto translate(Host host) {
+
+ if (host != null) {
+ return HostProto.newBuilder()
+ .setHostId(HostIdProtoTranslator.translate(host.id()))
+ .setConfigured(host.configured())
+ .setVlan(host.vlan().toShort())
+ .addAllIpAddresses(host.ipAddresses().stream()
+ .map(IpAddress::toString)
+ .collect(Collectors.toList()))
+ .setLocation(HostLocationProtoTranslator.translate(host.location()))
+ .build();
+ }
+
+ return HostProto.getDefaultInstance();
+ }
+
+ /**
+ * Translates gRPC Host message to {@link Host}.
+ *
+ * @param host gRPC message
+ * @return {@link Host}
+ */
+ public static Host translate(HostProto host) {
+ if (host.equals(HostProto.getDefaultInstance())) {
+ return null;
+ }
+
+ return new DefaultHost(ProviderIdProtoTranslator.translate(host.getProviderId()),
+ HostIdProtoTranslator.translate(host.getHostId()),
+ MacAddress.valueOf(host.getHostId().getMac()),
+ VlanId.vlanId((short) host.getVlan()),
+ HostLocationProtoTranslator.translate(host.getLocation()),
+ host.getIpAddressesList().stream().map(x -> IpAddress.valueOf(x))
+ .collect(Collectors.toSet()),
+ DefaultAnnotations.builder().putAll(host.getAnnotationsMap()).build());
+ }
+
+ // Utility class not intended for instantiation.
+ private HostProtoTranslator() {}
+}