Merge "Added support of network-domain in EP's network-containment for vpp-rednerer"
[groupbasedpolicy.git] / renderers / vpp / src / main / java / org / opendaylight / groupbasedpolicy / renderer / vpp / iface / VppEndpointLocationProvider.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.renderer.vpp.iface;
10
11 import static com.google.common.base.Preconditions.checkNotNull;
12
13 import javax.annotation.Nonnull;
14
15 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.CloseOnFailTransactionChain;
20 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.KeyFactory;
21 import org.opendaylight.groupbasedpolicy.util.IidFactory;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.AbsoluteLocation;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.AbsoluteLocationBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.location.type.ExternalLocationCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.location.type.ExternalLocationCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.ProviderName;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.LocationProvider;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.LocationProviderBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderAddressEndpointLocation;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderAddressEndpointLocationBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderAddressEndpointLocationKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.endpoints.AddressEndpointWithLocationKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.config.VppEndpoint;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import com.google.common.util.concurrent.FutureCallback;
38 import com.google.common.util.concurrent.Futures;
39
40 public class VppEndpointLocationProvider implements AutoCloseable {
41
42     private static final Logger LOG = LoggerFactory.getLogger(VppEndpointLocationProvider.class);
43     public static final ProviderName VPP_ENDPOINT_LOCATION_PROVIDER =
44             new ProviderName("VPP endpoint location provider");
45     public static final long PROVIDER_PRIORITY = 10L;
46     private final BindingTransactionChain txChain;
47
48     public VppEndpointLocationProvider(DataBroker dataProvider) {
49         LocationProvider locationProvider = new LocationProviderBuilder().setProvider(VPP_ENDPOINT_LOCATION_PROVIDER)
50             .setPriority(PROVIDER_PRIORITY)
51             .build();
52         txChain = checkNotNull(dataProvider)
53             .createTransactionChain(new CloseOnFailTransactionChain(VppEndpointLocationProvider.class.getSimpleName()));
54         WriteTransaction wTx = txChain.newWriteOnlyTransaction();
55         wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.locationProviderIid(VPP_ENDPOINT_LOCATION_PROVIDER),
56                 locationProvider, true);
57
58         Futures.addCallback(wTx.submit(), new FutureCallback<Void>() {
59
60             @Override
61             public void onSuccess(Void result) {
62                 LOG.debug("{} was created", VPP_ENDPOINT_LOCATION_PROVIDER.getValue());
63             }
64
65             @Override
66             public void onFailure(Throwable t) {
67                 LOG.error("{} was NOT created", VPP_ENDPOINT_LOCATION_PROVIDER.getValue());
68             }
69         });
70     }
71
72     public void createLocationForVppEndpoint(VppEndpoint vppEndpoint) {
73         ProviderAddressEndpointLocation providerAddressEndpointLocation =
74                 createProviderAddressEndpointLocation(vppEndpoint);
75         WriteTransaction wTx = txChain.newWriteOnlyTransaction();
76         wTx.put(LogicalDatastoreType.CONFIGURATION,
77                 IidFactory.providerAddressEndpointLocationIid(VPP_ENDPOINT_LOCATION_PROVIDER,
78                         providerAddressEndpointLocation.getKey()),
79                 providerAddressEndpointLocation);
80         LOG.debug("Creating location for {}", providerAddressEndpointLocation.getKey());
81         Futures.addCallback(wTx.submit(), new FutureCallback<Void>() {
82
83             @Override
84             public void onSuccess(Void result) {
85                 LOG.debug("{} provided location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
86                         providerAddressEndpointLocation);
87             }
88
89             @Override
90             public void onFailure(Throwable t) {
91                 LOG.error("{} failed to provide location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
92                         providerAddressEndpointLocation, t);
93             }
94         });
95     }
96
97     public static ProviderAddressEndpointLocation createProviderAddressEndpointLocation(VppEndpoint vppEndpoint) {
98         String restIfacePath = VppPathMapper.interfaceToRestPath(vppEndpoint.getVppInterfaceName());
99         AbsoluteLocation absoluteLocation = new AbsoluteLocationBuilder()
100             .setLocationType(new ExternalLocationCaseBuilder().setExternalNodeMountPoint(vppEndpoint.getVppNodePath())
101                 .setExternalNodeConnector(restIfacePath)
102                 .build())
103             .build();
104         return new ProviderAddressEndpointLocationBuilder()
105             .setKey(createProviderAddressEndpointLocationKey(vppEndpoint))
106             .setAbsoluteLocation(absoluteLocation)
107             .build();
108     }
109
110     public void deleteLocationForVppEndpoint(VppEndpoint vppEndpoint) {
111         ProviderAddressEndpointLocationKey provAddrEpLocKey = createProviderAddressEndpointLocationKey(vppEndpoint);
112         WriteTransaction wTx = txChain.newWriteOnlyTransaction();
113         wTx.delete(LogicalDatastoreType.CONFIGURATION,
114                 IidFactory.providerAddressEndpointLocationIid(VPP_ENDPOINT_LOCATION_PROVIDER, provAddrEpLocKey));
115         LOG.debug("Deleting location for {}", provAddrEpLocKey);
116         Futures.addCallback(wTx.submit(), new FutureCallback<Void>() {
117
118             @Override
119             public void onSuccess(Void result) {
120                 LOG.debug("{} removed location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(), provAddrEpLocKey);
121             }
122
123             @Override
124             public void onFailure(Throwable t) {
125                 LOG.error("{} failed to remove location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
126                         provAddrEpLocKey, t);
127             }
128         });
129     }
130
131     private static ProviderAddressEndpointLocationKey createProviderAddressEndpointLocationKey(
132             VppEndpoint vppEndpoint) {
133         return new ProviderAddressEndpointLocationKey(vppEndpoint.getAddress(), vppEndpoint.getAddressType(),
134                 vppEndpoint.getContextId(), vppEndpoint.getContextType());
135     }
136
137     public void replaceLocationForEndpoint(@Nonnull ExternalLocationCase location, @Nonnull AddressEndpointWithLocationKey addrEpWithLocKey) {
138         ProviderAddressEndpointLocationKey provAddrEpLocKey =
139                 KeyFactory.providerAddressEndpointLocationKey(addrEpWithLocKey);
140         AbsoluteLocation absoluteLocation =
141                 new AbsoluteLocationBuilder().setLocationType(location).build();
142         ProviderAddressEndpointLocation providerAddressEndpointLocation = new ProviderAddressEndpointLocationBuilder()
143             .setKey(provAddrEpLocKey).setAbsoluteLocation(absoluteLocation).build();
144         WriteTransaction wTx = txChain.newWriteOnlyTransaction();
145         wTx.put(LogicalDatastoreType.CONFIGURATION,
146                 IidFactory.providerAddressEndpointLocationIid(VPP_ENDPOINT_LOCATION_PROVIDER,
147                         providerAddressEndpointLocation.getKey()),
148                 providerAddressEndpointLocation);
149         LOG.debug("Updating location for {}", provAddrEpLocKey);
150         Futures.addCallback(wTx.submit(), new FutureCallback<Void>() {
151
152             @Override
153             public void onSuccess(Void result) {
154                 LOG.debug("{} replaced location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
155                         providerAddressEndpointLocation);
156             }
157
158             @Override
159             public void onFailure(Throwable t) {
160                 LOG.error("{} failed to replace location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
161                         providerAddressEndpointLocation, t);
162             }
163         });
164     }
165
166     @Override
167     public void close() throws Exception {
168         WriteTransaction wTx = txChain.newWriteOnlyTransaction();
169         wTx.delete(LogicalDatastoreType.CONFIGURATION, IidFactory.locationProviderIid(VPP_ENDPOINT_LOCATION_PROVIDER));
170         wTx.submit();
171     }
172 }