Code cleanup for vpnintent module as per comments
[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.vpnservice.utils.IidFactory;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.constraints.rev150122.FailoverType;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.Intents;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.IntentsBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.Actions;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.ActionsBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.Constraints;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.ConstraintsBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.Subjects;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.SubjectsBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.actions.action.allow.AllowBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.actions.action.block.BlockBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.constraints.constraints.failover.constraint.FailoverConstraintBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.constraints.constraints.protection.constraint.ProtectionConstraintBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.subjects.subject.end.point.group.EndPointGroup;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.subjects.subject.end.point.group.EndPointGroupBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intents.Intent;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intents.IntentBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intents.IntentKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.types.rev150122.Uuid;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 import com.google.common.base.Preconditions;
43
44 /**
45  * This class is used to build Intents object and
46  * write it to Network Intent Composition md-sal tree
47  * in order to create/delete intents between two endpoint groups.
48  */
49 public class IntentServiceManager {
50
51     private static final Logger LOG = LoggerFactory.getLogger(IntentServiceManager.class);
52     private static final short FIRST_SUBJECT = 1;
53     private static final short SECOND_SUBJECT = 2;
54     public static final String ACTION_ALLOW = "ALLOW";
55     public static final String ACTION_BLOCK = "BLOCK";
56     public static final String FAST_REROUTE = "fast-reroute";
57     public static final String SLOW_REROUTE = "slow-reroute";
58     private final DataBroker dataBroker;
59     private static final InstanceIdentifier<Intents> INTENTS_IID = IidFactory.getIntentsIid();
60     private final MdsalUtils mdsal;
61
62     public IntentServiceManager(DataBroker dataBroker) {
63         this.dataBroker = dataBroker;
64         this.mdsal = new MdsalUtils(dataBroker);
65     }
66
67     /**
68      * Create Intents object and write to to config tree to trigger intents
69      * @param src :Source Site Name
70      * @param dst :Destination Site Name
71      * @param intentAction :Intent verb: ALLOW or BLOCK
72      * @param failOverType
73      */
74     public void addIntent(String src, String dst, String intentAction, String failOverType) {
75         Preconditions.checkNotNull(src);
76         Preconditions.checkNotNull(dst);
77         Preconditions.checkNotNull(intentAction);
78
79         List<Intent> intentList = null;
80         List<Subjects> subjects = createSubjects(dst, src);
81         List<Actions> intentActions = createActions(intentAction);
82         List<Constraints> intentConstraints = createConstraints(failOverType);
83
84         Intent intent  = new IntentBuilder().setId(new Uuid(UUID.randomUUID().toString()))
85                 .setSubjects(subjects).setActions(intentActions)
86                 .setConstraints(intentConstraints)
87                 .build();
88
89         Intents currentIntents = mdsal.read(LogicalDatastoreType.CONFIGURATION, INTENTS_IID);
90         if (currentIntents == null) {
91             intentList = new ArrayList<>();
92         } else {
93             intentList = currentIntents.getIntent();
94         }
95         intentList.add(intent);
96         Intents intents = new IntentsBuilder().setIntent(intentList).build();
97         mdsal.put(LogicalDatastoreType.CONFIGURATION, INTENTS_IID, intents);
98         LOG.info("AddIntent: config populated: {}", intents);
99     }
100
101     /**
102      * Delete an Intent
103      * @param id :Uuid of the Intent to be deleted
104      */
105     public void removeIntent(Uuid id) {
106         Preconditions.checkNotNull(id);
107         InstanceIdentifier<Intent> iid = InstanceIdentifier.create(Intents.class).child(Intent.class, new IntentKey(id));
108         mdsal.delete(LogicalDatastoreType.CONFIGURATION, iid);
109         LOG.info("RemoveIntent succeeded");
110     }
111
112     /**
113      * Remove all associated intents by endpointGroupName
114      * @param endpointGroupName
115      */
116     public void removeIntentsByEndpoint(String endpointGroupName) {
117         Preconditions.checkNotNull(endpointGroupName);
118
119         Intents intents = mdsal.read(LogicalDatastoreType.CONFIGURATION, INTENTS_IID);
120
121         if (intents != null && intents.getIntent() != null) {
122             for (Intent intent : intents.getIntent()) {
123                 if (intent.getSubjects() != null && intent.getSubjects().size() > 0) {
124                     String endpointValue = "";
125                     for (Subjects subject : intent.getSubjects()) {
126                         if (subject
127                                 .getSubject() instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.subjects.subject.EndPointGroup) {
128                             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
129                                     .getSubject();
130                             endpointValue = epg.getEndPointGroup().getName();
131                         } else if (subject
132                                 .getSubject() instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.subjects.subject.EndPointSelector) {
133                             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
134                                     .getSubject();
135                             endpointValue = epg.getEndPointSelector().getEndPointSelector();
136                         } else if (subject
137                                 .getSubject() instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.subjects.subject.EndPointGroupSelector) {
138                             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
139                                     .getSubject();
140                             endpointValue = epg.getEndPointGroupSelector().getEndPointGroupSelector();
141                         }
142                         if (endpointValue.equalsIgnoreCase(endpointGroupName)) {
143                             removeIntent(intent.getId());
144                             LOG.info("Deleted Intent ID : {} for endpoint: {}", intent.getId(), endpointGroupName);
145                         }
146                     }
147                 }
148             }
149         }
150     }
151
152     /**
153      * Create a list of Intent actions
154      * @param intentAction
155      * @return :a list of Actions
156      */
157     private List<Actions> createActions(String intentAction) {
158         List<Actions> actionsList = new ArrayList<Actions>();
159         short order = 1;
160         org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.actions.Action action = null;
161         if (intentAction.equalsIgnoreCase(ACTION_ALLOW)) {
162             action = new org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.actions.action
163                     .AllowBuilder().setAllow(new AllowBuilder().build()).build();
164         } else if (intentAction.equalsIgnoreCase(ACTION_BLOCK)) {
165             action = new org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.actions.action
166                     .BlockBuilder().setBlock(new BlockBuilder().build()).build();
167         }
168
169         Actions intentActions = new ActionsBuilder().setOrder(order).setAction(action).build();
170         actionsList.add(intentActions);
171         return actionsList;
172     }
173
174     /**
175      * Create a list of Intent subjects
176      * @param src :Source Site Name
177      * @param dst :Destination Site Name
178      * @return :a list of Subjects
179      */
180     private List<Subjects> createSubjects(String src, String dst) {
181         List<Subjects> subjectList = new ArrayList<Subjects>();
182
183         EndPointGroup endpointGroupFrom = new EndPointGroupBuilder().setName(src).build();
184         org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.subjects.subject.EndPointGroup fromEPG =
185                 new org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.subjects.subject
186                 .EndPointGroupBuilder().setEndPointGroup(endpointGroupFrom).build();
187         Subjects subjects1 = new SubjectsBuilder().setOrder(FIRST_SUBJECT).setSubject(fromEPG).build();
188
189         EndPointGroup endpointGroupTo = new EndPointGroupBuilder().setName(dst).build();
190         org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.subjects.subject.EndPointGroup toEPG =
191                 new org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.subjects.subject
192                 .EndPointGroupBuilder().setEndPointGroup(endpointGroupTo).build();
193         Subjects subjects2 = new SubjectsBuilder().setOrder(SECOND_SUBJECT).setSubject(toEPG).build();
194
195         subjectList.add(subjects1);
196         subjectList.add(subjects2);
197         return subjectList;
198     }
199
200     /**
201      * Create a list of Intent constraints
202      * @param failOverType :Type of failover, fast-reroute or slow-reroute
203      * @return
204      */
205     private List<Constraints> createConstraints(String failOverType) {
206         List<Constraints> intentConstraints = new ArrayList<Constraints>();
207         if (failOverType==null) {
208             return intentConstraints;
209         }
210         short order = 1;
211         org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.constraints.Constraints
212             protectionConstraint = null;
213         org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.constraints.Constraints
214             failoverConstraint = null;
215         if (failOverType.equals(FAST_REROUTE)) {
216             protectionConstraint = new org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent
217                     .constraints.constraints.ProtectionConstraintBuilder()
218                     .setProtectionConstraint(new ProtectionConstraintBuilder().setIsProtected(true).build()).build();
219             failoverConstraint = new org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.constraints
220                     .constraints.FailoverConstraintBuilder()
221                     .setFailoverConstraint(new FailoverConstraintBuilder().setFailoverSelector(FailoverType.FastReroute).build())
222                     .build();
223         } else if (failOverType.equals(SLOW_REROUTE)) {
224             protectionConstraint = new org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent
225                     .constraints.constraints.ProtectionConstraintBuilder()
226                     .setProtectionConstraint(new ProtectionConstraintBuilder().setIsProtected(true).build()).build();
227             failoverConstraint = new org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.constraints
228                     .constraints.FailoverConstraintBuilder()
229                     .setFailoverConstraint(new FailoverConstraintBuilder().setFailoverSelector(FailoverType.SlowReroute).build())
230                     .build();
231         }
232         Constraints constraint1 = new ConstraintsBuilder().setOrder(order).setConstraints(protectionConstraint).build();
233         Constraints constraint2 = new ConstraintsBuilder().setOrder(++order).setConstraints(failoverConstraint).build();
234         intentConstraints.add(constraint1);
235         intentConstraints.add(constraint2);
236         return intentConstraints;
237     }
238 }