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