2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.groupbasedpolicy.neutron.mapper.util;
11 import com.google.common.base.Objects;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.FluentIterable;
15 import java.util.Collections;
16 import java.util.List;
18 import javax.annotation.Nullable;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.SecurityRules;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRule;
24 public final class SecurityRuleUtils {
25 private SecurityRuleUtils() {
28 public static Optional<SecurityRule> findSecurityRule(Uuid uuid, @Nullable SecurityRules securityRules) {
29 if (securityRules == null || securityRules.getSecurityRule() == null) {
30 return Optional.absent();
32 for (SecurityRule secRule : securityRules.getSecurityRule()) {
33 if (secRule.getUuid().equals(uuid)) {
34 return Optional.of(secRule);
37 return Optional.absent();
40 public static Set<SecurityRule> findSecurityRulesBySecGroupAndRemoteSecGroup(Uuid secGroup,
41 @Nullable Uuid remoteSecGroup, Neutron neutron) {
42 Preconditions.checkNotNull(secGroup);
43 return FluentIterable.from(findAllSecurityRules(neutron)).filter(
44 secRule -> (secRule.getSecurityGroupId().equals(secGroup)
45 && Objects.equal(secRule.getRemoteGroupId(), remoteSecGroup))).toSet();
48 public static Set<Uuid> findSecurityGroupsHavingSecurityRules(Neutron neutron) {
49 return FluentIterable.from(findAllSecurityRules(neutron)).transform(
50 secRule -> secRule.getSecurityGroupId()).toSet();
53 public static List<SecurityRule> findAllSecurityRules(Neutron neutron) {
54 Preconditions.checkNotNull(neutron);
55 SecurityRules securityRules = neutron.getSecurityRules();
56 if (securityRules == null || securityRules.getSecurityRule() == null) {
57 return Collections.emptyList();
59 return securityRules.getSecurityRule();