BUG-6650: ep-sgt/ip, propose initial sxp-ep-provider
[groupbasedpolicy.git] / groupbasedpolicy-ise-adapter / src / main / java / org / opendaylight / groupbasedpolicy / gbp_ise_adapter / impl / SgtToEpgGeneratorImpl.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.gbp_ise_adapter.impl;
10
11 import com.google.common.util.concurrent.CheckedFuture;
12 import java.util.List;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
17 import org.opendaylight.groupbasedpolicy.util.IidFactory;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.Description;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.Name;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroup;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroupBuilder;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * Purpose: process given sgt+name - create {@link EndpointGroup} and write it to tenants/tenant/policy/endpoint-group
30  */
31 public class SgtToEpgGeneratorImpl implements SgtInfoProcessor {
32
33     private static final Logger LOG = LoggerFactory.getLogger(SgtToEpgGeneratorImpl.class);
34
35     private final DataBroker dataBroker;
36
37     public SgtToEpgGeneratorImpl(final DataBroker dataBroker) {
38         this.dataBroker = dataBroker;
39     }
40
41     @Override
42     public CheckedFuture<Void, TransactionCommitFailedException> processSgtInfo(final TenantId tenantId, final List<SgtInfo> sgtInfos) {
43         final WriteTransaction wTx = dataBroker.newWriteOnlyTransaction();
44
45         // create and write endpointgroups
46         boolean createParent = true;
47         for (SgtInfo sgtInfo : sgtInfos) {
48             final Integer sgtValue = sgtInfo.getSgt().getValue();
49             final String sgtName = sgtInfo.getName();
50             LOG.trace("processing sgtInfo: {} - {}", sgtValue, sgtName);
51
52             final EndpointGroupId epgId = new EndpointGroupId(sgtName);
53             final InstanceIdentifier<EndpointGroup> epgPath = IidFactory.endpointGroupIid(tenantId, epgId);
54             final EndpointGroup epg = new EndpointGroupBuilder()
55                     .setId(epgId)
56                     .setDescription(new Description("imported from ISE for sgt=" + sgtValue))
57                     .setName(new Name(sgtName.replaceAll(" ", "_") + "--" + sgtValue))
58                     .build();
59             wTx.put(LogicalDatastoreType.CONFIGURATION, epgPath, epg, createParent);
60             createParent = false;
61         }
62         return wTx.submit();
63     }
64 }