Neutron port removal fixed
[groupbasedpolicy.git] / neutron-mapper / src / main / java / org / opendaylight / groupbasedpolicy / neutron / mapper / EndpointRegistrator.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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 package org.opendaylight.groupbasedpolicy.neutron.mapper;
10
11 import javax.annotation.Nullable;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.concurrent.ExecutionException;
15 import com.google.common.base.Preconditions;
16 import com.google.common.collect.ImmutableList;
17 import org.opendaylight.groupbasedpolicy.neutron.mapper.util.MappingUtils;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.BaseEndpointService;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.RegisterEndpointInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.RegisterEndpointInputBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.UnregisterEndpointInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.UnregisterEndpointInputBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.register.endpoint.input.AddressEndpointReg;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.unregister.endpoint.input.AddressEndpointUnreg;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.unregister.endpoint.input.AddressEndpointUnregBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L3ContextId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.NetworkDomainId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.EndpointService;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterL3PrefixEndpointInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterL3PrefixEndpointInputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.fields.L3AddressBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.l3.prefix.fields.EndpointL3Gateways;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.l3.prefix.fields.EndpointL3GatewaysBuilder;
37 import org.opendaylight.yangtools.yang.common.RpcResult;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 public class EndpointRegistrator {
42
43     private static final Logger LOG = LoggerFactory.getLogger(EndpointRegistrator.class);
44     private final EndpointService epService;
45     private final BaseEndpointService baseEpService;
46
47     public EndpointRegistrator(EndpointService epService, BaseEndpointService baseEpService) {
48         this.epService = Preconditions.checkNotNull(epService);
49         this.baseEpService = Preconditions.checkNotNull(baseEpService);
50     }
51
52     public boolean registerEndpoint(AddressEndpointReg regEndpointInput) {
53         RegisterEndpointInput regBaseEpInput = new RegisterEndpointInputBuilder().setAddressEndpointReg(
54                 ImmutableList.<AddressEndpointReg>of(regEndpointInput))
55             .build();
56         return registerEndpoint(regBaseEpInput);
57     }
58
59     public boolean registerEndpoint(RegisterEndpointInput regBaseEpInput) {
60         try {
61             RpcResult<Void> rpcResult = baseEpService.registerEndpoint(regBaseEpInput).get();
62             if (!rpcResult.isSuccessful()) {
63                 LOG.warn("Illegal state - registerEndpoint was not successful. Input of RPC: {}", regBaseEpInput);
64                 return false;
65             }
66             return true;
67         } catch (InterruptedException | ExecutionException e) {
68             LOG.error("Base endpoint registration failed. {}", regBaseEpInput, e);
69             return false;
70         }
71     }
72
73     public boolean unregisterEndpoint(AddressEndpointUnreg addrEpUnreg) {
74         UnregisterEndpointInput input = new UnregisterEndpointInputBuilder().setAddressEndpointUnreg(
75                 ImmutableList.<AddressEndpointUnreg>of(new AddressEndpointUnregBuilder().setKey(addrEpUnreg.getKey())
76                     .build())).build();
77         return unregisterEndpoint(input);
78     }
79
80     public boolean unregisterEndpoint(UnregisterEndpointInput input) {
81         try {
82             RpcResult<Void> rpcResult = baseEpService.unregisterEndpoint(input).get();
83             if (!rpcResult.isSuccessful()) {
84                 LOG.warn("Illegal state - unregisterEndpoint was not successful. Input of RPC: {}", input);
85                 return false;
86             }
87             return true;
88         } catch (InterruptedException | ExecutionException e) {
89             LOG.error("unregisterEndpoint failed. {}", input, e);
90             return false;
91         }
92     }
93
94     @Deprecated
95     public boolean registerEndpoint(org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterEndpointInput regEndpointInput) {
96         try {
97             RpcResult<Void> rpcResult = epService.registerEndpoint(regEndpointInput).get();
98             if (!rpcResult.isSuccessful()) {
99                 LOG.warn("Illegal state - registerEndpoint was not successful. Input of RPC: {}", regEndpointInput);
100                 return false;
101             }
102             return true;
103         } catch (InterruptedException | ExecutionException e) {
104             LOG.error("registerEndpoint failed. {}", regEndpointInput, e);
105             return false;
106         }
107     }
108
109     @Deprecated
110     public boolean unregisterEndpoint(org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.UnregisterEndpointInput unregEndpointInput) {
111         try {
112             RpcResult<Void> rpcResult = epService.unregisterEndpoint(unregEndpointInput).get();
113             if (!rpcResult.isSuccessful()) {
114                 LOG.warn("Illegal state - unregisterEndpoint was not successful. Input of RPC: {}", unregEndpointInput);
115                 return false;
116             }
117             return true;
118         } catch (InterruptedException | ExecutionException e) {
119             LOG.error("unregisterEndpoint failed. {}", unregEndpointInput, e);
120             return false;
121         }
122     }
123
124     @Deprecated
125     public boolean registerExternalL3PrefixEndpoint(IpPrefix ipPrefix, L3ContextId l3Context,
126             @Nullable IpAddress gatewayIp, TenantId tenantId) {
127         List<EndpointL3Gateways> l3Gateways = new ArrayList<EndpointL3Gateways>();
128         if (gatewayIp != null) {
129             EndpointL3Gateways l3Gateway =
130                     new EndpointL3GatewaysBuilder().setIpAddress(gatewayIp).setL3Context(l3Context).build();
131             l3Gateways.add(l3Gateway);
132         }
133         RegisterL3PrefixEndpointInput registerL3PrefixEpRpcInput = new RegisterL3PrefixEndpointInputBuilder()
134             .setL3Context(l3Context)
135             .setIpPrefix(ipPrefix)
136             .setEndpointGroup(MappingUtils.EPG_EXTERNAL_ID)
137             .setTenant(tenantId)
138             .setEndpointL3Gateways(l3Gateways)
139             .setTimestamp(System.currentTimeMillis())
140             .build();
141         try {
142             RpcResult<Void> rpcResult = epService.registerL3PrefixEndpoint(registerL3PrefixEpRpcInput).get();
143             if (!rpcResult.isSuccessful()) {
144                 LOG.warn("Illegal state - registerExternalL3PrefixEndpoint was not successful. Input of RPC: {}",
145                         registerL3PrefixEpRpcInput);
146                 return false;
147             }
148             return true;
149         } catch (InterruptedException | ExecutionException e) {
150             LOG.error("registerExternalL3PrefixEndpoint failed. {}", registerL3PrefixEpRpcInput, e);
151             return false;
152         }
153     }
154
155     @Deprecated
156     public boolean registerL3EpAsExternalGateway(TenantId tenantId, IpAddress ipAddress, L3ContextId l3Context,
157             NetworkDomainId networkContainment) {
158         org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterEndpointInput registerEndpointInput =
159                 new org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterEndpointInputBuilder()
160                     .setL3Address(ImmutableList
161                         .of(new L3AddressBuilder().setL3Context(l3Context).setIpAddress(ipAddress).build()))
162                     .setTenant(tenantId)
163                     .setNetworkContainment(networkContainment)
164                     .setEndpointGroups(ImmutableList.of(MappingUtils.EPG_EXTERNAL_ID))
165                     .setTimestamp(System.currentTimeMillis())
166                     .build();
167         try {
168             RpcResult<Void> rpcResult = epService.registerEndpoint(registerEndpointInput).get();
169             if (!rpcResult.isSuccessful()) {
170                 LOG.warn("Illegal state - registerL3EndpointAsExternalGateway was not successful. Input of RPC: {}",
171                         registerEndpointInput);
172                 return false;
173             }
174         } catch (InterruptedException | ExecutionException e) {
175             LOG.error("registerL3EndpointAsExternalGateway failed. {}", registerEndpointInput, e);
176             return false;
177         }
178         return true;
179     }
180
181 }