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.yangtools.yang.common.RpcResult;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 public class EndpointRegistrator {
43 private static final Logger LOG = LoggerFactory.getLogger(EndpointRegistrator.class);
44 private final EndpointService epService;
45 private final BaseEndpointService baseEpService;
47 public EndpointRegistrator(EndpointService epService, BaseEndpointService baseEpService) {
48 this.epService = Preconditions.checkNotNull(epService);
49 this.baseEpService = Preconditions.checkNotNull(baseEpService);
52 public boolean registerEndpoint(AddressEndpointReg regEndpointInput) {
53 RegisterEndpointInput regBaseEpInput = new RegisterEndpointInputBuilder().setAddressEndpointReg(
54 ImmutableList.<AddressEndpointReg>of(regEndpointInput))
56 return registerEndpoint(regBaseEpInput);
59 public boolean registerEndpoint(RegisterEndpointInput regBaseEpInput) {
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);
67 } catch (InterruptedException | ExecutionException e) {
68 LOG.error("Base endpoint registration failed. {}", regBaseEpInput, e);
73 public boolean unregisterEndpoint(AddressEndpointUnreg addrEpUnreg) {
74 UnregisterEndpointInput input = new UnregisterEndpointInputBuilder().setAddressEndpointUnreg(
75 ImmutableList.<AddressEndpointUnreg>of(new AddressEndpointUnregBuilder().setKey(addrEpUnreg.getKey())
77 return unregisterEndpoint(input);
80 public boolean unregisterEndpoint(UnregisterEndpointInput input) {
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);
88 } catch (InterruptedException | ExecutionException e) {
89 LOG.error("unregisterEndpoint failed. {}", input, e);
95 public boolean registerEndpoint(org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterEndpointInput regEndpointInput) {
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);
103 } catch (InterruptedException | ExecutionException e) {
104 LOG.error("registerEndpoint failed. {}", regEndpointInput, e);
110 public boolean unregisterEndpoint(org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.UnregisterEndpointInput unregEndpointInput) {
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);
118 } catch (InterruptedException | ExecutionException e) {
119 LOG.error("unregisterEndpoint failed. {}", unregEndpointInput, e);
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);
133 RegisterL3PrefixEndpointInput registerL3PrefixEpRpcInput = new RegisterL3PrefixEndpointInputBuilder()
134 .setL3Context(l3Context)
135 .setIpPrefix(ipPrefix)
136 .setEndpointGroup(MappingUtils.EPG_EXTERNAL_ID)
138 .setEndpointL3Gateways(l3Gateways)
139 .setTimestamp(System.currentTimeMillis())
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);
149 } catch (InterruptedException | ExecutionException e) {
150 LOG.error("registerExternalL3PrefixEndpoint failed. {}", registerL3PrefixEpRpcInput, e);
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()))
163 .setNetworkContainment(networkContainment)
164 .setEndpointGroups(ImmutableList.of(MappingUtils.EPG_EXTERNAL_ID))
165 .setTimestamp(System.currentTimeMillis())
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);
174 } catch (InterruptedException | ExecutionException e) {
175 LOG.error("registerL3EndpointAsExternalGateway failed. {}", registerEndpointInput, e);