Add INFO.yaml for GBP
[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 com.google.common.base.Preconditions;
12 import com.google.common.collect.ImmutableList;
13
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.concurrent.ExecutionException;
17
18 import javax.annotation.Nullable;
19
20 import org.opendaylight.groupbasedpolicy.neutron.mapper.util.MappingUtils;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.BaseEndpointService;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.RegisterEndpointInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.RegisterEndpointInputBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.UnregisterEndpointInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.UnregisterEndpointInputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.register.endpoint.input.AddressEndpointReg;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.unregister.endpoint.input.AddressEndpointUnreg;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.unregister.endpoint.input.AddressEndpointUnregBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L3ContextId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.NetworkDomainId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.EndpointService;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterL3PrefixEndpointInput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterL3PrefixEndpointInputBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.fields.L3AddressBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.l3.prefix.fields.EndpointL3Gateways;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.l3.prefix.fields.EndpointL3GatewaysBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.unregister.endpoint.input.L3Builder;
41 import org.opendaylight.yangtools.yang.common.RpcResult;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 public class EndpointRegistrator {
46
47     private static final Logger LOG = LoggerFactory.getLogger(EndpointRegistrator.class);
48     private final EndpointService epService;
49     private final BaseEndpointService baseEpService;
50
51     public EndpointRegistrator(EndpointService epService, BaseEndpointService baseEpService) {
52         this.epService = Preconditions.checkNotNull(epService);
53         this.baseEpService = Preconditions.checkNotNull(baseEpService);
54     }
55
56     public boolean registerEndpoint(AddressEndpointReg regEndpointInput) {
57         RegisterEndpointInput regBaseEpInput = new RegisterEndpointInputBuilder().setAddressEndpointReg(
58                 ImmutableList.<AddressEndpointReg>of(regEndpointInput))
59             .build();
60         return registerEndpoint(regBaseEpInput);
61     }
62
63     public boolean registerEndpoint(RegisterEndpointInput regBaseEpInput) {
64         try {
65             RpcResult<Void> rpcResult = baseEpService.registerEndpoint(regBaseEpInput).get();
66             if (!rpcResult.isSuccessful()) {
67                 LOG.warn("Illegal state - register Base Endpoint was not successful. Input of RPC: {}", regBaseEpInput);
68                 return false;
69             }
70             return true;
71         } catch (InterruptedException | ExecutionException e) {
72             LOG.error("Base endpoint registration failed. {}", regBaseEpInput, e);
73             return false;
74         }
75     }
76
77     @Deprecated
78     @SuppressWarnings("checkstyle:LineLength") // Longer lines in this method are caused by long package names,
79     // this will be removed when deprecated classes will be cleared.
80     public boolean registerEndpoint(org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterEndpointInput regEndpointInput) {
81         try {
82             RpcResult<Void> rpcResult = epService.registerEndpoint(regEndpointInput).get();
83             if (!rpcResult.isSuccessful()) {
84                 LOG.warn("Illegal state - register Endpoint was not successful. Input of RPC: {}", regEndpointInput);
85                 return false;
86             }
87             return true;
88         } catch (InterruptedException | ExecutionException e) {
89             LOG.error("registerEndpoint failed. {}", regEndpointInput, e);
90             return false;
91         }
92     }
93
94     public boolean unregisterEndpoint(AddressEndpointUnreg addrEpUnreg) {
95         UnregisterEndpointInput input = new UnregisterEndpointInputBuilder().setAddressEndpointUnreg(
96             ImmutableList.<AddressEndpointUnreg>of(new AddressEndpointUnregBuilder().setKey(addrEpUnreg.getKey())
97                 .build())).build();
98         return unregisterEndpoint(input);
99     }
100
101     public boolean unregisterEndpoint(UnregisterEndpointInput input) {
102         try {
103             RpcResult<Void> rpcResult = baseEpService.unregisterEndpoint(input).get();
104             if (!rpcResult.isSuccessful()) {
105                 LOG.warn("Illegal state - unregisterEndpoint was not successful. Input of RPC: {}", input);
106                 return false;
107             }
108             return true;
109         } catch (InterruptedException | ExecutionException e) {
110             LOG.error("unregisterEndpoint failed. {}", input, e);
111             return false;
112         }
113     }
114
115     @Deprecated
116     @SuppressWarnings("checkstyle:LineLength") // Longer lines in this method are caused by long package names,
117                                                // this will be removed when deprecated classes will be cleared.
118     public boolean unregisterEndpoint(org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.UnregisterEndpointInput unregEndpointInput) {
119         try {
120             RpcResult<Void> rpcResult = epService.unregisterEndpoint(unregEndpointInput).get();
121             if (!rpcResult.isSuccessful()) {
122                 LOG.warn("Illegal state - unregisterEndpoint was not successful. Input of RPC: {}", unregEndpointInput);
123                 return false;
124             }
125             return true;
126         } catch (InterruptedException | ExecutionException e) {
127             LOG.error("unregisterEndpoint failed. {}", unregEndpointInput, e);
128             return false;
129         }
130     }
131
132     @Deprecated
133     public boolean registerExternalL3PrefixEndpoint(IpPrefix ipPrefix, L3ContextId l3Context,
134             @Nullable IpAddress gatewayIp, TenantId tenantId) {
135         List<EndpointL3Gateways> l3Gateways = new ArrayList<EndpointL3Gateways>();
136         if (gatewayIp != null) {
137             EndpointL3Gateways l3Gateway =
138                     new EndpointL3GatewaysBuilder().setIpAddress(gatewayIp).setL3Context(l3Context).build();
139             l3Gateways.add(l3Gateway);
140         }
141         RegisterL3PrefixEndpointInput registerL3PrefixEpRpcInput = new RegisterL3PrefixEndpointInputBuilder()
142             .setL3Context(l3Context)
143             .setIpPrefix(ipPrefix)
144             .setEndpointGroup(MappingUtils.EPG_EXTERNAL_ID)
145             .setTenant(tenantId)
146             .setEndpointL3Gateways(l3Gateways)
147             .setTimestamp(System.currentTimeMillis())
148             .build();
149         try {
150             RpcResult<Void> rpcResult = epService.registerL3PrefixEndpoint(registerL3PrefixEpRpcInput).get();
151             if (!rpcResult.isSuccessful()) {
152                 LOG.warn("Illegal state - registerExternalL3PrefixEndpoint was not successful. Input of RPC: {}",
153                         registerL3PrefixEpRpcInput);
154                 return false;
155             }
156             return true;
157         } catch (InterruptedException | ExecutionException e) {
158             LOG.error("registerExternalL3PrefixEndpoint failed. {}", registerL3PrefixEpRpcInput, e);
159             return false;
160         }
161     }
162
163     @Deprecated
164     @SuppressWarnings("checkstyle:LineLength") // Longer lines in this method are caused by long package names,
165                                                // this will be removed when deprecated classes will be cleared.
166     public boolean registerL3EpAsExternalGateway(TenantId tenantId, IpAddress ipAddress, L3ContextId l3Context,
167             NetworkDomainId networkContainment) {
168         org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterEndpointInput
169             registerEndpointInput =
170             new org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterEndpointInputBuilder()
171                 .setL3Address(
172                     ImmutableList.of(new L3AddressBuilder().setL3Context(l3Context).setIpAddress(ipAddress).build()))
173                 .setTenant(tenantId)
174                 .setNetworkContainment(networkContainment)
175                 .setEndpointGroups(ImmutableList.of(MappingUtils.EPG_EXTERNAL_ID))
176                 .setTimestamp(System.currentTimeMillis())
177                 .build();
178         try {
179             RpcResult<Void> rpcResult = epService.registerEndpoint(registerEndpointInput).get();
180             if (!rpcResult.isSuccessful()) {
181                 LOG.warn("Illegal state - registerL3EndpointAsExternalGateway was not successful. Input of RPC: {}",
182                         registerEndpointInput);
183                 return false;
184             }
185         } catch (InterruptedException | ExecutionException e) {
186             LOG.error("registerL3EndpointAsExternalGateway failed. {}", registerEndpointInput, e);
187             return false;
188         }
189         return true;
190     }
191
192     @Deprecated
193     @SuppressWarnings("checkstyle:LineLength") // Longer lines in this method are caused by long package names,
194                                                // this will be removed when deprecated classes will be cleared.
195     public boolean unregisterL3EpAsExternalGateway(IpAddress ipAddress, L3ContextId l3Context) {
196         org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.UnregisterEndpointInput
197             unregisterEndpointInput =
198             new org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.UnregisterEndpointInputBuilder()
199                 .setL3(ImmutableList.of(new L3Builder().setL3Context(l3Context).setIpAddress(ipAddress).build()))
200                 .build();
201
202         try {
203             RpcResult<Void> rpcResult = epService.unregisterEndpoint(unregisterEndpointInput).get();
204             if (!rpcResult.isSuccessful()) {
205                 LOG.warn("Illegal state - unregisterL3EndpointAsExternalGateway was not successful. Input of RPC: {}",
206                     unregisterEndpointInput);
207                 return false;
208             }
209         } catch (InterruptedException | ExecutionException e) {
210             LOG.error("unregisterL3EndpointAsExternalGateway failed. {}", unregisterEndpointInput, e);
211             return false;
212         }
213         return true;
214     }
215 }