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;
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;
42 public class EndpointRegistrator {
44 private static final Logger LOG = LoggerFactory.getLogger(EndpointRegistrator.class);
45 private final EndpointService epService;
46 private final BaseEndpointService baseEpService;
48 public EndpointRegistrator(EndpointService epService, BaseEndpointService baseEpService) {
49 this.epService = Preconditions.checkNotNull(epService);
50 this.baseEpService = Preconditions.checkNotNull(baseEpService);
53 public boolean registerEndpoint(AddressEndpointReg regEndpointInput) {
54 RegisterEndpointInput regBaseEpInput = new RegisterEndpointInputBuilder().setAddressEndpointReg(
55 ImmutableList.<AddressEndpointReg>of(regEndpointInput))
57 return registerEndpoint(regBaseEpInput);
60 public boolean registerEndpoint(RegisterEndpointInput regBaseEpInput) {
62 RpcResult<Void> rpcResult = baseEpService.registerEndpoint(regBaseEpInput).get();
63 if (!rpcResult.isSuccessful()) {
64 LOG.warn("Illegal state - register Base Endpoint was not successful. Input of RPC: {}", regBaseEpInput);
68 } catch (InterruptedException | ExecutionException e) {
69 LOG.error("Base endpoint registration failed. {}", regBaseEpInput, e);
74 public boolean unregisterEndpoint(AddressEndpointUnreg addrEpUnreg) {
75 UnregisterEndpointInput input = new UnregisterEndpointInputBuilder().setAddressEndpointUnreg(
76 ImmutableList.<AddressEndpointUnreg>of(new AddressEndpointUnregBuilder().setKey(addrEpUnreg.getKey())
78 return unregisterEndpoint(input);
81 public boolean unregisterEndpoint(UnregisterEndpointInput input) {
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);
89 } catch (InterruptedException | ExecutionException e) {
90 LOG.error("unregisterEndpoint failed. {}", input, e);
96 public boolean registerEndpoint(org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterEndpointInput regEndpointInput) {
98 RpcResult<Void> rpcResult = epService.registerEndpoint(regEndpointInput).get();
99 if (!rpcResult.isSuccessful()) {
100 LOG.warn("Illegal state - register Endpoint was not successful. Input of RPC: {}", regEndpointInput);
104 } catch (InterruptedException | ExecutionException e) {
105 LOG.error("registerEndpoint failed. {}", regEndpointInput, e);
111 public boolean unregisterEndpoint(org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.UnregisterEndpointInput unregEndpointInput) {
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);
119 } catch (InterruptedException | ExecutionException e) {
120 LOG.error("unregisterEndpoint failed. {}", unregEndpointInput, e);
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);
134 RegisterL3PrefixEndpointInput registerL3PrefixEpRpcInput = new RegisterL3PrefixEndpointInputBuilder()
135 .setL3Context(l3Context)
136 .setIpPrefix(ipPrefix)
137 .setEndpointGroup(MappingUtils.EPG_EXTERNAL_ID)
139 .setEndpointL3Gateways(l3Gateways)
140 .setTimestamp(System.currentTimeMillis())
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);
150 } catch (InterruptedException | ExecutionException e) {
151 LOG.error("registerExternalL3PrefixEndpoint failed. {}", registerL3PrefixEpRpcInput, e);
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()))
164 .setNetworkContainment(networkContainment)
165 .setEndpointGroups(ImmutableList.of(MappingUtils.EPG_EXTERNAL_ID))
166 .setTimestamp(System.currentTimeMillis())
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);
175 } catch (InterruptedException | ExecutionException e) {
176 LOG.error("registerL3EndpointAsExternalGateway failed. {}", registerEndpointInput, e);
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)
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);
198 } catch (InterruptedException | ExecutionException e) {
199 LOG.error("unregisterL3EndpointAsExternalGateway failed. {}", unregisterEndpointInput, e);