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.Function;
12 import com.google.common.base.Objects;
13 import com.google.common.base.Optional;
14 import com.google.common.base.Preconditions;
15 import com.google.common.base.Predicate;
16 import com.google.common.collect.FluentIterable;
18 import java.util.Collections;
19 import java.util.List;
22 import javax.annotation.Nullable;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.SecurityRules;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRule;
29 public class SecurityRuleUtils {
31 public static Optional<SecurityRule> findSecurityRule(Uuid uuid, @Nullable SecurityRules securityRules) {
32 if (securityRules == null || securityRules.getSecurityRule() == null) {
33 return Optional.absent();
35 for (SecurityRule secRule : securityRules.getSecurityRule()) {
36 if (secRule.getUuid().equals(uuid)) {
37 return Optional.of(secRule);
40 return Optional.absent();
43 public static Set<SecurityRule> findSecurityRulesBySecGroupAndRemoteSecGroup(Uuid secGroup,
44 @Nullable Uuid remoteSecGroup, Neutron neutron) {
45 Preconditions.checkNotNull(secGroup);
46 return FluentIterable.from(findAllSecurityRules(neutron)).filter(new Predicate<SecurityRule>() {
49 public boolean apply(SecurityRule secRule) {
50 return (secRule.getSecurityGroupId().equals(secGroup)
51 && Objects.equal(secRule.getRemoteGroupId(), remoteSecGroup));
56 public static Set<Uuid> findSecurityGroupsHavingSecurityRules(Neutron neutron) {
57 return FluentIterable.from(findAllSecurityRules(neutron)).transform(new Function<SecurityRule, Uuid>() {
60 public Uuid apply(SecurityRule secRule) {
61 return secRule.getSecurityGroupId();
66 public static List<SecurityRule> findAllSecurityRules(Neutron neutron) {
67 Preconditions.checkNotNull(neutron);
68 SecurityRules securityRules = neutron.getSecurityRules();
69 if (securityRules == null || securityRules.getSecurityRule() == null) {
70 return Collections.emptyList();
72 return securityRules.getSecurityRule();