99d0edc9c66fe3c659f2aac12486e52b7f48de08
[vpnservice.git] / vpnintent / impl / src / main / java / org / opendaylight / vpnservice / impl / IntentServiceManager.java
1 /*
2  * Copyright (c) 2016 Inocybe Technologies 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.vpnservice.impl;
10
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.UUID;
14
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;
40
41 import com.google.common.base.Preconditions;
42
43
44 public class IntentServiceManager {
45
46     /**
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.
50      */
51
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;
62
63     public IntentServiceManager(DataBroker dataBroker) {
64         this.dataBroker = dataBroker;
65         this.mdsal = new MdsalUtils(dataBroker);
66     }
67
68     /**
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
73      * @param failOverType
74      */
75     public void addIntent(String src, String dst, String intentAction, String failOverType) {
76         Preconditions.checkNotNull(src);
77         Preconditions.checkNotNull(dst);
78         Preconditions.checkNotNull(intentAction);
79
80         List<Intent> intentList = null;
81         List<Subjects> subjects = createSubjects(dst, src);
82         List<Actions> intentActions = createActions(intentAction);
83         List<Constraints> intentConstraints = createConstraints(failOverType);
84
85         Intent intent  = new IntentBuilder().setId(new Uuid(UUID.randomUUID().toString()))
86                 .setSubjects(subjects).setActions(intentActions)
87                 .setConstraints(intentConstraints)
88                 .build();
89
90         Intents currentIntents = mdsal.read(LogicalDatastoreType.CONFIGURATION, INTENTS_IID);
91         if (currentIntents == null) {
92             intentList = new ArrayList<>();
93         } else {
94             intentList = currentIntents.getIntent();
95         }
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);
100     }
101
102     /**
103      * Delete an Intent
104      * @param id :Uuid of the Intent to be deleted
105      */
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");
111     }
112
113     /**
114      * Remove all associated intents by endpointGroupName
115      * @param endpointGroupName
116      */
117     public void removeIntentsByEndpoint(String endpointGroupName) {
118         Preconditions.checkNotNull(endpointGroupName);
119
120         Intents intents = mdsal.read(LogicalDatastoreType.CONFIGURATION, INTENTS_IID);
121
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()) {
127                         if (subject
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
130                                     .getSubject();
131                             endpointValue = epg.getEndPointGroup().getName();
132                         } else if (subject
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
135                                     .getSubject();
136                             endpointValue = epg.getEndPointSelector().getEndPointSelector();
137                         } else if (subject
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
140                                     .getSubject();
141                             endpointValue = epg.getEndPointGroupSelector().getEndPointGroupSelector();
142                         }
143                         if (endpointValue.equalsIgnoreCase(endpointGroupName)) {
144                             removeIntent(intent.getId());
145                             LOG.info("Deleted Intent ID : {} for endpoint: {}", intent.getId(), endpointGroupName);
146                         }
147                     }
148                 }
149             }
150         }
151     }
152
153     /**
154      * Create a list of Intent actions
155      * @param intentAction
156      * @return :a list of Actions
157      */
158     private List<Actions> createActions(String intentAction) {
159         List<Actions> actionsList = new ArrayList<Actions>();
160         short order = 1;
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();
168         }
169
170         Actions intentActions = new ActionsBuilder().setOrder(order).setAction(action).build();
171         actionsList.add(intentActions);
172         return actionsList;
173     }
174
175     /**
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
180      */
181     private List<Subjects> createSubjects(String src, String dst) {
182         List<Subjects> subjectList = new ArrayList<Subjects>();
183
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();
189
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();
195
196         subjectList.add(subjects1);
197         subjectList.add(subjects2);
198         return subjectList;
199     }
200
201     /**
202      * Create a list of Intent constraints
203      * @param failOverType :Type of failover, fast-reroute or slow-reroute
204      * @return
205      */
206     private List<Constraints> createConstraints(String failOverType) {
207         List<Constraints> intentConstraints = new ArrayList<Constraints>();
208         if (failOverType==null) {
209             return intentConstraints;
210         }
211         short order = 1;
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())
223                     .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())
231                     .build();
232         }
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;
238     }
239 }