Created RegionService and added unit tests.
Change-Id: If3735d160ead9269b8bb327a99d6dad85e5c57d0
diff --git a/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/RegionProtoTranslator.java b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/RegionProtoTranslator.java
new file mode 100644
index 0000000..5baea7a
--- /dev/null
+++ b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/RegionProtoTranslator.java
@@ -0,0 +1,94 @@
+/*
+ * 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 com.google.common.base.Strings;
+import org.onosproject.incubator.protobuf.models.ProtobufUtils;
+import org.onosproject.incubator.protobuf.models.net.region.RegionEnumsProtoTranslator;
+
+
+
+import org.onosproject.cluster.NodeId;
+import org.onosproject.grpc.net.models.RegionProtoOuterClass;
+import org.onosproject.net.Annotations;
+import org.onosproject.net.region.DefaultRegion;
+import org.onosproject.net.region.Region;
+import org.onosproject.net.region.RegionId;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * gRPC Region message to @link org.onosproject.net.region.Region conversion related utilities for region service.
+ */
+public final class RegionProtoTranslator {
+
+ /**
+ * Translates gRPC RegionProto message to {@link org.onosproject.net.region.Region}.
+ *
+ * @param region gRPC message
+ * @return {@link org.onosproject.net.region.Region}
+ */
+ public static Region translate(RegionProtoOuterClass.RegionProto region) {
+ RegionId id = RegionId.regionId(region.getRegionId());
+ Region.Type type = RegionEnumsProtoTranslator.translate(region.getType()).get();
+ String name = Strings.nullToEmpty(region.getName());
+
+ List<Set<NodeId>> masters = new ArrayList<>();
+
+ region.getMastersList().forEach(s -> {
+ Set<NodeId> nodeIdSet = new HashSet<NodeId>();
+ s.getNodeIdList().forEach(n -> {
+ nodeIdSet.add(new NodeId(n));
+ });
+ masters.add(nodeIdSet);
+ });
+
+ Annotations annots = ProtobufUtils.asAnnotations(region.getAnnotations());
+
+ return new DefaultRegion(id, name, type, annots, masters);
+ }
+
+ /**
+ * Translates {@link org.onosproject.net.region.Region} to gRPC RegionProto message.
+ *
+ * @param region {@link org.onosproject.net.region.Region}
+ * @return gRPC RegionProto message
+ */
+ public static RegionProtoOuterClass.RegionProto translate(Region region) {
+ return RegionProtoOuterClass.RegionProto.newBuilder()
+ .setRegionId(region.id().toString())
+ .setType(RegionEnumsProtoTranslator.translate(region.type()))
+ .setName(region.name().isEmpty() ? null : region.name())
+ .addAllMasters(region.masters()
+ .stream()
+ .map(s -> RegionProtoOuterClass.RegionProto.NodeIdSet
+ .newBuilder()
+ .addAllNodeId(s.stream().map(id -> {
+ return id.toString();
+ }).collect(Collectors.toList()))
+ .build())
+ .collect(Collectors.toList()))
+ .build();
+ }
+
+ // Utility class not intended for instantiation.
+ private RegionProtoTranslator() {}
+
+}
+
diff --git a/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/region/RegionEnumsProtoTranslator.java b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/region/RegionEnumsProtoTranslator.java
new file mode 100644
index 0000000..6f3130f
--- /dev/null
+++ b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/region/RegionEnumsProtoTranslator.java
@@ -0,0 +1,104 @@
+/*
+ * 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.region;
+
+import org.onosproject.grpc.net.region.models.RegionEnumsProto;
+import org.onosproject.net.region.Region;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Optional;
+
+/**
+ * gRPC RegionType message to equivalent ONOS enum conversion related utilities.
+ */
+public final class RegionEnumsProtoTranslator {
+
+ private static final Logger log = LoggerFactory.getLogger(RegionEnumsProtoTranslator.class);
+
+ /**
+ * Translates gRPC enum RegionType to Optional of ONOS enum.
+ *
+ * @param type regiontype type in gRPC enum
+ * @return Optional of equivalent ONOS enum or empty if not recognized
+ */
+ public static Optional<Region.Type> translate(RegionEnumsProto.RegionTypeProto type) {
+ switch (type) {
+ case CONTINENT:
+ return Optional.of(Region.Type.CONTINENT);
+ case COUNTRY:
+ return Optional.of(Region.Type.COUNTRY);
+ case METRO:
+ return Optional.of(Region.Type.METRO);
+ case CAMPUS:
+ return Optional.of(Region.Type.CAMPUS);
+ case BUILDING:
+ return Optional.of(Region.Type.BUILDING);
+ case DATA_CENTER:
+ return Optional.of(Region.Type.DATA_CENTER);
+ case FLOOR:
+ return Optional.of(Region.Type.FLOOR);
+ case ROOM:
+ return Optional.of(Region.Type.ROOM);
+ case RACK:
+ return Optional.of(Region.Type.RACK);
+ case LOGICAL_GROUP:
+ return Optional.of(Region.Type.LOGICAL_GROUP);
+ default:
+ log.warn("Unrecognized Type gRPC message: {}", type);
+ return Optional.empty();
+ }
+ }
+
+
+ /**
+ * Translates ONOS enum regionType to gRPC enum regionType.
+ *
+ * @param type ONOS' Type type
+ * @return equivalent gRPC message enum
+ */
+ public static RegionEnumsProto.RegionTypeProto translate(Region.Type type) {
+ switch (type) {
+ case CONTINENT:
+ return RegionEnumsProto.RegionTypeProto.CONTINENT;
+ case COUNTRY:
+ return RegionEnumsProto.RegionTypeProto.COUNTRY;
+ case METRO:
+ return RegionEnumsProto.RegionTypeProto.METRO;
+ case CAMPUS:
+ return RegionEnumsProto.RegionTypeProto.CAMPUS;
+ case BUILDING:
+ return RegionEnumsProto.RegionTypeProto.BUILDING;
+ case DATA_CENTER:
+ return RegionEnumsProto.RegionTypeProto.DATA_CENTER;
+ case FLOOR:
+ return RegionEnumsProto.RegionTypeProto.FLOOR;
+ case ROOM:
+ return RegionEnumsProto.RegionTypeProto.ROOM;
+ case RACK:
+ return RegionEnumsProto.RegionTypeProto.RACK;
+ case LOGICAL_GROUP:
+ return RegionEnumsProto.RegionTypeProto.LOGICAL_GROUP;
+ default:
+ log.warn("Unrecognized type", type);
+ throw new IllegalArgumentException("Unrecognized Type");
+ }
+ }
+
+ // Utility class not intended for instantiation.
+ private RegionEnumsProtoTranslator() {}
+}
+
diff --git a/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/region/package-info.java b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/region/package-info.java
new file mode 100644
index 0000000..459ad32
--- /dev/null
+++ b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/region/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.
+ */
+/**
+ * Utilities to handle ProtoBuf version of ONOS region models.
+ */
+package org.onosproject.incubator.protobuf.models.net.region;