blob: f569ff77ae5816a76d998c0e43f968f8d5a92349 [file] [log] [blame]
Yoonseon Han096cea02017-05-15 15:10:41 -07001/*
2 * Copyright 2017-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.incubator.net.virtual.impl.intent.phase;
18
19import org.onosproject.incubator.net.virtual.NetworkId;
20import org.onosproject.incubator.net.virtual.impl.intent.VirtualIntentProcessor;
21import org.onosproject.net.intent.Intent;
22import org.onosproject.net.intent.IntentData;
23import org.onosproject.net.intent.IntentException;
24import org.slf4j.Logger;
25import org.slf4j.LoggerFactory;
26
27import java.util.List;
28import java.util.Optional;
29
30import static com.google.common.base.Preconditions.checkNotNull;
31
32/**
33 * Represents a phase where an intent is being compiled or recompiled
34 * for virtual networks.
35 */
36public class VirtualIntentCompiling implements VirtualIntentProcessPhase {
37 private static final Logger log = LoggerFactory.getLogger(VirtualIntentCompiling.class);
38
39 private final NetworkId networkId;
40 private final VirtualIntentProcessor processor;
41 private final IntentData data;
42 private final Optional<IntentData> stored;
43
44 /**
45 * Creates a intent recompiling phase.
46 *
47 * @param networkId virtual network identifier
48 * @param processor intent processor that does work for recompiling
49 * @param data intent data containing an intent to be recompiled
50 * @param stored intent data stored in the store
51 */
52 VirtualIntentCompiling(NetworkId networkId, VirtualIntentProcessor processor,
53 IntentData data, Optional<IntentData> stored) {
54 this.networkId = checkNotNull(networkId);
55 this.processor = checkNotNull(processor);
56 this.data = checkNotNull(data);
57 this.stored = checkNotNull(stored);
58 }
59
60 @Override
61 public Optional<VirtualIntentProcessPhase> execute() {
62 try {
63 List<Intent> compiled = processor
64 .compile(networkId, data.intent(),
65 //TODO consider passing an optional here in the future
66 stored.map(IntentData::installables).orElse(null));
67 return Optional.of(new VirtualIntentInstalling(networkId, processor,
68 IntentData.compiled(data, compiled), stored));
69 } catch (IntentException e) {
70 log.warn("Unable to compile intent {} due to:", data.intent(), e);
71 if (stored.filter(x -> !x.installables().isEmpty()).isPresent()) {
72 // removing orphaned flows and deallocating resources
73 return Optional.of(
74 new VirtualIntentWithdrawing(networkId, processor,
Ray Milkeye1077f82019-02-20 09:31:12 -080075 IntentData.compiled(data, stored.get().installables())));
Yoonseon Han096cea02017-05-15 15:10:41 -070076 } else {
77 return Optional.of(new VirtualIntentFailed(data));
78 }
79 }
80 }
81}