router delete implementation in neutron mapper
[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.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.unregister.endpoint.input.L3Builder;
38 import org.opendaylight.yangtools.yang.common.RpcResult;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 public class EndpointRegistrator {
43
44     private static final Logger LOG = LoggerFactory.getLogger(EndpointRegistrator.class);
45     private final EndpointService epService;
46     private final BaseEndpointService baseEpService;
47
48     public EndpointRegistrator(EndpointService epService, BaseEndpointService baseEpService) {
49         this.epService = Preconditions.checkNotNull(epService);
50         this.baseEpService = Preconditions.checkNotNull(baseEpService);
51     }
52
53     public boolean registerEndpoint(AddressEndpointReg regEndpointInput) {
54         RegisterEndpointInput regBaseEpInput = new RegisterEndpointInputBuilder().setAddressEndpointReg(
55                 ImmutableList.<AddressEndpointReg>of(regEndpointInput))
56             .build();
57         return registerEndpoint(regBaseEpInput);
58     }
59
60     public boolean registerEndpoint(RegisterEndpointInput regBaseEpInput) {
61         try {
62             RpcResult<Void> rpcResult = baseEpService.registerEndpoint(regBaseEpInput).get();
63             if (!rpcResult.isSuccessful()) {
64                 LOG.warn("Illegal state - registerEndpoint was not successful. Input of RPC: {}", regBaseEpInput);
65                 return false;
66             }
67             return true;
68         } catch (InterruptedException | ExecutionException e) {
69             LOG.error("Base endpoint registration failed. {}", regBaseEpInput, e);
70             return false;
71         }
72     }
73
74     public boolean unregisterEndpoint(AddressEndpointUnreg addrEpUnreg) {
75         UnregisterEndpointInput input = new UnregisterEndpointInputBuilder().setAddressEndpointUnreg(
76                 ImmutableList.<AddressEndpointUnreg>of(new AddressEndpointUnregBuilder().setKey(addrEpUnreg.getKey())
77                     .build())).build();
78         return unregisterEndpoint(input);
79     }
80
81     public boolean unregisterEndpoint(UnregisterEndpointInput input) {
82         try {
83             RpcResult<Void> rpcResult = baseEpService.unregisterEndpoint(input).get();
84             if (!rpcResult.isSuccessful()) {
85                 LOG.warn("Illegal state - unregisterEndpoint was not successful. Input of RPC: {}", input);
86                 return false;
87             }
88             return true;
89         } catch (InterruptedException | ExecutionException e) {
90             LOG.error("unregisterEndpoint failed. {}", input, e);
91             return false;
92         }
93     }
94
95     @Deprecated
96     public boolean registerEndpoint(org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterEndpointInput regEndpointInput) {
97         try {
98             RpcResult<Void> rpcResult = epService.registerEndpoint(regEndpointInput).get();
99             if (!rpcResult.isSuccessful()) {
100                 LOG.warn("Illegal state - registerEndpoint was not successful. Input of RPC: {}", regEndpointInput);
101                 return false;
102             }
103             return true;
104         } catch (InterruptedException | ExecutionException e) {
105             LOG.error("registerEndpoint failed. {}", regEndpointInput, e);
106             return false;
107         }
108     }
109
110     @Deprecated
111     public boolean unregisterEndpoint(org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.UnregisterEndpointInput unregEndpointInput) {
112         try {
113             RpcResult<Void> rpcResult = epService.unregisterEndpoint(unregEndpointInput).get();
114             if (!rpcResult.isSuccessful()) {
115                 LOG.warn("Illegal state - unregisterEndpoint was not successful. Input of RPC: {}", unregEndpointInput);
116                 return false;
117             }
118             return true;
119         } catch (InterruptedException | ExecutionException e) {
120             LOG.error("unregisterEndpoint failed. {}", unregEndpointInput, e);
121             return false;
122         }
123     }
124
125     @Deprecated
126     public boolean registerExternalL3PrefixEndpoint(IpPrefix ipPrefix, L3ContextId l3Context,
127             @Nullable IpAddress gatewayIp, TenantId tenantId) {
128         List<EndpointL3Gateways> l3Gateways = new ArrayList<EndpointL3Gateways>();
129         if (gatewayIp != null) {
130             EndpointL3Gateways l3Gateway =
131                     new EndpointL3GatewaysBuilder().setIpAddress(gatewayIp).setL3Context(l3Context).build();
132             l3Gateways.add(l3Gateway);
133         }
134         RegisterL3PrefixEndpointInput registerL3PrefixEpRpcInput = new RegisterL3PrefixEndpointInputBuilder()
135             .setL3Context(l3Context)
136             .setIpPrefix(ipPrefix)
137             .setEndpointGroup(MappingUtils.EPG_EXTERNAL_ID)
138             .setTenant(tenantId)
139             .setEndpointL3Gateways(l3Gateways)
140             .setTimestamp(System.currentTimeMillis())
141             .build();
142         try {
143             RpcResult<Void> rpcResult = epService.registerL3PrefixEndpoint(registerL3PrefixEpRpcInput).get();
144             if (!rpcResult.isSuccessful()) {
145                 LOG.warn("Illegal state - registerExternalL3PrefixEndpoint was not successful. Input of RPC: {}",
146                         registerL3PrefixEpRpcInput);
147                 return false;
148             }
149             return true;
150         } catch (InterruptedException | ExecutionException e) {
151             LOG.error("registerExternalL3PrefixEndpoint failed. {}", registerL3PrefixEpRpcInput, e);
152             return false;
153         }
154     }
155
156     @Deprecated
157     public boolean registerL3EpAsExternalGateway(TenantId tenantId, IpAddress ipAddress, L3ContextId l3Context,
158             NetworkDomainId networkContainment) {
159         org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterEndpointInput registerEndpointInput =
160                 new org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterEndpointInputBuilder()
161                     .setL3Address(ImmutableList
162                         .of(new L3AddressBuilder().setL3Context(l3Context).setIpAddress(ipAddress).build()))
163                     .setTenant(tenantId)
164                     .setNetworkContainment(networkContainment)
165                     .setEndpointGroups(ImmutableList.of(MappingUtils.EPG_EXTERNAL_ID))
166                     .setTimestamp(System.currentTimeMillis())
167                     .build();
168         try {
169             RpcResult<Void> rpcResult = epService.registerEndpoint(registerEndpointInput).get();
170             if (!rpcResult.isSuccessful()) {
171                 LOG.warn("Illegal state - registerL3EndpointAsExternalGateway was not successful. Input of RPC: {}",
172                         registerEndpointInput);
173                 return false;
174             }
175         } catch (InterruptedException | ExecutionException e) {
176             LOG.error("registerL3EndpointAsExternalGateway failed. {}", registerEndpointInput, e);
177             return false;
178         }
179         return true;
180     }
181
182     @Deprecated
183     public boolean unregisterL3EpAsExternalGateway(IpAddress ipAddress, L3ContextId l3Context) {
184         org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.UnregisterEndpointInput unregisterEndpointInput =
185             new org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.UnregisterEndpointInputBuilder()
186                 .setL3(ImmutableList.of(new L3Builder().setL3Context(l3Context)
187                     .setIpAddress(ipAddress)
188                     .build()))
189                 .build();
190
191         try {
192             RpcResult<Void> rpcResult = epService.unregisterEndpoint(unregisterEndpointInput).get();
193             if (!rpcResult.isSuccessful()) {
194                 LOG.warn("Illegal state - unregisterL3EndpointAsExternalGateway was not successful. Input of RPC: {}",
195                     unregisterEndpointInput);
196                 return false;
197             }
198         } catch (InterruptedException | ExecutionException e) {
199             LOG.error("unregisterL3EndpointAsExternalGateway failed. {}", unregisterEndpointInput, e);
200             return false;
201         }
202         return true;
203     }
204 }