Merge "EPG controller and service"
[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 java.util.ArrayList;
12 import java.util.List;
13 import java.util.concurrent.ExecutionException;
14
15 import javax.annotation.Nullable;
16
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.rev100924.IpAddress;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L3ContextId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.NetworkDomainId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.EndpointService;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterEndpointInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterEndpointInputBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterL3PrefixEndpointInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterL3PrefixEndpointInputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.UnregisterEndpointInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.fields.L3AddressBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.l3.prefix.fields.EndpointL3Gateways;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.l3.prefix.fields.EndpointL3GatewaysBuilder;
32 import org.opendaylight.yangtools.yang.common.RpcResult;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import com.google.common.base.Preconditions;
37 import com.google.common.collect.ImmutableList;
38
39 public class EndpointRegistrator {
40
41     private static final Logger LOG = LoggerFactory.getLogger(EndpointRegistrator.class);
42     private final EndpointService epService;
43
44     public EndpointRegistrator(EndpointService epService) {
45         this.epService = Preconditions.checkNotNull(epService);
46     }
47
48     public boolean registerEndpoint(RegisterEndpointInput regEndpointInput) {
49         try {
50             RpcResult<Void> rpcResult = epService.registerEndpoint(regEndpointInput).get();
51             if (!rpcResult.isSuccessful()) {
52                 LOG.warn("Illegal state - registerEndpoint was not successful. Input of RPC: {}", regEndpointInput);
53                 return false;
54             }
55             return true;
56         } catch (InterruptedException | ExecutionException e) {
57             LOG.error("registerEndpoint failed. {}", regEndpointInput, e);
58             return false;
59         }
60     }
61
62     public boolean unregisterEndpoint(UnregisterEndpointInput unregEndpointInput) {
63         try {
64             RpcResult<Void> rpcResult = epService.unregisterEndpoint(unregEndpointInput).get();
65             if (!rpcResult.isSuccessful()) {
66                 LOG.warn("Illegal state - unregisterEndpoint was not successful. Input of RPC: {}", unregEndpointInput);
67                 return false;
68             }
69             return true;
70         } catch (InterruptedException | ExecutionException e) {
71             LOG.error("unregisterEndpoint failed. {}", unregEndpointInput, e);
72             return false;
73         }
74     }
75
76     public boolean registerExternalL3PrefixEndpoint(IpPrefix ipPrefix, L3ContextId l3Context,
77             @Nullable IpAddress gatewayIp, TenantId tenantId) {
78         List<EndpointL3Gateways> l3Gateways = new ArrayList<EndpointL3Gateways>();
79         if (gatewayIp != null) {
80             EndpointL3Gateways l3Gateway =
81                     new EndpointL3GatewaysBuilder().setIpAddress(gatewayIp).setL3Context(l3Context).build();
82             l3Gateways.add(l3Gateway);
83         }
84         RegisterL3PrefixEndpointInput registerL3PrefixEpRpcInput = new RegisterL3PrefixEndpointInputBuilder()
85             .setL3Context(l3Context)
86             .setIpPrefix(ipPrefix)
87             .setEndpointGroup(MappingUtils.EPG_EXTERNAL_ID)
88             .setTenant(tenantId)
89             .setEndpointL3Gateways(l3Gateways)
90             .setTimestamp(System.currentTimeMillis())
91             .build();
92         try {
93             RpcResult<Void> rpcResult = epService.registerL3PrefixEndpoint(registerL3PrefixEpRpcInput).get();
94             if (!rpcResult.isSuccessful()) {
95                 LOG.warn("Illegal state - registerExternalL3PrefixEndpoint was not successful. Input of RPC: {}",
96                         registerL3PrefixEpRpcInput);
97                 return false;
98             }
99             return true;
100         } catch (InterruptedException | ExecutionException e) {
101             LOG.error("registerExternalL3PrefixEndpoint failed. {}", registerL3PrefixEpRpcInput, e);
102             return false;
103         }
104     }
105
106     public boolean registerL3EndpointAsExternalGateway(TenantId tenantId, IpAddress ipAddress, L3ContextId l3Context,
107             NetworkDomainId networkContainment) {
108         RegisterEndpointInput registerEndpointInput =
109                 new RegisterEndpointInputBuilder()
110                     .setL3Address(ImmutableList
111                         .of(new L3AddressBuilder().setL3Context(l3Context).setIpAddress(ipAddress).build()))
112                     .setTenant(tenantId)
113                     .setNetworkContainment(networkContainment)
114                     .setEndpointGroups(ImmutableList.of(MappingUtils.EPG_EXTERNAL_ID))
115                     .setTimestamp(System.currentTimeMillis())
116                     .build();
117         try {
118             RpcResult<Void> rpcResult = epService.registerEndpoint(registerEndpointInput).get();
119             if (!rpcResult.isSuccessful()) {
120                 LOG.warn("Illegal state - registerL3EndpointAsExternalGateway was not successful. Input of RPC: {}",
121                         registerEndpointInput);
122                 return false;
123             }
124         } catch (InterruptedException | ExecutionException e) {
125             LOG.error("registerL3EndpointAsExternalGateway failed. {}", registerEndpointInput, e);
126             return false;
127         }
128         return true;
129     }
130
131 }