2 * Copyright (c) 2016 Inocybe Technologies 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.vpnservice.impl;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.UUID;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.nic.utils.MdsalUtils;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.constraints.rev150122.FailoverType;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.Intents;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.IntentsBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.Actions;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.ActionsBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.Constraints;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.ConstraintsBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.Subjects;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.SubjectsBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.actions.action.allow.AllowBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.actions.action.block.BlockBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.constraints.constraints.failover.constraint.FailoverConstraintBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.constraints.constraints.protection.constraint.ProtectionConstraintBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.subjects.subject.end.point.group.EndPointGroup;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.subjects.subject.end.point.group.EndPointGroupBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intents.Intent;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intents.IntentBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intents.IntentKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.types.rev150122.Uuid;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 import com.google.common.base.Preconditions;
44 public class IntentServiceManager {
47 * This class is used to build Intents object and
48 * write it to Network Intent Composition md-sal tree
49 * in order to create/delete intents between two endpoint groups.
52 private static final Logger LOG = LoggerFactory.getLogger(IntentServiceManager.class);
53 private static final short FIRST_SUBJECT = 1;
54 private static final short SECOND_SUBJECT = 2;
55 public static final String ACTION_ALLOW = "ALLOW";
56 public static final String ACTION_BLOCK = "BLOCK";
57 public static final String FAST_REROUTE = "fast-reroute";
58 public static final String SLOW_REROUTE = "slow-reroute";
59 private final DataBroker dataBroker;
60 private static final InstanceIdentifier<Intents> INTENTS_IID = InstanceIdentifier.builder(Intents.class).build();
61 private MdsalUtils mdsal;
63 public IntentServiceManager(DataBroker dataBroker) {
64 this.dataBroker = dataBroker;
65 this.mdsal = new MdsalUtils(dataBroker);
69 * Create Intents object and write to to config tree to trigger intents
70 * @param src :Source Site Name
71 * @param dst :Destination Site Name
72 * @param intentAction :Intent verb: ALLOW or BLOCK
75 public void addIntent(String src, String dst, String intentAction, String failOverType) {
76 Preconditions.checkNotNull(src);
77 Preconditions.checkNotNull(dst);
78 Preconditions.checkNotNull(intentAction);
80 List<Intent> intentList = null;
81 List<Subjects> subjects = createSubjects(dst, src);
82 List<Actions> intentActions = createActions(intentAction);
83 List<Constraints> intentConstraints = createConstraints(failOverType);
85 Intent intent = new IntentBuilder().setId(new Uuid(UUID.randomUUID().toString()))
86 .setSubjects(subjects).setActions(intentActions)
87 .setConstraints(intentConstraints)
90 Intents currentIntents = mdsal.read(LogicalDatastoreType.CONFIGURATION, INTENTS_IID);
91 if (currentIntents == null) {
92 intentList = new ArrayList<>();
94 intentList = currentIntents.getIntent();
96 intentList.add(intent);
97 Intents intents = new IntentsBuilder().setIntent(intentList).build();
98 mdsal.put(LogicalDatastoreType.CONFIGURATION, INTENTS_IID, intents);
99 LOG.info("AddIntent: config populated: {}", intents);
104 * @param id :Uuid of the Intent to be deleted
106 public void removeIntent(Uuid id) {
107 Preconditions.checkNotNull(id);
108 InstanceIdentifier<Intent> iid = InstanceIdentifier.create(Intents.class).child(Intent.class, new IntentKey(id));
109 mdsal.delete(LogicalDatastoreType.CONFIGURATION, iid);
110 LOG.info("RemoveIntent succeeded");
114 * Remove all associated intents by endpointGroupName
115 * @param endpointGroupName
117 public void removeIntentsByEndpoint(String endpointGroupName) {
118 Preconditions.checkNotNull(endpointGroupName);
120 Intents intents = mdsal.read(LogicalDatastoreType.CONFIGURATION, INTENTS_IID);
122 if (intents != null && intents.getIntent() != null) {
123 for (Intent intent : intents.getIntent()) {
124 if (intent.getSubjects() != null && intent.getSubjects().size() > 0) {
125 String endpointValue = "";
126 for (Subjects subject : intent.getSubjects()) {
128 .getSubject() instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.subjects.subject.EndPointGroup) {
129 org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.subjects.subject.EndPointGroup epg = (org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.subjects.subject.EndPointGroup) subject
131 endpointValue = epg.getEndPointGroup().getName();
133 .getSubject() instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.subjects.subject.EndPointSelector) {
134 org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.subjects.subject.EndPointSelector epg = (org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.subjects.subject.EndPointSelector) subject
136 endpointValue = epg.getEndPointSelector().getEndPointSelector();
138 .getSubject() instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.subjects.subject.EndPointGroupSelector) {
139 org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.subjects.subject.EndPointGroupSelector epg = (org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.subjects.subject.EndPointGroupSelector) subject
141 endpointValue = epg.getEndPointGroupSelector().getEndPointGroupSelector();
143 if (endpointValue.equalsIgnoreCase(endpointGroupName)) {
144 removeIntent(intent.getId());
145 LOG.info("Deleted Intent ID : {} for endpoint: {}", intent.getId(), endpointGroupName);
154 * Create a list of Intent actions
155 * @param intentAction
156 * @return :a list of Actions
158 private List<Actions> createActions(String intentAction) {
159 List<Actions> actionsList = new ArrayList<Actions>();
161 org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.actions.Action action = null;
162 if (intentAction.equalsIgnoreCase(ACTION_ALLOW)) {
163 action = new org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.actions.action
164 .AllowBuilder().setAllow(new AllowBuilder().build()).build();
165 } else if (intentAction.equalsIgnoreCase(ACTION_BLOCK)) {
166 action = new org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.actions.action
167 .BlockBuilder().setBlock(new BlockBuilder().build()).build();
170 Actions intentActions = new ActionsBuilder().setOrder(order).setAction(action).build();
171 actionsList.add(intentActions);
176 * Create a list of Intent subjects
177 * @param src :Source Site Name
178 * @param dst :Destination Site Name
179 * @return :a list of Subjects
181 private List<Subjects> createSubjects(String src, String dst) {
182 List<Subjects> subjectList = new ArrayList<Subjects>();
184 EndPointGroup endpointGroupFrom = new EndPointGroupBuilder().setName(src).build();
185 org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.subjects.subject.EndPointGroup fromEPG =
186 new org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.subjects.subject
187 .EndPointGroupBuilder().setEndPointGroup(endpointGroupFrom).build();
188 Subjects subjects1 = new SubjectsBuilder().setOrder(FIRST_SUBJECT).setSubject(fromEPG).build();
190 EndPointGroup endpointGroupTo = new EndPointGroupBuilder().setName(dst).build();
191 org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.subjects.subject.EndPointGroup toEPG =
192 new org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.subjects.subject
193 .EndPointGroupBuilder().setEndPointGroup(endpointGroupTo).build();
194 Subjects subjects2 = new SubjectsBuilder().setOrder(SECOND_SUBJECT).setSubject(toEPG).build();
196 subjectList.add(subjects1);
197 subjectList.add(subjects2);
202 * Create a list of Intent constraints
203 * @param failOverType :Type of failover, fast-reroute or slow-reroute
206 private List<Constraints> createConstraints(String failOverType) {
207 List<Constraints> intentConstraints = new ArrayList<Constraints>();
208 if (failOverType==null) {
209 return intentConstraints;
212 org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.constraints.Constraints
213 protectionConstraint = null;
214 org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.constraints.Constraints
215 failoverConstraint = null;
216 if (failOverType.equals(FAST_REROUTE)) {
217 protectionConstraint = new org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent
218 .constraints.constraints.ProtectionConstraintBuilder()
219 .setProtectionConstraint(new ProtectionConstraintBuilder().setIsProtected(true).build()).build();
220 failoverConstraint = new org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.constraints
221 .constraints.FailoverConstraintBuilder()
222 .setFailoverConstraint(new FailoverConstraintBuilder().setFailoverSelector(FailoverType.FastReroute).build())
224 } else if (failOverType.equals(SLOW_REROUTE)) {
225 protectionConstraint = new org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent
226 .constraints.constraints.ProtectionConstraintBuilder()
227 .setProtectionConstraint(new ProtectionConstraintBuilder().setIsProtected(true).build()).build();
228 failoverConstraint = new org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.constraints
229 .constraints.FailoverConstraintBuilder()
230 .setFailoverConstraint(new FailoverConstraintBuilder().setFailoverSelector(FailoverType.SlowReroute).build())
233 Constraints constraint1 = new ConstraintsBuilder().setOrder(order).setConstraints(protectionConstraint).build();
234 Constraints constraint2 = new ConstraintsBuilder().setOrder(++order).setConstraints(failoverConstraint).build();
235 intentConstraints.add(constraint1);
236 intentConstraints.add(constraint2);
237 return intentConstraints;