Merge "Add md-transcribing for LoadBalancerListener."
[neutron.git] / transcriber / src / main / java / org / opendaylight / neutron / transcriber / NeutronSecurityRuleInterface.java
1 /*
2  * Copyright (C) 2014 Red Hat, Inc.
3  *
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
7  *
8  */
9
10 package org.opendaylight.neutron.transcriber;
11
12 import java.lang.reflect.Method;
13 import java.util.ArrayList;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Map.Entry;
17 import java.util.Set;
18 import java.util.concurrent.ConcurrentHashMap;
19 import java.util.concurrent.ConcurrentMap;
20
21 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
22 import org.opendaylight.neutron.spi.INeutronSecurityRuleCRUD;
23 import org.opendaylight.neutron.spi.NeutronSecurityRule;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150325.Neutron;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev141002.SecurityRuleAttrs;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev141002.security.rules.attributes.SecurityRules;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev141002.security.rules.attributes.security.rules.SecurityRule;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev141002.security.rules.attributes.security.rules.SecurityRuleBuilder;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34
35 public class NeutronSecurityRuleInterface extends AbstractNeutronInterface<SecurityRule, NeutronSecurityRule> implements INeutronSecurityRuleCRUD {
36     private static final Logger logger = LoggerFactory.getLogger(NeutronSecurityRuleInterface.class);
37     private ConcurrentMap<String, NeutronSecurityRule> securityRuleDB  = new ConcurrentHashMap<String, NeutronSecurityRule>();
38
39
40     NeutronSecurityRuleInterface(ProviderContext providerContext) {
41         super(providerContext);
42     }
43
44     // this method uses reflection to update an object from it's delta.
45     private boolean overwrite(Object target, Object delta) {
46         Method[] methods = target.getClass().getMethods();
47
48         for (Method toMethod : methods) {
49             if (toMethod.getDeclaringClass().equals(target.getClass())
50                 && toMethod.getName().startsWith("set")) {
51
52                 String toName = toMethod.getName();
53                 String fromName = toName.replace("set", "get");
54
55                 try {
56                     Method fromMethod = delta.getClass().getMethod(fromName);
57                     Object value = fromMethod.invoke(delta, (Object[]) null);
58                     if (value != null) {
59                         toMethod.invoke(target, value);
60                     }
61                 } catch (Exception e) {
62                     e.printStackTrace();
63                     return false;
64                 }
65             }
66         }
67         return true;
68     }
69
70     @Override
71     public boolean neutronSecurityRuleExists(String uuid) {
72         return securityRuleDB.containsKey(uuid);
73     }
74
75     @Override
76     public NeutronSecurityRule getNeutronSecurityRule(String uuid) {
77         if (!neutronSecurityRuleExists(uuid)) {
78             logger.debug("No Security Rules Have Been Defined");
79             return null;
80         }
81         return securityRuleDB.get(uuid);
82     }
83
84     @Override
85     public List<NeutronSecurityRule> getAllNeutronSecurityRules() {
86         Set<NeutronSecurityRule> allSecurityRules = new HashSet<NeutronSecurityRule>();
87         for (Entry<String, NeutronSecurityRule> entry : securityRuleDB.entrySet()) {
88             NeutronSecurityRule securityRule = entry.getValue();
89             allSecurityRules.add(securityRule);
90         }
91         logger.debug("Exiting getSecurityRule, Found {} OpenStackSecurityRule", allSecurityRules.size());
92         List<NeutronSecurityRule> ans = new ArrayList<NeutronSecurityRule>();
93         ans.addAll(allSecurityRules);
94         return ans;
95     }
96
97     @Override
98     public boolean addNeutronSecurityRule(NeutronSecurityRule input) {
99         if (neutronSecurityRuleExists(input.getSecurityRuleUUID())) {
100             return false;
101         }
102         securityRuleDB.putIfAbsent(input.getSecurityRuleUUID(), input);
103         addMd(input);
104         return true;
105     }
106
107     @Override
108     public boolean removeNeutronSecurityRule(String uuid) {
109         if (!neutronSecurityRuleExists(uuid)) {
110             return false;
111         }
112         securityRuleDB.remove(uuid);
113         removeMd(toMd(uuid));
114         return true;
115     }
116
117     @Override
118     public boolean updateNeutronSecurityRule(String uuid, NeutronSecurityRule delta) {
119         if (!neutronSecurityRuleExists(uuid)) {
120             return false;
121         }
122         NeutronSecurityRule target = securityRuleDB.get(uuid);
123         updateMd(delta);
124         return overwrite(target, delta);
125     }
126
127     @Override
128     public boolean neutronSecurityRuleInUse(String securityRuleUUID) {
129         return !neutronSecurityRuleExists(securityRuleUUID);
130     }
131
132     @Override
133     protected SecurityRule toMd(NeutronSecurityRule securityRule) {
134         SecurityRuleBuilder securityRuleBuilder = new SecurityRuleBuilder();
135
136         if (securityRule.getSecurityRuleTenantID() != null) {
137             securityRuleBuilder.setTenantId(toUuid(securityRule.getSecurityRuleTenantID()));
138         }
139         if (securityRule.getSecurityRuleDirection() != null) {
140             securityRuleBuilder.setDirection(SecurityRuleAttrs.Direction.valueOf(securityRule
141                     .getSecurityRuleDirection()));
142         }
143         if (securityRule.getSecurityRuleGroupID() != null) {
144             securityRuleBuilder.setSecurityGroupId(toUuid(securityRule.getSecurityRuleGroupID()));
145         }
146         if (securityRule.getSecurityRemoteGroupID() != null) {
147             securityRuleBuilder.setRemoteGroupId(toUuid(securityRule.getSecurityRemoteGroupID()));
148         }
149         if (securityRule.getSecurityRuleRemoteIpPrefix() != null) {
150             IpAddress ipAddress = new IpAddress(securityRule.getSecurityRuleRemoteIpPrefix().toCharArray());
151             securityRuleBuilder.setRemoteIpPrefix(ipAddress);
152         }
153         if (securityRule.getSecurityRuleProtocol() != null) {
154             securityRuleBuilder.setProtocol(SecurityRuleAttrs.Protocol.valueOf(securityRule.getSecurityRuleProtocol()));
155         }
156         if (securityRule.getSecurityRuleEthertype() != null) {
157             securityRuleBuilder.setEthertype(SecurityRuleAttrs.Ethertype.valueOf(securityRule
158                     .getSecurityRuleEthertype()));
159         }
160         if (securityRule.getSecurityRulePortMin() != null) {
161             securityRuleBuilder.setPortRangeMin(new Long(securityRule.getSecurityRulePortMin()));
162         }
163         if (securityRule.getSecurityRulePortMax() != null) {
164             securityRuleBuilder.setPortRangeMax(new Long(securityRule.getSecurityRulePortMax()));
165         }
166         if (securityRule.getSecurityRuleUUID() != null) {
167             securityRuleBuilder.setId(toUuid(securityRule.getSecurityRuleUUID()));
168         } else {
169             logger.warn("Attempting to write neutron securityRule without UUID");
170         }
171         return securityRuleBuilder.build();
172     }
173
174     @Override
175     protected InstanceIdentifier<SecurityRule> createInstanceIdentifier(SecurityRule securityRule) {
176         return InstanceIdentifier.create(Neutron.class).child(SecurityRules.class).child(SecurityRule.class);
177     }
178
179     @Override
180     protected SecurityRule toMd(String uuid) {
181         SecurityRuleBuilder securityRuleBuilder = new SecurityRuleBuilder();
182         securityRuleBuilder.setId(toUuid(uuid));
183         return securityRuleBuilder.build();
184     }
185 }